From ee219833c4c1c8aa626b65b705bae419dec9049d Mon Sep 17 00:00:00 2001 From: Vince Buffalo Date: Thu, 22 Feb 2024 15:26:13 -0800 Subject: [PATCH] Fixed bug in sdf mv that occurred in addressing issue #14. - New `test_mv()` function to test mv and catch future bugs. - Fixed the bug, in which the wrong variable was used. --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib/project.rs | 4 ++-- tests/test_project.rs | 48 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 230203a..ab6acd2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2184,7 +2184,7 @@ dependencies = [ [[package]] name = "scidataflow" -version = "0.8.8" +version = "0.8.9" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 4653b27..826d5c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "scidataflow" -version = "0.8.8" +version = "0.8.9" edition = "2021" exclude = ["logo.png", "tests/test_data/**"] license = "MIT" diff --git a/src/lib/project.rs b/src/lib/project.rs index f61d7ce..3402f94 100644 --- a/src/lib/project.rs +++ b/src/lib/project.rs @@ -482,10 +482,10 @@ impl Project { } // move the actual file - rename(source, destination_path).context("Error encountered when moving file.")?; + rename(source, &destination_path).context("Error encountered when moving file.")?; // update the relative path - let relative_destination = self.relative_path_string(Path::new(destination))?; + let relative_destination = self.relative_path_string(destination_path.as_path())?; // modify the DataFile let mut new_file = file.clone(); diff --git a/tests/test_project.rs b/tests/test_project.rs index a702700..79d400b 100644 --- a/tests/test_project.rs +++ b/tests/test_project.rs @@ -13,6 +13,7 @@ mod tests { use super::get_statuses; use super::setup; use scidataflow::lib::data::LocalStatusCode; + use std::fs; use std::path::PathBuf; #[tokio::test] @@ -160,4 +161,51 @@ mod tests { } } } + + #[tokio::test] + async fn test_mv() { + let mut fixture = setup(false).await; + let path_context = fixture.project.path_context(); + let statuses = get_statuses(&mut fixture, &path_context).await; + + // at this point the status should be empty + assert!(statuses.is_empty()); + + // get the files to add + let files = &fixture.env.files.as_ref().unwrap(); + let add_files: Vec = files + .into_iter() + .filter(|f| f.add) + .map(|f| f.path.clone()) + .collect(); + + // add those files + let _ = fixture.project.add(&add_files).await; + + let new_name = "data/data_alt.tsv"; + let target_path = PathBuf::from(new_name); + + let statuses = get_statuses(&mut fixture, &path_context).await; + let exists = statuses.iter().any(|(path, _status)| path == &target_path); + assert!(!exists); // not there before move + + // try moving a file (renaming) + fixture.project.mv("data/data.tsv", new_name).await.unwrap(); + + let exists = statuses.iter().any(|(path, _status)| path == &target_path); + assert!(!exists); // now it should be there + + // now let's try moving to a directory + fs::create_dir_all("new_data/").unwrap(); + fixture + .project + .mv("data/supplement/big_1.tsv.gz", "new_data/") + .await + .unwrap(); + + let statuses = get_statuses(&mut fixture, &path_context).await; + let target_path = PathBuf::from("data/supplement/big_1.tsv.gz"); + let exists = statuses.iter().any(|(path, _status)| path == &target_path); + assert!(!exists); // now it should be there + } }