Skip to content

Commit

Permalink
fix tests by using 'files'
Browse files Browse the repository at this point in the history
  • Loading branch information
PgBiel committed Nov 8, 2024
1 parent 8506b46 commit c4a6cf1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
21 changes: 9 additions & 12 deletions compiler-core/src/io/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Utf8PathBuf> {
/// All files currently in the filesystem (directories are not included).
pub fn files(&self) -> Vec<Utf8PathBuf> {
self.files
.borrow()
.keys()
.filter(|p| p.as_path() != Utf8Path::new("/"))
.iter()
.filter(|(_, f)| !f.is_directory())
.map(|(path, _)| path)
.cloned()
.collect()
}
Expand Down Expand Up @@ -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(())
Expand Down
6 changes: 2 additions & 4 deletions test-package-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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();
}
}
Expand Down
6 changes: 2 additions & 4 deletions test-project-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
}
Expand Down

0 comments on commit c4a6cf1

Please sign in to comment.