From adef254b235dc1f7d950da77c5c1a0ff9c067bf3 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sat, 5 Nov 2016 01:19:54 -0700 Subject: [PATCH] Avoid bashisms in tests (#18) Since `sh` might be different depending on the system, avoid testing status codes and error messages that are not the same across all `sh`s --- src/integration.rs | 61 ++++++------------------------------------- src/unit.rs | 64 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 64 insertions(+), 61 deletions(-) diff --git a/src/integration.rs b/src/integration.rs index d0e3a89..27d877a 100644 --- a/src/integration.rs +++ b/src/integration.rs @@ -199,11 +199,11 @@ recipe: */ #[test] -fn status() { +fn status_passthrough() { let text = " recipe: - @function f { return 100; }; f"; + @exit 100"; integration_test( &[], text, @@ -254,13 +254,13 @@ fn backtick_trimming() { fn backtick_code_assignment() { integration_test( &[], - "b = a\na = `function f { return 100; }; f`\nbar:\n echo '{{`function f { return 200; }; f`}}'", + "b = a\na = `exit 100`\nbar:\n echo '{{`exit 200`}}'", 100, "", "backtick failed with exit code 100 | -2 | a = `function f { return 100; }; f` - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +2 | a = `exit 100` + | ^^^^^^^^^^ ", ); } @@ -269,13 +269,13 @@ fn backtick_code_assignment() { fn backtick_code_interpolation() { integration_test( &[], - "b = a\na = `echo hello`\nbar:\n echo '{{`function f { return 200; }; f`}}'", + "b = a\na = `echo hello`\nbar:\n echo '{{`exit 200`}}'", 200, "", "backtick failed with exit code 200 | -4 | echo '{{`function f { return 200; }; f`}}' - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +4 | echo '{{`exit 200`}}' + | ^^^^^^^^^^ ", ); } @@ -454,29 +454,6 @@ wut: ); } - -#[test] -fn export_failure() { - integration_test( - &[], - r#" -export foo = "a" -baz = "c" -export bar = "b" -export abc = foo + bar + baz - -wut: - echo $foo $bar $baz -"#, - 127, - "", - r#"echo $foo $bar $baz -sh: baz: unbound variable -Recipe "wut" failed with exit code 127 -"#, - ); -} - #[test] fn export_shebang() { integration_test( @@ -497,28 +474,6 @@ wut: ); } -#[test] -fn export_assignment_backtick() { - integration_test( - &[], - r#" -export exported_variable = "A" -b = `echo $exported_variable` - -recipe: - echo {{b}} -"#, - 127, - "", - "sh: exported_variable: unbound variable -backtick failed with exit code 127 - | -3 | b = `echo $exported_variable` - | ^^^^^^^^^^^^^^^^^^^^^^^^^ -", - ); -} - #[test] fn export_recipe_backtick() { integration_test( diff --git a/src/unit.rs b/src/unit.rs index e88663d..7da2a73 100644 --- a/src/unit.rs +++ b/src/unit.rs @@ -727,7 +727,7 @@ c: b fn run_shebang() { // this test exists to make sure that shebang recipes // run correctly. although this script is still - // executed by sh its behavior depends on the value of a + // executed by a shell its behavior depends on the value of a // variable and continuing even though a command fails, // whereas in plain recipes variables are not available // in subsequent lines and execution stops when a line @@ -736,7 +736,7 @@ fn run_shebang() { a: #!/usr/bin/env sh code=200 - function x { return $code; } + x() { return $code; } x x "; @@ -752,7 +752,7 @@ a: #[test] fn code_error() { - match parse_success("fail:\n @function x { return 100; }; x") + match parse_success("fail:\n @exit 100") .run(&["fail"], &Default::default()).unwrap_err() { RunError::Code{recipe, code} => { assert_eq!(recipe, "fail"); @@ -766,7 +766,7 @@ fn code_error() { fn run_args() { let text = r#" a return code: - @function x { {{return}} {{code + "0"}}; }; x"#; + @x() { {{return}} {{code + "0"}}; }; x"#; match parse_success(text).run(&["a", "return", "15"], &Default::default()).unwrap_err() { RunError::Code{recipe, code} => { @@ -804,11 +804,11 @@ fn missing_default() { #[test] fn backtick_code() { - match parse_success("a:\n echo {{`function f { return 100; }; f`}}") + match parse_success("a:\n echo {{`f() { return 100; }; f`}}") .run(&["a"], &Default::default()).unwrap_err() { RunError::BacktickCode{code, token} => { assert_eq!(code, 100); - assert_eq!(token.lexeme, "`function f { return 100; }; f`"); + assert_eq!(token.lexeme, "`f() { return 100; }; f`"); }, other => panic!("expected an code run error, but got: {}", other), } @@ -819,8 +819,8 @@ fn unknown_overrides() { let mut options: RunOptions = Default::default(); options.overrides.insert("foo", "bar"); options.overrides.insert("baz", "bob"); - match parse_success("a:\n echo {{`function f { return 100; }; f`}}") - .run(&["a"], &options).unwrap_err() { + match parse_success("a:\n echo {{`f() { return 100; }; f`}}") + .run(&["a"], &options).unwrap_err() { RunError::UnknownOverrides{overrides} => { assert_eq!(overrides, &["baz", "foo"]); }, @@ -828,6 +828,54 @@ fn unknown_overrides() { } } +#[test] +fn export_assignment_backtick() { + let text = r#" +export exported_variable = "A" +b = `echo $exported_variable` + +recipe: + echo {{b}} +"#; + + let options = RunOptions { + quiet: true, + ..Default::default() + }; + + match parse_success(text).run(&["recipe"], &options).unwrap_err() { + RunError::BacktickCode{code: _, token} => { + assert_eq!(token.lexeme, "`echo $exported_variable`"); + }, + other => panic!("expected a backtick code errror, but got: {}", other), + } +} + +#[test] +fn export_failure() { + let text = r#" +export foo = "a" +baz = "c" +export bar = "b" +export abc = foo + bar + baz + +wut: + echo $foo $bar $baz +"#; + + let options = RunOptions { + quiet: true, + ..Default::default() + }; + + match parse_success(text).run(&["wut"], &options).unwrap_err() { + RunError::Code{code: _, recipe} => { + assert_eq!(recipe, "wut"); + }, + other => panic!("expected a recipe code errror, but got: {}", other), + } +} + #[test] fn readme_test() { let mut justfiles = vec![];