From 94fe5011e2a242a30b9f2a538bc64fbacd36f619 Mon Sep 17 00:00:00 2001 From: Chris Peterson Date: Thu, 16 Feb 2012 23:09:12 -0800 Subject: [PATCH] std: Add some env() and getenv() tests --- src/libstd/generic_os.rs | 55 +++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/src/libstd/generic_os.rs b/src/libstd/generic_os.rs index e6b784c2a13..15d52c76fbe 100644 --- a/src/libstd/generic_os.rs +++ b/src/libstd/generic_os.rs @@ -99,21 +99,31 @@ fn setenv(n: str, v: str) { #[cfg(test)] mod tests { + fn make_rand_name() -> str { + import rand; + let rng: rand::rng = rand::mk_rng(); + let n = "TEST" + rng.gen_str(10u); + assert option::is_none(getenv(n)); + n + } + #[test] #[ignore(reason = "fails periodically on mac")] fn test_setenv() { - // NB: Each test of setenv needs to use different variable names or - // the tests will not be threadsafe - setenv("NAME1", "VALUE"); - assert (getenv("NAME1") == option::some("VALUE")); + let n = make_rand_name(); + setenv(n, "VALUE"); + assert getenv(n) == option::some("VALUE"); } #[test] #[ignore(reason = "fails periodically on mac")] fn test_setenv_overwrite() { - setenv("NAME2", "1"); - setenv("NAME2", "2"); - assert (getenv("NAME2") == option::some("2")); + let n = make_rand_name(); + setenv(n, "1"); + setenv(n, "2"); + assert getenv(n) == option::some("2"); + setenv(n, ""); + assert getenv(n) == option::some(""); } // Windows GetEnvironmentVariable requires some extra work to make sure @@ -124,9 +134,10 @@ fn test_getenv_big() { let s = ""; let i = 0; while i < 100 { s += "aaaaaaaaaa"; i += 1; } - setenv("test_getenv_big", s); + let n = make_rand_name(); + setenv(n, s); log(debug, s); - assert (getenv("test_getenv_big") == option::some(s)); + assert getenv(n) == option::some(s); } #[test] @@ -143,6 +154,32 @@ fn test_get_exe_path() { assert path[1] == ':' as u8; } } + + #[test] + fn test_env_getenv() { + let e = env(); + assert vec::len(e) > 0u; + for (n, v) in e { + log(debug, n); + let v2 = getenv(n); + // MingW seems to set some funky environment variables like + // "=C:=C:\MinGW\msys\1.0\bin" and "!::=::\" that are returned + // from env() but not visible from getenv(). + assert option::is_none(v2) || v2 == option::some(v); + } + } + + #[test] + fn test_env_setenv() { + let n = make_rand_name(); + + let e = env(); + setenv(n, "VALUE"); + assert !vec::contains(e, (n, "VALUE")); + + e = env(); + assert vec::contains(e, (n, "VALUE")); + } } // Local Variables: