From c4a6cf1d6a924fcccb7e0a6dcae5eb2d479fa6ee Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Thu, 7 Nov 2024 23:09:50 -0300 Subject: [PATCH] fix tests by using 'files' --- compiler-core/src/io/memory.rs | 21 +++++++++------------ test-package-compiler/src/lib.rs | 6 ++---- test-project-compiler/src/lib.rs | 6 ++---- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/compiler-core/src/io/memory.rs b/compiler-core/src/io/memory.rs index 90430316ed0..6d6a71807cf 100644 --- a/compiler-core/src/io/memory.rs +++ b/compiler-core/src/io/memory.rs @@ -64,13 +64,13 @@ impl InMemoryFileSystem { .collect() } - /// All paths (files and directories) currently in the filesystem, - /// excluding the filesystem root ("/"). - pub fn subpaths(&self) -> Vec { + /// All files currently in the filesystem (directories are not included). + pub fn files(&self) -> Vec { self.files .borrow() - .keys() - .filter(|p| p.as_path() != Utf8Path::new("/")) + .iter() + .filter(|(_, f)| !f.is_directory()) + .map(|(path, _)| path) .cloned() .collect() } @@ -468,25 +468,22 @@ fn test_cannot_remove_root_from_in_memory_fs() -> Result<(), Error> { } #[test] -fn test_subpaths_exclude_root() -> Result<(), Error> { +fn test_files() -> Result<(), Error> { let imfs = InMemoryFileSystem::new(); imfs.write(&Utf8PathBuf::from("/a/b/c.txt"), "a")?; imfs.write(&Utf8PathBuf::from("/d/e.txt"), "a")?; - let mut subpaths = imfs.subpaths(); + let mut files = imfs.files(); // Sort for test determinism due to hash map usage. - subpaths.sort_unstable(); + files.sort_unstable(); assert_eq!( vec![ - Utf8PathBuf::from("/a"), - Utf8PathBuf::from("/a/b"), Utf8PathBuf::from("/a/b/c.txt"), - Utf8PathBuf::from("/d"), Utf8PathBuf::from("/d/e.txt"), ], - subpaths + files ); Ok(()) diff --git a/test-package-compiler/src/lib.rs b/test-package-compiler/src/lib.rs index 17c018a0df1..29c21ad3dfe 100644 --- a/test-package-compiler/src/lib.rs +++ b/test-package-compiler/src/lib.rs @@ -40,7 +40,7 @@ pub fn prepare(path: &str) -> String { let warnings = VectorWarningEmitterIO::default(); let warning_emitter = WarningEmitter::new(Rc::new(warnings.clone())); let filesystem = test_helpers_rs::to_in_memory_filesystem(&root); - let initial_files = filesystem.subpaths(); + let initial_files = filesystem.files(); let root = Utf8PathBuf::from(""); let out = Utf8PathBuf::from("/out/lib/the_package"); let lib = Utf8PathBuf::from("/out/lib"); @@ -69,9 +69,7 @@ pub fn prepare(path: &str) -> String { match result { Outcome::Ok(_) => { for path in initial_files { - if filesystem.is_directory(&path) { - filesystem.delete_directory(&path).unwrap(); - } else { + if filesystem.is_file(&path) { filesystem.delete_file(&path).unwrap(); } } diff --git a/test-project-compiler/src/lib.rs b/test-project-compiler/src/lib.rs index 1c088130875..738401f9d88 100644 --- a/test-project-compiler/src/lib.rs +++ b/test-project-compiler/src/lib.rs @@ -15,7 +15,7 @@ use std::rc::Rc; pub fn prepare(path: &str, mode: Mode) -> String { let root = Utf8PathBuf::from(path).canonicalize_utf8().unwrap(); let filesystem = test_helpers_rs::to_in_memory_filesystem(&root); - let initial_files = filesystem.subpaths(); + let initial_files = filesystem.files(); let toml = std::fs::read_to_string(root.join("gleam.toml")).unwrap(); let config: PackageConfig = toml::from_str(&toml).unwrap(); @@ -45,9 +45,7 @@ pub fn prepare(path: &str, mode: Mode) -> String { compiler.compile().unwrap(); for path in initial_files { - if filesystem.is_directory(&path) { - filesystem.delete_directory(&path).unwrap(); - } else { + if filesystem.is_file(&path) { filesystem.delete_file(&path).unwrap(); } }