|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +mydir=$(readlink -f "$0") |
| 4 | +mydir=$(dirname "$mydir") |
| 5 | +testdir="$mydir" |
| 6 | +scriptdir=$(readlink -f "$mydir"/../scripts/) |
| 7 | +fs="$mydir"/../internal_filesystem/ |
| 8 | +mpremote="$mydir"/../lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py |
| 9 | + |
| 10 | +# Parse arguments |
| 11 | +ondevice="" |
| 12 | +onetest="" |
| 13 | + |
| 14 | +while [ $# -gt 0 ]; do |
| 15 | + case "$1" in |
| 16 | + --ondevice) |
| 17 | + ondevice="yes" |
| 18 | + ;; |
| 19 | + *) |
| 20 | + onetest="$1" |
| 21 | + ;; |
| 22 | + esac |
| 23 | + shift |
| 24 | +done |
| 25 | + |
| 26 | +# print os and set binary |
| 27 | +os_name=$(uname -s) |
| 28 | +if [ "$os_name" = "Darwin" ]; then |
| 29 | + echo "Running on macOS" |
| 30 | + binary="$scriptdir"/../lvgl_micropython/build/lvgl_micropy_macOS |
| 31 | +else |
| 32 | + # other cases can be added here |
| 33 | + echo "Running on $os_name" |
| 34 | + binary="$scriptdir"/../lvgl_micropython/build/lvgl_micropy_unix |
| 35 | +fi |
| 36 | + |
| 37 | +binary=$(readlink -f "$binary") |
| 38 | +chmod +x "$binary" |
| 39 | + |
| 40 | +one_test() { |
| 41 | + file="$1" |
| 42 | + if [ ! -f "$file" ]; then |
| 43 | + echo "ERROR: $file is not a regular, existing file!" |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | + pushd "$fs" |
| 47 | + echo "Testing $file" |
| 48 | + |
| 49 | + # Detect if this is a graphical test (filename contains "graphical") |
| 50 | + if echo "$file" | grep -q "graphical"; then |
| 51 | + echo "Detected graphical test - including boot and main files" |
| 52 | + is_graphical=1 |
| 53 | + # Get absolute path to tests directory for imports |
| 54 | + tests_abs_path=$(readlink -f "$testdir") |
| 55 | + else |
| 56 | + is_graphical=0 |
| 57 | + fi |
| 58 | + |
| 59 | + if [ -z "$ondevice" ]; then |
| 60 | + # Desktop execution |
| 61 | + if [ $is_graphical -eq 1 ]; then |
| 62 | + # Graphical test: include boot_unix.py and main.py |
| 63 | + "$binary" -X heapsize=8M -c "$(cat main.py) ; import mpos.main ; import mpos.apps; sys.path.append(\"$tests_abs_path\") |
| 64 | +$(cat $file) |
| 65 | +result = unittest.main() ; sys.exit(0 if result.wasSuccessful() else 1) " |
| 66 | + result=$? |
| 67 | + else |
| 68 | + # Regular test: no boot files |
| 69 | + "$binary" -X heapsize=8M -c "$(cat main.py) |
| 70 | +$(cat $file) |
| 71 | +result = unittest.main() ; sys.exit(0 if result.wasSuccessful() else 1) " |
| 72 | + result=$? |
| 73 | + fi |
| 74 | + else |
| 75 | + if [ ! -z "$ondevice" ]; then |
| 76 | + echo "Hack: reset the device to make sure no previous UnitTest classes have been registered..." |
| 77 | + "$mpremote" reset |
| 78 | + sleep 15 |
| 79 | + fi |
| 80 | + |
| 81 | + echo "Device execution" |
| 82 | + # NOTE: On device, the OS is already running with boot.py and main.py executed, |
| 83 | + # so we don't need to (and shouldn't) re-run them. The system is already initialized. |
| 84 | + cleanname=$(echo "$file" | sed "s#/#_#g") |
| 85 | + testlog=/tmp/"$cleanname".log |
| 86 | + echo "$test logging to $testlog" |
| 87 | + if [ $is_graphical -eq 1 ]; then |
| 88 | + # Graphical test: system already initialized, just add test paths |
| 89 | + "$mpremote" exec "$(cat main.py) ; sys.path.append('tests') |
| 90 | +$(cat $file) |
| 91 | +result = unittest.main() |
| 92 | +if result.wasSuccessful(): |
| 93 | + print('TEST WAS A SUCCESS') |
| 94 | +else: |
| 95 | + print('TEST WAS A FAILURE') |
| 96 | +" | tee "$testlog" |
| 97 | + else |
| 98 | + # Regular test: no boot files |
| 99 | + "$mpremote" exec "$(cat main.py) |
| 100 | +$(cat $file) |
| 101 | +result = unittest.main() |
| 102 | +if result.wasSuccessful(): |
| 103 | + print('TEST WAS A SUCCESS') |
| 104 | +else: |
| 105 | + print('TEST WAS A FAILURE') |
| 106 | +" | tee "$testlog" |
| 107 | + fi |
| 108 | + grep -q "TEST WAS A SUCCESS" "$testlog" |
| 109 | + result=$? |
| 110 | + fi |
| 111 | + popd |
| 112 | + return "$result" |
| 113 | +} |
| 114 | + |
| 115 | +failed=0 |
| 116 | +ran=0 |
| 117 | + |
| 118 | +if [ -z "$onetest" ]; then |
| 119 | + echo "Usage: $0 [one_test_to_run.py] [--ondevice]" |
| 120 | + echo "Example: $0 tests/simple.py" |
| 121 | + echo "Example: $0 tests/simple.py --ondevice" |
| 122 | + echo "Example: $0 --ondevice" |
| 123 | + echo |
| 124 | + echo "If no test is specified: run all tests from $testdir on local machine." |
| 125 | + echo |
| 126 | + 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." |
| 127 | + while read file; do |
| 128 | + one_test "$file" |
| 129 | + result=$? |
| 130 | + if [ $result -ne 0 ]; then |
| 131 | + echo -e "\n\n\nWARNING: test $file got error $result !!!\n\n\n" |
| 132 | + failed=$(expr $failed \+ 1) |
| 133 | + exit 1 |
| 134 | + else |
| 135 | + ran=$(expr $ran \+ 1) |
| 136 | + fi |
| 137 | + done < <( find "$testdir" -iname "test_*.py" ) |
| 138 | +else |
| 139 | + echo "doing $onetest" |
| 140 | + one_test $(readlink -f "$onetest") |
| 141 | + [ $? -ne 0 ] && failed=1 |
| 142 | +fi |
| 143 | + |
| 144 | + |
| 145 | +if [ $failed -ne 0 ]; then |
| 146 | + echo "ERROR: $failed of the $ran tests failed" |
| 147 | + exit 1 |
| 148 | +else |
| 149 | + echo "GOOD: none of the $ran tests failed" |
| 150 | + exit 0 |
| 151 | +fi |
| 152 | + |
0 commit comments