--- a/Cargo.lock +++ b/Cargo.lock @@ -1413,6 +1413,7 @@ name = "tests" version = "0.1.0" dependencies = [ + "insta", "jrsonnet-evaluator", "jrsonnet-gcmodule", "jrsonnet-stdlib", --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -14,3 +14,6 @@ serde.workspace = true json-structural-diff.workspace = true serde_json.workspace = true + +[dev-dependencies] +insta.workspace = true --- a/tests/golden/array_comp.jsonnet.golden +++ /dev/null @@ -1,38 +0,0 @@ -[ - [ - 1, - 4 - ], - [ - 1, - 5 - ], - [ - 1, - 6 - ], - [ - 2, - 4 - ], - [ - 2, - 5 - ], - [ - 2, - 6 - ], - [ - 3, - 4 - ], - [ - 3, - 5 - ], - [ - 3, - 6 - ] -] \ No newline at end of file --- a/tests/golden/builtin_json.jsonnet.golden +++ /dev/null @@ -1 +0,0 @@ -"{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}" \ No newline at end of file --- a/tests/golden/builtin_json_minified.jsonnet.golden +++ /dev/null @@ -1 +0,0 @@ -"{\"a\":3,\"b\":4,\"c\":6}" \ No newline at end of file --- a/tests/golden/builtin_parseJson.jsonnet.golden +++ /dev/null @@ -1,6 +0,0 @@ -{ - "a": -1, - "b": 1, - "c": 3.141, - "d": [ ] -} \ No newline at end of file --- a/tests/golden/builtin_strings.jsonnet.golden +++ /dev/null @@ -1,20 +0,0 @@ -{ - "split": [ - "ab", - "cd", - "ef" - ], - "splitlimit": [ - "ab", - "cd::ef" - ], - "splitlimitR": [ - "ab::cd", - "ef" - ], - "splitlimitRNoLimit": [ - "ab", - "cd", - "ef" - ] -} \ No newline at end of file --- a/tests/golden/builtin_strings_string.jsonnet.golden +++ /dev/null @@ -1,17 +0,0 @@ -{ - "lstripChars_array": "rward", - "lstripChars_multipleChars": "mn", - "lstripChars_singleChar": "bcdef", - "rstripChars_array": "cool just c", - "rstripChars_multipleChars": "amogu", - "rstripChars_singleChar": "nice_bo", - "stripChars_array": " Lel Sto", - "stripChars_arrayL": "hel medvedo svin", - "stripChars_arrayR": "lach-vs-m", - "stripChars_multipleChars": " sus ", - "stripChars_multipleCharsL": "defg", - "stripChars_multipleCharsR": "still brea", - "stripChars_singleChar": "oper je", - "stripChars_singleCharL": "eefoofaa", - "stripChars_singleCharR": "lolkek" -} \ No newline at end of file --- a/tests/golden/issue126.jsonnet.golden +++ /dev/null @@ -1,4 +0,0 @@ -{ - "int_lhs": 4294967296, - "unary_plus": 1 -} \ No newline at end of file --- a/tests/golden/issue153.jsonnet.golden +++ /dev/null @@ -1,5 +0,0 @@ -{ - "foo": { - "bar": "nope" - } -} \ No newline at end of file --- a/tests/golden/issue172.jsonnet.golden +++ /dev/null @@ -1,4 +0,0 @@ -local is not defined: b - issue172.jsonnet:1:45-47: local access - issue172.jsonnet:1:4-10: field access - elem <0> evaluation \ No newline at end of file --- a/tests/golden/issue187.jsonnet.golden +++ /dev/null @@ -1 +0,0 @@ -"foo bar � baz" \ No newline at end of file --- a/tests/golden/issue187.rev.jsonnet.golden +++ /dev/null @@ -1,2 +0,0 @@ -runtime error: bad utf8 - issue187.rev.jsonnet:1:1-92: function call \ No newline at end of file --- a/tests/golden/issue188.jsonnet.golden +++ /dev/null @@ -1 +0,0 @@ -{ } \ No newline at end of file --- a/tests/golden/issue195.jsonnet.golden +++ /dev/null @@ -1,6 +0,0 @@ -{ - "x": 42, - "y": { - "false": false - } -} \ No newline at end of file --- a/tests/golden/issue23.jsonnet.golden +++ /dev/null @@ -1,2 +0,0 @@ -infinite recursion detected - issue23.jsonnet:1:1-26: import "issue23.jsonnet" \ No newline at end of file --- a/tests/golden/issue40.jsonnet.golden +++ /dev/null @@ -1,3 +0,0 @@ -assert failed: is number - issue40.jsonnet:6:10-31: assertion failure - issue40.jsonnet:9:1-32: function call \ No newline at end of file --- a/tests/golden/missing_binding.jsonnet.golden +++ /dev/null @@ -1,3 +0,0 @@ -local is not defined: sta -There is a local with similar name present: std - missing_binding.jsonnet:1:1-5: local access \ No newline at end of file --- a/tests/golden/object_comp.jsonnet.golden +++ /dev/null @@ -1,9 +0,0 @@ -{ - "h1_2": "0a", - "h1_3": "0a", - "h1_4": "0a", - "h2_3": "a1", - "h2_4": "a1", - "h3_2": "0a", - "h3_4": "a1" -} \ No newline at end of file --- a/tests/golden/test_assertThrow.jsonnet.golden +++ /dev/null @@ -1,2 +0,0 @@ -runtime error: expected argument to throw on evaluation, but it returned instead - test_assertThrow.jsonnet:2:1-26: function call \ No newline at end of file --- a/tests/tests/golden.rs +++ b/tests/tests/golden.rs @@ -1,8 +1,6 @@ -use std::{ - fs, io, - path::{Path, PathBuf}, -}; +use std::path::Path; +use insta::{assert_snapshot, glob}; use jrsonnet_evaluator::{ FileImportResolver, State, manifest::JsonFormat, @@ -40,66 +38,10 @@ } #[test] -fn test() -> io::Result<()> { - use json_structural_diff::JsonDiff; - - let mut root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - root.push("golden"); +fn golden() { + glob!("../", "golden/*.jsonnet", |path| { + let result = run(&path); - for entry in fs::read_dir(&root)? { - let entry = entry?; - if !entry.path().extension().map_or(false, |e| e == "jsonnet") { - continue; - } - - let result = run(&entry.path()); - - let mut golden_path = entry.path(); - golden_path.set_extension("jsonnet.golden"); - - if !golden_path.exists() { - fs::write(golden_path, &result)?; - continue; - } - - let golden = fs::read_to_string(golden_path)?; - - match (serde_json::from_str(&result), serde_json::from_str(&golden)) { - (Err(_), Ok(_)) => assert_eq!( - result, - golden, - "unexpected error for golden {}", - entry.path().display() - ), - (Ok(_), Err(_)) => assert_eq!( - result, - golden, - "expected error for golden {}", - entry.path().display() - ), - (Ok(result), Ok(golden)) => { - // Show diff relative to golden`. - let diff = JsonDiff::diff_string(&golden, &result, false); - if let Some(diff) = diff { - panic!( - "Result \n{result:#}\n\ - and golden \n{golden:#}\n\ - did not match structurally:\n{diff:#}\n\ - for golden {}", - entry.path().display() - ); - } - } - (Err(_), Err(_)) => {} - }; - - assert_eq!( - result, - golden, - "golden didn't match for {}", - entry.path().display() - ); - } - - Ok(()) + assert_snapshot!(result) + }); } --- /dev/null +++ b/tests/tests/snapshots/golden__golden@array_comp.jsonnet.snap @@ -0,0 +1,43 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/array_comp.jsonnet +--- +[ + [ + 1, + 4 + ], + [ + 1, + 5 + ], + [ + 1, + 6 + ], + [ + 2, + 4 + ], + [ + 2, + 5 + ], + [ + 2, + 6 + ], + [ + 3, + 4 + ], + [ + 3, + 5 + ], + [ + 3, + 6 + ] +] --- /dev/null +++ b/tests/tests/snapshots/golden__golden@builtin_json.jsonnet.snap @@ -0,0 +1,6 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/builtin_json.jsonnet +--- +"{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}" --- /dev/null +++ b/tests/tests/snapshots/golden__golden@builtin_json_minified.jsonnet.snap @@ -0,0 +1,6 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/builtin_json_minified.jsonnet +--- +"{\"a\":3,\"b\":4,\"c\":6}" --- /dev/null +++ b/tests/tests/snapshots/golden__golden@builtin_parseJson.jsonnet.snap @@ -0,0 +1,11 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/builtin_parseJson.jsonnet +--- +{ + "a": -1, + "b": 1, + "c": 3.141, + "d": [ ] +} --- /dev/null +++ b/tests/tests/snapshots/golden__golden@builtin_strings.jsonnet.snap @@ -0,0 +1,25 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/builtin_strings.jsonnet +--- +{ + "split": [ + "ab", + "cd", + "ef" + ], + "splitlimit": [ + "ab", + "cd::ef" + ], + "splitlimitR": [ + "ab::cd", + "ef" + ], + "splitlimitRNoLimit": [ + "ab", + "cd", + "ef" + ] +} --- /dev/null +++ b/tests/tests/snapshots/golden__golden@builtin_strings_string.jsonnet.snap @@ -0,0 +1,22 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/builtin_strings_string.jsonnet +--- +{ + "lstripChars_array": "rward", + "lstripChars_multipleChars": "mn", + "lstripChars_singleChar": "bcdef", + "rstripChars_array": "cool just c", + "rstripChars_multipleChars": "amogu", + "rstripChars_singleChar": "nice_bo", + "stripChars_array": " Lel Sto", + "stripChars_arrayL": "hel medvedo svin", + "stripChars_arrayR": "lach-vs-m", + "stripChars_multipleChars": " sus ", + "stripChars_multipleCharsL": "defg", + "stripChars_multipleCharsR": "still brea", + "stripChars_singleChar": "oper je", + "stripChars_singleCharL": "eefoofaa", + "stripChars_singleCharR": "lolkek" +} --- /dev/null +++ b/tests/tests/snapshots/golden__golden@issue126.jsonnet.snap @@ -0,0 +1,9 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/issue126.jsonnet +--- +{ + "int_lhs": 4294967296, + "unary_plus": 1 +} --- /dev/null +++ b/tests/tests/snapshots/golden__golden@issue153.jsonnet.snap @@ -0,0 +1,10 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/issue153.jsonnet +--- +{ + "foo": { + "bar": "nope" + } +} --- /dev/null +++ b/tests/tests/snapshots/golden__golden@issue172.jsonnet.snap @@ -0,0 +1,9 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/issue172.jsonnet +--- +local is not defined: b + issue172.jsonnet:1:45-47: local access + issue172.jsonnet:1:4-10: field access + elem <0> evaluation --- /dev/null +++ b/tests/tests/snapshots/golden__golden@issue187.jsonnet.snap @@ -0,0 +1,6 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/issue187.jsonnet +--- +"foo bar � baz" --- /dev/null +++ b/tests/tests/snapshots/golden__golden@issue187.rev.jsonnet.snap @@ -0,0 +1,7 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/issue187.rev.jsonnet +--- +runtime error: bad utf8 + issue187.rev.jsonnet:1:1-92: function call --- /dev/null +++ b/tests/tests/snapshots/golden__golden@issue188.jsonnet.snap @@ -0,0 +1,6 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/issue188.jsonnet +--- +{ } --- /dev/null +++ b/tests/tests/snapshots/golden__golden@issue195.jsonnet.snap @@ -0,0 +1,11 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/issue195.jsonnet +--- +{ + "x": 42, + "y": { + "false": false + } +} --- /dev/null +++ b/tests/tests/snapshots/golden__golden@issue23.jsonnet.snap @@ -0,0 +1,7 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/issue23.jsonnet +--- +infinite recursion detected + issue23.jsonnet:1:1-26: import "issue23.jsonnet" --- /dev/null +++ b/tests/tests/snapshots/golden__golden@issue40.jsonnet.snap @@ -0,0 +1,8 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/issue40.jsonnet +--- +assert failed: is number + issue40.jsonnet:6:10-31: assertion failure + issue40.jsonnet:9:1-32: function call --- /dev/null +++ b/tests/tests/snapshots/golden__golden@missing_binding.jsonnet.snap @@ -0,0 +1,8 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/missing_binding.jsonnet +--- +local is not defined: sta +There is a local with similar name present: std + missing_binding.jsonnet:1:1-5: local access --- /dev/null +++ b/tests/tests/snapshots/golden__golden@object_comp.jsonnet.snap @@ -0,0 +1,14 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/object_comp.jsonnet +--- +{ + "h1_2": "0a", + "h1_3": "0a", + "h1_4": "0a", + "h2_3": "a1", + "h2_4": "a1", + "h3_2": "0a", + "h3_4": "a1" +} --- /dev/null +++ b/tests/tests/snapshots/golden__golden@test_assertThrow.jsonnet.snap @@ -0,0 +1,7 @@ +--- +source: tests/tests/golden.rs +expression: result +input_file: tests/golden/test_assertThrow.jsonnet +--- +runtime error: expected argument to throw on evaluation, but it returned instead + test_assertThrow.jsonnet:2:1-26: function call