Compare commits

...

2 commits

Author SHA1 Message Date
askiiart
4dd1b3bcc5
bump 2025-01-11 15:35:16 -06:00
askiiart
13c214780d
update tests 2025-01-11 15:34:58 -06:00
3 changed files with 58 additions and 36 deletions

2
Cargo.lock generated
View file

@ -4,4 +4,4 @@ version = 4
[[package]]
name = "better-commands"
version = "0.1.1"
version = "0.1.2"

View file

@ -1,10 +1,11 @@
[package]
name = "better-commands"
description = "A better way of running commands - get stdout and stderr together, in order with timestamps, while easily running code as it runs line-by-line"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
license = "GPL-3.0-only"
keywords = ["command", "cmd"]
repository = "https://git.askiiart.net/askiiart/better-commands-rs"
[profile.release]
opt-level = 3

View file

@ -1,11 +1,24 @@
#[cfg(test)]
use crate::*;
use std::process::Command;
use std::{
fs::remove_file,
hash::{BuildHasher, Hasher, RandomState},
};
use std::{fs::File, os::unix::fs::FileExt, thread::sleep};
use std::process::Command;
use crate::command;
#[macro_export]
macro_rules! command {
($command:expr, $($args:expr),*) => {
{
Command::new($command)
$(
.arg($args)
)*
}
};
}
/// Tests what stdout prints
#[test]
@ -89,8 +102,8 @@ fn shuffle_vec<T>(vec: &mut [T]) {
#[test]
fn test_run_funcs() {
let _ = thread::spawn(|| {
let _ = run_funcs(
let threads = thread::spawn(|| {
return run_funcs(
Command::new("bash")
.arg("-c")
.arg("echo hi; >&2 echo hello"),
@ -98,9 +111,7 @@ fn test_run_funcs() {
|stdout_lines| {
sleep(Duration::from_secs(1));
for _ in stdout_lines {
Command::new("bash")
.arg("-c")
.arg("echo stdout >> ./tmp-run_runcs")
command!("bash", "-c", "echo stdout >> ./tmp-run_funcs") // col
.output()
.unwrap();
}
@ -110,9 +121,7 @@ fn test_run_funcs() {
|stderr_lines| {
sleep(Duration::from_secs(3));
for _ in stderr_lines {
Command::new("bash")
.arg("-c")
.arg("echo stderr >> ./tmp-run_runcs")
command!("bash", "-c", "echo stderr >> ./tmp-run_funcs") // col
.output()
.unwrap();
}
@ -121,7 +130,7 @@ fn test_run_funcs() {
);
});
sleep(Duration::from_secs(2));
let f = File::open("./tmp-run_runcs").unwrap();
let f = File::open("./tmp-run_funcs").unwrap();
let mut buf: [u8; 14] = [0u8; 14];
f.read_at(&mut buf, 0).unwrap();
assert_eq!(buf, [115, 116, 100, 111, 117, 116, 10, 0, 0, 0, 0, 0, 0, 0]);
@ -133,48 +142,53 @@ fn test_run_funcs() {
[115, 116, 100, 111, 117, 116, 10, 115, 116, 100, 101, 114, 114, 10]
);
remove_file("./tmp-run_runcs").unwrap();
remove_file("./tmp-run_funcs").unwrap();
let output = threads.join().unwrap();
assert_eq!(output.clone().lines(), None);
}
#[test]
fn test_run_funcs_with_lines() {
let _ = thread::spawn(|| {
let _ = run_funcs_with_lines(
let threads = thread::spawn(|| {
return run_funcs_with_lines(
Command::new("bash")
.arg("-c")
.arg("echo hi; >&2 echo hello"),
{
|stdout_lines| {
let mut lines: Vec<Line> = Vec::new();
sleep(Duration::from_secs(1));
for line in stdout_lines {
assert_eq!(line.unwrap(), "hi");
Command::new("bash")
.arg("-c")
.arg("echo stdout >> ./tmp-run_runcs_with_lines")
let line = line.unwrap();
lines.push(Line::from_stdout(&line));
assert_eq!(&line, "hi");
command!("bash", "-c", "echo stdout >> ./tmp-run_funcs_with_lines")
.output()
.unwrap();
}
return Vec::new();
return lines;
}
},
{
|stderr_lines| {
let mut lines: Vec<Line> = Vec::new();
sleep(Duration::from_secs(3));
for line in stderr_lines {
assert_eq!(line.unwrap(), "hello");
Command::new("bash")
.arg("-c")
.arg("echo stderr >> ./tmp-run_runcs_with_lines")
let line = line.unwrap();
lines.push(Line::from_stdout(&line));
assert_eq!(line, "hello");
command!("bash", "-c", "echo stderr >> ./tmp-run_funcs_with_lines") // col
.output()
.unwrap();
.unwrap(); // oops sorry lol
}
return Vec::new();
return lines;
}
},
);
});
sleep(Duration::from_secs(2));
let f = File::open("./tmp-run_runcs_with_lines").unwrap();
let f = File::open("./tmp-run_funcs_with_lines").unwrap();
let mut buf: [u8; 14] = [0u8; 14];
f.read_at(&mut buf, 0).unwrap();
assert_eq!(buf, [115, 116, 100, 111, 117, 116, 10, 0, 0, 0, 0, 0, 0, 0]);
@ -186,5 +200,12 @@ fn test_run_funcs_with_lines() {
[115, 116, 100, 111, 117, 116, 10, 115, 116, 100, 101, 114, 114, 10]
);
remove_file("./tmp-run_runcs_with_lines").unwrap();
remove_file("./tmp-run_funcs_with_lines").unwrap();
let output = threads.join().unwrap();
println!("{:?}", output);
assert_eq!(output.clone().lines().unwrap()[0].content, "hi");
assert_eq!(output.lines().unwrap()[1].content, "hello");
}