1 // This warning is unavoidable when reusing testing utils.
13 use assert_cmd::Command;
15 use rand::{Rng, RngCore};
17 // Run ouch with the provided arguments, returns `assert_cmd::Output`
21 $crate::utils::cargo_bin()
28 pub fn cargo_bin() -> Command {
31 (k.starts_with("CARGO_TARGET_") && k.ends_with("_RUNNER")).then(|| {
32 let mut runner = v.split_whitespace();
33 let mut cmd = Command::new(runner.next().unwrap());
34 cmd.args(runner).arg(assert_cmd::cargo::cargo_bin("ouch"));
38 .unwrap_or_else(|| Command::cargo_bin("ouch").expect("Failed to find ouch executable"))
41 /// Run a command inside of another folder.
43 /// example: `run_in("/tmp", "touch", "a b c")`
44 pub fn run_in(folder: impl AsRef<Path>, bin: impl AsRef<OsStr>, args: &str) -> io::Result<Output> {
46 .args(args.split_whitespace())
51 // write random content to a file
52 pub fn write_random_content(file: &mut impl Write, rng: &mut impl RngCore) {
53 let mut data = Vec::new();
54 data.resize(rng.gen_range(0..4096), 0);
55 rng.fill_bytes(&mut data);
56 file.write_all(&data).unwrap();
59 // check that two directories have the exact same content recursively
60 // checks equility of file types if preserve_permissions is true, ignored on non-unix
61 pub fn assert_same_directory(x: impl Into<PathBuf>, y: impl Into<PathBuf>, preserve_permissions: bool) {
62 fn read_dir(dir: impl Into<PathBuf>) -> impl Iterator<Item = fs::DirEntry> {
63 let mut dir: Vec<_> = fs::read_dir(dir).unwrap().map(|entry| entry.unwrap()).collect();
64 dir.sort_by_key(|x| x.file_name());
68 let mut x = read_dir(x);
69 let mut y = read_dir(y);
72 match (x.next(), y.next()) {
73 (Some(x), Some(y)) => {
74 assert_eq!(x.file_name(), y.file_name());
76 let meta_x = x.metadata().unwrap();
77 let meta_y = y.metadata().unwrap();
78 let ft_x = meta_x.file_type();
79 let ft_y = meta_y.file_type();
82 if preserve_permissions {
83 assert_eq!(ft_x, ft_y);
86 if ft_x.is_dir() && ft_y.is_dir() {
87 assert_same_directory(x.path(), y.path(), preserve_permissions);
88 } else if ft_x.is_file() && ft_y.is_file() {
89 assert_eq!(meta_x.len(), meta_y.len());
90 assert_eq!(fs::read(x.path()).unwrap(), fs::read(y.path()).unwrap());
93 "entries should be both directories or both files\n left: `{:?}`,\n right: `{:?}`",
100 (None, None) => break,
104 "directories don't have the same number of entries\n left: `{:?}`,\n right: `{:?}`",
115 assert_same_directory("src", "src", true);
120 fn src_is_not_tests() {
121 assert_same_directory("src", "tests", false);