From 9d9d360db9988a8d0e73939273a5bfe3b077fb3e Mon Sep 17 00:00:00 2001 From: Eric Long Date: Mon, 16 Jun 2025 13:01:25 +0800 Subject: [PATCH] fix(test): running test as root (#1817) * fix(test): running test as root The dummy root path /root conflicts with root user's home directory on Linux and redacts incorrectly. Our downstream CI environment builds tinymist in docker as root and encounters the problem. Change the path to `/dummy-root` fixes it. To ensure consistency, other similar occurences are also prefixed with `dummy-`. * dev: deduplicate a bit --------- Co-authored-by: Myriad-Dreamin --- crates/tinymist-project/src/entry.rs | 54 ++++++++++---------------- crates/tinymist-project/src/model.rs | 2 +- crates/tinymist-query/src/tests.rs | 4 +- crates/tinymist-task/src/primitives.rs | 2 +- crates/tinymist-tests/src/lib.rs | 4 +- crates/tinymist/src/config.rs | 6 ++- 6 files changed, 32 insertions(+), 40 deletions(-) diff --git a/crates/tinymist-project/src/entry.rs b/crates/tinymist-project/src/entry.rs index 8ca75c01..124ad957 100644 --- a/crates/tinymist-project/src/entry.rs +++ b/crates/tinymist-project/src/entry.rs @@ -200,20 +200,27 @@ mod entry_tests { use super::*; use std::path::Path; + const ROOT: &str = if cfg!(windows) { + "C:\\dummy-root" + } else { + "/dummy-root" + }; + const ROOT2: &str = if cfg!(windows) { + "C:\\dummy-root2" + } else { + "/dummy-root2" + }; + #[test] fn test_entry_resolution() { - let root_path = Path::new(if cfg!(windows) { "C:\\root" } else { "/root" }); + let root_path = Path::new(ROOT); let entry = EntryResolver { root_path: Some(ImmutPath::from(root_path)), ..Default::default() }; - let entry = entry.resolve(if cfg!(windows) { - Some(Path::new("C:\\root\\main.typ").into()) - } else { - Some(Path::new("/root/main.typ").into()) - }); + let entry = entry.resolve(Some(root_path.join("main.typ").into())); assert_eq!(entry.root(), Some(ImmutPath::from(root_path))); assert_eq!( @@ -227,8 +234,8 @@ mod entry_tests { #[test] fn test_entry_resolution_multi_root() { - let root_path = Path::new(if cfg!(windows) { "C:\\root" } else { "/root" }); - let root2_path = Path::new(if cfg!(windows) { "C:\\root2" } else { "/root2" }); + let root_path = Path::new(ROOT); + let root2_path = Path::new(ROOT2); let entry = EntryResolver { root_path: Some(ImmutPath::from(root_path)), @@ -237,11 +244,7 @@ mod entry_tests { }; { - let entry = entry.resolve(if cfg!(windows) { - Some(Path::new("C:\\root\\main.typ").into()) - } else { - Some(Path::new("/root/main.typ").into()) - }); + let entry = entry.resolve(Some(root_path.join("main.typ").into())); assert_eq!(entry.root(), Some(ImmutPath::from(root_path))); assert_eq!( @@ -254,11 +257,7 @@ mod entry_tests { } { - let entry = entry.resolve(if cfg!(windows) { - Some(Path::new("C:\\root2\\main.typ").into()) - } else { - Some(Path::new("/root2/main.typ").into()) - }); + let entry = entry.resolve(Some(root2_path.join("main.typ").into())); assert_eq!(entry.root(), Some(ImmutPath::from(root2_path))); assert_eq!( @@ -273,8 +272,8 @@ mod entry_tests { #[test] fn test_entry_resolution_default_multi_root() { - let root_path = Path::new(if cfg!(windows) { "C:\\root" } else { "/root" }); - let root2_path = Path::new(if cfg!(windows) { "C:\\root2" } else { "/root2" }); + let root_path = Path::new(ROOT); + let root2_path = Path::new(ROOT2); let mut entry = EntryResolver { root_path: Some(ImmutPath::from(root_path)), @@ -283,11 +282,7 @@ mod entry_tests { }; { - entry.entry = if cfg!(windows) { - Some(Path::new("C:\\root\\main.typ").into()) - } else { - Some(Path::new("/root/main.typ").into()) - }; + entry.entry = Some(root_path.join("main.typ").into()); let default_entry = entry.resolve_default(); @@ -299,14 +294,7 @@ mod entry_tests { let default_entry = entry.resolve_default(); - assert_eq!( - default_entry, - if cfg!(windows) { - Some(Path::new("C:\\root\\main.typ").into()) - } else { - Some(Path::new("/root/main.typ").into()) - } - ); + assert_eq!(default_entry, Some(root_path.join("main.typ").into())); } } } diff --git a/crates/tinymist-project/src/model.rs b/crates/tinymist-project/src/model.rs index 219f3dc2..fb61e59a 100644 --- a/crates/tinymist-project/src/model.rs +++ b/crates/tinymist-project/src/model.rs @@ -154,7 +154,7 @@ mod tests { #[test] fn test_substitute_path() { - let root = Path::new("/root"); + let root = Path::new("/dummy-root"); let entry = EntryState::new_rooted(root.into(), Some(VirtualPath::new("/dir1/dir2/file.txt"))); diff --git a/crates/tinymist-query/src/tests.rs b/crates/tinymist-query/src/tests.rs index ee1c746a..10fc5dca 100644 --- a/crates/tinymist-query/src/tests.rs +++ b/crates/tinymist-query/src/tests.rs @@ -446,9 +446,9 @@ pub(crate) fn file_path(uri: &str) -> String { pub(crate) fn file_path_(uri: &lsp_types::Url) -> String { let root = if cfg!(windows) { - PathBuf::from("C:\\root") + PathBuf::from("C:\\dummy-root") } else { - PathBuf::from("/root") + PathBuf::from("/dummy-root") }; let uri = uri.to_file_path().unwrap(); let abs_path = Path::new(&uri).strip_prefix(root).map(|p| p.to_owned()); diff --git a/crates/tinymist-task/src/primitives.rs b/crates/tinymist-task/src/primitives.rs index e1ba2adb..c8a5eddd 100644 --- a/crates/tinymist-task/src/primitives.rs +++ b/crates/tinymist-task/src/primitives.rs @@ -364,7 +364,7 @@ mod tests { #[test] fn test_substitute_path() { - let root = Path::new("/root"); + let root = Path::new("/dummy-root"); let entry = EntryState::new_rooted(root.into(), Some(VirtualPath::new("/dir1/dir2/file.txt"))); diff --git a/crates/tinymist-tests/src/lib.rs b/crates/tinymist-tests/src/lib.rs index 47e8a85c..7473b06a 100644 --- a/crates/tinymist-tests/src/lib.rs +++ b/crates/tinymist-tests/src/lib.rs @@ -52,9 +52,9 @@ pub fn run_with_sources(source: &str, f: impl FnOnce(&mut LspUniverse, PathBu }); let root = if cfg!(windows) { - PathBuf::from("C:\\root") + PathBuf::from("C:\\dummy-root") } else { - PathBuf::from("/root") + PathBuf::from("/dummy-root") }; let mut verse = LspUniverseBuilder::build( EntryState::new_rooted(root.as_path().into(), None), diff --git a/crates/tinymist/src/config.rs b/crates/tinymist/src/config.rs index ea83800c..3d7d958c 100644 --- a/crates/tinymist/src/config.rs +++ b/crates/tinymist/src/config.rs @@ -905,7 +905,11 @@ mod tests { fn test_config_update() { let mut config = Config::default(); - let root_path = Path::new(if cfg!(windows) { "C:\\root" } else { "/root" }); + let root_path = Path::new(if cfg!(windows) { + "C:\\dummy-root" + } else { + "/dummy-root" + }); let update = json!({ "outputPath": "out",