-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_codecat.sh
More file actions
649 lines (585 loc) · 23.6 KB
/
test_codecat.sh
File metadata and controls
649 lines (585 loc) · 23.6 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#!/bin/bash
# Exit on error, treat unset variables as error
set -eu
# Exit on pipe failures
set -o pipefail
# --- Configuration ---
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
BUILD_OUTPUT_NAME="codecat_test_binary"
BUILD_OUTPUT_PATH="${SCRIPT_DIR}/${BUILD_OUTPUT_NAME}"
CODECAT_CMD="${BUILD_OUTPUT_PATH}"
TEST_DIR_BASE="codecat_test_env_"
VERBOSE=true # Set to false to reduce diff output on failure
TEST_PASSED_COUNT=0
TEST_FAILED_COUNT=0
# --- Test Numbering ---
TEST_CASE_NUMBER=0
TEST_CASE_NAME="" # Store current test name globally for helpers
# --- State Variables ---
declare TEST_DIR="" # Holds path to current test's temp dir
declare ORIGINAL_PWD="" # Holds the PWD before cd'ing into TEST_DIR
declare CODE_DIFF_LOG="" # Path to code diff log if created
declare SUMMARY_DIFF_LOG="" # Path to summary diff log if created
declare STDOUT_LOG="" # Path to stdout log
declare STDERR_LOG="" # Path to stderr log
# --- Helper Functions (With Test Numbering) ---
info() { echo -e "\033[0;36m[INFO] $*\033[0m"; } # General info
test_info() { echo -e "\033[0;36m[INFO] Test #${TEST_CASE_NUMBER} ($TEST_CASE_NAME): $*\033[0m"; } # Test-specific info
pass() {
local msg="$1"
echo -e "\033[0;32m[PASS] Test #${TEST_CASE_NUMBER} ($TEST_CASE_NAME): $msg\033[0m"
TEST_PASSED_COUNT=$((TEST_PASSED_COUNT + 1))
}
fail() {
local reason="$1"
TEST_FAILED_COUNT=$((TEST_FAILED_COUNT + 1))
echo -e "\033[0;31m[FAIL] Test #${TEST_CASE_NUMBER} ($TEST_CASE_NAME): $reason\033[0m" >&2 # Print errors to stderr
# Dump logs/diffs if they exist
if [ -n "${STDOUT_LOG:-}" ] && [ -f "$STDOUT_LOG" ]; then
echo -e "\033[0;33m--- Stdout Log (${STDOUT_LOG}) ---\033[0m" >&2
cat "$STDOUT_LOG" >&2
echo -e "\033[0;33m--- End Stdout Log ---\033[0m" >&2
fi
if [ -n "${STDERR_LOG:-}" ] && [ -f "$STDERR_LOG" ]; then
echo -e "\033[0;33m--- Stderr Log (${STDERR_LOG}) ---\033[0m" >&2
cat "$STDERR_LOG" >&2
echo -e "\033[0;33m--- End Stderr Log ---\033[0m" >&2
fi
if [ -n "${CODE_DIFF_LOG:-}" ] && [ -f "$CODE_DIFF_LOG" ]; then
echo -e "\033[0;33m--- Code Diff (${CODE_DIFF_LOG}) ---\033[0m" >&2
cat "$CODE_DIFF_LOG" >&2
echo -e "\033[0;33m--- End Code Diff ---\033[0m" >&2
fi
if [ -n "${SUMMARY_DIFF_LOG:-}" ] && [ -f "$SUMMARY_DIFF_LOG" ]; then
echo -e "\033[0;33m--- Summary Diff (${SUMMARY_DIFF_LOG}) ---\033[0m" >&2
cat "$SUMMARY_DIFF_LOG" >&2
echo -e "\033[0;33m--- End Summary Diff ---\033[0m" >&2
fi
# Preserve directory
if [ -n "${TEST_DIR:-}" ] && [ -d "$TEST_DIR" ]; then
echo -e "\033[0;33m[INFO] Preserving temporary directory for inspection: $TEST_DIR\033[0m" >&2
else
echo -e "\033[0;33m[WARN] Cannot preserve TEST_DIR (not set or doesn't exist).\033[0m" >&2
fi
# IMPORTANT: Disable the cleanup trap so the directory isn't removed
trap - EXIT ERR
exit 1 # Exit immediately
}
cleanup() {
local exit_code=$?
if [ -n "$ORIGINAL_PWD" ] && [ -d "$ORIGINAL_PWD" ]; then
(cd "$ORIGINAL_PWD") || echo "[Cleanup Warn] Failed cd back to $ORIGINAL_PWD" >&2
fi
if [ "$exit_code" -eq 0 ]; then
if [ -n "${TEST_DIR:-}" ] && [ -d "$TEST_DIR" ]; then
info "Cleaning up test directory: $TEST_DIR"
rm -rf "$TEST_DIR"
fi
if [ -f "$BUILD_OUTPUT_PATH" ]; then
info "Cleaning up test binary: $BUILD_OUTPUT_PATH"
rm -f "$BUILD_OUTPUT_PATH"
fi
info "All $TEST_PASSED_COUNT tests passed successfully."
else
if [ -n "${TEST_DIR:-}" ] && [ -d "$TEST_DIR" ]; then
info "Exiting due to error. Cleanup skipped for: $TEST_DIR"
fi
info "Test run failed. See errors above."
# Keep binary on failure for retry
fi
# Preserve original exit code if non-zero
if [ "$exit_code" -ne 0 ]; then exit "$exit_code"; fi
}
# --- Setup ---
trap cleanup EXIT ERR
ORIGINAL_PWD=$(pwd)
build_binary() {
info "Building test binary..."
(cd "$SCRIPT_DIR" && \
go build -ldflags="-X main.Version=test" -o "$BUILD_OUTPUT_NAME" ./cmd/codecat)
if [ $? -ne 0 ]; then fail "Build failed."; fi # Use fail (which knows test #/name if setup)
info "Test binary built successfully: $BUILD_OUTPUT_PATH"
info "Checking existence and permissions of built binary..."
if [ ! -x "$BUILD_OUTPUT_PATH" ]; then
fail "Built binary not found or not executable at: $BUILD_OUTPUT_PATH"
fi
info "Binary verified."
}
# Setup common file structure - now called explicitly by tests that need it
setup_common_files() {
test_info "Creating common test file structure..." # Use test_info
mkdir -p proj/data
echo 'package main // main.go content' > proj/main.go
echo 'print("util") # utils.py content' > proj/utils.py
echo 'text data' > proj/data/file.txt
echo 'log stuff' > proj/data/ignored.log
touch proj/empty.txt
echo '*.log' > proj/.gitignore
echo 'empty.txt' >> proj/.gitignore
echo 'manual content' > manual.txt
echo '{"key": "value"}' > proj/config.json
}
# Base setup: Creates dir, sets paths, cds into it. DOES NOT create files.
setup_test_case_base() {
local test_name="$1"
TEST_CASE_NUMBER=$((TEST_CASE_NUMBER + 1))
TEST_CASE_NAME="$test_name" # Set global name for helpers
TEST_DIR=$(mktemp -d "${TMPDIR:-/tmp}/${TEST_DIR_BASE}${test_name}_XXXXXX") || \
fail "Could not create temp dir" # fail() uses TEST_CASE_NAME
test_info "Created test directory: $TEST_DIR" # Use test_info
STDOUT_LOG="$TEST_DIR/actual_stdout.log"
STDERR_LOG="$TEST_DIR/actual_stderr.log"
CODE_DIFF_LOG="$TEST_DIR/diff_code.log"
SUMMARY_DIFF_LOG="$TEST_DIR/diff_summary.log"
cd "$TEST_DIR" || fail "Could not cd into test directory: $TEST_DIR"
}
extract_output_filename() {
local args_string="$1"
local filename=""
# This logic now handles arguments properly as an array
local args_array=($args_string)
for i in "${!args_array[@]}"; do
if [[ "${args_array[$i]}" == "--output" || "${args_array[$i]}" == "-o" ]]; then
if [[ $i+1 -lt ${#args_array[@]} ]]; then
filename="${args_array[$i+1]}"
break
fi
fi
done
if [[ -z "$filename" ]] || [[ "$filename" == -* ]]; then
echo ""
else
echo "$filename"
fi
}
# Modified run_test to use order-insensitive block checking
run_test() {
local codecat_args="$1"; local expected_output_file="$2"; local expected_summary_file="$3"
test_info "Running test case..."
test_info "Command: $CODECAT_CMD $codecat_args"
local output_file=""; local summary_output_target="stderr"; local code_output_target="stdout"
output_file=$(extract_output_filename "$codecat_args")
if [ -n "$output_file" ]; then
summary_output_target="stdout"; code_output_target="file"
test_info "Expecting code in '$output_file', summary on stdout."
else
test_info "Expecting code on stdout, summary on stderr."
fi
set +e
# Execute command without eval by passing args as separate elements
"$CODECAT_CMD" $codecat_args > "$STDOUT_LOG" 2> "$STDERR_LOG"
local exit_code=$?
set -e
if [ "$exit_code" -ne 0 ]; then
fail "Command failed with exit code $exit_code."
else
test_info "Command executed successfully."
fi
test_info "Checking code output (target: $code_output_target)..."
local actual_code_source=""
if [ "$code_output_target" == "file" ]; then
actual_code_source="$output_file"
if [ ! -f "$output_file" ]; then fail "Expected output file '$output_file' not created."; fi
else
actual_code_source="$STDOUT_LOG"
fi
if [ -n "$expected_output_file" ] && [ -f "$expected_output_file" ]; then
test_info "Performing order-insensitive check of code blocks..."
local all_blocks_found=true
local header_line=""
# Simple marker deduction
local marker="---"
# Extract header
if read -r header_line < "$expected_output_file" && [[ "$header_line" != "$marker "* ]]; then
actual_header_line=""
read -r actual_header_line < "$actual_code_source" || true
if [[ "$header_line" != "$actual_header_line" ]]; then
fail "Header mismatch. Expected '$header_line', Got '$actual_header_line'."
fi
test_info "Header matched: '$header_line'"
else
header_line=""
fi
# *** REVISED AWK SCRIPT ***
# This is a much more robust way to extract blocks.
# It finds sections between a start marker and an end marker.
local expected_blocks_tmp_file="$TEST_DIR/expected_blocks_null.tmp"
awk -v marker_pattern="^${marker} " -v end_marker="^${marker}$" '
$0 ~ marker_pattern { in_block=1; block = ""; }
in_block { block = block ? block ORS $0 : $0; }
$0 ~ end_marker {
if (in_block) {
print block;
printf "\0";
in_block=0;
}
}
' "$expected_output_file" > "$expected_blocks_tmp_file"
# Count blocks correctly
# The `grep .` ensures we don't count an empty line from a trailing null byte
local expected_block_count
expected_block_count=$(tr '\0' '\n' < "$expected_blocks_tmp_file" | grep . | wc -l | tr -d '[:space:]' || echo 0)
if ! [[ "$expected_block_count" =~ ^[0-9]+$ ]] ; then
expected_block_count=0
fi
test_info "Found $expected_block_count expected code blocks."
if [ "$expected_block_count" -gt 0 ]; then
while IFS= read -r -d $'\0' expected_block; do
if [[ -z "$expected_block" ]]; then continue; fi
if ! grep -Fzq -- "$expected_block" "$actual_code_source"; then
echo "--- Expected Block Not Found ---" >&2
echo "$expected_block" | cat -v >&2
echo "--- End Expected Block ---" >&2
all_blocks_found=false
fi
done < "$expected_blocks_tmp_file"
fi
rm -f "$expected_blocks_tmp_file"
if [ "$all_blocks_found" = false ]; then
diff -u "$expected_output_file" "$actual_code_source" > "$CODE_DIFF_LOG" || true
fail "One or more expected code blocks not found in output ($actual_code_source)."
fi
local actual_block_count
actual_block_count=$(grep -Ec "^${marker} " "$actual_code_source" || echo 0)
if [[ "$expected_block_count" -ne "$actual_block_count" ]]; then
diff -u "$expected_output_file" "$actual_code_source" > "$CODE_DIFF_LOG" || true
fail "Number of code blocks mismatch. Expected $expected_block_count, found $actual_block_count in output ($actual_code_source)."
fi
test_info "All $expected_block_count expected code blocks found in output."
rm -f "$CODE_DIFF_LOG"
elif [ -z "$expected_output_file" ]; then
actual_content=$(cat "$actual_code_source")
actual_header_line=""
if read -r actual_header_line < "$actual_code_source" && [[ "$actual_header_line" != "$marker "* ]]; then
if [[ "$(echo "${actual_content}" | sed -e "s/^[[:space:]]*//;s/[[:space:]]*$//")" != "$actual_header_line" ]]; then
fail "Code output ($actual_code_source) not empty when expected empty (has content beyond header)."
fi
elif [ -s "$actual_code_source" ]; then
fail "Code output ($actual_code_source) not empty when expected empty."
fi
test_info "Code output ($actual_code_source) empty (or header-only) as expected."
else
fail "Expected output reference file '$expected_output_file' not found."
fi
# --- Check Summary Output ---
test_info "Checking summary output (target: $summary_output_target)..."
local summary_source_file="$STDERR_LOG"; if [ "$summary_output_target" == "stdout" ]; then summary_source_file="$STDOUT_LOG"; fi
local actual_summary_body="$TEST_DIR/actual_summary_body.txt"
local expected_summary_body="$TEST_DIR/expected_summary_body.txt"
if [ -n "$expected_summary_file" ] && [ -f "$expected_summary_file" ]; then
if [ -f "$summary_source_file" ]; then
awk '/--- Summary ---/{summary_started=1; next} /---------------/{exit} summary_started && !/^Included .* relative to CWD/{print}' "$summary_source_file" > "$actual_summary_body"
else
test_info "[Warn] Summary source file '$summary_source_file' not found."; touch "$actual_summary_body"
fi
tail -n +2 "$expected_summary_file" > "$expected_summary_body"
if ! diff -u "$expected_summary_body" "$actual_summary_body" > "$SUMMARY_DIFF_LOG"; then
fail "Summary output body mismatch. Comparing content below CWD header line."
else
test_info "Summary output body matches expected."
rm -f "$SUMMARY_DIFF_LOG" "$actual_summary_body" "$expected_summary_body"
fi
elif [ -z "$expected_summary_file" ]; then
summary_content=$(awk '/--- Summary ---/{flag=1; next} /---------------/{flag=0} flag' "$summary_source_file" 2>/dev/null || true)
if grep -q '--- Summary ---' "$summary_source_file" && [ -z "$summary_content" ] && \
! grep -q -E 'Included [1-9]|Empty files found \([1-9]|Errors encountered \([1-9]' "$summary_source_file"; then
test_info "Summary output ($summary_output_target) empty (only headers) as expected."
else
echo "--- Summary Content Found (Expected Empty) ---" >&2
awk '/--- Summary ---/,/---------------/' "$summary_source_file" >&2
echo "--- End Summary Content ---" >&2
fail "Summary output ($summary_output_target) not empty when expected empty."
fi
else
fail "Expected summary reference file '$expected_summary_file' not found."
fi
pass "Success."
rm -f "$STDOUT_LOG" "$STDERR_LOG"
}
# --- Test Definitions ---
# Test Case 1: Basename Exclusion
test_case_1_basename_exclusion() {
local test_name="basename_exclusion"
setup_test_case_base "$test_name"
test_info "Creating specific files for $test_name"
mkdir -p scantest/sample-docs
echo "include me" > scantest/a.txt
echo "exclude me" > scantest/sample-docs/b.txt
echo "another exclude" > scantest/c.log
local test_config_file="test_config.toml"
cat << EOF > "$test_config_file"
include_extensions = ["txt", "log"]
exclude_basenames = ["sample-docs", "*.log"]
use_gitignore = false
header_text = "----- Codebase for analysis -----\n"
EOF
cat << EOF > expected_output.txt
----- Codebase for analysis -----
--- scantest/a.txt
include me
---
EOF
cat << EOF > expected_summary.txt
Included 1 files (11 B total) relative to CWD 'PLACEHOLDER_CWD':
└── scantest
└── a.txt (11 B)
Empty files found (0):
Errors encountered (0):
EOF
# NOTE: Arguments are now passed without quotes around the whole string
run_test \
"-d scantest -o output.txt --loglevel=warn -c $test_config_file --legacy-format" \
"expected_output.txt" \
"expected_summary.txt"
}
# Test Case 2: Flags mode (-d proj -e go,py) using Hardcoded Defaults
test_case_2_flags_exts_defaults() {
local test_name="flags_exts_defaults"
setup_test_case_base "$test_name"
setup_common_files
cat << EOF > expected_output.txt
----- Codebase for analysis -----
--- proj/main.go
package main // main.go content
---
--- proj/utils.py
print("util") # utils.py content
---
EOF
cat << EOF > expected_summary.txt
Included 2 files (65 B total) relative to CWD 'PLACEHOLDER_CWD':
└── proj
├── main.go (32 B)
└── utils.py (33 B)
Empty files found (0):
Errors encountered (0):
EOF
run_test \
"--config /dev/null -d proj -e go,py --legacy-format" \
"expected_output.txt" \
"expected_summary.txt"
}
# Test Case 3: Flags mode with output file (-o output.txt) using Hardcoded Defaults
test_case_3_flags_output_defaults() {
local test_name="flags_output_defaults"
setup_test_case_base "$test_name"
setup_common_files
cat << EOF > expected_output.txt
----- Codebase for analysis -----
--- proj/config.json
{"key": "value"}
---
--- proj/data/file.txt
text data
---
--- proj/main.go
package main // main.go content
---
--- proj/utils.py
print("util") # utils.py content
---
EOF
cat << EOF > expected_summary.txt
Included 4 files (101 B total) relative to CWD 'PLACEHOLDER_CWD':
└── proj
├── config.json (17 B)
├── data
│ └── file.txt (10 B)
├── main.go (31 B)
└── utils.py (33 B)
Empty files found (0):
Errors encountered (0):
EOF
run_test \
"--config /dev/null -d proj -o output.txt --legacy-format" \
"expected_output.txt" \
"expected_summary.txt"
}
# Test Case 4: Flags mode with --no-gitignore using Hardcoded Defaults
test_case_4_flags_no_gitignore_defaults() {
local test_name="flags_no_gitignore_defaults"
setup_test_case_base "$test_name"
setup_common_files
cat << EOF > expected_output.txt
----- Codebase for analysis -----
--- proj/config.json
{"key": "value"}
---
--- proj/data/file.txt
text data
---
--- proj/data/ignored.log
log stuff
---
--- proj/main.go
package main // main.go content
---
--- proj/utils.py
print("util") # utils.py content
---
EOF
cat << EOF > expected_summary.txt
Included 5 files (111 B total) relative to CWD 'PLACEHOLDER_CWD':
└── proj
├── config.json (17 B)
├── data
│ ├── file.txt (10 B)
│ └── ignored.log (10 B)
├── main.go (31 B)
└── utils.py (33 B)
Empty files found (1):
- proj/empty.txt
Errors encountered (0):
EOF
run_test \
"--config /dev/null -d proj --no-gitignore -x \"\" -e go,py,txt,log,json --legacy-format" \
"expected_output.txt" \
"expected_summary.txt"
}
# Test Case 5: Flags mode with manual file (-f manual.txt -d proj -e go) using Hardcoded Defaults
test_case_5_flags_manual_defaults() {
local test_name="flags_manual_defaults"
setup_test_case_base "$test_name"
setup_common_files
cat << EOF > expected_output.txt
----- Codebase for analysis -----
--- manual.txt
manual content
---
--- proj/main.go
package main // main.go content
---
EOF
cat << EOF > expected_summary.txt
Included 2 files (46 B total) relative to CWD 'PLACEHOLDER_CWD':
├── manual.txt (15 B)
└── proj
└── main.go (31 B)
Empty files found (0):
Errors encountered (0):
EOF
run_test \
"--config /dev/null -f manual.txt -d proj -e go --legacy-format" \
"expected_output.txt" \
"expected_summary.txt"
}
# Test Case 6: Ambiguity - Multiple Positional Args
test_case_6_ambiguous_positional() {
local test_name="ambiguous_positional"
setup_test_case_base "$test_name"
test_info "Running test case (expecting failure)..."
test_info "Command: $CODECAT_CMD proj manual.txt"
set +e
"$CODECAT_CMD" proj manual.txt > "$STDOUT_LOG" 2> "$STDERR_LOG"
local exit_code=$?
set -e
if [ "$exit_code" -eq 0 ]; then fail "Expected non-zero exit code, got 0"; fi
grep -q 'Too many positional arguments' "$STDERR_LOG" || fail "Expected 'Too many positional arguments' message missing from stderr."
grep -q 'Error: Expected at most one positional argument' "$STDERR_LOG" || fail "Expected 'Error: Expected at most one positional argument' message missing from stderr."
[ ! -s "$STDOUT_LOG" ] || fail "Stdout not empty."
pass "Detected expected failure."
rm -f "$STDOUT_LOG" "$STDERR_LOG"
}
# Test Case 7: Ambiguity - Positional Arg + Flag
test_case_7_ambiguous_positional_flag() {
local test_name="ambiguous_positional_flag"
setup_test_case_base "$test_name"
test_info "Running test case (expecting failure)..."
test_info "Command: $CODECAT_CMD proj -e go"
set +e
"$CODECAT_CMD" proj -e go > "$STDOUT_LOG" 2> "$STDERR_LOG"
local exit_code=$?
set -e
if [ "$exit_code" -eq 0 ]; then fail "Expected non-zero exit code, got 0"; fi
grep -q 'Cannot specify a target directory via positional argument' "$STDERR_LOG" || fail "Expected positional/flag conflict message missing from stderr."
grep -q -- '-d flag' "$STDERR_LOG" || fail "Reference to conflicting '-d' flag missing/incorrect."
[ ! -s "$STDOUT_LOG" ] || fail "Stdout not empty."
pass "Detected expected failure."
rm -f "$STDOUT_LOG" "$STDERR_LOG"
}
# Test Case 8: Ambiguity - Positional Arg + Output Flag
test_case_8_ambiguous_positional_output() {
local test_name="ambiguous_positional_output"
setup_test_case_base "$test_name"
test_info "Running test case (expecting failure)..."
test_info "Command: $CODECAT_CMD proj -o out.txt"
set +e
"$CODECAT_CMD" proj -o out.txt > "$STDOUT_LOG" 2> "$STDERR_LOG"
local exit_code=$?
set -e
if [ "$exit_code" -eq 0 ]; then fail "Expected non-zero exit code, got 0"; fi
grep -q 'Cannot specify a target directory via positional argument' "$STDERR_LOG" || fail "Expected positional/flag conflict message missing from stderr."
grep -q -- '-d flag' "$STDERR_LOG" || fail "Reference to conflicting '-d' flag missing/incorrect."
[ ! -s "$STDOUT_LOG" ] || fail "Stdout not empty."
[ ! -f out.txt ] || fail "Output file 'out.txt' was incorrectly created."
pass "Detected expected failure."
rm -f "$STDOUT_LOG" "$STDERR_LOG"
}
# Test Case 9: Non-existent directory
test_case_9_non_existent_dir() {
local test_name="non_existent_dir"
setup_test_case_base "$test_name"
test_info "Running test case (expecting failure)..."
test_info "Command: $CODECAT_CMD -d no_such_dir"
set +e
"$CODECAT_CMD" -d no_such_dir > "$STDOUT_LOG" 2> "$STDERR_LOG"
local exit_code=$?
set -e
if [ "$exit_code" -eq 0 ]; then fail "Expected non-zero exit code, got 0"; fi
grep -q 'scan directory not found: no_such_dir' "$STDERR_LOG" || \
fail "Expected 'scan directory not found' message missing from stderr."
grep -q '--- Errors Encountered ---' "$STDERR_LOG" || fail "Error summary block missing from stderr."
[ ! -s "$STDOUT_LOG" ] || fail "Stdout not empty."
pass "Detected expected failure."
rm -f "$STDOUT_LOG" "$STDERR_LOG"
}
# Test Case 10: Custom Config File
test_case_10_custom_config() {
local test_name="custom_config"
setup_test_case_base "$test_name"
setup_common_files
local test_config_file="test_config.toml"
cat << EOF > "$test_config_file"
header_text = "Test Config Header\n"
include_extensions = ["py", "json"]
exclude_basenames = ["main.go"]
use_gitignore = false
comment_marker = "###"
EOF
test_info "Created custom config: $test_config_file"
cat << EOF > expected_output.txt
Test Config Header
### proj/config.json
{"key": "value"}
###
### proj/utils.py
print("util") # utils.py content
###
EOF
cat << EOF > expected_summary.txt
Included 2 files (50 B total) relative to CWD 'PLACEHOLDER_CWD':
└── proj
├── config.json (17 B)
└── utils.py (33 B)
Empty files found (0):
Errors encountered (0):
EOF
run_test \
"-d proj --config $test_config_file --legacy-format" \
"expected_output.txt" \
"expected_summary.txt"
}
# --- Main Execution ---
build_binary
test_case_1_basename_exclusion
test_case_2_flags_exts_defaults
test_case_3_flags_output_defaults
test_case_4_flags_no_gitignore_defaults
test_case_5_flags_manual_defaults
test_case_6_ambiguous_positional
test_case_7_ambiguous_positional_flag
test_case_8_ambiguous_positional_output
test_case_9_non_existent_dir
test_case_10_custom_config
# If execution reaches here, all tests passed
exit 0