Skip to content

Commit d850361

Browse files
committed
test(e2e): assert compile_commands.json is generated on all platforms
Add a dedicated e2e (no requires → runs Linux/macOS/Windows) that builds a minimal `mcpp new` project and verifies the Clang compilation database is emitted at the project root: file exists + non-empty, top-level JSON array, required keys (directory/file + command|arguments), an entry for src/main.cpp, and a deeper python json parse where available. CDB generation is unconditional in the ninja backend (write_compile_commands), so this guards the IDE/clangd contract directly. Existing CDB tests cover specifics (47 prebuilt-module-path abs, 59 std flag, gcc-only); this is the plain cross-platform existence+validity guarantee. Verified: 47 already passes on Windows/macOS CI, confirming the build path works there.
1 parent 20a1942 commit d850361

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env bash
2+
# requires:
3+
# 76_compile_commands_generated.sh — `mcpp build` of a minimal project must
4+
# emit a valid Clang compilation database (compile_commands.json) at the
5+
# project root, on EVERY platform (Linux / macOS / Windows). IDE/clangd
6+
# integration depends on it, and generation is unconditional in the ninja
7+
# backend, so this guards the contract directly rather than as a side effect
8+
# of the more specific CDB tests (47 prebuilt-module-path, 59 std flag).
9+
#
10+
# No `requires:` capability → runs on all three CI platforms.
11+
set -e
12+
13+
TMP=$(mktemp -d)
14+
trap "rm -rf $TMP" EXIT
15+
16+
cd "$TMP"
17+
"$MCPP" new app > /dev/null
18+
cd app
19+
"$MCPP" build > /dev/null
20+
21+
cdb=compile_commands.json
22+
[[ -f "$cdb" ]] || { echo "FAIL: no $cdb generated at project root"; exit 1; }
23+
[[ -s "$cdb" ]] || { echo "FAIL: $cdb is empty"; exit 1; }
24+
25+
# Top-level must be a JSON array.
26+
first="$(head -c 1 "$cdb")"
27+
[[ "$first" == "[" ]] || {
28+
echo "FAIL: $cdb does not start with '[' (got '$first') — not a JSON array"
29+
cat "$cdb"; exit 1
30+
}
31+
32+
# CDB-required keys + the clang command form (command OR arguments).
33+
for key in '"directory"' '"file"'; do
34+
grep -q "$key" "$cdb" || { echo "FAIL: $cdb missing $key"; cat "$cdb"; exit 1; }
35+
done
36+
grep -qE '"command"|"arguments"' "$cdb" || {
37+
echo "FAIL: $cdb has neither \"command\" nor \"arguments\""; cat "$cdb"; exit 1
38+
}
39+
40+
# The minimal project's source (src/main.cpp) must have an entry.
41+
grep -q 'main\.cpp' "$cdb" || { echo "FAIL: $cdb has no entry for src/main.cpp"; cat "$cdb"; exit 1; }
42+
43+
# Deeper structural validation when a JSON parser is available (GitHub-hosted
44+
# runners ship python3). Skips cleanly where it isn't, keeping the grep checks
45+
# above as the portable baseline.
46+
if command -v python3 >/dev/null 2>&1; then
47+
python3 - "$cdb" <<'PY' || exit 1
48+
import json, sys
49+
d = json.load(open(sys.argv[1], encoding="utf-8"))
50+
assert isinstance(d, list) and d, "CDB must be a non-empty JSON array"
51+
for e in d:
52+
assert "file" in e and "directory" in e, "entry missing file/directory: %r" % e
53+
assert ("command" in e) or ("arguments" in e), "entry missing command/arguments: %r" % e
54+
print(" json validation OK (%d entries)" % len(d))
55+
PY
56+
fi
57+
58+
echo "OK"

0 commit comments

Comments
 (0)