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
This commit is contained in:
commit
26c98689f2
@ -1595,3 +1595,19 @@ fn test_read_dir_infinite_loop() {
|
||||
// Check for duplicate errors
|
||||
assert!(dir.filter(|e| e.is_err()).take(2).count() < 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rename_directory() {
|
||||
let tmpdir = tmpdir();
|
||||
let old_path = tmpdir.join("foo/bar/baz");
|
||||
fs::create_dir_all(&old_path).unwrap();
|
||||
let test_file = &old_path.join("temp.txt");
|
||||
|
||||
File::create(test_file).unwrap();
|
||||
|
||||
let new_path = tmpdir.join("quux/blat");
|
||||
fs::create_dir_all(&new_path).unwrap();
|
||||
fs::rename(&old_path, &new_path.join("newdir")).unwrap();
|
||||
assert!(new_path.join("newdir").is_dir());
|
||||
assert!(new_path.join("newdir/temp.txt").exists());
|
||||
}
|
||||
|
58
library/std/tests/common/mod.rs
Normal file
58
library/std/tests/common/mod.rs
Normal file
@ -0,0 +1,58 @@
|
||||
#![allow(unused)]
|
||||
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::thread;
|
||||
use rand::RngCore;
|
||||
|
||||
/// Copied from `std::test_helpers::test_rng`, since these tests rely on the
|
||||
/// seed not being the same for every RNG invocation too.
|
||||
#[track_caller]
|
||||
pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
|
||||
use core::hash::{BuildHasher, Hash, Hasher};
|
||||
let mut hasher = std::collections::hash_map::RandomState::new().build_hasher();
|
||||
core::panic::Location::caller().hash(&mut hasher);
|
||||
let hc64 = hasher.finish();
|
||||
let seed_vec = hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<Vec<u8>>();
|
||||
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
|
||||
rand::SeedableRng::from_seed(seed)
|
||||
}
|
||||
|
||||
// Copied from std::sys_common::io
|
||||
pub struct TempDir(PathBuf);
|
||||
|
||||
impl TempDir {
|
||||
pub fn join(&self, path: &str) -> PathBuf {
|
||||
let TempDir(ref p) = *self;
|
||||
p.join(path)
|
||||
}
|
||||
|
||||
pub fn path(&self) -> &Path {
|
||||
let TempDir(ref p) = *self;
|
||||
p
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TempDir {
|
||||
fn drop(&mut self) {
|
||||
// Gee, seeing how we're testing the fs module I sure hope that we
|
||||
// at least implement this correctly!
|
||||
let TempDir(ref p) = *self;
|
||||
let result = fs::remove_dir_all(p);
|
||||
// Avoid panicking while panicking as this causes the process to
|
||||
// immediately abort, without displaying test results.
|
||||
if !thread::panicking() {
|
||||
result.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[track_caller] // for `test_rng`
|
||||
pub fn tmpdir() -> TempDir {
|
||||
let p = env::temp_dir();
|
||||
let mut r = test_rng();
|
||||
let ret = p.join(&format!("rust-{}", r.next_u32()));
|
||||
fs::create_dir(&ret).unwrap();
|
||||
TempDir(ret)
|
||||
}
|
39
library/std/tests/create_dir_all_bare.rs
Normal file
39
library/std/tests/create_dir_all_bare.rs
Normal file
@ -0,0 +1,39 @@
|
||||
#![cfg(all(test, not(any(target_os = "emscripten", target_env = "sgx"))))]
|
||||
|
||||
//! Note that this test changes the current directory so
|
||||
//! should not be in the same process as other tests.
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
mod common;
|
||||
|
||||
// On some platforms, setting the current directory will prevent deleting it.
|
||||
// So this helper ensures the current directory is reset.
|
||||
struct CurrentDir(PathBuf);
|
||||
impl CurrentDir {
|
||||
fn new() -> Self {
|
||||
Self(env::current_dir().unwrap())
|
||||
}
|
||||
fn set(&self, path: &Path) {
|
||||
env::set_current_dir(path).unwrap();
|
||||
}
|
||||
fn with(path: &Path, f: impl FnOnce()) {
|
||||
let current_dir = Self::new();
|
||||
current_dir.set(path);
|
||||
f();
|
||||
}
|
||||
}
|
||||
impl Drop for CurrentDir {
|
||||
fn drop(&mut self) {
|
||||
env::set_current_dir(&self.0).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_dir_all_bare() {
|
||||
let tmpdir = common::tmpdir();
|
||||
CurrentDir::with(tmpdir.path(), || {
|
||||
fs::create_dir_all("create-dir-all-bare").unwrap();
|
||||
});
|
||||
}
|
@ -3,18 +3,8 @@
|
||||
|
||||
use rand::distributions::{Alphanumeric, DistString};
|
||||
|
||||
/// Copied from `std::test_helpers::test_rng`, since these tests rely on the
|
||||
/// seed not being the same for every RNG invocation too.
|
||||
#[track_caller]
|
||||
pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
|
||||
use core::hash::{BuildHasher, Hash, Hasher};
|
||||
let mut hasher = std::collections::hash_map::RandomState::new().build_hasher();
|
||||
core::panic::Location::caller().hash(&mut hasher);
|
||||
let hc64 = hasher.finish();
|
||||
let seed_vec = hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<Vec<u8>>();
|
||||
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
|
||||
rand::SeedableRng::from_seed(seed)
|
||||
}
|
||||
mod common;
|
||||
use common::test_rng;
|
||||
|
||||
#[track_caller]
|
||||
fn make_rand_name() -> OsString {
|
||||
|
@ -1,11 +0,0 @@
|
||||
// run-pass
|
||||
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
let path = PathBuf::from(env::var_os("RUST_TEST_TMPDIR").unwrap());
|
||||
env::set_current_dir(&path).unwrap();
|
||||
fs::create_dir_all("create-dir-all-bare").unwrap();
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
// run-pass
|
||||
|
||||
#![allow(unused_must_use)]
|
||||
#![allow(unused_imports)]
|
||||
// This test can't be a unit test in std,
|
||||
// because it needs TempDir, which is in extra
|
||||
|
||||
// ignore-cross-compile
|
||||
|
||||
use std::env;
|
||||
use std::ffi::CString;
|
||||
use std::fs::{self, File};
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn rename_directory() {
|
||||
let tmpdir = PathBuf::from(env::var_os("RUST_TEST_TMPDIR").unwrap());
|
||||
let old_path = tmpdir.join("foo/bar/baz");
|
||||
fs::create_dir_all(&old_path).unwrap();
|
||||
let test_file = &old_path.join("temp.txt");
|
||||
|
||||
File::create(test_file).unwrap();
|
||||
|
||||
let new_path = tmpdir.join("quux/blat");
|
||||
fs::create_dir_all(&new_path).unwrap();
|
||||
fs::rename(&old_path, &new_path.join("newdir"));
|
||||
assert!(new_path.join("newdir").is_dir());
|
||||
assert!(new_path.join("newdir/temp.txt").exists());
|
||||
}
|
||||
|
||||
pub fn main() { rename_directory() }
|
Loading…
Reference in New Issue
Block a user