1 use std::{io::Write, path::PathBuf};
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 data = &mut Vec::with_capacity((rng.next_u32() % 8192) as usize);
20 file.write_all(data).unwrap();
23 // check that two directories have the exact same content recursively
24 // checks equility of file types if preserve_permissions is true, ignored on non-unix
25 pub fn assert_same_directory(x: impl Into<PathBuf>, y: impl Into<PathBuf>, preserve_permissions: bool) {
26 fn read_dir(dir: impl Into<PathBuf>) -> impl Iterator<Item = fs::DirEntry> {
27 let mut dir: Vec<_> = fs::read_dir(dir).unwrap().map(|entry| entry.unwrap()).collect();
28 dir.sort_by_key(|x| x.file_name());
32 let mut x = read_dir(x);
33 let mut y = read_dir(y);
36 match (x.next(), y.next()) {
37 (Some(x), Some(y)) => {
38 assert_eq!(x.file_name(), y.file_name());
40 let meta_x = x.metadata().unwrap();
41 let meta_y = y.metadata().unwrap();
42 let ft_x = meta_x.file_type();
43 let ft_y = meta_y.file_type();
46 if preserve_permissions {
47 assert_eq!(ft_x, ft_y);
50 if ft_x.is_dir() && ft_y.is_dir() {
51 assert_same_directory(x.path(), y.path(), preserve_permissions);
52 } else if ft_x.is_file() && ft_y.is_file() {
53 assert_eq!(meta_x.len(), meta_y.len());
54 assert_eq!(fs::read(x.path()).unwrap(), fs::read(y.path()).unwrap());
57 "entries should be both directories or both files\n left: `{:?}`,\n right: `{:?}`",
64 (None, None) => break,
68 "directories don't have the same number of entires\n left: `{:?}`,\n right: `{:?}`",
79 assert_same_directory("src", "src", true);
84 fn src_is_not_tests() {
85 assert_same_directory("src", "tests", false);