From 57c4d63e718bfb1ca4e402d60697308f174e5237 Mon Sep 17 00:00:00 2001 From: Elichai Turkel <elichai.turkel@gmail.com> Date: Wed, 27 Mar 2024 18:40:17 +0200 Subject: [PATCH 1/3] Add Clone to Shell --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 63da33c..1ffa36d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -380,7 +380,7 @@ macro_rules! cmd { /// assert_eq!(cwd, process_cwd.join("./target")); /// # Ok::<(), xshell::Error>(()) /// ``` -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Shell { cwd: RefCell<PathBuf>, env: RefCell<HashMap<OsString, OsString>>, From 8590604f81da069792e6ed3a580ae288747dd345 Mon Sep 17 00:00:00 2001 From: Elichai Turkel <elichai.turkel@gmail.com> Date: Wed, 27 Mar 2024 18:56:50 +0200 Subject: [PATCH 2/3] Add tests for cloned shell --- tests/it/env.rs | 41 +++++++++++++++++++++++------------------ tests/it/main.rs | 12 ++++++++++++ 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/tests/it/env.rs b/tests/it/env.rs index 9498694..1933dfe 100644 --- a/tests/it/env.rs +++ b/tests/it/env.rs @@ -11,29 +11,34 @@ fn test_env() { let v1 = "xshell_test_123"; let v2 = "xshell_test_456"; - assert_env(cmd!(sh, "xecho -$ {v1}").env(v1, "123"), &[(v1, Some("123"))]); - - assert_env( - cmd!(sh, "xecho -$ {v1} {v2}").envs([(v1, "123"), (v2, "456")].iter().copied()), - &[(v1, Some("123")), (v2, Some("456"))], - ); - assert_env( - cmd!(sh, "xecho -$ {v1} {v2}") - .envs([(v1, "123"), (v2, "456")].iter().copied()) - .env_remove(v2), - &[(v1, Some("123")), (v2, None)], - ); - assert_env( - cmd!(sh, "xecho -$ {v1} {v2}") - .envs([(v1, "123"), (v2, "456")].iter().copied()) - .env_remove("nothing"), - &[(v1, Some("123")), (v2, Some("456"))], - ); + let cloned_sh = sh.clone(); + + for sh in [&sh, &cloned_sh] { + assert_env(cmd!(sh, "xecho -$ {v1}").env(v1, "123"), &[(v1, Some("123"))]); + + assert_env( + cmd!(sh, "xecho -$ {v1} {v2}").envs([(v1, "123"), (v2, "456")].iter().copied()), + &[(v1, Some("123")), (v2, Some("456"))], + ); + assert_env( + cmd!(sh, "xecho -$ {v1} {v2}") + .envs([(v1, "123"), (v2, "456")].iter().copied()) + .env_remove(v2), + &[(v1, Some("123")), (v2, None)], + ); + assert_env( + cmd!(sh, "xecho -$ {v1} {v2}") + .envs([(v1, "123"), (v2, "456")].iter().copied()) + .env_remove("nothing"), + &[(v1, Some("123")), (v2, Some("456"))], + ); + } let _g1 = sh.push_env(v1, "foobar"); let _g2 = sh.push_env(v2, "quark"); assert_env(cmd!(sh, "xecho -$ {v1} {v2}"), &[(v1, Some("foobar")), (v2, Some("quark"))]); + assert_env(cmd!(cloned_sh, "xecho -$ {v1} {v2}"), &[(v1, None), (v2, None)]); assert_env( cmd!(sh, "xecho -$ {v1} {v2}").env(v1, "wombo"), diff --git a/tests/it/main.rs b/tests/it/main.rs index 5dc5a85..3214840 100644 --- a/tests/it/main.rs +++ b/tests/it/main.rs @@ -311,6 +311,18 @@ fn test_push_env() { assert_eq!(e5, e1); } +#[test] +fn test_push_env_clone() { + let sh = setup(); + + assert!(sh.var_os(VAR).is_none()); + let guard = sh.push_env(VAR, "1"); + let cloned = sh.clone(); + drop(guard); + assert_eq!(sh.var_os(VAR), None); + assert_eq!(cloned.var_os(VAR), Some("1".into())); +} + #[test] fn test_push_env_and_set_var() { let sh = setup(); From f5c1cc07a2c77a87d66d7ca2e419cf325ec0e9af Mon Sep 17 00:00:00 2001 From: Elichai Turkel <elichai.turkel@gmail.com> Date: Thu, 28 Mar 2024 19:53:30 +0200 Subject: [PATCH 3/3] Bump version to 0.2.6 --- CHANGELOG.md | 4 ++++ Cargo.toml | 4 ++-- tests/it/tidy.rs | 2 -- xshell-macros/Cargo.toml | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb62b24..016ce40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.2.6 + +- Implement `Clone` for `Shell`. + ## 0.2.5 - Improve error message when a working directory for `cmd!` does not exist. diff --git a/Cargo.toml b/Cargo.toml index 7060906..8815165 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "xshell" description = "Utilities for quick shell scripting in Rust" categories = ["development-tools::build-utils", "filesystem"] -version = "0.2.5" # also update xshell-macros/Cargo.toml and CHANGELOG.md +version = "0.2.6" # also update xshell-macros/Cargo.toml and CHANGELOG.md license = "MIT OR Apache-2.0" repository = "https://github.com/matklad/xshell" authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"] @@ -14,7 +14,7 @@ exclude = [".github/", "bors.toml", "rustfmt.toml", "cbench", "mock_bin/"] [workspace] [dependencies] -xshell-macros = { version = "=0.2.5", path = "./xshell-macros" } +xshell-macros = { version = "=0.2.6", path = "./xshell-macros" } [dev-dependencies] anyhow = "1.0.56" diff --git a/tests/it/tidy.rs b/tests/it/tidy.rs index 2b55b29..3d6f90d 100644 --- a/tests/it/tidy.rs +++ b/tests/it/tidy.rs @@ -12,8 +12,6 @@ fn versions_match() { }; let v1 = read_version("./Cargo.toml"); - let v2 = read_version("./xshell-macros/Cargo.toml"); - assert_eq!(v1, v2); let cargo_toml = sh.read_file("./Cargo.toml").unwrap(); let dep = format!("xshell-macros = {{ version = \"={}\",", v1); diff --git a/xshell-macros/Cargo.toml b/xshell-macros/Cargo.toml index e791acb..08d2b5e 100644 --- a/xshell-macros/Cargo.toml +++ b/xshell-macros/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "xshell-macros" description = "Private implementation detail of xshell crate" -version = "0.2.5" +version = "0.2.6" license = "MIT OR Apache-2.0" repository = "https://github.com/matklad/xshell" authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]