-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_tests.sh
executable file
·89 lines (82 loc) · 2.8 KB
/
run_tests.sh
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
#!/bin/bash
# Run all the .gol tests to make sure they do what they're meant to.
## Usage: clr color_code text
# Changes text color according to given code, from 0 thru 7.
clr() {
echo -e "\e[0;3${1}m$2\e[0m"
}
## Usage: test-dir directory
# Tests every .gol file in given directory
test-dir() {
echo -n " ..."
local messages
messages=("$(clr 2 "Testing $1")")
cd "$1" || exit 1
local f
local out_file
local output
local expected
local show="failed"
local failed
if [ -f "./output.go" ]; then
expected="$(go run ./output.go)"
else
messages+=("$(clr 2 "No output.go in $1. Showing raw output instead.")")
show="all"
fi
for f in ./*; do
if [ "$f" = "${f/.gol/}.gol" ]; then
f="${f/.\//}" # strip leading ./
out_file="gol_${f/.gol/}.go" # Golid → Go name conversion
if golid "$f" 2>/dev/null; then # convert to Go
# run generated file if Golid worked
if ! output="$(go run "$out_file" 2>/dev/null)"; then
failed="yes"
messages+=("$(clr 1 "Error in $f: Could not run generated '$out_file'.")")
messages+=("$(clr 2 "Examine '$out_file' to see what went wrong.")")
else
rm "$out_file" # and then clean up
if [ "$show" = "all" ]; then
messages+=("$(clr 7 "$f:")")
messages+=("$output")
elif [ "$output" != "$expected" ]; then
failed="yes"
messages+=("$(clr 1 "$f doesn't produce expected output.")")
messages+=("$(clr 1 "$f's results:")")
messages+=("$output")
fi
fi
else
failed="yes"
messages+=("$(clr 1 "Error! Golid failed on '$f'!")")
messages+=("$(clr 2 "Run test.sh for debugging info.")")
fi
fi
done
if [ -z "$failed" ]; then
messages+=("$(clr 3 "Success!")")
elif [ "$show" = "failed" ]; then
messages+=("$(clr 1 "At least one test failed. Here's the expected output.")")
messages+=("$expected")
fi
if [ -n "$failed" ] || [ "$show" = "all" ] || [ -n "$DEBUG" ]; then
echo
for message in "${messages[@]}"; do
echo -e "$message"
done
fi
}
# Make sure we're at the right place before doing stuff.
test_dir="$(dirname "$(readlink -f "$0")")/tests" || exit 1
# Make sure the latest Golid version is installed.
"$test_dir"/../install.sh
echo -n "Running tests. Please wait."
# Enable debug if told to
if [ "$1" = "debug" ]; then
DEBUG="true"
fi
# Run the tests.
for d in "$test_dir"/*; do
test-dir "$d"
done
echo