Skip to content

Commit 5b40abf

Browse files
authored
Merge pull request #592 from lemeurherve/golden-file-tags
chore: introduce golden file testing of tags
2 parents 0ddcb6e + c7815fa commit 5b40abf

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ every-build: check-reqs
6363
@set -x; $(bake_base_cli) linux
6464

6565
show:
66-
@$(bake_cli) linux --print
66+
@$(bake_base_cli) --progress=quiet linux --print | jq
67+
68+
tags:
69+
@make show | jq -r '.target[].tags[]' | LC_ALL=C sort
6770

6871
list: check-reqs
6972
@set -x; make --silent show | jq -r '.target | path(.. | select(.platforms[] | contains("linux/$(ARCH)"))?) | add'

tests/golden/expected_tags.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
docker.io/jenkins/ssh-agent:alpine
2+
docker.io/jenkins/ssh-agent:alpine-jdk17
3+
docker.io/jenkins/ssh-agent:alpine-jdk21
4+
docker.io/jenkins/ssh-agent:alpine-jdk25
5+
docker.io/jenkins/ssh-agent:alpine3.23
6+
docker.io/jenkins/ssh-agent:alpine3.23-jdk17
7+
docker.io/jenkins/ssh-agent:alpine3.23-jdk21
8+
docker.io/jenkins/ssh-agent:alpine3.23-jdk25
9+
docker.io/jenkins/ssh-agent:debian-jdk17
10+
docker.io/jenkins/ssh-agent:debian-jdk21
11+
docker.io/jenkins/ssh-agent:debian-jdk25
12+
docker.io/jenkins/ssh-agent:jdk17
13+
docker.io/jenkins/ssh-agent:jdk21
14+
docker.io/jenkins/ssh-agent:jdk25
15+
docker.io/jenkins/ssh-agent:latest
16+
docker.io/jenkins/ssh-agent:latest-alpine-jdk17
17+
docker.io/jenkins/ssh-agent:latest-alpine-jdk21
18+
docker.io/jenkins/ssh-agent:latest-alpine-jdk25
19+
docker.io/jenkins/ssh-agent:latest-alpine3.23
20+
docker.io/jenkins/ssh-agent:latest-alpine3.23-jdk17
21+
docker.io/jenkins/ssh-agent:latest-alpine3.23-jdk21
22+
docker.io/jenkins/ssh-agent:latest-alpine3.23-jdk25
23+
docker.io/jenkins/ssh-agent:latest-debian-jdk17
24+
docker.io/jenkins/ssh-agent:latest-debian-jdk21
25+
docker.io/jenkins/ssh-agent:latest-debian-jdk25
26+
docker.io/jenkins/ssh-agent:latest-jdk17
27+
docker.io/jenkins/ssh-agent:latest-jdk21
28+
docker.io/jenkins/ssh-agent:latest-jdk25
29+
docker.io/jenkins/ssh-agent:latest-trixie-jdk17
30+
docker.io/jenkins/ssh-agent:latest-trixie-jdk21
31+
docker.io/jenkins/ssh-agent:latest-trixie-jdk25
32+
docker.io/jenkins/ssh-agent:trixie-jdk17
33+
docker.io/jenkins/ssh-agent:trixie-jdk21
34+
docker.io/jenkins/ssh-agent:trixie-jdk25

tests/tags.bats

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bats
2+
3+
load test_helpers
4+
5+
SUT_DESCRIPTION="tags"
6+
7+
@test "[${SUT_DESCRIPTION}] Default tags unchanged" {
8+
assert_matches_golden expected_tags make --silent tags
9+
}

tests/test_helpers.bash

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@ function assert_run_cmd_output_equal {
2323
assert_output "${expected_output}"
2424
}
2525

26+
# Assert that golden file $1 matches the output of a command $2
27+
assert_matches_golden() {
28+
local golden="$1"
29+
shift
30+
local golden_path="tests/golden/${golden}.txt"
31+
32+
if [[ ! -f "${golden_path}" ]]; then
33+
echo "Golden file '${golden_path}' does not exist"
34+
return 1
35+
fi
36+
37+
# Run the command passed as arguments and capture its output
38+
local output
39+
output="$(mktemp)"
40+
"$@" > "${output}"
41+
42+
# Compare with golden file
43+
diff -u "${golden_path}" <(cat "${output}")
44+
}
45+
2646
function get_sut_image {
2747
test -n "${IMAGE:?"[sut_image] Please set the variable 'IMAGE' to the name of the image to test in 'docker-bake.hcl'."}"
2848
## Retrieve the SUT image name from buildx

tests/update-golden-file.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# This script runs a specified command, captures its output,
5+
# and compares it against a "golden file" representing the expected output.
6+
# If the output differs, the script shows a diff and allows the user to update the golden file.
7+
# If the output matches, it reports that the golden file is up-to-date.
8+
#
9+
# Usage:
10+
# ./update-golden-file.sh <test-name> <command...>
11+
#
12+
# Arguments:
13+
# <test-name> Name of the test, used to determine the golden file path.
14+
# The corresponding golden file will be stored as:
15+
# golden/<test-name>.txt
16+
#
17+
# <command...> Command to run, whose stdout will be compared to the golden file.
18+
# This can include arguments, e.g.:
19+
# ./update-golden-file.sh expected_tags make tags
20+
#
21+
# Notes:
22+
# - Requires Bash 4+ for `BASH_SOURCE` handling.
23+
# - The script is safe to run from any directory; golden files are always relative to the script's own location.
24+
25+
if [[ $# -lt 2 ]]; then
26+
echo "Usage: $0 <test-name> <command...>"
27+
echo "Example:"
28+
echo " $0 expected_tags make tags"
29+
exit 1
30+
fi
31+
32+
name="$1"
33+
shift
34+
35+
# Ensure golden folder is always relative to this script
36+
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
37+
golden_file="${name}.txt"
38+
golden_path="${script_dir}/golden/${golden_file}"
39+
tmp="$(mktemp)"
40+
41+
echo
42+
echo "Golden file path:"
43+
echo " ${golden_path}"
44+
echo
45+
echo "Running command:"
46+
echo " $*"
47+
echo
48+
49+
"$@" > "${tmp}"
50+
51+
action="create"
52+
if [[ -f "${golden_path}" ]]; then
53+
if diff -u "${golden_path}" "${tmp}" > /dev/null; then
54+
echo "Golden file '${golden_file}' is already up-to-date."
55+
rm "${tmp}"
56+
exit 0
57+
fi
58+
echo "Diff against existing golden file '${golden_file}':"
59+
diff -u "${golden_path}" "${tmp}" || true
60+
action="update"
61+
else
62+
echo "Golden file '${golden_file}' does not exist yet."
63+
fi
64+
65+
echo
66+
echo "Golden file to ${action}: '${golden_file}'"
67+
read -rp "Proceed? [y/N] " answer
68+
69+
if [[ "${answer}" =~ ^[Yy]$ ]]; then
70+
mkdir -p "$(dirname "${golden_path}")"
71+
mv "${tmp}" "${golden_path}"
72+
echo "Golden file '${golden_file}' ${action}d."
73+
else
74+
rm "${tmp}"
75+
echo "Aborted. Golden file '${golden_file}' unchanged."
76+
fi

0 commit comments

Comments
 (0)