Compare commits

..

No commits in common. "4dd1b3bcc52572ce490a4eb94b78954b3d8007fa" and "8c353259505946df2e79f4c649ce4be5ddb20994" have entirely different histories.

3 changed files with 36 additions and 58 deletions

2
Cargo.lock generated
View file

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

View file

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