PackageManager: add unit tests

This commit is contained in:
Thomas Farstrike
2025-11-02 13:28:29 +01:00
parent d8e0fc066b
commit 0d2afb6f70
8 changed files with 134 additions and 2 deletions
+37
View File
@@ -0,0 +1,37 @@
import unittest
from mpos import PackageManager
class TestCompareVersions(unittest.TestCase):
def test_lower_short(self):
self.assertFalse(PackageManager.compare_versions("1" , "4"))
def test_lower(self):
self.assertFalse(PackageManager.compare_versions("1.2.3" , "4.5.6"))
def test_equal(self):
self.assertFalse(PackageManager.compare_versions("1.2.3" , "1.2.3"))
def test_higher(self):
self.assertTrue(PackageManager.compare_versions("4.5.6", "1.2.3"))
def test_higher_medium_and_long(self):
self.assertTrue(PackageManager.compare_versions("4.5", "1.2.3"))
def test_words(self):
self.assertFalse(PackageManager.compare_versions("weird" , "input"))
def test_one_empty(self):
self.assertFalse(PackageManager.compare_versions("1.2.3" , ""))
class TestPackageManager_is_installed_by_name(unittest.TestCase):
def test_installed_builtin(self):
self.assertTrue(PackageManager.is_installed_by_name("com.micropythonos.appstore"))
def test_installed_not_builtin(self):
self.assertTrue(PackageManager.is_installed_by_name("com.micropythonos.helloworld"))
def test_not_installed(self):
self.assertFalse(PackageManager.is_installed_by_name("com.micropythonos.badname"))
+6
View File
@@ -0,0 +1,6 @@
import unittest
class TestMath(unittest.TestCase):
def test_add(self):
self.assertEqual(1 + 1, 2)
+20
View File
@@ -0,0 +1,20 @@
#!/bin/bash
mydir=$(readlink -f "$0")
mydir=$(dirname "$mydir")
fs="$mydir"/../../internal_filesystem/
cross="$mydir"/../../lvgl_micropython/lib/micropython/mpy-cross/build/mpy-cross
failed=0
while read file; do
"$cross" -march=x64 -o /dev/null "$file"
exitcode="$?"
if [ $exitcode -ne 0 ]; then
echo "$file got exitcode $exitcode"
failed=$(expr $failed \+ 1)
fi
done < <(find "$fs" -iname "*.py")
if [ $failed -ne 0 ]; then
echo "ERROR: $failed .py files have syntax errors"
exit 1
fi
+63
View File
@@ -0,0 +1,63 @@
#!/bin/bash
mydir=$(readlink -f "$0")
mydir=$(dirname "$mydir")
testdir="$mydir"
scriptdir=$(readlink -f "$mydir"/../scripts/)
fs="$mydir"/../internal_filesystem/
onetest="$1"
# 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
one_test() {
file="$1"
pushd "$fs"
echo "Testing $file"
"$binary" -X heapsize=8M -c "import sys ; sys.path.append('lib')
$(cat $file)
result = unittest.main() ; sys.exit(0 if result.wasSuccessful() else 1) "
result=$?
popd
return "$result"
}
failed=0
if [ -z "$onetest" ]; then
echo "Usage: $0 [one_test_to_run.py]"
echo "Example: $0 tests/simple.py"
echo
echo "If no test is specified: run all tests from $testdir"
while read file; do
one_test "$file"
result=$?
if [ $result -ne 0 ]; then
echo "test $file got error $result"
failed=$(expr $failed \+ 1)
fi
done < <( find "$testdir" -iname "*.py" )
else
one_test $(readlink -f "$onetest")
[ $? -ne 0 ] && failed=1
fi
if [ $failed -ne 0 ]; then
echo "ERROR: $failed .py files have failing unit tests"
exit 1
else
echo "GOOD: no .py files have failing unit tests"
exit 0
fi