Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding assert_equals_golden and assert_output_equals_golden #67

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

CauhxMilloy
Copy link

Adding support for "golden files".
Golden files are (often text) files that are checked into a repo and are used in testing for asserting strings against their content. They have a number of useful properties that make them a great alternative to single (possibly repetitive) assertions in files.
Some benefits include:

  • WYSIWYG plaintext output assertions (separating asserted output from test case logic).
  • Test failure output that is searchable (asserted output is checked into repo as files).
  • Clear file diffs of test assertions during merge / code review.
  • Terse assertions in test cases (single assert instead of many verbose assert_line and refute_line for every line of output).
  • Reusable golden files (declared once, used for many test cases).
  • Clear intention (same exact expected output) when asserted against same goldens in multiple test cases.
  • Can be clearly diff'd across multiple lines in failure message(s).
  • Easily updated.

This PR adds support to bats for golden files with the assert_equals_golden and assert_output_equals_golden test helper functions. assert_equals_golden takes 2 arguments, and , to assert is the same as the contents in . assert_output_equals_golden takes 1 argument, , to assert output is the same as the contents in .

These functions support a number of features, including --regexp and automatic updating of golden files (via setting the BATS_ASSERT_UPDATE_GOLDENS_ON_FAILURE environment variable). Golden file assertions will properly account for empty lines or trailing newlines (e.g. when asserting against output obtained by run --keep-empty-lines).

This PR adds assert_equals_golden.bats which contains test cases for the newly added functionality.

Note that the output of these functions (that is asserted on in tests) has incorrect "lines" count. This is due to an incompatibility between run --keep-empty-lines and how various bats-support helper functions work.
See bats-core/bats-support#11.

Adding support for "golden files". Golden files are (often text) files that are checked into a repo and are used in testing for asserting strings against their content.
They have a number of useful properties that make them a great alternative to single (possibly repetitive) assertions in files.
Some benefits include:
  * WYSIWYG plaintext output assertions (separating asserted output from test case logic).
  * Test failure output that is searchable (asserted output is checked into repo as files).
  * Clear file diffs of test assertions during merge / code review.
  * Terse assertions in test cases (single assert instead of many verbose `assert_line` and `refute_line` for every line of output).
  * Reusable golden files (declared once, used for many test cases).
  * Clear intention (same exact expected output) when asserted against same goldens in multiple test cases.
  * Can be clearly diff'd across multiple lines in failure message(s).
  * Easily updated.

This PR adds support to bats for golden files with the `assert_equals_golden` and `assert_output_equals_golden` test helper functions.
`assert_equals_golden` takes 2 arguments, <actual> and <golden file path>, to assert <actual> is the same as the contents in <golden file path>.
`assert_output_equals_golden` takes 1 argument, <golden file path>, to assert `output` is the same as the contents in <golden file path>.

These functions support a number of features, including `--regexp` and automatic updating of golden files (via setting the `BATS_ASSERT_UPDATE_GOLDENS_ON_FAILURE` environment variable). Golden file assertions will properly account for empty lines or trailing newlines (e.g. when asserting against output obtained by `run --keep-empty-lines`).

This PR adds `assert_equals_golden.bats` which contains test cases for the newly added functionality.

Note that the output of these functions (that is asserted on in tests) has incorrect "lines" count.
This is due to an incompatibility between `run --keep-empty-lines` and how various bats-support helper functions work.
See bats-core/bats-support#11.
…ailed (using `$output` instead of passed string).

* Updating `assert_equals_golden` tests in `test/assert_equals_golden.bats` to invalidate `output` and use another variable instead (to better catch any similar issues).
* Adding new test cases for `assert_file_equals_golden`.
* Adding `\` to list of special characters in doc strings.
* Adding test cases to cover all special characters with `BATS_ASSERT_UPDATE_GOLDENS_ON_FAILURE`.
* Cleaning up some unnecessary lines in a few existing `assert_equals_golden` tests.
…even if the golden ends up being updated).

* Adding prefix of golden file path before error messages.
* Simplifying return status from assert functions.
* Updating tests to assert output with new changes.
  * Adding helper function to save tmp file path used in test cases (to then be used in assertion messages).
  * Adding a space after `\` in some test cases to handle/workaround `\\\n` in docstrings not being parsed properly by bash.
@jasonkarns
Copy link
Member

jasonkarns commented May 28, 2024

Is there a strong case for having a dedicated matcher instead of using $(cat fixtures/expected.txt) with whatever assertion helper best fits?

I recognize some other conveniences still need built out (better diffing with the assert_output, and more consistent options like partial/regex for assert_equals.

But I would propose we beef up the core assertions and then use them for what is still a text matching assertion.

If we do want some additional helper, I could see a fixture helper of some kind that had conventional pathing built-in. But the assertion side of the equations doesn't feel dissimilar enough from the core methods to necessitate a new one. (Some evidence to support this is that many other test utilities in other frameworks have plenty of helpers for fixtures in general; but none that I've seen ever combine fixture support with the matcher.)

@CauhxMilloy
Copy link
Author

Using $(cat expected.txt) has in fact been something that I've used quite a bit and was the motivator to create these functions. Handling trailing new lines, protecting against authoring errors, etc, leads to a large chunk of code at every assertion. Having that wrapped up into a simple function really does help clean up the actual test case logic (easier to see what is actually tested). In addition to that, having the ability to automatically update goldens (i.e. when output purposefully changes) is really useful as a codebase changes over time (exhaustive assertions, instantly updated). Being able to get that functionality, but very succinctly, is really nice.

I made these into separate assertions to both allow for clarity of how the assertion is done and to keep existing functions from being bloated. When I had added in bats_pipe (bats-core/bats-core#663, me from another account), I had started off making it part of run. It then got moved out into its own helper. assert_output already has many bells and whistles (and can actually lead to some authoring errors when accidentally used slightly incorrectly, but that's a separate topic) and I didn't want to lump this golden file functionality on top. Given that there's many flavors of asserts in bats, each to simplify writing of tests, I felt like this would follow suit. Can this be handled by combinations of other functions? Maybe (but not as clearly/nicely), and the same goes for "cant assert_output 'want' just be assert_equals "$output" 'want'?".

I've seen libraries with golden file support and sometimes automatic updates (which must be based on the text asserted) is done via built-in functionality or via scripts. But it's usually a common desire when working with them.

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