Compare commits
No commits in common. "4dd1b3bcc52572ce490a4eb94b78954b3d8007fa" and "8c353259505946df2e79f4c649ce4be5ddb20994" have entirely different histories.
4dd1b3bcc5
...
8c35325950
3 changed files with 36 additions and 58 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -4,4 +4,4 @@ version = 4
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "better-commands"
|
name = "better-commands"
|
||||||
version = "0.1.2"
|
version = "0.1.1"
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
[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.2"
|
version = "0.1.1"
|
||||||
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
|
73
src/tests.rs
73
src/tests.rs
|
@ -1,24 +1,11 @@
|
||||||
#[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 crate::command;
|
use std::process::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]
|
||||||
|
@ -102,8 +89,8 @@ fn shuffle_vec<T>(vec: &mut [T]) {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_run_funcs() {
|
fn test_run_funcs() {
|
||||||
let threads = thread::spawn(|| {
|
let _ = thread::spawn(|| {
|
||||||
return run_funcs(
|
let _ = run_funcs(
|
||||||
Command::new("bash")
|
Command::new("bash")
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg("echo hi; >&2 echo hello"),
|
.arg("echo hi; >&2 echo hello"),
|
||||||
|
@ -111,7 +98,9 @@ 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!("bash", "-c", "echo stdout >> ./tmp-run_funcs") // col
|
Command::new("bash")
|
||||||
|
.arg("-c")
|
||||||
|
.arg("echo stdout >> ./tmp-run_runcs")
|
||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
@ -121,7 +110,9 @@ 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!("bash", "-c", "echo stderr >> ./tmp-run_funcs") // col
|
Command::new("bash")
|
||||||
|
.arg("-c")
|
||||||
|
.arg("echo stderr >> ./tmp-run_runcs")
|
||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
@ -130,7 +121,7 @@ fn test_run_funcs() {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
sleep(Duration::from_secs(2));
|
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];
|
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]);
|
||||||
|
@ -142,53 +133,48 @@ 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_funcs").unwrap();
|
remove_file("./tmp-run_runcs").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 threads = thread::spawn(|| {
|
let _ = thread::spawn(|| {
|
||||||
return run_funcs_with_lines(
|
let _ = 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 {
|
||||||
let line = line.unwrap();
|
assert_eq!(line.unwrap(), "hi");
|
||||||
lines.push(Line::from_stdout(&line));
|
Command::new("bash")
|
||||||
assert_eq!(&line, "hi");
|
.arg("-c")
|
||||||
command!("bash", "-c", "echo stdout >> ./tmp-run_funcs_with_lines")
|
.arg("echo stdout >> ./tmp-run_runcs_with_lines")
|
||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
return lines;
|
return Vec::new();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|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 {
|
||||||
let line = line.unwrap();
|
assert_eq!(line.unwrap(), "hello");
|
||||||
lines.push(Line::from_stdout(&line));
|
Command::new("bash")
|
||||||
assert_eq!(line, "hello");
|
.arg("-c")
|
||||||
command!("bash", "-c", "echo stderr >> ./tmp-run_funcs_with_lines") // col
|
.arg("echo stderr >> ./tmp-run_runcs_with_lines")
|
||||||
.output()
|
.output()
|
||||||
.unwrap(); // oops sorry lol
|
.unwrap();
|
||||||
}
|
}
|
||||||
return lines;
|
return Vec::new();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
sleep(Duration::from_secs(2));
|
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];
|
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]);
|
||||||
|
@ -200,12 +186,5 @@ 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_funcs_with_lines").unwrap();
|
remove_file("./tmp-run_runcs_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");
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue