Replace shell commands with File::create

This commit is contained in:
Antonios Barotsis 2024-01-27 10:51:25 +01:00 committed by João Marcos
parent 5954e98427
commit 63ad6b419c
2 changed files with 17 additions and 16 deletions

View File

@ -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));

View File

@ -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<Path>, bin: impl AsRef<OsStr>, args: &str) -> io::Result<Output> {
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) {