Skip to content

Commit cd4d4b4

Browse files
olehermanseclaude
andcommitted
Added shell test for cfengine format --check
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Ole Herman Schumacher Elgesem <ole.elgesem@northern.tech>
1 parent 0e4b93c commit cd4d4b4

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

tests/shell/004-format-check.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
set -e
4+
set -x
5+
6+
# Setup: create a temp directory for test files
7+
tmpdir=$(mktemp -d)
8+
trap "rm -rf $tmpdir" EXIT
9+
10+
write_formatted() {
11+
printf 'bundle agent main\n{\n vars:\n "v" string => "hello";\n}\n' > "$1"
12+
}
13+
14+
write_unformatted() {
15+
printf 'bundle agent main { vars: "v" string => "hello"; }\n' > "$1"
16+
}
17+
18+
# Case 1: format without --check on already-formatted file -> exit 0
19+
write_formatted "$tmpdir/good.cf"
20+
cfengine format "$tmpdir/good.cf"
21+
22+
# Case 2: format without --check on unformatted file -> exit 0 (reformats it)
23+
write_unformatted "$tmpdir/bad.cf"
24+
cfengine format "$tmpdir/bad.cf"
25+
# Verify it was actually reformatted to the correct output
26+
write_formatted "$tmpdir/expected.cf"
27+
diff "$tmpdir/expected.cf" "$tmpdir/bad.cf"
28+
29+
# Case 3: --check on already-formatted file -> exit 0
30+
write_formatted "$tmpdir/good2.cf"
31+
cfengine format --check "$tmpdir/good2.cf"
32+
33+
# Case 4: --check on unformatted file -> exit 1
34+
write_unformatted "$tmpdir/bad2.cf"
35+
cp "$tmpdir/bad2.cf" "$tmpdir/bad2_orig.cf"
36+
if cfengine format --check "$tmpdir/bad2.cf"; then
37+
echo "FAIL: expected exit code 1 for --check on unformatted file"
38+
exit 1
39+
fi
40+
# Verify the file was NOT modified
41+
diff "$tmpdir/bad2_orig.cf" "$tmpdir/bad2.cf"

0 commit comments

Comments
 (0)