run_tests.sh raw
1 #!/bin/bash
2 # Run stdlib tests using the stage-2 (moxie-compiled) compiler
3 set -euo pipefail
4
5 MOXIE="${MOXIE:-./moxie}"
6 TMPBASE="${TMPBASE:-/home/mleku/tmp/mxtest}"
7 PASSED=0
8 FAILED=0
9 ERRORS=""
10
11 if [ ! -x "$MOXIE" ]; then
12 echo "FATAL: $MOXIE not found or not executable"
13 exit 1
14 fi
15
16 echo "=== Running stdlib tests with: $MOXIE ==="
17 echo " $($MOXIE version 2>&1)"
18 echo
19
20 rm -rf "$TMPBASE"
21 mkdir -p "$TMPBASE"
22
23 # Find all test packages
24 TEST_FILES=$(find ./src -name "*_test.mx" -not -path "./legacy/*")
25
26 # Group by directory
27 DIRS=$(echo "$TEST_FILES" | xargs -n1 dirname | sort -u)
28
29 for dir in $DIRS; do
30 pkgname=$(head -5 "$dir"/*_test.mx 2>/dev/null | grep "^package " | head -1 | awk '{print $2}')
31 if [ -z "$pkgname" ]; then
32 continue
33 fi
34
35 # Collect test functions
36 testfuncs=$(grep -h "^func Test" "$dir"/*_test.mx 2>/dev/null | sed 's/func \(Test[^ (]*\).*/\1/')
37 if [ -z "$testfuncs" ]; then
38 continue
39 fi
40
41 testdir="$TMPBASE/$(echo "$dir" | tr '/' '_')"
42 mkdir -p "$testdir"
43
44 # Copy all .mx files from the package dir (including _test.mx)
45 for f in "$dir"/*.mx; do
46 [ -f "$f" ] || continue
47 base=$(basename "$f")
48 # Rewrite package declaration to main
49 sed "s/^package $pkgname$/package main/" "$f" > "$testdir/$base"
50 done
51
52 # Generate test main
53 {
54 echo "package main"
55 echo ""
56 echo "import \"testing\""
57 echo ""
58 echo "func main() {"
59 echo " tests := []testing.InternalTest{"
60 for fn in $testfuncs; do
61 echo " {\"$fn\", $fn},"
62 done
63 echo " }"
64 echo " testing.Main(tests)"
65 echo "}"
66 } > "$testdir/testmain_gen.mx"
67
68 # Build
69 shortname=$(echo "$dir" | sed 's|^\./src/||')
70 printf "%-50s" "TEST $shortname"
71
72 if $MOXIE build -o "$testdir/test.bin" "$testdir" >"$testdir/build.log" 2>&1; then
73 # Run
74 if output=$("$testdir/test.bin" 2>&1); then
75 echo "PASS"
76 PASSED=$((PASSED + 1))
77 else
78 echo "FAIL (runtime)"
79 echo "$output" | tail -10
80 ERRORS="$ERRORS FAIL(run): $shortname\n"
81 FAILED=$((FAILED + 1))
82 fi
83 else
84 echo "FAIL (build)"
85 tail -5 "$testdir/build.log"
86 ERRORS="$ERRORS FAIL(build): $shortname\n"
87 FAILED=$((FAILED + 1))
88 fi
89 done
90
91 echo
92 echo "=== Results ==="
93 echo "PASSED: $PASSED"
94 echo "FAILED: $FAILED"
95 if [ -n "$ERRORS" ]; then
96 echo
97 echo "Failures:"
98 printf "$ERRORS"
99 fi
100
101 if [ "$FAILED" -gt 0 ]; then
102 exit 1
103 fi
104