Skip to content

Commit 26c9868

Browse files
committed
Auto merge of #108233 - ChrisDenton:move-std-tests, r=thomcc
Move some std tests from `tests/ui-fulldeps` into `library/std` This allows them to be tested normally along with other `./x test std` tests. Moving `rename_directory` is simple enough but `create_dir_all_bare` needed to be an std integration test. Additionally, some tests that I couldn't move atm have instead been placed in an `std` subdirectory. These tests include ones that do fun things with processes or that intentionally abort the test process. r? libs
2 parents 313219c + 0f221cc commit 26c9868

10 files changed

+115
-53
lines changed

library/std/src/fs/tests.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1595,3 +1595,19 @@ fn test_read_dir_infinite_loop() {
15951595
// Check for duplicate errors
15961596
assert!(dir.filter(|e| e.is_err()).take(2).count() < 2);
15971597
}
1598+
1599+
#[test]
1600+
fn rename_directory() {
1601+
let tmpdir = tmpdir();
1602+
let old_path = tmpdir.join("foo/bar/baz");
1603+
fs::create_dir_all(&old_path).unwrap();
1604+
let test_file = &old_path.join("temp.txt");
1605+
1606+
File::create(test_file).unwrap();
1607+
1608+
let new_path = tmpdir.join("quux/blat");
1609+
fs::create_dir_all(&new_path).unwrap();
1610+
fs::rename(&old_path, &new_path.join("newdir")).unwrap();
1611+
assert!(new_path.join("newdir").is_dir());
1612+
assert!(new_path.join("newdir/temp.txt").exists());
1613+
}

library/std/tests/common/mod.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#![allow(unused)]
2+
3+
use std::env;
4+
use std::fs;
5+
use std::path::{Path, PathBuf};
6+
use std::thread;
7+
use rand::RngCore;
8+
9+
/// Copied from `std::test_helpers::test_rng`, since these tests rely on the
10+
/// seed not being the same for every RNG invocation too.
11+
#[track_caller]
12+
pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
13+
use core::hash::{BuildHasher, Hash, Hasher};
14+
let mut hasher = std::collections::hash_map::RandomState::new().build_hasher();
15+
core::panic::Location::caller().hash(&mut hasher);
16+
let hc64 = hasher.finish();
17+
let seed_vec = hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<Vec<u8>>();
18+
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
19+
rand::SeedableRng::from_seed(seed)
20+
}
21+
22+
// Copied from std::sys_common::io
23+
pub struct TempDir(PathBuf);
24+
25+
impl TempDir {
26+
pub fn join(&self, path: &str) -> PathBuf {
27+
let TempDir(ref p) = *self;
28+
p.join(path)
29+
}
30+
31+
pub fn path(&self) -> &Path {
32+
let TempDir(ref p) = *self;
33+
p
34+
}
35+
}
36+
37+
impl Drop for TempDir {
38+
fn drop(&mut self) {
39+
// Gee, seeing how we're testing the fs module I sure hope that we
40+
// at least implement this correctly!
41+
let TempDir(ref p) = *self;
42+
let result = fs::remove_dir_all(p);
43+
// Avoid panicking while panicking as this causes the process to
44+
// immediately abort, without displaying test results.
45+
if !thread::panicking() {
46+
result.unwrap();
47+
}
48+
}
49+
}
50+
51+
#[track_caller] // for `test_rng`
52+
pub fn tmpdir() -> TempDir {
53+
let p = env::temp_dir();
54+
let mut r = test_rng();
55+
let ret = p.join(&format!("rust-{}", r.next_u32()));
56+
fs::create_dir(&ret).unwrap();
57+
TempDir(ret)
58+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#![cfg(all(test, not(any(target_os = "emscripten", target_env = "sgx"))))]
2+
3+
//! Note that this test changes the current directory so
4+
//! should not be in the same process as other tests.
5+
use std::env;
6+
use std::fs;
7+
use std::path::{Path, PathBuf};
8+
9+
mod common;
10+
11+
// On some platforms, setting the current directory will prevent deleting it.
12+
// So this helper ensures the current directory is reset.
13+
struct CurrentDir(PathBuf);
14+
impl CurrentDir {
15+
fn new() -> Self {
16+
Self(env::current_dir().unwrap())
17+
}
18+
fn set(&self, path: &Path) {
19+
env::set_current_dir(path).unwrap();
20+
}
21+
fn with(path: &Path, f: impl FnOnce()) {
22+
let current_dir = Self::new();
23+
current_dir.set(path);
24+
f();
25+
}
26+
}
27+
impl Drop for CurrentDir {
28+
fn drop(&mut self) {
29+
env::set_current_dir(&self.0).unwrap();
30+
}
31+
}
32+
33+
#[test]
34+
fn create_dir_all_bare() {
35+
let tmpdir = common::tmpdir();
36+
CurrentDir::with(tmpdir.path(), || {
37+
fs::create_dir_all("create-dir-all-bare").unwrap();
38+
});
39+
}

library/std/tests/env.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,8 @@ use std::ffi::{OsStr, OsString};
33

44
use rand::distributions::{Alphanumeric, DistString};
55

6-
/// Copied from `std::test_helpers::test_rng`, since these tests rely on the
7-
/// seed not being the same for every RNG invocation too.
8-
#[track_caller]
9-
pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
10-
use core::hash::{BuildHasher, Hash, Hasher};
11-
let mut hasher = std::collections::hash_map::RandomState::new().build_hasher();
12-
core::panic::Location::caller().hash(&mut hasher);
13-
let hc64 = hasher.finish();
14-
let seed_vec = hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<Vec<u8>>();
15-
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
16-
rand::SeedableRng::from_seed(seed)
17-
}
6+
mod common;
7+
use common::test_rng;
188

199
#[track_caller]
2010
fn make_rand_name() -> OsString {

tests/ui-fulldeps/create-dir-all-bare.rs

-11
This file was deleted.

tests/ui-fulldeps/rename-directory.rs

-30
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)