From 08df71a7e4f5930ebd362a3f0411664f7c1bdc7d Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Wed, 7 Aug 2019 17:50:05 -0700 Subject: [PATCH] Change rojo-test to be macro-based --- Cargo.lock | 1 + rojo-test/Cargo.toml | 1 + rojo-test/src/build_test.rs | 53 +++++++++++++++++++ rojo-test/src/lib.rs | 45 +--------------- ..._gitkeep.snap => build_test__gitkeep.snap} | 4 +- ...er.snap => build_test__txt_in_folder.snap} | 4 +- 6 files changed, 61 insertions(+), 47 deletions(-) create mode 100644 rojo-test/src/build_test.rs rename rojo-test/src/snapshots/{rojo_test__build_gitkeep.snap => build_test__gitkeep.snap} (72%) rename rojo-test/src/snapshots/{rojo_test__build_txt_in_folder.snap => build_test__txt_in_folder.snap} (83%) diff --git a/Cargo.lock b/Cargo.lock index c70d5de0..016d4c2a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1541,6 +1541,7 @@ name = "rojo-test" version = "0.1.0" dependencies = [ "insta 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", + "paste 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/rojo-test/Cargo.toml b/rojo-test/Cargo.toml index 56736dbd..cb768b77 100644 --- a/rojo-test/Cargo.toml +++ b/rojo-test/Cargo.toml @@ -7,4 +7,5 @@ publish = false [dependencies] insta = "0.10.0" +paste = "0.1.5" tempfile = "3.1.0" \ No newline at end of file diff --git a/rojo-test/src/build_test.rs b/rojo-test/src/build_test.rs new file mode 100644 index 00000000..3d2734f3 --- /dev/null +++ b/rojo-test/src/build_test.rs @@ -0,0 +1,53 @@ +use std::{ + fs, + path::Path, + process::Command, +}; + +use insta::assert_snapshot_matches; +use tempfile::tempdir; + +macro_rules! gen_build_tests { + ( $($test_name: ident,)* ) => { + $( + paste::item! { + #[test] + fn []() { + run_build_test(stringify!($test_name)); + } + } + )* + }; +} + +gen_build_tests! { + gitkeep, + txt_in_folder, +} + +fn run_build_test(test_name: &str) { + let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")); + let build_test_path = manifest_dir.join("build-tests"); + let working_dir = manifest_dir.parent().unwrap(); + + let output_dir = tempdir().expect("couldn't create temporary directory"); + + let input_path = build_test_path.join(test_name); + let output_path = output_dir.path().join(format!("{}.rbxmx", test_name)); + + let status = Command::new("cargo") + .args(&[ + "run", "--quiet", "--", + "build", input_path.to_str().unwrap(), "-o", output_path.to_str().unwrap(), + ]) + .current_dir(working_dir) + .status() + .expect("Couldn't start Rojo"); + + assert!(status.success(), "Rojo did not exit successfully"); + + let contents = fs::read_to_string(&output_path) + .expect("Couldn't read output file"); + + assert_snapshot_matches!(test_name, contents); +} \ No newline at end of file diff --git a/rojo-test/src/lib.rs b/rojo-test/src/lib.rs index 79e4d643..58bd5ab1 100644 --- a/rojo-test/src/lib.rs +++ b/rojo-test/src/lib.rs @@ -1,43 +1,2 @@ -use std::{ - fs, - path::Path, - process::Command, -}; - -use insta::assert_snapshot_matches; -use tempfile::tempdir; - -static BUILD_TESTS: &[&str] = &[ - "gitkeep", - "txt_in_folder", -]; - -#[test] -fn build_tests() { - let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")); - let build_test_path = manifest_dir.join("build-tests"); - let working_dir = manifest_dir.parent().unwrap(); - - let output_dir = tempdir().expect("couldn't create temporary directory"); - - for &test_name in BUILD_TESTS { - let input_path = build_test_path.join(test_name); - let output_path = output_dir.path().join(format!("{}.rbxmx", test_name)); - - let status = Command::new("cargo") - .args(&[ - "run", "--", - "build", input_path.to_str().unwrap(), "-o", output_path.to_str().unwrap(), - ]) - .current_dir(working_dir) - .status() - .expect("Couldn't start Rojo"); - - assert!(status.success(), "Rojo did not exit successfully"); - - let contents = fs::read_to_string(&output_path) - .expect("Couldn't read output file"); - - assert_snapshot_matches!(format!("build_{}", test_name), contents); - } -} \ No newline at end of file +#[cfg(test)] +mod build_test; \ No newline at end of file diff --git a/rojo-test/src/snapshots/rojo_test__build_gitkeep.snap b/rojo-test/src/snapshots/build_test__gitkeep.snap similarity index 72% rename from rojo-test/src/snapshots/rojo_test__build_gitkeep.snap rename to rojo-test/src/snapshots/build_test__gitkeep.snap index f3c5e8e5..5db8bcd0 100644 --- a/rojo-test/src/snapshots/rojo_test__build_gitkeep.snap +++ b/rojo-test/src/snapshots/build_test__gitkeep.snap @@ -1,7 +1,7 @@ --- -created: "2019-08-08T00:40:36.892733600Z" +created: "2019-08-08T00:48:33.382271800Z" creator: insta@0.10.0 -source: rojo-test/src/lib.rs +source: rojo-test/src/build_test.rs expression: contents --- diff --git a/rojo-test/src/snapshots/rojo_test__build_txt_in_folder.snap b/rojo-test/src/snapshots/build_test__txt_in_folder.snap similarity index 83% rename from rojo-test/src/snapshots/rojo_test__build_txt_in_folder.snap rename to rojo-test/src/snapshots/build_test__txt_in_folder.snap index f7119804..71292e94 100644 --- a/rojo-test/src/snapshots/rojo_test__build_txt_in_folder.snap +++ b/rojo-test/src/snapshots/build_test__txt_in_folder.snap @@ -1,7 +1,7 @@ --- -created: "2019-08-08T00:40:50.026718300Z" +created: "2019-08-08T00:48:33.502257600Z" creator: insta@0.10.0 -source: rojo-test/src/lib.rs +source: rojo-test/src/build_test.rs expression: contents ---