• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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]
test_rng() -> rand_xorshift::XorShiftRng12 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(crate) struct TempDir(PathBuf);
24 
25 impl TempDir {
join(&self, path: &str) -> PathBuf26     pub(crate) fn join(&self, path: &str) -> PathBuf {
27         let TempDir(ref p) = *self;
28         p.join(path)
29     }
30 
path(&self) -> &Path31     pub(crate) fn path(&self) -> &Path {
32         let TempDir(ref p) = *self;
33         p
34     }
35 }
36 
37 impl Drop for TempDir {
drop(&mut self)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`
tmpdir() -> TempDir52 pub(crate) 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 }
59