Repository navigation
Expand file tree
/
Copy pathrun
More file actions
executable file
·232 lines (198 loc) · 4.57 KB
/
Copy pathrun
File metadata and controls
executable file
·232 lines (198 loc) · 4.57 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
#!/bin/bash
RED=';31'
GREEN=';32'
BLUE=';34'
NORMAL='0'
BOLD='1'
color() {
if [[ $# -ge 1 ]];then
echo "\033[$1$2m"
else
echo '\033[0m'
fi
}
OUT_DIR="out"
OUT_BUILD="$OUT_DIR/build"
OUT_NAME="pod-interpreter"
OUT_BUILD_PATH="$OUT_BUILD/$OUT_NAME"
TEST_DIR="$OUT_DIR/test-tmp"
P_STDERR="$TEST_DIR/stderr"
P_STDOUT="$TEST_DIR/stdout"
exit_usage() {
echo "Usage: $0 {argument} {file}"
exit 1
}
clean_output() {
mkdir -p $OUT_BUILD
# checks that out_dir is set properly (or atleast i try)
if [[ ! "$OUT_DIR/.." -ef $(pwd) ]]; then
echo "invalid out_dir"
exit 1
fi
rm --preserve-root -rf $OUT_DIR
mkdir -p $OUT_BUILD
}
build() {
# Code Gen
echo -ne "$(color $BOLD $RED)"
go run ./cmd/code-gen
exit_code=$?
echo -ne "$(color)"
if [[ $exit_code -ne 0 ]]; then
exit $exit_code
fi
# Build actual binary
if [[ $WASM ]]; then
GOOS=js GOARCH=wasm go build -o $OUT_BUILD_PATH ./cmd/pod-interpreter
else
go build -o $OUT_BUILD_PATH ./cmd/pod-interpreter
fi
exit_code=$?
echo -ne "$(color)"
if [[ $exit_code -ne 0 ]]; then
exit $exit_code
fi
}
run-test() {
exit_code_to_temp_file() {
exitcode_msg="ExitCode: $1"
echo "$(echo "$exitcode_msg"; echo -n "$(<$2)")" >$2
}
run_type=$2
test_path=$3
./$OUT_BUILD_PATH $run_type $1 >$P_STDOUT 2>$P_STDERR
exit_code=$?
exit_code_to_temp_file $exit_code $P_STDOUT
exit_code_to_temp_file $exit_code $P_STDERR
expect_stdout_file="$test_path.stdout"
expect_stderr_file="$test_path.stderr"
diff_out=$(diff $P_STDOUT $expect_stdout_file 2>/dev/null)
diff_out_fail=$?
diff_err=$(diff $P_STDERR $expect_stderr_file 2>/dev/null)
diff_err_fail=$?
if [[ $diff_err_fail -eq 0 ]] && [[ $diff_out_fail -eq 0 ]]; then
((PASS++))
elif [[ $BLESS ]]; then
echo "$(<$P_STDOUT)" >$expect_stdout_file
echo "$(<$P_STDERR)" >$expect_stderr_file
echo -e "\n > Saved output state of $(color $BOLD $BLUE)$1$(color)"
((PASS++))
else
msg="\n -> Failed: $(color $BOLD $RED)$1$(color) with error code $?"
echo -e "$msg"
if [[ $diff_out_fail -ne 0 ]]; then
echo -e "=======$(color $GREEN)STDOUT$(color)=========="
if [[ $diff_out_fail -eq 1 ]];then
echo "$diff_out"
fi
fi
if [[ $diff_out_fail -eq 2 ]]; then
echo "$(<$P_STDOUT)"
fi
if [[ $diff_err_fail -ne 0 ]]; then
echo -e "=======$(color $RED)STDERR$(color)=========="
if [[ $diff_err_fail -eq 1 ]];then
echo "$diff_err"
fi
fi
if [[ $diff_err_fail -eq 2 ]]; then
echo "$(<$P_STDERR)"
fi
if [[ $diff_err_fail -eq 2 ]] || [[ $diff_out_fail -eq 2 ]]; then
echo -e "\nFile not found. Try '$0 test bless'"
fi
((FAIL++))
fi
((COUNT++))
}
tests() {
echo "[ Running Tests ]"
mkdir -p $TEST_DIR
touch $P_STDERR
touch $P_STDOUT
COUNT=0
PASS=0
FAIL=0
if [[ -f $FILE ]]; then
if [[ $FILE =~ (tests\/([^\/]+)[^.]+).[pd|pod]+ ]]; then
run_type=${BASH_REMATCH[2]}
test_path=${BASH_REMATCH[1]}
run-test $FILE $run_type $test_path
fi
else
search_path="."
if [[ -d $FILE ]]; then
search_path="$FILE"
fi
for i in $(find $search_path -iregex ".*\.\(pod\|pd\)"); do
if [[ $i =~ (tests\/([^\/]+)[^.]+).[pd|pod]+ ]]; then
run_type=${BASH_REMATCH[2]}
test_path=${BASH_REMATCH[1]}
run-test $i $run_type $test_path
fi
done
fi
echo -e "\n ===> $(color $GREEN)$PASS/$COUNT$(color) passed : $(color $RED)$FAIL/$COUNT$(color) failed"
if [[ $FAIL -ne 0 ]]; then
echo -e "$(color $RED)$FAIL test(s)$(color) failed"
exit 1
fi
clean_output
}
if [[ ! $RUN_TYPE ]]; then
RUN_TYPE=run
fi
FILE=${@: -1}
for arg in "$@"; do
if [[ $arg == "build" ]]; then
BUILD=true
elif [[ $arg == "wasm" ]]; then
WASM=true
elif [[ $arg == "test" ]]; then
TEST=true
elif [[ $arg == "bless" ]]; then
BLESS=true
fi
done
if [[ $# -lt 1 ]]; then
exit_usage
fi
clean_output
if [[ $TEST ]]; then
OUT_BUILD_PATH="$OUT_BUILD/$OUT_NAME"
build
tests
if [[ ! $WASM ]] && [[ ! $BUILD ]]; then
exit 0
fi
fi
if [[ $WASM ]]; then
GOOS=js
GOARCH=wasm
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" ./$OUT_BUILD
cp ./web/* ./$OUT_BUILD/
rm ./$OUT_BUILD/*.go
cp -r ./examples ./$OUT_BUILD
python3 ./examples/example-index.py
rm ./$OUT_BUILD/examples/*.py
OUT_BUILD_PATH="$OUT_BUILD/main.wasm"
echo "[ Building for WASM ]"
else
OUT_BUILD_PATH="$OUT_BUILD/$OUT_NAME"
echo "[ Building for native ]"
fi
build
if [[ ! $BUILD ]] && [[ $WASM ]]; then
go run web/main.go
elif [[ ! $BUILD ]] && [[ -f $FILE ]]; then
OUT_MSG="[ Running $FILE ]"
echo $OUT_MSG
for i in $(seq ${#OUT_MSG}); do
echo -n "-"
done
echo
./$OUT_BUILD_PATH $RUN_TYPE $FILE
elif [[ ! $BUILD ]] && [[ ! $TEST ]]; then
echo "Invalid file"
exit_usage
fi