Skip to content

Commit

Permalink
Add a basic byte-checked test for examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
corranwebster committed Oct 18, 2024
1 parent b62d148 commit 614d5cd
Show file tree
Hide file tree
Showing 6 changed files with 475 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/run_tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: "Run Tests"

on:
- workflow_dispatch
- pull_request

jobs:
run-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install dependencies and local packages
run: python -m pip install mpremote click
- name: Install MicroPython
uses: BrianPugh/install-micropython@v2
- name: Install Micropython dependencies
run: micropython -m mip install unittest
- name: Run tests
run: python -m ci.test
38 changes: 38 additions & 0 deletions ci/rgb565_to_png.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import array
from pathlib import Path

from PIL import Image

def read_rgb565(path):
with open(path, 'rb') as fp:
buffer = fp.read()

rgb565 = array.array('H', buffer)
rgb565.byteswap()
return rgb565

def rgb565_to_rgb24(rgb565):
rgb24 = array.array('B', bytearray(3*len(rgb565)))
for i, pixel in enumerate(rgb565):
r = (pixel & 0b1111100000000000) >> 11
g = (pixel & 0b0000011111100000) >> 5
b = (pixel & 0b0000000000011111)
rgb24[3 * i] = int((r / 31) * 255)
rgb24[3 * i + 1] = int(round((g / 63) * 255))
rgb24[3 * i + 2] = int(round((b / 31) * 255))
return rgb24


def rgb565_to_png(size, in_path, out_path=None):
if out_path is None:
out_path = Path(in_path.stem + '.png')

rgb565 = read_rgb565(in_path)
rgb24 = rgb565_to_rgb24(rgb565)
image = Image.frombuffer("RGB", size, rgb24)
image.save(out_path)

if __name__ == "__main__":
import sys

rgb565_to_png((320, 240), Path(sys.argv[1]))
53 changes: 53 additions & 0 deletions ci/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# SPDX-FileCopyrightText: 2024-present Unital Software <[email protected]>
#
# SPDX-License-Identifier: MIT

from pathlib import Path
import os
import subprocess
import sys

import click



@click.command()
def test():
"""Run unit tests in micropython"""
print("Running Tests")
failures = []
test_dir = Path("tests/tempe")
os.environ["MICROPYPATH"] = "src:" + os.environ.get('MICROPYPATH', ":.frozen:~/.micropython/lib:/usr/lib/micropython")
for path in sorted(test_dir.glob("*.py")):
print(path.name, "... ", end="", flush=True)
result = run_test(path)
if result:
failures.append(result)
print('FAILED')
else:
print('OK')
print()

for path, stdout, stderr in failures:
print("FAILURE: ", path.name)
print("STDOUT ", "="*70)
print(stdout.decode('utf-8'))
print()
print("STDERR ", "="*70)
print(stderr.decode('utf-8'))
print()

if failures:
sys.exit(1)
else:
print("PASSED")


def run_test(path):
try:
result = subprocess.run(["micropython", path], capture_output=True, check=True)
except subprocess.CalledProcessError as exc:
return (path, exc.stdout, exc.stderr)

if __name__ == "__main__":
test()
Loading

0 comments on commit 614d5cd

Please sign in to comment.