forked from MicroPythonOS/MicroPythonOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunittest.sh
More file actions
executable file
·161 lines (146 loc) · 4.46 KB
/
unittest.sh
File metadata and controls
executable file
·161 lines (146 loc) · 4.46 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
#!/bin/bash
mydir=$(readlink -f "$0")
mydir=$(dirname "$mydir")
testdir="$mydir"
#testdir=/home/user/projects/MicroPythonOS/claude/MicroPythonOS/tests2
scriptdir=$(readlink -f "$mydir"/../scripts/)
fs="$mydir"/../internal_filesystem/
mpremote="$mydir"/../lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py
# Parse arguments
ondevice=""
onetest=""
while [ $# -gt 0 ]; do
case "$1" in
--ondevice)
ondevice="yes"
;;
*)
onetest="$1"
;;
esac
shift
done
# print os and set binary
os_name=$(uname -s)
if [ "$os_name" = "Darwin" ]; then
echo "Running on macOS"
binary="$scriptdir"/../lvgl_micropython/build/lvgl_micropy_macOS
else
# other cases can be added here
echo "Running on $os_name"
binary="$scriptdir"/../lvgl_micropython/build/lvgl_micropy_unix
fi
binary=$(readlink -f "$binary")
chmod +x "$binary"
# make sure no autostart is configured:
rm "$scriptdir"/../internal_filesystem/data/com.micropythonos.settings/config.json
one_test() {
file="$1"
if [ ! -f "$file" ]; then
echo "ERROR: $file is not a regular, existing file!"
exit 1
fi
pushd "$fs"
echo "Testing $file"
# Detect if this is a graphical test (filename contains "graphical")
if echo "$file" | grep -q "graphical"; then
echo "Detected graphical test - including boot and main files"
is_graphical=1
# Get absolute path to tests directory for imports
tests_abs_path=$(readlink -f "$testdir")
else
is_graphical=0
fi
if [ -z "$ondevice" ]; then
# Desktop execution
if [ $is_graphical -eq 1 ]; then
echo "Graphical test: include main.py"
"$binary" -X heapsize=8M -c "import sys ; sys.path.insert(0, 'lib') ; import mpos ; mpos.TaskManager.disable() ; $(cat main.py) ; sys.path.append(\"$tests_abs_path\")
$(cat $file)
result = unittest.main() ; sys.exit(0 if result.wasSuccessful() else 1) "
result=$?
else
echo "Regular test: no boot files"
"$binary" -X heapsize=8M -c "import sys ; sys.path.insert(0, 'lib') ; import mpos ; mpos.TaskManager.disable() ; $(cat main.py)
$(cat $file)
result = unittest.main() ; sys.exit(0 if result.wasSuccessful() else 1) "
result=$?
fi
else
if [ ! -z "$ondevice" ]; then
echo "Hack: reset the device to make sure no previous UnitTest classes have been registered..."
"$mpremote" reset
sleep 15
fi
echo "Device execution"
# NOTE: On device, the OS is already running with boot.py and main.py executed,
# so we don't need to (and shouldn't) re-run them. The system is already initialized.
cleanname=$(echo "$file" | sed "s#/#_#g")
testlog=/tmp/"$cleanname".log
echo "$test logging to $testlog"
if [ $is_graphical -eq 1 ]; then
# Graphical test: system already initialized, just add test paths
"$mpremote" exec "import sys ; sys.path.insert(0, 'lib') ; import mpos ; mpos.TaskManager.disable() ; $(cat main.py) ; sys.path.append('tests')
$(cat $file)
result = unittest.main()
if result.wasSuccessful():
print('TEST WAS A SUCCESS')
else:
print('TEST WAS A FAILURE')
" | tee "$testlog"
else
# Regular test: no boot files
"$mpremote" exec "import sys ; sys.path.insert(0, 'lib') ; import mpos ; mpos.TaskManager.disable() ; $(cat main.py)
$(cat $file)
result = unittest.main()
if result.wasSuccessful():
print('TEST WAS A SUCCESS')
else:
print('TEST WAS A FAILURE')
" | tee "$testlog"
fi
grep -q "TEST WAS A SUCCESS" "$testlog"
result=$?
fi
popd
return "$result"
}
failed=0
ran=0
if [ -z "$onetest" ]; then
echo "Usage: $0 [one_test_to_run.py] [--ondevice]"
echo "Example: $0 tests/simple.py"
echo "Example: $0 tests/simple.py --ondevice"
echo "Example: $0 --ondevice"
echo
echo "If no test is specified: run all tests from $testdir on local machine."
echo
echo "The '--ondevice' flag will run the test(s) on a connected device using mpremote.py (should be on the PATH) over a serial connection."
files=$(find "$testdir" -iname "test_*.py" )
for file in $files; do
one_test "$file"
result=$?
if [ $result -ne 0 ]; then
echo -e "\n\n\nWARNING: test $file got error $result !!!\n\n\n"
failed=$(expr $failed \+ 1)
exit 1
else
ran=$(expr $ran \+ 1)
fi
done
else
echo "doing $onetest"
one_test $(readlink -f "$onetest")
result=$?
if [ $result -ne 0 ]; then
echo "Test returned result: $result"
failed=1
fi
fi
if [ $failed -ne 0 ]; then
echo "ERROR: $failed of the $ran tests failed"
exit 1
else
echo "GOOD: none of the $ran tests failed"
exit 0
fi