Skip to content

Commit 9b18b44

Browse files
committed
Make create_dir_all_bare an std integration test
Moving `create_dir_all` out of `ui-fulldeps` is complicated by the fact it sets the current directory. This means it can't be a unit test. Instead, move it to its own integration test.
1 parent f7a132f commit 9b18b44

File tree

4 files changed

+97
-23
lines changed

4 files changed

+97
-23
lines changed

Diff for: 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+
}

Diff for: library/std/tests/create_dir_all_bare.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//! Note that this test changes the current directory so
2+
//! should not be in the same process as other tests.
3+
use std::env;
4+
use std::fs;
5+
use std::path::{Path, PathBuf};
6+
7+
mod common;
8+
9+
// On some platforms, setting the current directory will prevent deleting it.
10+
// So this helper ensures the current directory is reset.
11+
struct CurrentDir(PathBuf);
12+
impl CurrentDir {
13+
fn new() -> Self {
14+
Self(env::current_dir().unwrap())
15+
}
16+
fn set(&self, path: &Path) {
17+
env::set_current_dir(path).unwrap();
18+
}
19+
fn with(path: &Path, f: impl FnOnce()) {
20+
let current_dir = Self::new();
21+
current_dir.set(path);
22+
f();
23+
}
24+
}
25+
impl Drop for CurrentDir {
26+
fn drop(&mut self) {
27+
env::set_current_dir(&self.0).unwrap();
28+
}
29+
}
30+
31+
#[test]
32+
fn create_dir_all_bare() {
33+
let tmpdir = common::tmpdir();
34+
CurrentDir::with(tmpdir.path(), || {
35+
fs::create_dir_all("create-dir-all-bare").unwrap();
36+
});
37+
}

Diff for: 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 {

Diff for: tests/ui-fulldeps/std/create-dir-all-bare.rs

-11
This file was deleted.

0 commit comments

Comments
 (0)