diff --git a/Cargo.lock b/Cargo.lock index bda8f5a..ca9b14c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -395,6 +395,12 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" +[[package]] +name = "clean-path" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aaa6b4b263a5d737e9bf6b7c09b72c41a5480aec4d7219af827f6564e950b6a5" + [[package]] name = "cocoa" version = "0.24.1" @@ -668,16 +674,20 @@ version = "0.1.2" dependencies = [ "clap", "indexmap 2.2.5", + "indoc", "insta", + "itertools", "open", "parking_lot", "poem", "rust-embed", "serde", + "tempdir", "thiserror", "tokio", "toml 0.8.12", "tracing-subscriber", + "txtar", "walkdir", ] @@ -753,6 +763,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b" +[[package]] +name = "either" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" + [[package]] name = "embed-resource" version = "2.4.2" @@ -881,6 +897,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + [[package]] name = "futf" version = "0.1.5" @@ -1567,6 +1589,12 @@ dependencies = [ "serde", ] +[[package]] +name = "indoc" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e186cfbae8084e513daff4240b4797e342f988cecda4fb6c939150f96315fd8" + [[package]] name = "infer" version = "0.13.0" @@ -1620,6 +1648,15 @@ dependencies = [ "once_cell", ] +[[package]] +name = "itertools" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "0.4.8" @@ -2474,6 +2511,19 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi", +] + [[package]] name = "rand" version = "0.7.3" @@ -2519,6 +2569,21 @@ dependencies = [ "rand_core 0.6.4", ] +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + [[package]] name = "rand_core" version = "0.5.1" @@ -2561,6 +2626,15 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9" +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + [[package]] name = "redox_syscall" version = "0.4.1" @@ -2625,6 +2699,15 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + [[package]] name = "rfc7239" version = "0.1.0" @@ -3371,6 +3454,16 @@ dependencies = [ "toml 0.7.8", ] +[[package]] +name = "tempdir" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" +dependencies = [ + "rand 0.4.6", + "remove_dir_all", +] + [[package]] name = "tempfile" version = "3.10.1" @@ -3669,6 +3762,16 @@ dependencies = [ "serde_json", ] +[[package]] +name = "txtar" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99ffb5ecda6b7281c18c96cce4e96f6dbb8ccb05cb016cd334ad34ac3faf85f7" +dependencies = [ + "clean-path", + "thiserror", +] + [[package]] name = "typenum" version = "1.17.0" diff --git a/Cargo.toml b/Cargo.toml index 9e1ba56..9399b25 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,9 +39,13 @@ tracing-subscriber = "0.3.18" walkdir = "2.4.0" [dev-dependencies] +indoc = "2.0.4" insta = { version = "1.34.0", features = [ "redactions", "serde", "yaml", "json", ] } +itertools = "0.12.1" +tempdir = "0.3.7" +txtar = "1.0.0" diff --git a/src/fs.rs b/src/fs.rs index bd5e7cc..e857da3 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -142,3 +142,93 @@ fn scan_several(roots: [&PathBuf; 3]) -> Result } Ok(result) } + +#[cfg(test)] +mod tests { + use indexmap::indexmap; + use indoc::indoc; + use itertools::Itertools; + use tempdir::TempDir; + + use super::*; + + fn left_right_edit_threedirinput(base: &Path) -> ThreeDirInput { + ThreeDirInput { + left: base.join("left").to_owned(), + right: base.join("right").to_owned(), + edit: base.join("edit").to_owned(), + } + } + + #[test] + fn it_works() { + let tmp_dir = TempDir::new("de3test").unwrap(); + txtar::from_str(indoc! {" + -- left/subdir/txt -- + Some text + "}) + .materialize(tmp_dir.path()) + .unwrap(); + let result = scan(tmp_dir.path()) + .map(|(dir_path, file_type)| { + ( + dir_path + .path() + .strip_prefix(tmp_dir.path()) + .unwrap() + .to_owned(), + file_type, + ) + }) + .collect_vec(); + insta::assert_yaml_snapshot!(result, @r###" + --- + - - left/subdir/txt + - type: Text + value: "Some text\n" + "###); + let mut input = left_right_edit_threedirinput(tmp_dir.path()); + insta::assert_yaml_snapshot!(input.scan().unwrap(), @r###" + --- + subdir/txt: + - type: Text + value: "Some text\n" + - type: Missing + - type: Missing + "###); + let result = input.save(indexmap! { + "subdir/txt".to_string() => "".to_string() + }); + insta::assert_debug_snapshot!(result, @r###" + Err( + IOError( + "/var/folders/lj/rv4h95_d0mxb9ryztzpz4qph0000gn/T/de3test.hg2lQoslcgCd/edit/subdir/txt", + Os { + code: 2, + kind: NotFound, + message: "No such file or directory", + }, + ), + ) + "###); + let result = input.save(indexmap! { + "another_txt".to_string() => "".to_string(), + "subdir/txt".to_string() => "".to_string() + }); + insta::assert_debug_snapshot!(result, @r###" + Err( + ValidationFailError( + "another_txt", + ), + ) + "###); + insta::assert_yaml_snapshot!(input.scan().unwrap(), @r###" + --- + subdir/txt: + - type: Text + value: "Some text\n" + - type: Missing + - type: Missing + "###); + } +}