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"]
|
[submodule "tests/golden/helpers/bats-support"]
|
||||||
path = tests/golden/helpers/bats-support
|
path = tests/golden/helpers/bats-support
|
||||||
url = git@github.com:bats-core/bats-support.git
|
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-support/load'
|
||||||
load '../helpers/bats-assert/load'
|
load '../helpers/bats-assert/load'
|
||||||
|
load '../helpers/bats-file/load'
|
||||||
load '../helpers'
|
load '../helpers'
|
||||||
|
|
||||||
|
|
||||||
@test "Relative anchor, check error report" {
|
@test "Relative anchor, check error report" {
|
||||||
xrefcheck | prepare > /tmp/check-anchors.test || true
|
to_temp xrefcheck
|
||||||
|
|
||||||
diff /tmp/check-anchors.test expected.gold \
|
assert_diff - <<EOF
|
||||||
--ignore-space-change \
|
=== Invalid references found ===
|
||||||
--ignore-blank-lines \
|
|
||||||
--new-file # treat absent files as empty
|
|
||||||
|
|
||||||
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-support/load'
|
||||||
load '../helpers/bats-assert/load'
|
load '../helpers/bats-assert/load'
|
||||||
|
load '../helpers/bats-file/load'
|
||||||
load '../helpers'
|
load '../helpers'
|
||||||
|
|
||||||
@test "No redundant slashes" {
|
@test "No redundant slashes" {
|
||||||
@ -41,41 +42,63 @@ load '../helpers'
|
|||||||
}
|
}
|
||||||
|
|
||||||
@test "Basic root, check errors report" {
|
@test "Basic root, check errors report" {
|
||||||
xrefcheck \
|
to_temp xrefcheck --root .
|
||||||
--root . \
|
|
||||||
| prepare > /tmp/check-cli.test || true
|
|
||||||
|
|
||||||
diff /tmp/check-cli.test expected.gold \
|
assert_diff - <<EOF
|
||||||
--ignore-space-change \
|
=== Invalid references found ===
|
||||||
--ignore-blank-lines \
|
|
||||||
--new-file # treat absent files as empty
|
|
||||||
|
|
||||||
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" {
|
@test "Root with redundant slashes, check errors report" {
|
||||||
xrefcheck \
|
to_temp xrefcheck --root ././///././././//./
|
||||||
--root ././///././././//./ \
|
|
||||||
| prepare > /tmp/check-cli.test || true
|
|
||||||
|
|
||||||
diff /tmp/check-cli.test expected.gold \
|
assert_diff - <<EOF
|
||||||
--ignore-space-change \
|
=== Invalid references found ===
|
||||||
--ignore-blank-lines \
|
|
||||||
--new-file # treat absent files as empty
|
|
||||||
|
|
||||||
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" {
|
@test "No root, check errors report" {
|
||||||
xrefcheck \
|
to_temp xrefcheck
|
||||||
| prepare > /tmp/check-cli.test || true
|
|
||||||
|
|
||||||
diff /tmp/check-cli.test expected.gold \
|
assert_diff - <<EOF
|
||||||
--ignore-space-change \
|
=== Invalid references found ===
|
||||||
--ignore-blank-lines \
|
|
||||||
--new-file # treat absent files as empty
|
|
||||||
|
|
||||||
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" {
|
@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-support/load'
|
||||||
load '../helpers/bats-assert/load'
|
load '../helpers/bats-assert/load'
|
||||||
|
load '../helpers/bats-file/load'
|
||||||
load '../helpers'
|
load '../helpers'
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
load '../helpers/bats-support/load'
|
load '../helpers/bats-support/load'
|
||||||
load '../helpers/bats-assert/load'
|
load '../helpers/bats-assert/load'
|
||||||
|
load '../helpers/bats-file/load'
|
||||||
load '../helpers'
|
load '../helpers'
|
||||||
|
|
||||||
@test "Ignore localhost" {
|
@test "Ignore localhost" {
|
||||||
@ -17,17 +18,11 @@ load '../helpers'
|
|||||||
}
|
}
|
||||||
|
|
||||||
@test "Ignore localhost, check errors" {
|
@test "Ignore localhost, check errors" {
|
||||||
xrefcheck \
|
to_temp xrefcheck \
|
||||||
-c config-check-enabled.yaml \
|
-c config-check-enabled.yaml \
|
||||||
-r . \
|
-r .
|
||||||
| prepare > /tmp/check-localhost.test || true
|
|
||||||
|
|
||||||
diff /tmp/check-localhost.test expected.gold \
|
assert_diff expected.gold
|
||||||
--ignore-space-change \
|
|
||||||
--ignore-blank-lines \
|
|
||||||
--new-file # treat absent files as empty
|
|
||||||
|
|
||||||
rm /tmp/check-localhost.test
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "Ignore localhost, no config specified" {
|
@test "Ignore localhost, no config specified" {
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
load '../helpers/bats-support/load'
|
load '../helpers/bats-support/load'
|
||||||
load '../helpers/bats-assert/load'
|
load '../helpers/bats-assert/load'
|
||||||
|
load '../helpers/bats-file/load'
|
||||||
load '../helpers'
|
load '../helpers'
|
||||||
|
|
||||||
|
|
||||||
@ -33,14 +34,8 @@ load '../helpers'
|
|||||||
assert_output --partial "All repository links are valid."
|
assert_output --partial "All repository links are valid."
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "Ignore file with broken xrefcheck annotation: directory, check filure" {
|
@test "Ignore file with broken xrefcheck annotation: directory, check failure" {
|
||||||
xrefcheck --ignored ./to-ignore/inner-directory/ \
|
to_temp xrefcheck --ignored ./to-ignore/inner-directory/
|
||||||
| prepare > /tmp/check-ignored.test || true
|
|
||||||
|
|
||||||
diff /tmp/check-ignored.test expected.gold \
|
assert_diff expected.gold
|
||||||
--ignore-space-change \
|
|
||||||
--ignore-blank-lines \
|
|
||||||
--new-file # treat absent files as empty
|
|
||||||
|
|
||||||
rm /tmp/check-ignored.test
|
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
load '../helpers/bats-support/load'
|
load '../helpers/bats-support/load'
|
||||||
load '../helpers/bats-assert/load'
|
load '../helpers/bats-assert/load'
|
||||||
|
load '../helpers/bats-file/load'
|
||||||
load '../helpers'
|
load '../helpers'
|
||||||
|
|
||||||
|
|
||||||
@ -28,13 +29,21 @@ load '../helpers'
|
|||||||
}
|
}
|
||||||
|
|
||||||
@test "Not scanned: directory, check failure" {
|
@test "Not scanned: directory, check failure" {
|
||||||
xrefcheck -c config-directory.yaml \
|
to_temp xrefcheck -c config-directory.yaml
|
||||||
| prepare > /tmp/check-notScanned.test || true
|
|
||||||
|
|
||||||
diff /tmp/check-notScanned.test expected.gold \
|
assert_diff - <<EOF
|
||||||
--ignore-space-change \
|
=== Invalid references found ===
|
||||||
--ignore-blank-lines \
|
|
||||||
--new-file # treat absent files as empty
|
|
||||||
|
|
||||||
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-support/load'
|
||||||
load '../helpers/bats-assert/load'
|
load '../helpers/bats-assert/load'
|
||||||
|
load '../helpers/bats-file/load'
|
||||||
load '../helpers'
|
load '../helpers'
|
||||||
|
|
||||||
|
|
||||||
@test "Dump all errors along with broken links" {
|
@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 \
|
assert_diff expected.gold
|
||||||
--ignore-space-change \
|
|
||||||
--ignore-blank-lines \
|
|
||||||
--new-file # treat absent files as empty
|
|
||||||
|
|
||||||
rm /tmp/check-scan-errors.test
|
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
load '../helpers/bats-support/load'
|
load '../helpers/bats-support/load'
|
||||||
load '../helpers/bats-assert/load'
|
load '../helpers/bats-assert/load'
|
||||||
|
load '../helpers/bats-file/load'
|
||||||
load '../helpers'
|
load '../helpers'
|
||||||
|
|
||||||
|
|
||||||
@ -16,12 +17,7 @@ load '../helpers'
|
|||||||
}
|
}
|
||||||
|
|
||||||
@test "Virtual files: check failure" {
|
@test "Virtual files: check failure" {
|
||||||
xrefcheck | prepare > /tmp/check-virtualFiles.test || true
|
to_temp xrefcheck
|
||||||
|
|
||||||
diff /tmp/check-virtualFiles.test expected.gold \
|
assert_diff expected.gold
|
||||||
--ignore-space-change \
|
|
||||||
--ignore-blank-lines \
|
|
||||||
--new-file # treat absent files as empty
|
|
||||||
|
|
||||||
rm /tmp/check-virtualFiles.test
|
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,11 @@
|
|||||||
setup () {
|
setup () {
|
||||||
# change working directory to the location of the running `bats` suite.
|
# change working directory to the location of the running `bats` suite.
|
||||||
cd "$( dirname "$BATS_TEST_FILENAME")"
|
cd "$( dirname "$BATS_TEST_FILENAME")"
|
||||||
|
TEST_TEMP_DIR="$(temp_make)"
|
||||||
|
}
|
||||||
|
|
||||||
|
teardown() {
|
||||||
|
temp_del "$TEST_TEMP_DIR"
|
||||||
}
|
}
|
||||||
|
|
||||||
# this function is used for:
|
# this function is used for:
|
||||||
@ -17,3 +22,54 @@ prepare () {
|
|||||||
| sed 's/socket: [0-9]*/socket: N/g' \
|
| sed 's/socket: [0-9]*/socket: N/g' \
|
||||||
| sed 's/Network.Socket.connect: <socket: N>: does not exist (Connection refused),//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