From 63ad6b419c219945c46afa46f49d3a0b57664888 Mon Sep 17 00:00:00 2001 From: Antonios Barotsis Date: Sat, 27 Jan 2024 10:51:25 +0100 Subject: [PATCH] Replace shell commands with `File::create` --- tests/ui.rs | 10 +++++----- tests/utils.rs | 23 ++++++++++++----------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/tests/ui.rs b/tests/ui.rs index 698c86e..9800780 100644 --- a/tests/ui.rs +++ b/tests/ui.rs @@ -11,7 +11,7 @@ use std::{ffi::OsStr, io, path::Path, process::Output}; use insta::assert_display_snapshot as ui; use regex::Regex; -use crate::utils::run_in; +use crate::utils::create_files_in; fn testdir() -> io::Result<(tempfile::TempDir, &'static Path)> { let dir = tempfile::tempdir()?; @@ -54,7 +54,7 @@ fn ui_test_err_compress_missing_extension() { let (_dropper, dir) = testdir().unwrap(); // prepare - run_in(dir, "touch", "input").unwrap(); + create_files_in(dir, &["input"]); ui!(run_ouch("ouch compress input output", dir)); } @@ -63,7 +63,7 @@ fn ui_test_err_compress_missing_extension() { fn ui_test_err_decompress_missing_extension() { let (_dropper, dir) = testdir().unwrap(); - run_in(dir, "touch", "a b.unknown").unwrap(); + create_files_in(dir, &["a", "b.unknown"]); let name = { let suffix = if cfg!(feature = "unrar") { @@ -92,7 +92,7 @@ fn ui_test_ok_compress() { let (_dropper, dir) = testdir().unwrap(); // prepare - run_in(dir, "touch", "input").unwrap(); + create_files_in(dir, &["input"]); ui!(run_ouch("ouch compress input output.zip", dir)); ui!(run_ouch("ouch compress input output.gz", dir)); @@ -103,7 +103,7 @@ fn ui_test_ok_decompress() { let (_dropper, dir) = testdir().unwrap(); // prepare - run_in(dir, "touch", "input").unwrap(); + create_files_in(dir, &["input"]); run_ouch("ouch compress input output.zst", dir); ui!(run_ouch("ouch decompress output.zst", dir)); diff --git a/tests/utils.rs b/tests/utils.rs index 3682e99..ed7237a 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -3,11 +3,8 @@ use std::{ env, - ffi::OsStr, - io, io::Write, path::{Path, PathBuf}, - process::Output, }; use assert_cmd::Command; @@ -38,15 +35,19 @@ pub fn cargo_bin() -> Command { .unwrap_or_else(|| Command::cargo_bin("ouch").expect("Failed to find ouch executable")) } -/// Run a command inside of another folder. +/// Creates files in the specified directory. /// -/// example: `run_in("/tmp", "touch", "a b c")` -pub fn run_in(folder: impl AsRef, bin: impl AsRef, args: &str) -> io::Result { - Command::new(bin) - .args(args.split_whitespace()) - .current_dir(folder) - .output() -} +/// ## Example +/// +/// ```no_run +/// let (_dropper, dir) = testdir().unwrap(); +/// create_files_in(dir, &["file1.txt", "file2.txt"]); +/// ``` +pub fn create_files_in(dir: &Path, files: &[&str]) { + for f in files { + std::fs::File::create(dir.join(f)).unwrap(); + } + } // write random content to a file pub fn write_random_content(file: &mut impl Write, rng: &mut impl RngCore) {