diff --git a/Cargo.lock b/Cargo.lock index 52d24072..eb5886be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -139,6 +139,7 @@ version = "0.0.0" dependencies = [ "assert_cmd", "bough-cli-test-macros", + "serde_json", "tempfile", ] diff --git a/crates/bough-cli-test-macros/src/lib.rs b/crates/bough-cli-test-macros/src/lib.rs index 9d5cde7d..72bb17f0 100644 --- a/crates/bough-cli-test-macros/src/lib.rs +++ b/crates/bough-cli-test-macros/src/lib.rs @@ -233,3 +233,44 @@ pub fn cmd(input: TokenStream) -> TokenStream { pub fn cmd_err(input: TokenStream) -> TokenStream { build_cmd(parse_macro_input!(input as CmdArgs), false) } + +struct AssertFileArgs { + dir: Expr, + path: LitStr, + patterns: Vec, +} + +impl Parse for AssertFileArgs { + fn parse(input: syn::parse::ParseStream) -> syn::Result { + let dir = input.parse()?; + input.parse::()?; + let path = input.parse()?; + let mut patterns = Vec::new(); + while input.peek(Token![,]) { + input.parse::()?; + if !input.is_empty() { + patterns.push(input.parse()?); + } + } + Ok(Self { dir, path, patterns }) + } +} + +#[proc_macro] +pub fn assert_file(input: TokenStream) -> TokenStream { + let args = parse_macro_input!(input as AssertFileArgs); + let dir = &args.dir; + let path_lit = &args.path; + let path_expr = build_cmd_expr(path_lit); + let pattern_stmts = build_pattern_stmts(&args.patterns); + + quote! { + let __file_path = ::std::path::Path::new(#dir.as_ref()).join(#path_expr); + let __text = ::std::fs::read_to_string(&__file_path) + .unwrap_or_else(|e| panic!("failed to read {}: {e}", __file_path.display())); + let __lines: Vec<&str> = __text.lines().collect(); + let mut __matched: Vec = vec![false; __lines.len()]; + #(#pattern_stmts)* + } + .into() +} diff --git a/crates/bough-cli-test/Cargo.toml b/crates/bough-cli-test/Cargo.toml index 61784232..a13bf8ac 100644 --- a/crates/bough-cli-test/Cargo.toml +++ b/crates/bough-cli-test/Cargo.toml @@ -8,3 +8,4 @@ license.workspace = true tempfile = "3.26" assert_cmd = "2.1" bough-cli-test-macros = { path = "../bough-cli-test-macros" } +serde_json = "1" diff --git a/crates/bough-cli-test/src/lib.rs b/crates/bough-cli-test/src/lib.rs index f81df3c3..74426465 100644 --- a/crates/bough-cli-test/src/lib.rs +++ b/crates/bough-cli-test/src/lib.rs @@ -1,3 +1,3 @@ mod macros; pub use macros::*; -pub use bough_cli_test_macros::{cmd, cmd_err}; +pub use bough_cli_test_macros::{assert_file, cmd, cmd_err}; diff --git a/crates/bough-cli-test/tests/workspace_test.rs b/crates/bough-cli-test/tests/workspace_test.rs new file mode 100644 index 00000000..dfac4bc9 --- /dev/null +++ b/crates/bough-cli-test/tests/workspace_test.rs @@ -0,0 +1,101 @@ +use bough_cli_test::{TestPlan, assert_file, cmd, cmd_err}; + +fn plan(test_command: &str) -> TestPlan { + TestPlan::new() + .config(&format!( + r#" +[vcs] +kind = "none" + +[dirs] +working = "./work" +state = "./state" + +[runner] +pwd = "." +test.commands = ["{test_command}"] + +[runner.js] +files.include = ["src/*.js"] +files.exclude = [] +"#, + )) + .file("src/app.js", "export function add(a, b) { return a + b; }\n") + .file("work/.keep", "") + .file("state/.keep", "") +} + +#[test] +fn caught_when_test_fails() { + let dir = plan("exit 1").setup(); + + cmd!(dir, "bough workspace make", "created workspace at {!ws_path}"); + cmd!( + dir, + "bough --output-style verbose workspace list", + "{!ws_name} {?ws_path}", + ); + cmd!( + dir, + "bough workspace test {?ws_name} 7c43fed8aeccd70bfd659b389dd2647fe348e5f9e97d06d94f25192ed371cfb0", + "caught mutation 7c43fed8aeccd70bfd659b389dd2647fe348e5f9e97d06d94f25192ed371cfb0 in workspace {?ws_path}", + ); +} + +#[test] +fn missed_when_test_passes() { + let dir = plan("exit 0").setup(); + + cmd!(dir, "bough workspace make", "created workspace at {!ws_path}"); + cmd!( + dir, + "bough --output-style verbose workspace list", + "{!ws_name} {?ws_path}", + ); + cmd!( + dir, + "bough workspace test {?ws_name} 7c43fed8aeccd70bfd659b389dd2647fe348e5f9e97d06d94f25192ed371cfb0", + "missed mutation 7c43fed8aeccd70bfd659b389dd2647fe348e5f9e97d06d94f25192ed371cfb0 in workspace {?ws_path}", + ); +} + +#[test] +fn persists_result_to_state_dir() { + let dir = plan("exit 1").setup(); + + cmd!(dir, "bough workspace make", "created workspace at {!ws_path}"); + cmd!( + dir, + "bough --output-style verbose workspace list", + "{!ws_name} {?ws_path}", + ); + cmd!( + dir, + "bough workspace test {?ws_name} 7c43fed8aeccd70bfd659b389dd2647fe348e5f9e97d06d94f25192ed371cfb0", + "caught mutation 7c43fed8aeccd70bfd659b389dd2647fe348e5f9e97d06d94f25192ed371cfb0 in workspace {?ws_path}", + ); + + assert_file!( + dir, + "state/7c43fed8aeccd70bfd659b389dd2647fe348e5f9e97d06d94f25192ed371cfb0.json", + r#""outcome": "Caught","#, + r#""replacement": "-""#, + ); +} + +#[test] +fn unknown_mutation_hash_fails() { + let dir = plan("exit 0").setup(); + + cmd!(dir, "bough workspace make", "created workspace at {!ws_path}"); + cmd!( + dir, + "bough --output-style verbose workspace list", + "{!ws_name} {?ws_path}", + ); + cmd_err!( + dir, + "bough workspace test {?ws_name} 0000000000000000000000000000000000000000000000000000000000000000", + "no mutation found with hash 0000000000000000000000000000000000000000000000000000000000000000", + ); +}