Repository: dogtopus/minipb (language: python)

Issue:
Run test on actual MicroPython instead of CPython 3.4
There's https://hub.docker.com/r/micropython/unix but we need to either somehow run this on GitHub Actions or just come up with our own Actions image.


Discussion / hints:

Fix patch:
diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml
deleted file mode 100644
index e66c740..0000000
--- a/.github/workflows/pythonpackage.yml
+++ /dev/null
@@ -1,49 +0,0 @@
-name: Lint and Run Test Suite
-
-on: [push]
-
-jobs:
-  lint:
-    runs-on: ubuntu-latest
-    steps:
-    - uses: actions/checkout@v3
-    - name: Set up Python.
-      uses: actions/setup-python@v4
-      with:
-        python-version: '3.10'
-    - name: Lint with flake8
-      run: |
-        pip install flake8
-        # stop the build if there are Python syntax errors or undefined names
-        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
-        # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
-        flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
-
-  # TODO can we use real micropython for this?
-  test-py34:
-    runs-on: ubuntu-18.04
-    steps:
-    - uses: actions/checkout@v3
-    - name: Set up Python.
-      uses: actions/setup-python@v4
-      with:
-        python-version: '3.4.10'
-    - name: Run test suite
-      run: |
-        python test.py
-
-  test:
-    runs-on: ubuntu-latest
-    strategy:
-      max-parallel: 4
-      matrix:
-        python-version: ['3.8', '3.9', '3.10']
-    steps:
-    - uses: actions/checkout@v3
-    - name: Set up Python ${{ matrix.python-version }}
-      uses: actions/setup-python@v4
-      with:
-        python-version: ${{ matrix.python-version }}
-    - name: Run test suite
-      run: |
-        python test.py


Test patch:
diff --git a/.github/workflows/python-lint-test.yml b/.github/workflows/python-lint-test.yml
new file mode 100644
index 0000000..b82584e
--- /dev/null
+++ b/.github/workflows/python-lint-test.yml
@@ -0,0 +1,65 @@
+name: Lint and Run Test Suite
+
+on: [push]
+
+env:
+  MPY_STDLIB_COMMIT: f6723531802661e5a1fd3d63d3b7b68c38f40cd2
+
+jobs:
+  lint:
+    runs-on: ubuntu-latest
+    steps:
+    - name: 'Checkout the code'
+      uses: actions/checkout@v3
+    - name: Set up Python.
+      uses: actions/setup-python@v4
+      with:
+        python-version: '3.11'
+    - name: Lint with flake8
+      run: |
+        pip install flake8
+        # stop the build if there are Python syntax errors or undefined names
+        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
+        # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
+        flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
+
+  test-mpy:
+    runs-on: ubuntu-latest
+    container:
+      image: docker.io/micropython/unix:v1.19
+    steps:
+    - name: 'Setup basic environment.'
+      run: apt-get update && apt-get install -y git
+    - name: 'Checkout the code'
+      uses: actions/checkout@v3
+    - name: 'Download MicroPython stdlib and create test environment.'
+      run: |
+        mkdir /tmp/testenv && \
+        cp minipb.py /tmp/testenv/ && \
+        cp test.py /tmp/testenv/ && \
+        git clone https://github.com/micropython/micropython-lib.git /tmp/micropython-lib && \
+        cd /tmp/micropython-lib && \
+        git checkout "${MPY_STDLIB_COMMIT}" && \
+        cp python-stdlib/bisect/bisect.py /tmp/testenv && \
+        cp python-stdlib/logging/logging.py /tmp/testenv && \
+        cp -r python-stdlib/unittest/unittest /tmp/testenv
+    - name: Run test suite
+      run: |
+        cd /tmp/testenv && micropython-dev test.py
+
+  test:
+    runs-on: ubuntu-latest
+    strategy:
+      max-parallel: 4
+      matrix:
+        python-version: ['3.8', '3.9', '3.10']
+    steps:
+    - name: 'Checkout the code'
+      uses: actions/checkout@v3
+    - name: Set up Python ${{ matrix.python-version }}
+      uses: actions/setup-python@v4
+      with:
+        python-version: ${{ matrix.python-version }}
+    - name: Run test suite
+      run: |
+        python test.py
diff --git a/test.py b/test.py
index 2f8eabb..cc00240 100644
--- a/test.py
+++ b/test.py
@@ -1,8 +1,22 @@
 #!/usr/bin/env python3
 import unittest
 import collections
+import sys
 import minipb
 
+
+# MicroPython hack
+_IS_MPY = __import__('sys').implementation.name == 'micropython'
+
+if _IS_MPY:
+    class TestCase(unittest.TestCase):
+        # Redirect assertTupleEqual to assertEqual since that wasn't implemented
+        def assertTupleEqual(self, x, y, msg=""):
+            return self.assertEqual(x, y, msg)
+else:
+    TestCase = unittest.TestCase
+
+
 TEST_RAW_ENCODED = b'\x08\x7b\x12\x04\x74\x65\x73\x74\x1a\x0b\x0a\x06\x73\x74\x72\x69\x6e\x67\x10\xf8\x06\x1a\x13\x0a\x0e\x61\x6e\x6f\x74\x68\x65\x72\x5f\x73\x74\x72\x69\x6e\x67\x10\xb9\x60'
 
 TEST_RAW_DECODED = ({'data': 123, 'id': 1, 'wire_type': 0}, {'data': b'test', 'id': 2, 'wire_type': 2}, {'data': b'\n\x06string\x10\xf8\x06', 'id': 3, 'wire_type': 2}, {'data': b'\n\x0eanother_string\x10\xb9`', 'id': 3, 'wire_type': 2})
@@ -11,7 +25,7 @@
 
 TEST_FIELD_SEEK_COMPLEX = b'\xa2\x01\t\x08\x02R\x05hello\xf2\x01\x06\x12\x04str1\xf2\x01\x06\x12\x04str2'
 
-class TestMiniPB(unittest.TestCase):
+class TestMiniPB(TestCase):
     # some of the following data were taken from
     # https://developers.google.com/protocol-buffers/docs/encoding
     def test_codec_vint(self):
@@ -595,4 +609,6 @@ class AnotherMessage(minipb.Message):
 
 
 if __name__ == '__main__':
-    unittest.main()
+    result = unittest.main()
+    if _IS_MPY:
+        sys.exit(result.failuresNum != 0 or result.errorsNum != 0)