1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 11:07:44 +00:00

Create the uutest crate + adjust the code

+ move some of the tests into the program test
This commit is contained in:
Sylvestre Ledru 2025-03-28 09:22:03 +01:00
parent bf337a29af
commit 50fe623447
11 changed files with 211 additions and 64 deletions

View file

@ -2,15 +2,26 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
#![allow(unused_imports)]
mod common;
use common::util::TestScenario;
use uutests::util::TestScenario;
#[cfg(unix)]
use std::os::unix::fs::symlink as symlink_file;
#[cfg(windows)]
use std::os::windows::fs::symlink_file;
use std::env;
pub const TESTS_BINARY: &str = env!("CARGO_BIN_EXE_coreutils");
// Set the environment variable for any tests
// Use the ctor attribute to run this function before any tests
#[ctor::ctor]
fn init() {
// No need for unsafe here
unsafe {
std::env::set_var("UUTESTS_BINARY_PATH", TESTS_BINARY);
}
// Print for debugging
eprintln!("Setting UUTESTS_BINARY_PATH={}", TESTS_BINARY);
}
#[test]
#[cfg(feature = "ls")]
@ -18,6 +29,10 @@ fn execution_phrase_double() {
use std::process::Command;
let scenario = TestScenario::new("ls");
if !scenario.bin_path.exists() {
println!("Skipping test: Binary not found at {:?}", scenario.bin_path);
return;
}
let output = Command::new(&scenario.bin_path)
.arg("ls")
.arg("--some-invalid-arg")
@ -30,25 +45,6 @@ fn execution_phrase_double() {
);
}
#[test]
#[cfg(feature = "ls")]
#[cfg(any(unix, windows))]
fn execution_phrase_single() {
use std::process::Command;
let scenario = TestScenario::new("ls");
symlink_file(&scenario.bin_path, scenario.fixtures.plus("uu-ls")).unwrap();
let output = Command::new(scenario.fixtures.plus("uu-ls"))
.arg("--some-invalid-arg")
.output()
.unwrap();
dbg!(String::from_utf8(output.stderr.clone()).unwrap());
assert!(String::from_utf8(output.stderr).unwrap().contains(&format!(
"Usage: {}",
scenario.fixtures.plus("uu-ls").display()
)));
}
#[test]
#[cfg(feature = "sort")]
fn util_name_double() {
@ -58,6 +54,10 @@ fn util_name_double() {
};
let scenario = TestScenario::new("sort");
if !scenario.bin_path.exists() {
println!("Skipping test: Binary not found at {:?}", scenario.bin_path);
return;
}
let mut child = Command::new(&scenario.bin_path)
.arg("sort")
.stdin(Stdio::piped())
@ -72,7 +72,7 @@ fn util_name_double() {
#[test]
#[cfg(feature = "sort")]
#[cfg(any(unix, windows))]
#[cfg(unix)]
fn util_name_single() {
use std::{
io::Write,
@ -80,6 +80,11 @@ fn util_name_single() {
};
let scenario = TestScenario::new("sort");
if !scenario.bin_path.exists() {
println!("Skipping test: Binary not found at {:?}", scenario.bin_path);
return;
}
symlink_file(&scenario.bin_path, scenario.fixtures.plus("uu-sort")).unwrap();
let mut child = Command::new(scenario.fixtures.plus("uu-sort"))
.stdin(Stdio::piped())
@ -96,14 +101,15 @@ fn util_name_single() {
}
#[test]
#[cfg(any(unix, windows))]
#[cfg(unix)]
fn util_invalid_name_help() {
use std::{
io::Write,
process::{Command, Stdio},
};
use std::process::{Command, Stdio};
let scenario = TestScenario::new("invalid_name");
if !scenario.bin_path.exists() {
println!("Skipping test: Binary not found at {:?}", scenario.bin_path);
return;
}
symlink_file(&scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
let child = Command::new(scenario.fixtures.plus("invalid_name"))
.arg("--help")
@ -132,14 +138,17 @@ fn util_non_utf8_name_help() {
// Make sure we don't crash even if the util name is invalid UTF-8.
use std::{
ffi::OsStr,
io::Write,
os::unix::ffi::OsStrExt,
path::Path,
process::{Command, Stdio},
};
let scenario = TestScenario::new("invalid_name");
let non_utf8_path = scenario.fixtures.plus(OsStr::from_bytes(b"\xff"));
if !scenario.bin_path.exists() {
println!("Skipping test: Binary not found at {:?}", scenario.bin_path);
return;
}
symlink_file(&scenario.bin_path, &non_utf8_path).unwrap();
let child = Command::new(&non_utf8_path)
.arg("--help")
@ -160,15 +169,17 @@ fn util_non_utf8_name_help() {
}
#[test]
#[cfg(any(unix, windows))]
#[cfg(unix)]
fn util_invalid_name_invalid_command() {
use std::{
io::Write,
process::{Command, Stdio},
};
use std::process::{Command, Stdio};
let scenario = TestScenario::new("invalid_name");
symlink_file(&scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
if !scenario.bin_path.exists() {
println!("Skipping test: Binary not found at {:?}", scenario.bin_path);
return;
}
let child = Command::new(scenario.fixtures.plus("invalid_name"))
.arg("definitely_invalid")
.stdin(Stdio::piped())
@ -188,12 +199,14 @@ fn util_invalid_name_invalid_command() {
#[test]
#[cfg(feature = "true")]
fn util_completion() {
use std::{
io::Write,
process::{Command, Stdio},
};
use std::process::{Command, Stdio};
let scenario = TestScenario::new("completion");
if !scenario.bin_path.exists() {
println!("Skipping test: Binary not found at {:?}", scenario.bin_path);
return;
}
let child = Command::new(&scenario.bin_path)
.arg("completion")
.arg("true")
@ -216,12 +229,14 @@ fn util_completion() {
#[test]
#[cfg(feature = "true")]
fn util_manpage() {
use std::{
io::Write,
process::{Command, Stdio},
};
use std::process::{Command, Stdio};
let scenario = TestScenario::new("completion");
if !scenario.bin_path.exists() {
println!("Skipping test: Binary not found at {:?}", scenario.bin_path);
return;
}
let child = Command::new(&scenario.bin_path)
.arg("manpage")
.arg("true")