Skip to content

Move some std tests from tests/ui-fulldeps into library/std #108233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions library/std/src/fs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 58 additions & 0 deletions library/std/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 39 additions & 0 deletions library/std/tests/create_dir_all_bare.rs
Original file line number Diff line number Diff line change
@@ -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();
});
}
14 changes: 2 additions & 12 deletions library/std/tests/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,8 @@ use std::ffi::{OsStr, OsString};

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 {
Expand Down
11 changes: 0 additions & 11 deletions tests/ui-fulldeps/create-dir-all-bare.rs

This file was deleted.

30 changes: 0 additions & 30 deletions tests/ui-fulldeps/rename-directory.rs

This file was deleted.

File renamed without changes.
File renamed without changes.