Skip to content

Commit 505151c

Browse files
committed
Add tests for gix_testtools::Env
The `nonoverlapping` test, which fortunately is what most closely resembles existing known usage (and most likely all current usage in gitoxide's own test suite), already passes. The other tests test the situation where the same environment variable is affected by multiple `set` or `unset` calls (or both a `set` and an `unset` call). These do not pass yet, because while the assertions about the immediate effect on the environment of each such call all pass, the assertions about the effect after drop fail. This is because, on drop, `Env` currently restores the state of a variable that was most recently saved, i.e., it puts it back to whatever it was just before the most recent modification it made to it. This goes against the intuitive expectation that `Env` will reset things to the way they were before the `Env` object was created and used (so long as all changes were by `set` and `unset` calls).
1 parent 3b17305 commit 505151c

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/tools/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ xz2 = { version = "0.1.6", optional = true }
4848

4949
document-features = { version = "0.2.1", optional = true }
5050

51+
[dev-dependencies]
52+
serial_test = { version = "3.1.0", default-features = false }
53+
5154
[package.metadata.docs.rs]
5255
all-features = true
5356
features = ["document-features"]

tests/tools/src/lib.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,4 +883,98 @@ mod tests {
883883
fn parse_version_with_trailing_newline() {
884884
assert_eq!(git_version_from_bytes(b"git version 2.37.2\n").unwrap(), (2, 37, 2));
885885
}
886+
887+
mod env {
888+
use super::Env;
889+
use serial_test::serial;
890+
891+
// We rely on these not already existing, to test `Env` without using or rewriting it.
892+
static VAR1: &str = "VAR_03FC4045_6043_4A61_9D15_852236CB632B";
893+
static VAR2: &str = "VAR_8C135840_05DB_4F3A_BFDD_FC755EC35B89";
894+
static VAR3: &str = "VAR_9B23A2BE_E20B_4670_93E2_3A6A8D47F274";
895+
896+
struct TestEnv;
897+
898+
impl TestEnv {
899+
fn new() -> Self {
900+
assert_eq!(std::env::var_os(VAR1), None);
901+
assert_eq!(std::env::var_os(VAR2), None);
902+
assert_eq!(std::env::var_os(VAR3), None);
903+
Self
904+
}
905+
}
906+
907+
impl Drop for TestEnv {
908+
fn drop(&mut self) {
909+
std::env::remove_var(VAR1);
910+
std::env::remove_var(VAR2);
911+
std::env::remove_var(VAR3);
912+
}
913+
}
914+
915+
#[test]
916+
#[serial]
917+
fn nonoverlapping() {
918+
let _meta = TestEnv::new();
919+
std::env::set_var(VAR1, "old1");
920+
std::env::set_var(VAR2, "old2");
921+
{
922+
let _env = Env::new().set(VAR1, "new1").unset(VAR2).set(VAR3, "new3");
923+
assert_eq!(std::env::var_os(VAR1), Some("new1".into()));
924+
assert_eq!(std::env::var_os(VAR2), None);
925+
assert_eq!(std::env::var_os(VAR3), Some("new3".into()));
926+
}
927+
assert_eq!(std::env::var_os(VAR1), Some("old1".into()));
928+
assert_eq!(std::env::var_os(VAR2), Some("old2".into()));
929+
assert_eq!(std::env::var_os(VAR3), None);
930+
}
931+
932+
#[test]
933+
#[serial]
934+
fn overlapping_reset() {
935+
let _meta = TestEnv::new();
936+
{
937+
let _env = Env::new().set(VAR1, "new1A").set(VAR1, "new1B");
938+
assert_eq!(std::env::var_os(VAR1), Some("new1B".into()));
939+
}
940+
assert_eq!(std::env::var_os(VAR1), None);
941+
}
942+
943+
#[test]
944+
#[serial]
945+
fn overlapping_unset() {
946+
let _meta = TestEnv::new();
947+
std::env::set_var(VAR1, "old1");
948+
{
949+
let _env = Env::new().unset(VAR1).unset(VAR1);
950+
assert_eq!(std::env::var_os(VAR1), None);
951+
}
952+
assert_eq!(std::env::var_os(VAR1), Some("old1".into()));
953+
}
954+
955+
#[test]
956+
#[serial]
957+
fn overlapping_combo() {
958+
let _meta = TestEnv::new();
959+
std::env::set_var(VAR1, "old1");
960+
std::env::set_var(VAR2, "old2");
961+
{
962+
let _env = Env::new()
963+
.set(VAR1, "new1A")
964+
.unset(VAR2)
965+
.set(VAR1, "new1B")
966+
.unset(VAR3)
967+
.set(VAR2, "new2")
968+
.set(VAR3, "new3")
969+
.unset(VAR1)
970+
.unset(VAR3);
971+
assert_eq!(std::env::var_os(VAR1), None);
972+
assert_eq!(std::env::var_os(VAR2), Some("new2".into()));
973+
assert_eq!(std::env::var_os(VAR3), None);
974+
}
975+
assert_eq!(std::env::var_os(VAR1), Some("old1".into()));
976+
assert_eq!(std::env::var_os(VAR2), Some("old2".into()));
977+
assert_eq!(std::env::var_os(VAR3), None);
978+
}
979+
}
886980
}

0 commit comments

Comments
 (0)