From dc4ff0dead240bb99341b018c3dc1311f53a660a Mon Sep 17 00:00:00 2001 From: schilkp Date: Wed, 3 Jul 2024 08:08:07 +0200 Subject: [PATCH] rust-rewrite: Moved test gating to features + insta testing. Added optional insta testing to be used during development. Snapshots are not commited. --- .github/workflows/ci.yml | 2 +- Cargo.lock | 19 +++++++++++ reginald_codegen/Cargo.toml | 7 ++-- .../src/builtin/c/funcpack/mod.rs | 20 ++++++++--- .../src/builtin/c/macromap/mod.rs | 19 ++++++++--- .../tests/generator_c_funcpack/.gitignore | 1 + .../tests/generator_c_funcpack/mod.rs | 33 +++++++++++++++---- .../tests/generator_c_macromap/.gitignore | 1 + .../tests/generator_c_macromap/mod.rs | 25 +++++++++++++- .../tests/generator_rs_structs/mod.rs | 2 +- 10 files changed, 110 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0c8fb1e..5d3376f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,4 +46,4 @@ jobs: toolchain: stable - uses: carlosperate/arm-none-eabi-gcc-action@v1 - name: Run generator tests. - run: REGINALD_TEST_C_FUNCPACK_ADDITIONAL_COMPILERS=arm-none-eabi-gcc cargo test --workspace -- --ignored --show-output + run: REGINALD_TEST_C_FUNCPACK_ADDITIONAL_COMPILERS=arm-none-eabi-gcc cargo test --workspace --features=test_gen_output -- --show-output diff --git a/Cargo.lock b/Cargo.lock index 2e81a5c..9201c91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -204,6 +204,18 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "insta" +version = "1.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "810ae6042d48e2c9e9215043563a58a80b877bc863228a74cf10c49d4620a6f5" +dependencies = [ + "console", + "lazy_static", + "linked-hash-map", + "similar", +] + [[package]] name = "itoa" version = "1.0.10" @@ -222,6 +234,12 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "linux-raw-sys" version = "0.4.13" @@ -306,6 +324,7 @@ dependencies = [ "clap_complete", "console", "deser-hjson", + "insta", "lazy_static", "pretty_assertions", "regex", diff --git a/reginald_codegen/Cargo.toml b/reginald_codegen/Cargo.toml index b5e8a79..e4db506 100644 --- a/reginald_codegen/Cargo.toml +++ b/reginald_codegen/Cargo.toml @@ -11,8 +11,10 @@ path = "src/main.rs" required-features = ["cli"] [features] -default = ["cli"] -cli = ["dep:clap", "dep:clap_complete", "dep:similar", "dep:console"] +default = ["cli"] +cli = ["dep:clap", "dep:clap_complete", "dep:similar", "dep:console"] +test_gen_output = [] +test_insta = [] [dependencies] reginald_utils = { path = "../reginald_utils", features = ["clap"] } @@ -30,3 +32,4 @@ console = { version = "0.15.8", features = [], optional = true } [dev-dependencies] tempfile = "3.10.1" pretty_assertions = "1.4.0" +insta = "1.39.0" diff --git a/reginald_codegen/src/builtin/c/funcpack/mod.rs b/reginald_codegen/src/builtin/c/funcpack/mod.rs index 127875b..e48c595 100644 --- a/reginald_codegen/src/builtin/c/funcpack/mod.rs +++ b/reginald_codegen/src/builtin/c/funcpack/mod.rs @@ -252,9 +252,15 @@ fn generate_header(out: &mut dyn Write, inp: &Input) -> Result<(), Error> { writeln!(out, "// clang-format off")?; } + let output_file_name = inp + .output_file + .file_name() + .ok_or(Error::GeneratorError("Failed to extract file name from output path!".to_string()))? + .to_string_lossy(); + // Doxy file comment: writeln!(out, "/**")?; - writeln!(out, " * @file {}", inp.output_file.to_string_lossy())?; + writeln!(out, " * @file {output_file_name}")?; writeln!(out, " * @brief {}", inp.map.name)?; if let Some(input_file) = &inp.map.from_file { writeln!( @@ -288,8 +294,8 @@ fn generate_header(out: &mut dyn Write, inp: &Input) -> Result<(), Error> { // Include guard if inp.opts.include_guards { - writeln!(out, "#ifndef REGINALD_{}", c_macro(&inp.output_file.to_string_lossy()))?; - writeln!(out, "#define REGINALD_{}", c_macro(&inp.output_file.to_string_lossy()))?; + writeln!(out, "#ifndef REGINALD_{}", c_macro(&output_file_name))?; + writeln!(out, "#define REGINALD_{}", c_macro(&output_file_name))?; } // Includes @@ -311,8 +317,14 @@ fn generate_footer(out: &mut dyn Write, inp: &Input) -> Result<(), Error> { // Include guard: writeln!(out)?; + let output_file_name = inp + .output_file + .file_name() + .ok_or(Error::GeneratorError("Failed to extract file name from output path!".to_string()))? + .to_string_lossy(); + if inp.opts.include_guards { - writeln!(out, "#endif /* REGINALD_{} */", c_macro(&inp.output_file.to_string_lossy()))?; + writeln!(out, "#endif /* REGINALD_{} */", c_macro(&output_file_name))?; } // Clang format: diff --git a/reginald_codegen/src/builtin/c/macromap/mod.rs b/reginald_codegen/src/builtin/c/macromap/mod.rs index 0dd94d2..656ee33 100644 --- a/reginald_codegen/src/builtin/c/macromap/mod.rs +++ b/reginald_codegen/src/builtin/c/macromap/mod.rs @@ -77,8 +77,13 @@ fn generate_header( writeln!(out, "// clang-format off")?; } + let output_file_name = output_file + .file_name() + .ok_or(Error::GeneratorError("Failed to extract file name from output path!".to_string()))? + .to_string_lossy(); + writeln!(out, "/**")?; - writeln!(out, " * @file {}", output_file.to_string_lossy())?; + writeln!(out, " * @file {output_file_name}")?; writeln!(out, " * @brief {}", map.name)?; if let Some(input_file) = &map.from_file { writeln!( @@ -103,8 +108,8 @@ fn generate_header( } } writeln!(out, " */")?; - writeln!(out, "#ifndef REGINALD_{}", c_macro(&output_file.to_string_lossy()))?; - writeln!(out, "#define REGINALD_{}", c_macro(&output_file.to_string_lossy()))?; + writeln!(out, "#ifndef REGINALD_{}", c_macro(&output_file_name))?; + writeln!(out, "#define REGINALD_{}", c_macro(&output_file_name))?; writeln!(out)?; writeln!(out, "#include ")?; for include in &opts.add_include { @@ -318,7 +323,13 @@ fn generate_layout_defines(out: &mut dyn Write, map: &RegisterMap, layout: &Layo fn generate_footer(out: &mut dyn Write, output_file: &Path, opts: &GeneratorOpts) -> Result<(), Error> { writeln!(out)?; - writeln!(out, "#endif /* REGINALD_{} */", c_macro(&output_file.to_string_lossy()))?; + + let output_file_name = output_file + .file_name() + .ok_or(Error::GeneratorError("Failed to extract file name from output path!".to_string()))? + .to_string_lossy(); + + writeln!(out, "#endif /* REGINALD_{} */", c_macro(&output_file_name))?; if opts.clang_format_guard { writeln!(out, "// clang-format on")?; diff --git a/reginald_codegen/tests/generator_c_funcpack/.gitignore b/reginald_codegen/tests/generator_c_funcpack/.gitignore index 388b79c..dbf3481 100644 --- a/reginald_codegen/tests/generator_c_funcpack/.gitignore +++ b/reginald_codegen/tests/generator_c_funcpack/.gitignore @@ -1,3 +1,4 @@ artifacts/ .cache/ compile_commands.json +snapshots/ diff --git a/reginald_codegen/tests/generator_c_funcpack/mod.rs b/reginald_codegen/tests/generator_c_funcpack/mod.rs index 838685c..f50fc12 100644 --- a/reginald_codegen/tests/generator_c_funcpack/mod.rs +++ b/reginald_codegen/tests/generator_c_funcpack/mod.rs @@ -136,7 +136,7 @@ fn run_reginald(d: &TempDir, output_name: &str, opts: GeneratorOpts) { // ==== Tests ================================================================== #[test] -#[ignore] +#[cfg_attr(not(feature = "test_gen_output"), ignore)] fn generator_c_funcpack_c99() { let d = tempdir().unwrap(); @@ -155,7 +155,7 @@ fn generator_c_funcpack_c99() { } #[test] -#[ignore] +#[cfg_attr(not(feature = "test_gen_output"), ignore)] fn generator_c_funcpack_c11() { let d = tempdir().unwrap(); @@ -173,7 +173,28 @@ fn generator_c_funcpack_c11() { } #[test] -#[ignore] +#[cfg_attr(not(feature = "test_insta"), ignore)] +fn generator_c_funcpack_c11_insta() { + let d = tempdir().unwrap(); + + run_reginald( + &d, + "out.h", + GeneratorOpts { + ..GeneratorOpts::default() + }, + ); + + test_generated_code(&d, &["-std=c11"], &[]); + + let file = fs::read_to_string(d.path().join("out.h")).unwrap(); + insta::assert_snapshot!(file); + + finish_test(d); +} + +#[test] +#[cfg_attr(not(feature = "test_gen_output"), ignore)] fn generator_c_funcpack_defer_to_le() { let d = tempdir().unwrap(); @@ -192,7 +213,7 @@ fn generator_c_funcpack_defer_to_le() { } #[test] -#[ignore] +#[cfg_attr(not(feature = "test_gen_output"), ignore)] fn generator_c_funcpack_defer_to_be() { let d = tempdir().unwrap(); @@ -211,7 +232,7 @@ fn generator_c_funcpack_defer_to_be() { } #[test] -#[ignore] +#[cfg_attr(not(feature = "test_gen_output"), ignore)] fn generator_c_funcpack_split_header_source() { let d = tempdir().unwrap(); @@ -243,7 +264,7 @@ fn generator_c_funcpack_split_header_source() { } #[test] -#[ignore] +#[cfg_attr(not(feature = "test_gen_output"), ignore)] fn generator_c_funcpack_split_headers() { let d = tempdir().unwrap(); diff --git a/reginald_codegen/tests/generator_c_macromap/.gitignore b/reginald_codegen/tests/generator_c_macromap/.gitignore index d4f588e..a923aa0 100644 --- a/reginald_codegen/tests/generator_c_macromap/.gitignore +++ b/reginald_codegen/tests/generator_c_macromap/.gitignore @@ -1 +1,2 @@ artifacts/ +snapshots/ diff --git a/reginald_codegen/tests/generator_c_macromap/mod.rs b/reginald_codegen/tests/generator_c_macromap/mod.rs index 672ab85..fe4a589 100644 --- a/reginald_codegen/tests/generator_c_macromap/mod.rs +++ b/reginald_codegen/tests/generator_c_macromap/mod.rs @@ -85,7 +85,7 @@ fn finish_test(d: TempDir) { // ==== Tests ================================================================== #[test] -#[ignore] +#[cfg_attr(not(feature = "test_gen_output"), ignore)] fn generator_c_macromap_c99() { let d = tempdir().unwrap(); @@ -105,3 +105,26 @@ fn generator_c_macromap_c99() { finish_test(d); } + +#[test] +#[cfg_attr(not(feature = "test_insta"), ignore)] +fn generator_c_macromap_c99_insta() { + let d = tempdir().unwrap(); + + gen::cmd(gen::Command { + input: TEST_MAP_FILE.to_owned(), + output: d.path().to_owned().join(PathBuf::from("out.h")), + overwrite_map_name: None, + verify: false, + generator: Generator::CMacromap(GeneratorOpts { + clang_format_guard: true, + add_include: vec![], + }), + }) + .unwrap(); + + let file = fs::read_to_string(d.path().join("out.h")).unwrap(); + insta::assert_snapshot!(file); + + finish_test(d); +} diff --git a/reginald_codegen/tests/generator_rs_structs/mod.rs b/reginald_codegen/tests/generator_rs_structs/mod.rs index 8872adb..5b5404a 100644 --- a/reginald_codegen/tests/generator_rs_structs/mod.rs +++ b/reginald_codegen/tests/generator_rs_structs/mod.rs @@ -27,7 +27,7 @@ fn run_reginald(output_name: &str, opts: GeneratorOpts) { // ==== Tests ================================================================== #[test] -#[ignore] +#[cfg_attr(not(feature = "test_gen_output"), ignore)] fn generator_rs_structs() { let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); let test_proj = manifest_dir.join(PathBuf::from("tests/generator_rs_structs/test_proj/"));