Skip to content

Commit

Permalink
Merge pull request #6 from triarius/integration-test
Browse files Browse the repository at this point in the history
Improve testing and add integration tests
  • Loading branch information
triarius authored Sep 2, 2024
2 parents 36671a9 + d576522 commit edf93b7
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 18 deletions.
143 changes: 143 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ clap = { version = "4.5.16", features = ["derive", "env"] }
crossbeam = "0.8.4"
eyre = "0.6.12"

[dev-dependencies]
assert_cmd = "2.0.16"
pretty_assertions = "1.4.0"

[lints.clippy]
all = "deny"
pedantic = "deny"
single_match_else = { level = "allow", priority = 1 }
enum_glob_use = { level = "allow", priority = 1 }
needless_for_each = { level = "allow", priority = 1 }
18 changes: 11 additions & 7 deletions src/flat_map_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod test {
#[test]
fn flat_map_err() {
use super::FlatMapErr;
use pretty_assertions::assert_eq;

#[derive(Debug, PartialEq, Eq)]
enum Error {
Expand All @@ -39,12 +40,15 @@ mod test {
Error::GraveError => Err(e),
};

let success = Ok(());
let ignorable_error = Err(Error::IgnorableError);
let grave_error = Err(Error::GraveError);

assert_eq!(success.flat_map_err(ignore), Ok(()));
assert_eq!(ignorable_error.flat_map_err(ignore), Ok(()));
assert_eq!(grave_error.flat_map_err(ignore), Err(Error::GraveError));
[
(Ok(()), Ok(())),
(Err(Error::IgnorableError), Ok(())),
(Err(Error::GraveError), Err(Error::GraveError)),
]
.into_iter()
.for_each(|(input, expected)| {
let actual = input.flat_map_err(ignore);
assert_eq!(actual, expected);
});
}
}
114 changes: 103 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,129 @@ use eyre::Result;
use std::path::PathBuf;

/// A command runner that optionally logs the I/O streams to files.
#[derive(Debug, Parser, PartialEq)]
#[derive(Debug, Parser, PartialEq, Eq)]
#[command(version)]
struct Cli {
struct Args {
/// The file to log stdin to.
#[clap(short, long, env)]
#[arg(short, long, env)]
in_file: Option<PathBuf>,

/// The file to log stdout to.
#[clap(short, long, env)]
#[arg(short, long, env)]
out_file: Option<PathBuf>,

/// The file to log stderr to.
#[clap(short, long, env)]
#[arg(short, long, env)]
err_file: Option<PathBuf>,

/// The command to run and its arguments. A command must be specified, arguments are space delimited.
#[clap(last = true, required = true, num_args = 1..)]
#[arg(last = true, required = true, num_args = 1..)]
exec: Vec<String>,
}

fn main() -> Result<()> {
let cli = Cli::parse();
let args = Args::parse();

let exec = cli.exec.iter().map(String::as_str).collect::<Vec<&str>>();
let exec = args.exec.iter().map(String::as_str).collect::<Vec<&str>>();
let code = runner::run(
exec[0],
&exec[1..],
cli.in_file.as_deref(),
cli.out_file.as_deref(),
cli.err_file.as_deref(),
args.in_file.as_deref(),
args.out_file.as_deref(),
args.err_file.as_deref(),
)?;

std::process::exit(code);
}

#[cfg(test)]
mod test {
#[test]
fn arg_parse() {
use super::Args;
use clap::Parser;
use pretty_assertions::assert_eq;

[
(
vec!["runner", "--", "echo", "hello"],
Args {
in_file: None,
out_file: None,
err_file: None,
exec: vec!["echo".to_string(), "hello".to_string()],
},
),
(
vec!["runner", "--in-file", "in.txt", "--", "echo", "hello"],
Args {
in_file: Some("in.txt".into()),
out_file: None,
err_file: None,
exec: vec!["echo".to_string(), "hello".to_string()],
},
),
(
vec!["runner", "--out-file", "out.txt", "--", "echo", "hello"],
Args {
in_file: None,
out_file: Some("out.txt".into()),
err_file: None,
exec: vec!["echo".to_string(), "hello".to_string()],
},
),
(
vec!["runner", "--err-file", "err.txt", "--", "echo", "hello"],
Args {
in_file: None,
out_file: None,
err_file: Some("err.txt".into()),
exec: vec!["echo".to_string(), "hello".to_string()],
},
),
(
vec![
"runner",
"--in-file",
"in.txt",
"--out-file",
"out.txt",
"--",
"echo",
"hello",
],
Args {
in_file: Some("in.txt".into()),
out_file: Some("out.txt".into()),
err_file: None,
exec: vec!["echo".to_string(), "hello".to_string()],
},
),
(
vec![
"runner",
"--in-file",
"in.txt",
"--out-file",
"out.txt",
"--err-file",
"err.txt",
"--",
"echo",
"hello",
],
Args {
in_file: Some("in.txt".into()),
out_file: Some("out.txt".into()),
err_file: Some("err.txt".into()),
exec: vec!["echo".to_string(), "hello".to_string()],
},
),
]
.into_iter()
.for_each(|(input, expected)| {
let actual = Args::try_parse_from(input).unwrap();
assert_eq!(actual, expected);
});
}
}
Loading

0 comments on commit edf93b7

Please sign in to comment.