test/command 5.2 KiB raw
1
#!/bin/sh
2
# Verify command-line parsing and validation through the compiler driver.
3
set -eu
4
work=$(mktemp -d)
5
trap 'rm -rf "$work"' EXIT HUP INT TERM
6
checks=0
7
8
# Use the compiler's standard Make memory and stack settings.
9
command() {
10
    timeout 30 "${MAKE:-make}" --no-print-directory driver-command \
11
        RAD_BIN="${RAD_BIN:-bin/radiance.rv64.dev}" \
12
        RAD_EMULATOR="${RAD_EMULATOR:-emulator}" \
13
        DRIVER_ARGUMENTS="$1" > "$work/command.log" 2>&1
14
}
15
16
# Require a specific diagnostic, including checks that precede file loading.
17
reject() {
18
    if command "$1"; then
19
        echo "command: unexpected success: $1"
20
        exit 1
21
    fi
22
    if ! grep -Fq -- "$2" "$work/command.log"; then
23
        cat "$work/command.log"
24
        echo "command: missing diagnostic: $2"
25
        exit 1
26
    fi
27
    checks=$((checks + 1))
28
}
29
30
# Require successful compilation or dumping.
31
accept() {
32
    if ! command "$1"; then
33
        cat "$work/command.log"
34
        exit 1
35
    fi
36
    checks=$((checks + 1))
37
}
38
39
while IFS='|' read -r arguments expected; do
40
    reject "$arguments" "$expected"
41
done <<'CASES'
42
|usage: radiance
43
-pkg|`-pkg` requires a package name
44
-mod|`-mod` requires a module path
45
-start|`-start` requires an assembly path
46
-entry|`-entry` requires a package name
47
-o|`-o` requires an output path
48
-ril|`-ril` requires an output directory
49
-dump|`-dump` requires a mode
50
-mod absent.rad|`-mod` must follow a `-pkg` argument
51
-start absent.ras|`-start` must follow a `-pkg` argument
52
-dump invalid|unknown dump mode invalid
53
-unknown|unknown argument -unknown
54
-test|no package specified
55
-pkg p|package p has no Radiance modules specified
56
-pkg p -mod absent.ras|package p has no Radiance modules specified
57
-pkg p -mod p.rad -start x.rad|`-start` requires a `.ras` assembly file
58
-pkg p -mod p.rad -start x.ras -start y.ras|has more than one startup file
59
-pkg p -mod p.rad -pkg q -mod q.rad|`-entry` required when multiple packages specified
60
-pkg p -mod p.rad -pkg q -mod q.rad -entry missing|entry package missing not found
61
-pkg p -mod p.rad -start x.ras -pkg q -mod q.rad -entry q|`-start` is only supported on the entry package
62
-pkg p -mod p.rad -ril out -o out.rv64|`-ril` requires a separate invocation
63
-pkg p -mod p.rad -ril out -dump ast|`-ril` requires a separate invocation
64
CASES
65
66
# At each path limit, parsing reaches the deliberately absent source file.
67
arguments='-pkg p'
68
for count in $(seq 1 129); do
69
    arguments="$arguments -mod $work/absent.rad"
70
    if [ "$count" -eq 128 ]; then reject "$arguments" 'error reading file'; fi
71
done
72
reject "$arguments" 'too many modules specified for package'
73
arguments="-pkg p -mod $work/absent.rad"
74
for count in $(seq 1 65); do
75
    arguments="$arguments -mod $work/absent.ras"
76
    if [ "$count" -eq 64 ]; then reject "$arguments" 'error reading file'; fi
77
done
78
reject "$arguments" 'too many assembly modules specified'
79
reject '-pkg a -pkg b -pkg c -pkg d -pkg e' 'too many packages specified'
80
81
printf '%s\n' '/// Executable command fixture.' '@default fn main() -> u32 { return 0; }' > "$work/p.rad"
82
for mode in ast graph il asm; do
83
    accept "-pkg p -mod $work/p.rad -dump $mode"
84
done
85
# A single package selects itself; the final output option selects the path.
86
accept "-pkg p -mod $work/p.rad -entry unused -debug -o $work/first.rv64 -o $work/final.rv64"
87
test ! -e "$work/first.rv64"
88
test -s "$work/final.rv64"
89
timeout 10 "${RAD_EMULATOR:-emulator}" -run "$work/final.rv64"
90
91
arguments=''
92
for name in a b c; do
93
    printf '%s\n' '/// Package fixture value.' 'constant value: u32 = 0;' > "$work/$name.rad"
94
    arguments="$arguments -pkg $name -mod $work/$name.rad"
95
done
96
accept "$arguments -pkg p -mod $work/p.rad -entry p -o $work/multiple.rv64"
97
timeout 10 "${RAD_EMULATOR:-emulator}" -run "$work/multiple.rv64"
98
99
# Binary package output preserves bytes across destinations and replacements.
100
mkdir "$work/packages"
101
accept "$arguments -pkg p -mod $work/p.rad -entry p -ril $work/packages"
102
for name in a b c p; do
103
    test -s "$work/packages/$name.ril"
104
done
105
cp "$work/packages/p.ril" "$work/expected.ril"
106
printf '%s\n' 'stale output bytes' >> "$work/packages/p.ril"
107
accept "-pkg p -mod $work/p.rad -ril $work/packages"
108
cmp "$work/expected.ril" "$work/packages/p.ril"
109
110
# MAX_PATH_LEN includes the terminating zero byte.
111
for length in 249 250 255 256; do
112
    leaf=$(awk -v n="$((length - ${#work} - 1))" 'BEGIN { for (i = 0; i < n; i++) printf "x" }')
113
    directory="$work/$leaf"
114
    if [ "$length" -eq 249 ]; then
115
        mkdir "$directory"
116
        accept "-pkg p -mod $work/p.rad -ril $directory"
117
        cmp "$work/expected.ril" "$directory/p.ril"
118
        reject "-pkg package -mod $work/p.rad -ril $directory" 'binary RIL output path is too long'
119
    else
120
        reject "-pkg p -mod $work/p.rad -ril $directory" 'binary RIL output path is too long'
121
    fi
122
done
123
reject "-pkg p -mod $work/p.rad -ril $work/missing" 'cannot write binary RIL package p'
124
mkdir -p "$work/blocked/p.ril"
125
reject "-pkg p -mod $work/p.rad -ril $work/blocked" 'cannot write binary RIL package p'
126
reject "-pkg p -mod $work/p.rad -mod absent.ras -ril $work/packages" 'binary RIL output requires Radiance source modules'
127
reject "-pkg p -mod $work/p.rad -start absent.ras -ril $work/packages" 'binary RIL output requires Radiance source modules'
128
echo "command tests: $checks passed"