1 use std::{env, io::Write, path::PathBuf};
3 use assert_cmd::Command;
5 use rand::{Rng, RngCore};
10 $crate::utils::cargo_bin()
17 pub fn cargo_bin() -> Command {
20 (k.starts_with("CARGO_TARGET_") && k.ends_with("_RUNNER")).then(|| {
21 let mut runner = v.split_whitespace();
22 let mut cmd = Command::new(runner.next().unwrap());
23 cmd.args(runner).arg(assert_cmd::cargo::cargo_bin("ouch"));
27 .unwrap_or_else(|| Command::cargo_bin("ouch").expect("Failed to find ouch executable"))
30 // write random content to a file
31 pub fn write_random_content(file: &mut impl Write, rng: &mut impl RngCore) {
32 let mut data = Vec::new();
33 data.resize(rng.gen_range(0..8192), 0);
34 rng.fill_bytes(&mut data);
35 file.write_all(&data).unwrap();
38 // check that two directories have the exact same content recursively
39 // checks equility of file types if preserve_permissions is true, ignored on non-unix
40 pub fn assert_same_directory(x: impl Into<PathBuf>, y: impl Into<PathBuf>, preserve_permissions: bool) {
41 fn read_dir(dir: impl Into<PathBuf>) -> impl Iterator<Item = fs::DirEntry> {
42 let mut dir: Vec<_> = fs::read_dir(dir).unwrap().map(|entry| entry.unwrap()).collect();
43 dir.sort_by_key(|x| x.file_name());
47 let mut x = read_dir(x);
48 let mut y = read_dir(y);
51 match (x.next(), y.next()) {
52 (Some(x), Some(y)) => {
53 assert_eq!(x.file_name(), y.file_name());
55 let meta_x = x.metadata().unwrap();
56 let meta_y = y.metadata().unwrap();
57 let ft_x = meta_x.file_type();
58 let ft_y = meta_y.file_type();
61 if preserve_permissions {
62 assert_eq!(ft_x, ft_y);
65 if ft_x.is_dir() && ft_y.is_dir() {
66 assert_same_directory(x.path(), y.path(), preserve_permissions);
67 } else if ft_x.is_file() && ft_y.is_file() {
68 assert_eq!(meta_x.len(), meta_y.len());
69 assert_eq!(fs::read(x.path()).unwrap(), fs::read(y.path()).unwrap());
72 "entries should be both directories or both files\n left: `{:?}`,\n right: `{:?}`",
79 (None, None) => break,
83 "directories don't have the same number of entries\n left: `{:?}`,\n right: `{:?}`",
94 assert_same_directory("src", "src", true);
99 fn src_is_not_tests() {
100 assert_same_directory("src", "tests", false);