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]] [[package]]
name = "better-commands" name = "better-commands"
version = "0.1.1" version = "0.1.2"

View file

@ -1,10 +1,11 @@
[package] [package]
name = "better-commands" 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" 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" edition = "2021"
license = "GPL-3.0-only" license = "GPL-3.0-only"
keywords = ["command", "cmd"] keywords = ["command", "cmd"]
repository = "https://git.askiiart.net/askiiart/better-commands-rs"
[profile.release] [profile.release]
opt-level = 3 opt-level = 3

View file

@ -1,11 +1,24 @@
#[cfg(test)] #[cfg(test)]
use crate::*; use crate::*;
use std::process::Command;
use std::{ use std::{
fs::remove_file, fs::remove_file,
hash::{BuildHasher, Hasher, RandomState}, hash::{BuildHasher, Hasher, RandomState},
}; };
use std::{fs::File, os::unix::fs::FileExt, thread::sleep}; 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 /// Tests what stdout prints
#[test] #[test]
@ -34,14 +47,14 @@ fn stderr_content() {
// `>&2` redirects to stderr // `>&2` redirects to stderr
assert_eq!( assert_eq!(
expected, expected,
run(&mut Command::new("bash") run(&mut Command::new("bash")
.arg("-c") .arg("-c")
.arg("echo -n 'helloooooooooo\nhiiiiiiiiiiiii' >&2")) .arg("echo -n 'helloooooooooo\nhiiiiiiiiiiiii' >&2"))
.stderr() .stderr()
.unwrap() .unwrap()
.into_iter() .into_iter()
.map(|line| { line.content }) .map(|line| { line.content })
.collect::<Vec<String>>() .collect::<Vec<String>>()
); );
} }
@ -89,8 +102,8 @@ fn shuffle_vec<T>(vec: &mut [T]) {
#[test] #[test]
fn test_run_funcs() { fn test_run_funcs() {
let _ = thread::spawn(|| { let threads = thread::spawn(|| {
let _ = run_funcs( return run_funcs(
Command::new("bash") Command::new("bash")
.arg("-c") .arg("-c")
.arg("echo hi; >&2 echo hello"), .arg("echo hi; >&2 echo hello"),
@ -98,9 +111,7 @@ fn test_run_funcs() {
|stdout_lines| { |stdout_lines| {
sleep(Duration::from_secs(1)); sleep(Duration::from_secs(1));
for _ in stdout_lines { for _ in stdout_lines {
Command::new("bash") command!("bash", "-c", "echo stdout >> ./tmp-run_funcs") // col
.arg("-c")
.arg("echo stdout >> ./tmp-run_runcs")
.output() .output()
.unwrap(); .unwrap();
} }
@ -110,9 +121,7 @@ fn test_run_funcs() {
|stderr_lines| { |stderr_lines| {
sleep(Duration::from_secs(3)); sleep(Duration::from_secs(3));
for _ in stderr_lines { for _ in stderr_lines {
Command::new("bash") command!("bash", "-c", "echo stderr >> ./tmp-run_funcs") // col
.arg("-c")
.arg("echo stderr >> ./tmp-run_runcs")
.output() .output()
.unwrap(); .unwrap();
} }
@ -121,7 +130,7 @@ fn test_run_funcs() {
); );
}); });
sleep(Duration::from_secs(2)); 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]; let mut buf: [u8; 14] = [0u8; 14];
f.read_at(&mut buf, 0).unwrap(); f.read_at(&mut buf, 0).unwrap();
assert_eq!(buf, [115, 116, 100, 111, 117, 116, 10, 0, 0, 0, 0, 0, 0, 0]); 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] [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] #[test]
fn test_run_funcs_with_lines() { fn test_run_funcs_with_lines() {
let _ = thread::spawn(|| { let threads = thread::spawn(|| {
let _ = run_funcs_with_lines( return run_funcs_with_lines(
Command::new("bash") Command::new("bash")
.arg("-c") .arg("-c")
.arg("echo hi; >&2 echo hello"), .arg("echo hi; >&2 echo hello"),
{ {
|stdout_lines| { |stdout_lines| {
let mut lines: Vec<Line> = Vec::new();
sleep(Duration::from_secs(1)); sleep(Duration::from_secs(1));
for line in stdout_lines { for line in stdout_lines {
assert_eq!(line.unwrap(), "hi"); let line = line.unwrap();
Command::new("bash") lines.push(Line::from_stdout(&line));
.arg("-c") assert_eq!(&line, "hi");
.arg("echo stdout >> ./tmp-run_runcs_with_lines") command!("bash", "-c", "echo stdout >> ./tmp-run_funcs_with_lines")
.output() .output()
.unwrap(); .unwrap();
} }
return Vec::new(); return lines;
} }
}, },
{ {
|stderr_lines| { |stderr_lines| {
let mut lines: Vec<Line> = Vec::new();
sleep(Duration::from_secs(3)); sleep(Duration::from_secs(3));
for line in stderr_lines { for line in stderr_lines {
assert_eq!(line.unwrap(), "hello"); let line = line.unwrap();
Command::new("bash") lines.push(Line::from_stdout(&line));
.arg("-c") assert_eq!(line, "hello");
.arg("echo stderr >> ./tmp-run_runcs_with_lines") command!("bash", "-c", "echo stderr >> ./tmp-run_funcs_with_lines") // col
.output() .output()
.unwrap(); .unwrap(); // oops sorry lol
} }
return Vec::new(); return lines;
} }
}, },
); );
}); });
sleep(Duration::from_secs(2)); 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]; let mut buf: [u8; 14] = [0u8; 14];
f.read_at(&mut buf, 0).unwrap(); f.read_at(&mut buf, 0).unwrap();
assert_eq!(buf, [115, 116, 100, 111, 117, 116, 10, 0, 0, 0, 0, 0, 0, 0]); 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] [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");
} }