Skip to content

Add C++ golden file test suite - #223

Merged
EricRahm merged 1 commit into
google:masterfrom
EricRahm:rust-backend-py-goldens
Jul 8, 2025
Merged

Add C++ golden file test suite#223
EricRahm merged 1 commit into
google:masterfrom
EricRahm:rust-backend-py-goldens

Conversation

@EricRahm

@EricRahm EricRahm commented Jul 2, 2025

Copy link
Copy Markdown
Collaborator

This change introduces a robust, granular testing infrastructure for the C++ backend using golden files. This test suite serves as a critical safety net to enable safe and efficient refactoring of the code generator in the future.

By comparing generated code against a set of "golden" (known-good) header files, we can quickly detect any unintended changes or regressions.

Key Features:

  • Granular Tests: Each golden file comparison is a standalone py_test target, allowing for parallel execution and precise failure identification.
  • cpp_golden_test Macro: A new Starlark macro, cpp_golden_test, has been added to compiler/back_end/cpp/build_defs.bzl. This simplifies adding new golden tests by abstracting away the underlying py_test implementation details.
  • Debugging: To simplify the diagnosis of regressions, test failures produce a diff of the generated file against its golden counterpart.
  • Full Coverage: The suite includes tests for all .emb files in the testdata directory, including those with complex dependencies and imports.

Usage:

To run all C++ golden tests:

bazel test //compiler/back_end/cpp/... --test_tag_filters=golden

To run a single golden test:

bazel test //compiler/back_end/cpp:bits_golden_test

To add a new golden test:

  1. Add your .emb file to testdata/.
  2. Generate the corresponding golden .emb.h file (e.g., by temporarily modifying and running the generate_golden_files.sh script via a sh_binary).
  3. Copy the new golden file into testdata/golden_cpp/.
  4. Add a cpp_golden_test() rule to compiler/back_end/cpp/BUILD.

@EricRahm

EricRahm commented Jul 2, 2025

Copy link
Copy Markdown
Collaborator Author

#224

@EricRahm
EricRahm force-pushed the rust-backend-py-goldens branch from c589fd2 to e9fa82a Compare July 2, 2025 23:55
@EricRahm
EricRahm requested a review from jasongraffius July 8, 2025 00:48
@EricRahm

EricRahm commented Jul 8, 2025

Copy link
Copy Markdown
Collaborator Author

Jason can you take a look? You can ignore the generated .h files

@jasongraffius jasongraffius left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM % minor copyright header nits

Comment thread compiler/back_end/cpp/one_golden_test.py Outdated
Comment thread compiler/back_end/cpp/run_one_golden_test.py
@EricRahm
EricRahm force-pushed the rust-backend-py-goldens branch from e9fa82a to d2f500b Compare July 8, 2025 02:35
This change introduces a robust, granular testing infrastructure for the C++ backend using golden files. This test suite serves as a critical safety net to enable safe and efficient refactoring of the code generator in the future.

By comparing generated code against a set of "golden" (known-good) header files, we can quickly detect any unintended changes or regressions.

Key Features:

*   **Granular Tests:** Each golden file comparison is a standalone `py_test` target, allowing for parallel execution and precise failure identification.
*   **`cpp_golden_test` Macro:** A new Starlark macro, `cpp_golden_test`, has been added to `compiler/back_end/cpp/build_defs.bzl`. This simplifies adding new golden tests by abstracting away the underlying `py_test` implementation details.
*   **Debugging:** To simplify the diagnosis of regressions, test failures produce a `diff` of the generated file against its golden counterpart.
*   **Full Coverage:** The suite includes tests for all `.emb` files in the `testdata` directory, including those with complex dependencies and imports.

Usage:

**To run all C++ golden tests:**
```
bazel test //compiler/back_end/cpp/... --test_tag_filters=golden
```

**To run a single golden test:**
```
bazel test //compiler/back_end/cpp:bits_golden_test
```

**To add a new golden test:**
1.  Add your `.emb` file to `testdata/`.
2.  Generate the corresponding golden `.emb.h` file (e.g., by temporarily modifying and running the `generate_golden_files.sh` script via a `sh_binary`).
3.  Copy the new golden file into `testdata/golden_cpp/`.
4.  Add a `cpp_golden_test()` rule to `compiler/back_end/cpp/BUILD`.
@EricRahm
EricRahm force-pushed the rust-backend-py-goldens branch from d2f500b to 3e36782 Compare July 8, 2025 02:38
@EricRahm
EricRahm merged commit f615220 into google:master Jul 8, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants