From 3f687deb0e4148588c728d50512eaef5ba58a5e2 Mon Sep 17 00:00:00 2001 From: Vladislav Zavialov Date: Sun, 17 Nov 2024 02:18:06 +0300 Subject: [PATCH] [Chore] Tests: golden files, add BATS_ACCEPT Problem: in golden tests, it was impossible to automatically accept changes en masse, e.g. if formatting changed. Solution: factor out golden files (i.e. don't inline the expected output directly in *.bats) and introduce a BATS_ACCEPT=1 option to update the expected output. The main change is in `assert_diff`, and the rest of the patch updates its use sites accordingly. --- tests/golden/check-anchors/check-anchors.bats | 67 ++----------------- tests/golden/check-anchors/expected1.gold | 32 +++++++++ tests/golden/check-anchors/expected2.gold | 27 ++++++++ .../check-autolinks/check-autolinks.bats | 30 +-------- tests/golden/check-autolinks/expected.gold | 26 +++++++ .../check-backslash/check-backslash.bats | 5 +- .../check-case-sensitivity-anchor.bats | 8 +-- .../check-case-sensitivity-path.bats | 8 +-- tests/golden/check-cli/check-cli.bats | 51 ++------------ tests/golden/check-cli/expected1.gold | 12 ++++ tests/golden/check-cli/expected2.gold | 12 ++++ tests/golden/check-cli/expected3.gold | 12 ++++ tests/golden/check-color/check-color.bats | 25 +++++-- .../check-dump-config/check-dump-config.bats | 4 +- .../check-footnotes/check-footnotes.bats | 4 +- tests/golden/check-git/check-git.bats | 52 +++----------- tests/golden/check-git/expected1.gold | 12 ++++ tests/golden/check-git/expected2.gold | 12 ++++ tests/golden/check-git/expected3.gold | 13 ++++ tests/golden/check-ignore/check-ignore.bats | 39 ++--------- .../{expected.gold => expected1.gold} | 2 +- tests/golden/check-ignore/expected2.gold | 21 ++++++ .../check-ignoreExternalRefsTo.bats | 15 ++++- .../check-ignoreLocalRefsTo.bats | 4 +- .../check-ignoreRefsFrom.bats | 17 +---- .../golden/check-ignoreRefsFrom/expected.gold | 12 ++++ tests/golden/check-images/check-images.bats | 4 +- .../check-local-refs/check-local-refs.bats | 12 ++-- .../check-redirect-parse.bats | 30 +++------ .../check-redirect-parse/expected1.gold | 1 + .../check-redirect-parse/expected2.gold | 1 + .../check-redirect-parse/expected3.gold | 1 + .../check-redirect-parse/expected4.gold | 1 + .../check-redirect-parse/expected5.gold | 1 + .../check-scan-errors/check-scan-errors.bats | 4 +- .../golden/check-symlinks/check-symlinks.bats | 10 +-- tests/golden/helpers.bash | 39 ++++------- 37 files changed, 315 insertions(+), 311 deletions(-) create mode 100644 tests/golden/check-anchors/expected1.gold create mode 100644 tests/golden/check-anchors/expected2.gold create mode 100644 tests/golden/check-autolinks/expected.gold create mode 100644 tests/golden/check-cli/expected1.gold create mode 100644 tests/golden/check-cli/expected2.gold create mode 100644 tests/golden/check-cli/expected3.gold create mode 100644 tests/golden/check-git/expected1.gold create mode 100644 tests/golden/check-git/expected2.gold create mode 100644 tests/golden/check-git/expected3.gold rename tests/golden/check-ignore/{expected.gold => expected1.gold} (74%) create mode 100644 tests/golden/check-ignore/expected2.gold create mode 100644 tests/golden/check-ignoreRefsFrom/expected.gold create mode 100644 tests/golden/check-redirect-parse/expected1.gold create mode 100644 tests/golden/check-redirect-parse/expected2.gold create mode 100644 tests/golden/check-redirect-parse/expected3.gold create mode 100644 tests/golden/check-redirect-parse/expected4.gold create mode 100644 tests/golden/check-redirect-parse/expected5.gold diff --git a/tests/golden/check-anchors/check-anchors.bats b/tests/golden/check-anchors/check-anchors.bats index ed9ee870..f7c1d8e0 100644 --- a/tests/golden/check-anchors/check-anchors.bats +++ b/tests/golden/check-anchors/check-anchors.bats @@ -10,72 +10,13 @@ load '../helpers/bats-file/load' load '../helpers' @test "We report ambiguous anchor references" { + golden_file=$(realpath expected1.gold) to_temp xrefcheck -r ambiguous-anchors -assert_diff - < https://commonmark.org - ^-- stopped before this one - -Invalid references dumped, 1 in total. -EOF + assert_diff } diff --git a/tests/golden/check-autolinks/expected.gold b/tests/golden/check-autolinks/expected.gold new file mode 100644 index 00000000..6c5d93b0 --- /dev/null +++ b/tests/golden/check-autolinks/expected.gold @@ -0,0 +1,26 @@ +=== Repository data === + + file-with-autolinks.md: + - references: + - reference (external): + - text: "https://www.google.com/doodles" + - link: https://www.google.com/doodles + - reference (external) at src:8:0-18: + - text: "www.commonmark.org" + - link: http://www.commonmark.org + - anchors: + none + +=== Invalid references found === + + ➥ In file file-with-autolinks.md + bad reference (external) at src:8:0-18: + - text: "www.commonmark.org" + - link: http://www.commonmark.org + + Permanent redirect found: + -| http://www.commonmark.org + -> https://commonmark.org + ^-- stopped before this one + +Invalid references dumped, 1 in total. diff --git a/tests/golden/check-backslash/check-backslash.bats b/tests/golden/check-backslash/check-backslash.bats index 41dd1aa5..5bec9c43 100644 --- a/tests/golden/check-backslash/check-backslash.bats +++ b/tests/golden/check-backslash/check-backslash.bats @@ -11,8 +11,9 @@ load '../helpers' @test "Checking files with backslash" { + golden_file=$(realpath expected.gold) + cp a.md $TEST_TEMP_DIR - cp expected.gold $TEST_TEMP_DIR touch "$TEST_TEMP_DIR/a\a.md" || \ return 0 # Cannot be tested on Windows @@ -29,5 +30,5 @@ EOF to_temp xrefcheck -v - assert_diff expected.gold + assert_diff } diff --git a/tests/golden/check-case-sensitivity-anchor/check-case-sensitivity-anchor.bats b/tests/golden/check-case-sensitivity-anchor/check-case-sensitivity-anchor.bats index cf7a4bf4..dba95e5c 100644 --- a/tests/golden/check-case-sensitivity-anchor/check-case-sensitivity-anchor.bats +++ b/tests/golden/check-case-sensitivity-anchor/check-case-sensitivity-anchor.bats @@ -11,13 +11,13 @@ load '../helpers' @test "GitHub anchors: check, ambiguous and similar detection is case-insensitive" { + golden_file=$(realpath expected1.gold) to_temp xrefcheck -c config-github.yaml - - assert_diff expected1.gold + assert_diff } @test "GitLab anchors: check and ambiguous detection is case-sensitive, but similar detection is not" { + golden_file=$(realpath expected2.gold) to_temp xrefcheck -c config-gitlab.yaml - - assert_diff expected2.gold + assert_diff } diff --git a/tests/golden/check-case-sensitivity-path/check-case-sensitivity-path.bats b/tests/golden/check-case-sensitivity-path/check-case-sensitivity-path.bats index cf03d52f..858c36a6 100644 --- a/tests/golden/check-case-sensitivity-path/check-case-sensitivity-path.bats +++ b/tests/golden/check-case-sensitivity-path/check-case-sensitivity-path.bats @@ -11,13 +11,13 @@ load '../helpers' @test "GitHub paths: case-sensitive" { + golden_file=$(realpath expected.gold) to_temp xrefcheck -v -c config-github.yaml - - assert_diff expected.gold + assert_diff } @test "GitLab paths: case-sensitive" { + golden_file=$(realpath expected.gold) to_temp xrefcheck -v -c config-gitlab.yaml - - assert_diff expected.gold + assert_diff } diff --git a/tests/golden/check-cli/check-cli.bats b/tests/golden/check-cli/check-cli.bats index 7ec02dbf..4e4db229 100644 --- a/tests/golden/check-cli/check-cli.bats +++ b/tests/golden/check-cli/check-cli.bats @@ -42,60 +42,21 @@ load '../helpers' } @test "Basic root, check errors report" { + golden_file=$(realpath expected1.gold) to_temp xrefcheck --root . - - assert_diff - < $output_file + assert_diff } @test "No color flag (not in CI)" { - CI=false xrefcheck -v --no-progress --no-color | diff - expected-no-color.gold + golden_file=$(realpath expected-no-color.gold) + output_file="$TEST_TEMP_DIR/temp_file.test" + CI=false xrefcheck -v --no-progress --no-color > $output_file + assert_diff } @test "No color default when pipe (not in CI)" { - CI=false xrefcheck -v --no-progress | diff - expected-no-color.gold + golden_file=$(realpath expected-no-color.gold) + output_file="$TEST_TEMP_DIR/temp_file.test" + CI=false xrefcheck -v --no-progress > $output_file + assert_diff } @test "Color default when CI" { - CI=true xrefcheck -v --no-progress | diff - expected-color.gold + golden_file=$(realpath expected-color.gold) + output_file="$TEST_TEMP_DIR/temp_file.test" + CI=true xrefcheck -v --no-progress > $output_file + assert_diff } @test "No color flag in CI" { - CI=true xrefcheck -v --no-progress --no-color | diff - expected-no-color.gold + golden_file=$(realpath expected-no-color.gold) + output_file="$TEST_TEMP_DIR/temp_file.test" + CI=true xrefcheck -v --no-progress --no-color > $output_file + assert_diff } diff --git a/tests/golden/check-dump-config/check-dump-config.bats b/tests/golden/check-dump-config/check-dump-config.bats index f2d09ab9..f9b3fb97 100644 --- a/tests/golden/check-dump-config/check-dump-config.bats +++ b/tests/golden/check-dump-config/check-dump-config.bats @@ -11,9 +11,9 @@ load '../helpers' @test "Dump config to stdout" { + golden_file=$(realpath ../../configs/github-config.yaml) to_temp xrefcheck dump-config --stdout -t GitHub - - assert_diff ../../configs/github-config.yaml + assert_diff } @test "Dump config to existent default file error" { diff --git a/tests/golden/check-footnotes/check-footnotes.bats b/tests/golden/check-footnotes/check-footnotes.bats index 939a8840..5ae90d58 100644 --- a/tests/golden/check-footnotes/check-footnotes.bats +++ b/tests/golden/check-footnotes/check-footnotes.bats @@ -11,9 +11,9 @@ load '../helpers' @test "We report broken links inside footnotes" { + golden_file=$(realpath expected.gold) to_temp xrefcheck -r broken-link-in-footnote - - assert_diff expected.gold + assert_diff } @test "We're not treating footnotes as 'shortcut reference links'" { diff --git a/tests/golden/check-git/check-git.bats b/tests/golden/check-git/check-git.bats index ee79d9b5..af504c2c 100644 --- a/tests/golden/check-git/check-git.bats +++ b/tests/golden/check-git/check-git.bats @@ -38,6 +38,8 @@ load '../helpers' } @test "Git: bad file not tracked, --include-untracked enabled, check failure" { + golden_file=$(realpath expected1.gold) + cd $TEST_TEMP_DIR git init @@ -46,23 +48,12 @@ load '../helpers' to_temp xrefcheck --include-untracked - assert_diff - <