You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
PackageManager: add unit tests
This commit is contained in:
@@ -4,6 +4,7 @@ conf.json*
|
||||
|
||||
# auto created when running on desktop:
|
||||
internal_filesystem/SDLPointer_2
|
||||
internal_filesystem/SDLPointer_3
|
||||
|
||||
# config files etc:
|
||||
internal_filesystem/data
|
||||
|
||||
@@ -7,4 +7,5 @@ This /lib folder contains:
|
||||
- mip.install("aiohttp") # easy websockets
|
||||
- mip.install("base64") # for nostr etc
|
||||
- mip.install("collections") # used by aiohttp
|
||||
- mip.install("unittest")
|
||||
|
||||
|
||||
@@ -166,8 +166,12 @@ class PackageManager:
|
||||
"""Compare two version numbers (e.g., '1.2.3' vs '4.5.6').
|
||||
Returns True if ver1 is greater than ver2, False otherwise."""
|
||||
print(f"Comparing versions: {ver1} vs {ver2}")
|
||||
v1_parts = [int(x) for x in ver1.split('.')]
|
||||
v2_parts = [int(x) for x in ver2.split('.')]
|
||||
try:
|
||||
v1_parts = [int(x) for x in ver1.split('.')]
|
||||
v2_parts = [int(x) for x in ver2.split('.')]
|
||||
except ValueError as e:
|
||||
print(f"Invalid input, got error: {e}")
|
||||
return False
|
||||
print(f"Version 1 parts: {v1_parts}")
|
||||
print(f"Version 2 parts: {v2_parts}")
|
||||
for i in range(max(len(v1_parts), len(v2_parts))):
|
||||
|
||||
Binary file not shown.
@@ -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"))
|
||||
@@ -0,0 +1,6 @@
|
||||
import unittest
|
||||
|
||||
class TestMath(unittest.TestCase):
|
||||
def test_add(self):
|
||||
self.assertEqual(1 + 1, 2)
|
||||
|
||||
Executable
+20
@@ -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
|
||||
Executable
+63
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user