-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_c-init.sh
More file actions
executable file
·400 lines (352 loc) · 9.66 KB
/
test_c-init.sh
File metadata and controls
executable file
·400 lines (352 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/env bash
set -uo pipefail
ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
RAW_CINIT="${1:-$ROOT/target/debug/c-init}"
# Resolve to absolute path if relative
if [[ "$RAW_CINIT" = /* ]]; then
CINIT="$RAW_CINIT"
else
CINIT="$(pwd)/$RAW_CINIT"
fi
if [ ! -x "$CINIT" ]; then
cargo build --quiet
fi
LAST_OUT=""
LAST_ERR=""
LAST_CODE=0
GREEN="\033[32m"
RED="\033[31m"
RESET="\033[0m"
TEST_NAME=""
CURRENT_FAILED=0
FAIL_COUNT=0
TMP_DIRS=()
cleanup() {
if [ "$FAIL_COUNT" -ne 0 ]; then
return 0
fi
local dir
for dir in "${TMP_DIRS[@]}"; do
rm -rf "$dir"
done
}
trap cleanup EXIT
ACUTEST_TMP=$(mktemp -d)
TMP_DIRS+=("$ACUTEST_TMP")
ACUTEST_BIN="$ACUTEST_TMP/bin"
mkdir -p "$ACUTEST_BIN"
ACUTEST_CACHE="$ACUTEST_TMP/acutest.h"
cat <<'EOF' > "$ACUTEST_BIN/curl"
#!/usr/bin/env bash
set -euo pipefail
OUT=""
URL=""
while [ $# -gt 0 ]; do
case "$1" in
-o)
OUT="$2"
shift 2
;;
*)
if [[ "$1" != -* ]]; then
URL="$1"
fi
shift
;;
esac
done
[ -n "$OUT" ] || { echo "curl stub missing -o" >&2; exit 2; }
[ -n "$URL" ] || { echo "curl stub missing URL" >&2; exit 2; }
if [ ! -f "__ACUTEST_CACHE__" ]; then
/usr/bin/curl -fsSL "$URL" -o "__ACUTEST_CACHE__"
fi
cat "__ACUTEST_CACHE__" > "$OUT"
EOF
sed -i "" "s|__ACUTEST_CACHE__|$ACUTEST_CACHE|g" "$ACUTEST_BIN/curl"
chmod +x "$ACUTEST_BIN/curl"
export PATH="$ACUTEST_BIN:$PATH"
test_begin() {
TEST_NAME="$1"
CURRENT_FAILED=0
printf "TEST: %s\n" "$TEST_NAME"
}
test_ok() {
if [ "$CURRENT_FAILED" -eq 0 ]; then
printf " ${GREEN}✔︎${RESET} pass\n"
fi
}
fail() {
CURRENT_FAILED=1
FAIL_COUNT=$((FAIL_COUNT + 1))
printf " ${RED}⨯${RESET} %s: %s\n" "$TEST_NAME" "$*" >&2
return 0
}
run() {
local LAST_OUT_FILE=$(mktemp)
local LAST_ERR_FILE=$(mktemp)
set +e
"$@" >"$LAST_OUT_FILE" 2>"$LAST_ERR_FILE"
LAST_CODE=$?
set -e
LAST_OUT=$(cat "$LAST_OUT_FILE")
LAST_ERR=$(cat "$LAST_ERR_FILE")
COMBINED="$LAST_OUT$LAST_ERR"
rm -f "$LAST_OUT_FILE" "$LAST_ERR_FILE"
}
assert_code() {
local expected="$1"
[ "$LAST_CODE" -eq "$expected" ] || fail "expected exit $expected, got $LAST_CODE"
}
assert_contains() {
local hay="$1"
local needle="$2"
case "$hay" in
*"$needle"*) ;;
*) fail "expected to contain: $needle" ;;
esac
}
assert_file() {
local path="$1"
[ -f "$path" ] || fail "expected file: $path"
}
assert_dir() {
local path="$1"
[ -d "$path" ] || fail "expected dir: $path"
}
assert_missing() {
local path="$1"
[ ! -e "$path" ] || fail "expected missing: $path"
}
# 1) --help should exit 0 and print usage
test_begin "--help prints usage and exits 0"
run "$CINIT" --help
assert_code 0
assert_contains "$LAST_OUT" "Usage:"
test_ok
# 2) Unknown flag should exit 1 and print help
test_begin "unknown flag exits 2 and prints help"
run "$CINIT" --garbage
# Clap prints "unexpected argument" and usage
assert_code 2
COMBINED="$LAST_OUT$LAST_ERR"
assert_contains "$(echo "$COMBINED" | tr '[:upper:]' '[:lower:]')" "unexpected argument"
assert_contains "$COMBINED" "--garbage"
assert_contains "$COMBINED" "Usage:"
test_ok
# 3) Create project with no hello and no git
test_begin "create project with --no-hello --no-git"
TMPDIR=$(mktemp -d)
TMP_DIRS+=("$TMPDIR")
PROJ="$TMPDIR/proj"
run "$CINIT" --cc gcc -s strict --no-hello --no-git "$PROJ"
assert_code 0
assert_dir "$PROJ/src"
assert_dir "$PROJ/include"
assert_dir "$PROJ/target"
assert_file "$PROJ/Makefile"
assert_file "$PROJ/compile_flags.txt"
assert_missing "$PROJ/src/main.c"
assert_dir "$PROJ/tests"
assert_file "$PROJ/tests/test-deps/acutest.h"
assert_file "$PROJ/tests/compile_flags.txt"
assert_contains "$(cat "$PROJ/Makefile")" "sanitize:"
assert_contains "$(cat "$PROJ/README.md")" "make sanitize"
test_ok
# 3c) --no-commit skips initial commit
test_begin "--no-commit skips initial commit"
TMPDIR_NO_COMMIT=$(mktemp -d)
TMP_DIRS+=("$TMPDIR_NO_COMMIT")
PROJ_NO_COMMIT="$TMPDIR_NO_COMMIT/proj"
run "$CINIT" --no-commit "$PROJ_NO_COMMIT"
assert_code 0
assert_dir "$PROJ_NO_COMMIT/.git"
run git -C "$PROJ_NO_COMMIT" rev-parse --verify HEAD
if [ "$LAST_CODE" -eq 0 ]; then
fail "expected no initial commit"
fi
test_ok
# 3b) --no-tests skips tests and vendoring
test_begin "--no-tests skips tests folder"
TMPDIR_NO_TESTS=$(mktemp -d)
TMP_DIRS+=("$TMPDIR_NO_TESTS")
PROJ_NO_TESTS="$TMPDIR_NO_TESTS/proj"
run "$CINIT" --no-tests "$PROJ_NO_TESTS"
assert_code 0
assert_missing "$PROJ_NO_TESTS/tests"
test_ok
# 4) Non-empty directory should fail without --force
test_begin "non-empty dir fails without --force"
TMPDIR2=$(mktemp -d)
TMP_DIRS+=("$TMPDIR2")
PROJ2="$TMPDIR2/proj"
mkdir -p "$PROJ2"
printf "x" > "$PROJ2/file.txt"
run "$CINIT" --no-git "$PROJ2"
assert_code 1
assert_contains "$LAST_ERR" "not empty"
test_ok
# 5) Linter strictness override should affect .clang-tidy
test_begin "linter strictness override creates strictest .clang-tidy"
TMPDIR3=$(mktemp -d)
TMP_DIRS+=("$TMPDIR3")
PROJ3="$TMPDIR3/proj"
run "$CINIT" --no-git --linter-strictness strictest "$PROJ3"
assert_code 0
assert_file "$PROJ3/.clang-tidy"
assert_contains "$(cat "$PROJ3/.clang-tidy")" "modernize"
test_ok
# 6) Interactive mode creates project
test_begin "interactive mode creates project"
TMPDIR4=$(mktemp -d)
TMP_DIRS+=("$TMPDIR4")
cd "$TMPDIR4"
# Give a name for the prompt
run "$CINIT" -i <<< "interactive_proj"
assert_code 0
assert_dir "interactive_proj"
assert_file "interactive_proj/src/main.c"
assert_dir "interactive_proj/.git"
cd "$ROOT"
test_ok
# 7) Interactive mode refuses overwrite
test_begin "interactive mode refuses overwrite"
TMPDIR5=$(mktemp -d)
TMP_DIRS+=("$TMPDIR5")
cd "$TMPDIR5"
mkdir -p "existing"
touch "existing/file"
# Input "existing" then "0" (No) for overwrite
run "$CINIT" -i <<< "$(printf "existing\n0\n")"
assert_code 1
assert_contains "$COMBINED" "Exiting..."
cd "$ROOT"
test_ok
# 7b) Interactive mode accepts overwrite
test_begin "interactive mode accepts overwrite"
TMPDIR5B=$(mktemp -d)
TMP_DIRS+=("$TMPDIR5B")
cd "$TMPDIR5B"
mkdir -p "existing"
touch "existing/file"
# Input "existing" then "1" (Yes) for overwrite
run "$CINIT" -i <<< "$(printf "existing\n1\n")"
assert_code 0
assert_dir "existing/src"
assert_file "existing/Makefile"
cd "$ROOT"
test_ok
# 8) GCC flags are correctly generated
test_begin "GCC flags are correctly generated"
TMPDIR6=$(mktemp -d)
TMP_DIRS+=("$TMPDIR6")
PROJ6="$TMPDIR6/proj"
run "$CINIT" --cc gcc -s strictest "$PROJ6"
assert_code 0
assert_file "$PROJ6/compile_flags.txt"
# Check for a GCC-specific flag like -Wlogical-op or -Wduplicated-cond
assert_contains "$(cat "$PROJ6/compile_flags.txt")" "-Wlogical-op"
assert_contains "$(cat "$PROJ6/compile_flags.txt")" "-Wduplicated-cond"
test_ok
# 9) Compilation tests for all strictness levels and both compilers
for cc in clang gcc; do
for s in loose strict strictest; do
test_begin "compilation: $cc with -s $s"
TMPDIR_COMP=$(mktemp -d)
TMP_DIRS+=("$TMPDIR_COMP")
PROJ_COMP="$TMPDIR_COMP/proj"
# Run c-init
run "$CINIT" --cc "$cc" -s "$s" "$PROJ_COMP"
assert_code 0
# Check that it mentions the compiler (could be gcc, clang, or gcc-15 etc)
assert_contains "$LAST_OUT" "using "
# Compile using the generated Makefile
# We use -C to run make in the project directory
run make -C "$PROJ_COMP"
assert_code 0
assert_file "$PROJ_COMP/target/debug/proj"
# Run the compiled binary
run "$PROJ_COMP/target/debug/proj"
assert_code 0
assert_contains "$LAST_OUT" "Hello from proj!"
test_ok
done
done
# 9b) make test builds and runs tests
test_begin "make test builds and runs tests"
TMPDIR_TESTS=$(mktemp -d)
TMP_DIRS+=("$TMPDIR_TESTS")
PROJ_TESTS="$TMPDIR_TESTS/proj"
run "$CINIT" --no-git "$PROJ_TESTS"
assert_code 0
run make -C "$PROJ_TESTS" test
assert_code 0
assert_file "$PROJ_TESTS/target/debug/tests/proj_tests"
test_ok
# 10) 'make run' with arguments
test_begin "'make run' passes arguments"
TMPDIR_ARGS=$(mktemp -d)
TMP_DIRS+=("$TMPDIR_ARGS")
PROJ_ARGS="$TMPDIR_ARGS/proj"
"$CINIT" --no-git "$PROJ_ARGS" > /dev/null
# Replace main.c with one that prints args
cat <<EOF > "$PROJ_ARGS/src/main.c"
#include <stdio.h>
int main(int argc, char **argv) {
for (int i = 1; i < argc; i++) printf("ARG:%s\n", argv[i]);
return 0;
}
EOF
run make -C "$PROJ_ARGS" run foo bar baz
assert_code 0
assert_contains "$LAST_OUT" "ARG:foo"
assert_contains "$LAST_OUT" "ARG:bar"
assert_contains "$LAST_OUT" "ARG:baz"
test_ok
# 11) 'make run --' consumes the dash-dash
test_begin "'make run --' consumes the separator"
TMPDIR_SEP=$(mktemp -d)
TMP_DIRS+=("$TMPDIR_SEP")
PROJ_SEP="$TMPDIR_SEP/proj"
"$CINIT" --no-git "$PROJ_SEP" > /dev/null
cat <<EOF > "$PROJ_SEP/src/main.c"
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--") == 0) printf("FOUND_SEP\n");
printf("ARG:%s\n", argv[i]);
}
return 0;
}
EOF
run make -C "$PROJ_SEP" run -- foo -v
assert_code 0
assert_contains "$LAST_OUT" "ARG:foo"
assert_contains "$LAST_OUT" "ARG:-v"
# Ensure "--" itself was NOT passed to the program
if [[ "$LAST_OUT" == *"FOUND_SEP"* ]]; then
fail "dash-dash was not consumed by Makefile"
fi
test_ok
# 12) 'make run-release' with arguments
test_begin "'make run-release' passes arguments"
TMPDIR_REL=$(mktemp -d)
TMP_DIRS+=("$TMPDIR_REL")
PROJ_REL="$TMPDIR_REL/proj"
"$CINIT" --no-git "$PROJ_REL" > /dev/null
cat <<EOF > "$PROJ_REL/src/main.c"
#include <stdio.h>
int main(int argc, char **argv) {
for (int i = 1; i < argc; i++) printf("ARG:%s\n", argv[i]);
return 0;
}
EOF
run make -C "$PROJ_REL" run-release rel_foo rel_bar
assert_code 0
assert_contains "$LAST_OUT" "ARG:rel_foo"
assert_contains "$LAST_OUT" "ARG:rel_bar"
assert_file "$PROJ_REL/target/release/proj"
test_ok
if [ "$FAIL_COUNT" -ne 0 ]; then
exit 1
fi