Skip to content

Commit

Permalink
Merge pull request #84 from elichai/clone
Browse files Browse the repository at this point in the history
Implement Clone to Shell
  • Loading branch information
matklad committed Mar 28, 2024
2 parents ebe98b4 + f5c1cc0 commit c300b94
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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>"]
Expand All @@ -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"
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
Expand Down
41 changes: 23 additions & 18 deletions tests/it/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
12 changes: 12 additions & 0 deletions tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 0 additions & 2 deletions tests/it/tidy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion xshell-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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>"]
Expand Down

0 comments on commit c300b94

Please sign in to comment.