Skip to content

Commit

Permalink
Run --run-tests with stdin if no args given, else with last file.
Browse files Browse the repository at this point in the history
  • Loading branch information
01mf02 committed Nov 27, 2024
1 parent 52ba190 commit a9455be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
6 changes: 2 additions & 4 deletions jaq/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct Cli {
pub files: Vec<PathBuf>,
pub args: Vec<String>,
//pub jsonargs: Vec<String>,
pub run_tests: Option<PathBuf>,
pub run_tests: Option<Vec<PathBuf>>,
/// If there is some last output value `v`,
/// then the exit status code is
/// 1 if `v < true` (that is, if `v` is `false` or `null`) and
Expand Down Expand Up @@ -110,9 +110,7 @@ impl Cli {

"args" => *mode = Mode::Args,
//"jsonargs" => *mode = Mode::JsonArgs,
"run-tests" => {
self.run_tests = Some(args.next().ok_or(Error::Path("--run-tests"))?.into())
}
"run-tests" => self.run_tests = Some(args.map(PathBuf::from).collect()),
"exit-status" => self.short('e', args)?,
"version" => self.short('V', args)?,
"help" => self.short('h', args)?,
Expand Down
11 changes: 7 additions & 4 deletions jaq/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ fn main() -> ExitCode {
}

fn real_main(cli: &Cli) -> Result<ExitCode, Error> {
if let Some(test_file) = &cli.run_tests {
return Ok(run_tests(std::fs::File::open(test_file)?));
if let Some(test_files) = &cli.run_tests {
return Ok(match test_files.last() {
Some(file) => run_tests(io::BufReader::new(std::fs::File::open(file)?)),
None => run_tests(io::stdin().lock()),
});
}

let (vars, mut ctx): (Vec<String>, Vec<Val>) = binds(cli)?.into_iter().unzip();
Expand Down Expand Up @@ -648,8 +651,8 @@ fn run_test(test: load::test::Test<String>) -> Result<(Val, Val), Error> {
Ok((expect?, obtain.map_err(Error::Jaq)?))
}

fn run_tests(file: std::fs::File) -> ExitCode {
let lines = io::BufReader::new(file).lines().map(Result::unwrap);
fn run_tests(read: impl BufRead) -> ExitCode {
let lines = read.lines().map(Result::unwrap);
let tests = load::test::Parser::new(lines);

let (mut passed, mut total) = (0, 0);
Expand Down

0 comments on commit a9455be

Please sign in to comment.