Skip to content

Commit

Permalink
Adding assert_equals_golden and assert_output_equals_golden
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CauhxMilloy committed May 8, 2024
1 parent e2d855b commit f121674
Show file tree
Hide file tree
Showing 3 changed files with 3,589 additions and 0 deletions.
1 change: 1 addition & 0 deletions load.bash
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ source "$(dirname "${BASH_SOURCE[0]}")/src/assert_line.bash"
source "$(dirname "${BASH_SOURCE[0]}")/src/refute_line.bash"
source "$(dirname "${BASH_SOURCE[0]}")/src/assert_regex.bash"
source "$(dirname "${BASH_SOURCE[0]}")/src/refute_regex.bash"
source "$(dirname "${BASH_SOURCE[0]}")/src/assert_equals_golden.bash"
Loading

0 comments on commit f121674

Please sign in to comment.