std: Apply deprecated_safe_2024
This commit is contained in:
parent
36733f3bce
commit
ef20a1b1f8
5 changed files with 66 additions and 42 deletions
|
@ -568,7 +568,7 @@ pub struct JoinPathsError {
|
||||||
/// let mut paths = env::split_paths(&path).collect::<Vec<_>>();
|
/// let mut paths = env::split_paths(&path).collect::<Vec<_>>();
|
||||||
/// paths.push(PathBuf::from("/home/xyz/bin"));
|
/// paths.push(PathBuf::from("/home/xyz/bin"));
|
||||||
/// let new_path = env::join_paths(paths)?;
|
/// let new_path = env::join_paths(paths)?;
|
||||||
/// env::set_var("PATH", &new_path);
|
/// unsafe { env::set_var("PATH", &new_path); }
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
|
|
|
@ -323,9 +323,13 @@ fn test_capture_env_at_spawn() {
|
||||||
|
|
||||||
// This variable will not be present if the environment has already
|
// This variable will not be present if the environment has already
|
||||||
// been captured above.
|
// been captured above.
|
||||||
|
unsafe {
|
||||||
env::set_var("RUN_TEST_NEW_ENV2", "456");
|
env::set_var("RUN_TEST_NEW_ENV2", "456");
|
||||||
|
}
|
||||||
let result = cmd.output().unwrap();
|
let result = cmd.output().unwrap();
|
||||||
|
unsafe {
|
||||||
env::remove_var("RUN_TEST_NEW_ENV2");
|
env::remove_var("RUN_TEST_NEW_ENV2");
|
||||||
|
}
|
||||||
|
|
||||||
let output = String::from_utf8_lossy(&result.stdout).to_string();
|
let output = String::from_utf8_lossy(&result.stdout).to_string();
|
||||||
|
|
||||||
|
|
|
@ -154,8 +154,8 @@ impl Command {
|
||||||
if let Some(e) = &env {
|
if let Some(e) = &env {
|
||||||
for (k, (_, v)) in e {
|
for (k, (_, v)) in e {
|
||||||
match v {
|
match v {
|
||||||
Some(v) => crate::env::set_var(k, v),
|
Some(v) => unsafe { crate::env::set_var(k, v) },
|
||||||
None => crate::env::remove_var(k),
|
None => unsafe { crate::env::remove_var(k) },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -166,8 +166,8 @@ impl Command {
|
||||||
if let Some(e) = env {
|
if let Some(e) = env {
|
||||||
for (k, (v, _)) in e {
|
for (k, (v, _)) in e {
|
||||||
match v {
|
match v {
|
||||||
Some(v) => crate::env::set_var(k, v),
|
Some(v) => unsafe { crate::env::set_var(k, v) },
|
||||||
None => crate::env::remove_var(k),
|
None => unsafe { crate::env::remove_var(k) },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -138,8 +138,10 @@ fn windows_env_unicode_case() {
|
||||||
let mut cmd = Command::new("cmd");
|
let mut cmd = Command::new("cmd");
|
||||||
cmd.env(a, "1");
|
cmd.env(a, "1");
|
||||||
cmd.env(b, "2");
|
cmd.env(b, "2");
|
||||||
|
unsafe {
|
||||||
env::set_var(a, "1");
|
env::set_var(a, "1");
|
||||||
env::set_var(b, "2");
|
env::set_var(b, "2");
|
||||||
|
}
|
||||||
|
|
||||||
for (key, value) in cmd.get_envs() {
|
for (key, value) in cmd.get_envs() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|
|
@ -26,26 +26,32 @@ fn eq(a: Option<OsString>, b: Option<&str>) {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_set_var() {
|
fn test_set_var() {
|
||||||
let n = make_rand_name();
|
let n = make_rand_name();
|
||||||
|
unsafe {
|
||||||
set_var(&n, "VALUE");
|
set_var(&n, "VALUE");
|
||||||
|
}
|
||||||
eq(var_os(&n), Some("VALUE"));
|
eq(var_os(&n), Some("VALUE"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_remove_var() {
|
fn test_remove_var() {
|
||||||
let n = make_rand_name();
|
let n = make_rand_name();
|
||||||
|
unsafe {
|
||||||
set_var(&n, "VALUE");
|
set_var(&n, "VALUE");
|
||||||
remove_var(&n);
|
remove_var(&n);
|
||||||
|
}
|
||||||
eq(var_os(&n), None);
|
eq(var_os(&n), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_set_var_overwrite() {
|
fn test_set_var_overwrite() {
|
||||||
let n = make_rand_name();
|
let n = make_rand_name();
|
||||||
|
unsafe {
|
||||||
set_var(&n, "1");
|
set_var(&n, "1");
|
||||||
set_var(&n, "2");
|
set_var(&n, "2");
|
||||||
eq(var_os(&n), Some("2"));
|
eq(var_os(&n), Some("2"));
|
||||||
set_var(&n, "");
|
set_var(&n, "");
|
||||||
eq(var_os(&n), Some(""));
|
eq(var_os(&n), Some(""));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -58,7 +64,9 @@ fn test_var_big() {
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
let n = make_rand_name();
|
let n = make_rand_name();
|
||||||
|
unsafe {
|
||||||
set_var(&n, &s);
|
set_var(&n, &s);
|
||||||
|
}
|
||||||
eq(var_os(&n), Some(&s));
|
eq(var_os(&n), Some(&s));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,10 +75,12 @@ fn test_var_big() {
|
||||||
fn test_env_set_get_huge() {
|
fn test_env_set_get_huge() {
|
||||||
let n = make_rand_name();
|
let n = make_rand_name();
|
||||||
let s = "x".repeat(10000);
|
let s = "x".repeat(10000);
|
||||||
|
unsafe {
|
||||||
set_var(&n, &s);
|
set_var(&n, &s);
|
||||||
eq(var_os(&n), Some(&s));
|
eq(var_os(&n), Some(&s));
|
||||||
remove_var(&n);
|
remove_var(&n);
|
||||||
eq(var_os(&n), None);
|
eq(var_os(&n), None);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -78,7 +88,9 @@ fn test_env_set_var() {
|
||||||
let n = make_rand_name();
|
let n = make_rand_name();
|
||||||
|
|
||||||
let mut e = vars_os();
|
let mut e = vars_os();
|
||||||
|
unsafe {
|
||||||
set_var(&n, "VALUE");
|
set_var(&n, "VALUE");
|
||||||
|
}
|
||||||
assert!(!e.any(|(k, v)| { &*k == &*n && &*v == "VALUE" }));
|
assert!(!e.any(|(k, v)| { &*k == &*n && &*v == "VALUE" }));
|
||||||
|
|
||||||
assert!(vars_os().any(|(k, v)| { &*k == &*n && &*v == "VALUE" }));
|
assert!(vars_os().any(|(k, v)| { &*k == &*n && &*v == "VALUE" }));
|
||||||
|
@ -102,10 +114,12 @@ fn env_home_dir() {
|
||||||
if #[cfg(unix)] {
|
if #[cfg(unix)] {
|
||||||
let oldhome = var_to_os_string(var("HOME"));
|
let oldhome = var_to_os_string(var("HOME"));
|
||||||
|
|
||||||
|
unsafe {
|
||||||
set_var("HOME", "/home/MountainView");
|
set_var("HOME", "/home/MountainView");
|
||||||
assert_eq!(home_dir(), Some(PathBuf::from("/home/MountainView")));
|
assert_eq!(home_dir(), Some(PathBuf::from("/home/MountainView")));
|
||||||
|
|
||||||
remove_var("HOME");
|
remove_var("HOME");
|
||||||
|
}
|
||||||
if cfg!(target_os = "android") {
|
if cfg!(target_os = "android") {
|
||||||
assert!(home_dir().is_none());
|
assert!(home_dir().is_none());
|
||||||
} else {
|
} else {
|
||||||
|
@ -115,11 +129,12 @@ fn env_home_dir() {
|
||||||
assert_ne!(home_dir(), Some(PathBuf::from("/home/MountainView")));
|
assert_ne!(home_dir(), Some(PathBuf::from("/home/MountainView")));
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(oldhome) = oldhome { set_var("HOME", oldhome); }
|
if let Some(oldhome) = oldhome { unsafe { set_var("HOME", oldhome); } }
|
||||||
} else if #[cfg(windows)] {
|
} else if #[cfg(windows)] {
|
||||||
let oldhome = var_to_os_string(var("HOME"));
|
let oldhome = var_to_os_string(var("HOME"));
|
||||||
let olduserprofile = var_to_os_string(var("USERPROFILE"));
|
let olduserprofile = var_to_os_string(var("USERPROFILE"));
|
||||||
|
|
||||||
|
unsafe {
|
||||||
remove_var("HOME");
|
remove_var("HOME");
|
||||||
remove_var("USERPROFILE");
|
remove_var("USERPROFILE");
|
||||||
|
|
||||||
|
@ -144,6 +159,7 @@ fn env_home_dir() {
|
||||||
if let Some(olduserprofile) = olduserprofile { set_var("USERPROFILE", olduserprofile); }
|
if let Some(olduserprofile) = olduserprofile { set_var("USERPROFILE", olduserprofile); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] // miri shouldn't detect any data race in this fn
|
#[test] // miri shouldn't detect any data race in this fn
|
||||||
|
@ -157,8 +173,10 @@ fn test_env_get_set_multithreaded() {
|
||||||
|
|
||||||
let setter = thread::spawn(|| {
|
let setter = thread::spawn(|| {
|
||||||
for _ in 0..100 {
|
for _ in 0..100 {
|
||||||
|
unsafe {
|
||||||
set_var("foo", "bar");
|
set_var("foo", "bar");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let _ = getter.join();
|
let _ = getter.join();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue