mirror of
https://github.com/serokell/xrefcheck.git
synced 2024-10-05 18:07:35 +03:00
[#133] Refactor golden tests
Problem: We're using a common pattern in our bats tests: Run xrefcheck, redirect output to a temp file Check the temp file matches some .gold file using `diff` Delete temp file We could encapsulate this pattern and make it easier to reuse. Solution: In the `setup` function, create a temp directory. In the `teardown` function, delete the temp directory. Create a `to_temp` function that runs xrefcheck with desired options, pipes its output through the `prepare` helper function and saves it in a file inside the temp directory. Create a `assert_diff` function that reads the temp file, and uses `diff` to compare it against some expected output.
This commit is contained in:
parent
08dd61bb9f
commit
95d5bad3cd
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -8,3 +8,6 @@
|
||||
[submodule "tests/golden/helpers/bats-support"]
|
||||
path = tests/golden/helpers/bats-support
|
||||
url = git@github.com:bats-core/bats-support.git
|
||||
[submodule "tests/golden/helpers/bats-file"]
|
||||
path = tests/golden/helpers/bats-file
|
||||
url = https://github.com/bats-core/bats-file.git
|
||||
|
@ -6,16 +6,25 @@
|
||||
|
||||
load '../helpers/bats-support/load'
|
||||
load '../helpers/bats-assert/load'
|
||||
load '../helpers/bats-file/load'
|
||||
load '../helpers'
|
||||
|
||||
|
||||
@test "Relative anchor, check error report" {
|
||||
xrefcheck | prepare > /tmp/check-anchors.test || true
|
||||
to_temp xrefcheck
|
||||
|
||||
diff /tmp/check-anchors.test expected.gold \
|
||||
--ignore-space-change \
|
||||
--ignore-blank-lines \
|
||||
--new-file # treat absent files as empty
|
||||
assert_diff - <<EOF
|
||||
=== Invalid references found ===
|
||||
|
||||
rm /tmp/check-anchors.test
|
||||
➥ In file check-relative-anchor.md
|
||||
bad reference (relative) at src:7:1-40:
|
||||
- text: "no-anchor"
|
||||
- link: no-anchor.md
|
||||
- anchor: invalid-anchor
|
||||
|
||||
⛀ Anchor 'invalid-anchor' is not present
|
||||
|
||||
|
||||
Invalid references dumped, 1 in total.
|
||||
EOF
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
=== Invalid references found ===
|
||||
|
||||
➥ In file check-relative-anchor.md
|
||||
bad reference (relative) at src:7:1-40:
|
||||
- text: "no-anchor"
|
||||
- link: no-anchor.md
|
||||
- anchor: invalid-anchor
|
||||
|
||||
⛀ Anchor 'invalid-anchor' is not present
|
||||
|
||||
|
||||
Invalid references dumped, 1 in total.
|
@ -6,6 +6,7 @@
|
||||
|
||||
load '../helpers/bats-support/load'
|
||||
load '../helpers/bats-assert/load'
|
||||
load '../helpers/bats-file/load'
|
||||
load '../helpers'
|
||||
|
||||
@test "No redundant slashes" {
|
||||
@ -41,41 +42,63 @@ load '../helpers'
|
||||
}
|
||||
|
||||
@test "Basic root, check errors report" {
|
||||
xrefcheck \
|
||||
--root . \
|
||||
| prepare > /tmp/check-cli.test || true
|
||||
to_temp xrefcheck --root .
|
||||
|
||||
diff /tmp/check-cli.test expected.gold \
|
||||
--ignore-space-change \
|
||||
--ignore-blank-lines \
|
||||
--new-file # treat absent files as empty
|
||||
assert_diff - <<EOF
|
||||
=== Invalid references found ===
|
||||
|
||||
rm /tmp/check-cli.test
|
||||
➥ In file to-ignore/broken-link.md
|
||||
bad reference (absolute) at src:7:1-25:
|
||||
- text: "my link"
|
||||
- link: /one/two/three
|
||||
- anchor: -
|
||||
|
||||
⛀ File does not exist:
|
||||
./one/two/three
|
||||
|
||||
|
||||
Invalid references dumped, 1 in total.
|
||||
EOF
|
||||
}
|
||||
|
||||
@test "Root with redundant slashes, check errors report" {
|
||||
xrefcheck \
|
||||
--root ././///././././//./ \
|
||||
| prepare > /tmp/check-cli.test || true
|
||||
to_temp xrefcheck --root ././///././././//./
|
||||
|
||||
diff /tmp/check-cli.test expected.gold \
|
||||
--ignore-space-change \
|
||||
--ignore-blank-lines \
|
||||
--new-file # treat absent files as empty
|
||||
assert_diff - <<EOF
|
||||
=== Invalid references found ===
|
||||
|
||||
rm /tmp/check-cli.test
|
||||
➥ In file to-ignore/broken-link.md
|
||||
bad reference (absolute) at src:7:1-25:
|
||||
- text: "my link"
|
||||
- link: /one/two/three
|
||||
- anchor: -
|
||||
|
||||
⛀ File does not exist:
|
||||
./one/two/three
|
||||
|
||||
|
||||
Invalid references dumped, 1 in total.
|
||||
EOF
|
||||
}
|
||||
|
||||
@test "No root, check errors report" {
|
||||
xrefcheck \
|
||||
| prepare > /tmp/check-cli.test || true
|
||||
to_temp xrefcheck
|
||||
|
||||
diff /tmp/check-cli.test expected.gold \
|
||||
--ignore-space-change \
|
||||
--ignore-blank-lines \
|
||||
--new-file # treat absent files as empty
|
||||
assert_diff - <<EOF
|
||||
=== Invalid references found ===
|
||||
|
||||
rm /tmp/check-cli.test
|
||||
➥ In file to-ignore/broken-link.md
|
||||
bad reference (absolute) at src:7:1-25:
|
||||
- text: "my link"
|
||||
- link: /one/two/three
|
||||
- anchor: -
|
||||
|
||||
⛀ File does not exist:
|
||||
./one/two/three
|
||||
|
||||
|
||||
Invalid references dumped, 1 in total.
|
||||
EOF
|
||||
}
|
||||
|
||||
@test "Single file as root" {
|
||||
|
@ -1,13 +0,0 @@
|
||||
=== Invalid references found ===
|
||||
|
||||
➥ In file to-ignore/broken-link.md
|
||||
bad reference (absolute) at src:7:1-25:
|
||||
- text: "my link"
|
||||
- link: /one/two/three
|
||||
- anchor: -
|
||||
|
||||
⛀ File does not exist:
|
||||
./one/two/three
|
||||
|
||||
|
||||
Invalid references dumped, 1 in total.
|
@ -6,6 +6,7 @@
|
||||
|
||||
load '../helpers/bats-support/load'
|
||||
load '../helpers/bats-assert/load'
|
||||
load '../helpers/bats-file/load'
|
||||
load '../helpers'
|
||||
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
load '../helpers/bats-support/load'
|
||||
load '../helpers/bats-assert/load'
|
||||
load '../helpers/bats-file/load'
|
||||
load '../helpers'
|
||||
|
||||
@test "Ignore localhost" {
|
||||
@ -17,17 +18,11 @@ load '../helpers'
|
||||
}
|
||||
|
||||
@test "Ignore localhost, check errors" {
|
||||
xrefcheck \
|
||||
to_temp xrefcheck \
|
||||
-c config-check-enabled.yaml \
|
||||
-r . \
|
||||
| prepare > /tmp/check-localhost.test || true
|
||||
-r .
|
||||
|
||||
diff /tmp/check-localhost.test expected.gold \
|
||||
--ignore-space-change \
|
||||
--ignore-blank-lines \
|
||||
--new-file # treat absent files as empty
|
||||
|
||||
rm /tmp/check-localhost.test
|
||||
assert_diff expected.gold
|
||||
}
|
||||
|
||||
@test "Ignore localhost, no config specified" {
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
load '../helpers/bats-support/load'
|
||||
load '../helpers/bats-assert/load'
|
||||
load '../helpers/bats-file/load'
|
||||
load '../helpers'
|
||||
|
||||
|
||||
@ -33,14 +34,8 @@ load '../helpers'
|
||||
assert_output --partial "All repository links are valid."
|
||||
}
|
||||
|
||||
@test "Ignore file with broken xrefcheck annotation: directory, check filure" {
|
||||
xrefcheck --ignored ./to-ignore/inner-directory/ \
|
||||
| prepare > /tmp/check-ignored.test || true
|
||||
@test "Ignore file with broken xrefcheck annotation: directory, check failure" {
|
||||
to_temp xrefcheck --ignored ./to-ignore/inner-directory/
|
||||
|
||||
diff /tmp/check-ignored.test expected.gold \
|
||||
--ignore-space-change \
|
||||
--ignore-blank-lines \
|
||||
--new-file # treat absent files as empty
|
||||
|
||||
rm /tmp/check-ignored.test
|
||||
assert_diff expected.gold
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
load '../helpers/bats-support/load'
|
||||
load '../helpers/bats-assert/load'
|
||||
load '../helpers/bats-file/load'
|
||||
load '../helpers'
|
||||
|
||||
|
||||
@ -28,13 +29,21 @@ load '../helpers'
|
||||
}
|
||||
|
||||
@test "Not scanned: directory, check failure" {
|
||||
xrefcheck -c config-directory.yaml \
|
||||
| prepare > /tmp/check-notScanned.test || true
|
||||
to_temp xrefcheck -c config-directory.yaml
|
||||
|
||||
diff /tmp/check-notScanned.test expected.gold \
|
||||
--ignore-space-change \
|
||||
--ignore-blank-lines \
|
||||
--new-file # treat absent files as empty
|
||||
assert_diff - <<EOF
|
||||
=== Invalid references found ===
|
||||
|
||||
rm /tmp/check-notScanned.test
|
||||
➥ In file notScanned/inner-directory/bad-reference.md
|
||||
bad reference (absolute) at src:7:1-28:
|
||||
- text: "Bad reference"
|
||||
- link: /no-file.md
|
||||
- anchor: -
|
||||
|
||||
⛀ File does not exist:
|
||||
./no-file.md
|
||||
|
||||
|
||||
Invalid references dumped, 1 in total.
|
||||
EOF
|
||||
}
|
||||
|
@ -1,13 +0,0 @@
|
||||
=== Invalid references found ===
|
||||
|
||||
➥ In file notScanned/inner-directory/bad-reference.md
|
||||
bad reference (absolute) at src:7:1-28:
|
||||
- text: "Bad reference"
|
||||
- link: /no-file.md
|
||||
- anchor: -
|
||||
|
||||
⛀ File does not exist:
|
||||
./no-file.md
|
||||
|
||||
|
||||
Invalid references dumped, 1 in total.
|
@ -6,16 +6,12 @@
|
||||
|
||||
load '../helpers/bats-support/load'
|
||||
load '../helpers/bats-assert/load'
|
||||
load '../helpers/bats-file/load'
|
||||
load '../helpers'
|
||||
|
||||
|
||||
@test "Dump all errors along with broken links" {
|
||||
xrefcheck | prepare > /tmp/check-scan-errors.test || true
|
||||
to_temp xrefcheck
|
||||
|
||||
diff /tmp/check-scan-errors.test expected.gold \
|
||||
--ignore-space-change \
|
||||
--ignore-blank-lines \
|
||||
--new-file # treat absent files as empty
|
||||
|
||||
rm /tmp/check-scan-errors.test
|
||||
assert_diff expected.gold
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
load '../helpers/bats-support/load'
|
||||
load '../helpers/bats-assert/load'
|
||||
load '../helpers/bats-file/load'
|
||||
load '../helpers'
|
||||
|
||||
|
||||
@ -16,12 +17,7 @@ load '../helpers'
|
||||
}
|
||||
|
||||
@test "Virtual files: check failure" {
|
||||
xrefcheck | prepare > /tmp/check-virtualFiles.test || true
|
||||
to_temp xrefcheck
|
||||
|
||||
diff /tmp/check-virtualFiles.test expected.gold \
|
||||
--ignore-space-change \
|
||||
--ignore-blank-lines \
|
||||
--new-file # treat absent files as empty
|
||||
|
||||
rm /tmp/check-virtualFiles.test
|
||||
assert_diff expected.gold
|
||||
}
|
||||
|
@ -5,6 +5,11 @@
|
||||
setup () {
|
||||
# change working directory to the location of the running `bats` suite.
|
||||
cd "$( dirname "$BATS_TEST_FILENAME")"
|
||||
TEST_TEMP_DIR="$(temp_make)"
|
||||
}
|
||||
|
||||
teardown() {
|
||||
temp_del "$TEST_TEMP_DIR"
|
||||
}
|
||||
|
||||
# this function is used for:
|
||||
@ -17,3 +22,54 @@ prepare () {
|
||||
| sed 's/socket: [0-9]*/socket: N/g' \
|
||||
| sed 's/Network.Socket.connect: <socket: N>: does not exist (Connection refused),//g'
|
||||
}
|
||||
|
||||
# Create temporary file with program output, used with `assert_diff`.
|
||||
to_temp() {
|
||||
output_file="$TEST_TEMP_DIR/temp_file.test"
|
||||
$@ | prepare > $output_file
|
||||
}
|
||||
|
||||
# Uses `diff` to compare output file created by `to_temp` against expected output.
|
||||
# Expected output could be given either:
|
||||
# - in the form of a filepath, e.g. `assert_diff expected.gold`
|
||||
# - via stdin when `-` is used, e.g. `assert_diff -`
|
||||
# Usage examples:
|
||||
# - filepath:
|
||||
#
|
||||
# @test "Ignore localhost, check errors" {
|
||||
# to_temp xrefcheck \
|
||||
# -c config-check-enabled.yaml \
|
||||
# -r .
|
||||
#
|
||||
# assert_diff expected.gold
|
||||
# }
|
||||
#
|
||||
# - stdin:
|
||||
# @test "Relative anchor, check error report" {
|
||||
# to_temp xrefcheck
|
||||
#
|
||||
# assert_diff - <<EOF
|
||||
# === Invalid references found ===
|
||||
#
|
||||
# ➥ In file check-relative-anchor.md
|
||||
# bad reference (relative) at src:7:1-40:
|
||||
# - text: "no-anchor"
|
||||
# - link: no-anchor.md
|
||||
# - anchor: invalid-anchor
|
||||
#
|
||||
# ⛀ Anchor 'invalid-anchor' is not present
|
||||
#
|
||||
#
|
||||
# Invalid references dumped, 1 in total.
|
||||
# EOF
|
||||
# }
|
||||
assert_diff() {
|
||||
: "{output_file?}"
|
||||
|
||||
diff $output_file $1 \
|
||||
--ignore-space-change \
|
||||
--ignore-tab-expansion \
|
||||
--ignore-trailing-space \
|
||||
--ignore-blank-lines \
|
||||
--new-file # treat absent files as empty
|
||||
}
|
||||
|
1
tests/golden/helpers/bats-file
Submodule
1
tests/golden/helpers/bats-file
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit f9154f43104322f9a30fb0a9e63c7cc46c5a8dc2
|
Loading…
Reference in New Issue
Block a user