1 use std::{io::Write, path::PathBuf};
4 use rand::{Rng, RngCore};
9 ::assert_cmd::Command::cargo_bin("ouch")
10 .expect("Failed to find ouch executable")
16 // write random content to a file
17 pub fn write_random_content(file: &mut impl Write, rng: &mut impl RngCore) {
18 let mut data = Vec::new();
19 data.resize(rng.gen_range(0..8192), 0);
20 rng.fill_bytes(&mut data);
21 file.write_all(&data).unwrap();
24 // check that two directories have the exact same content recursively
25 // checks equility of file types if preserve_permissions is true, ignored on non-unix
26 pub fn assert_same_directory(x: impl Into<PathBuf>, y: impl Into<PathBuf>, preserve_permissions: bool) {
27 fn read_dir(dir: impl Into<PathBuf>) -> impl Iterator<Item = fs::DirEntry> {
28 let mut dir: Vec<_> = fs::read_dir(dir).unwrap().map(|entry| entry.unwrap()).collect();
29 dir.sort_by_key(|x| x.file_name());
33 let mut x = read_dir(x);
34 let mut y = read_dir(y);
37 match (x.next(), y.next()) {
38 (Some(x), Some(y)) => {
39 assert_eq!(x.file_name(), y.file_name());
41 let meta_x = x.metadata().unwrap();
42 let meta_y = y.metadata().unwrap();
43 let ft_x = meta_x.file_type();
44 let ft_y = meta_y.file_type();
47 if preserve_permissions {
48 assert_eq!(ft_x, ft_y);
51 if ft_x.is_dir() && ft_y.is_dir() {
52 assert_same_directory(x.path(), y.path(), preserve_permissions);
53 } else if ft_x.is_file() && ft_y.is_file() {
54 assert_eq!(meta_x.len(), meta_y.len());
55 assert_eq!(fs::read(x.path()).unwrap(), fs::read(y.path()).unwrap());
58 "entries should be both directories or both files\n left: `{:?}`,\n right: `{:?}`",
65 (None, None) => break,
69 "directories don't have the same number of entires\n left: `{:?}`,\n right: `{:?}`",
80 assert_same_directory("src", "src", true);
85 fn src_is_not_tests() {
86 assert_same_directory("src", "tests", false);