From b5032023f1dc71d1f3aa26d1a7ba24d3a15aed0e Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Mon, 13 Jul 2026 02:48:55 +0200 Subject: [PATCH] Add basic software test and CI/GHA configuration Signed-off-by: Andreas Motl --- .github/workflows/tests.yml | 59 +++++++++++++++++++++++++++++++++++++ .gitignore | 2 ++ mqttdecode | 5 ++++ requirements-tests.txt | 2 ++ tests/test_cli.py | 44 +++++++++++++++++++++++++++ 5 files changed, 112 insertions(+) create mode 100644 .github/workflows/tests.yml create mode 100644 .gitignore create mode 100644 requirements-tests.txt create mode 100644 tests/test_cli.py diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..893309a --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,59 @@ +name: Tests + +on: + push: + branches: [ main ] + pull_request: + + # Allow job to be triggered manually. + workflow_dispatch: + +# Cancel in-progress jobs when pushing to the same branch. +concurrency: + cancel-in-progress: true + group: ${{ github.workflow }}-${{ github.ref }} + +jobs: + + tests: + + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: ["ubuntu-latest"] + python-version: + - "3.8" + - "3.9" + - "3.10" + - "3.11" + - "3.12" + - "3.13" + - "3.14" + + env: + OS: ${{ matrix.os }} + PYTHON: ${{ matrix.python-version }} + + name: Python ${{ matrix.python-version }} on OS ${{ matrix.os }} + steps: + + - name: Acquire sources + uses: actions/checkout@v3 + + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + architecture: x64 + cache: 'pip' + cache-dependency-path: | + requirements-*.txt + + - name: Setup project + run: | + pip install --use-pep517 --prefer-binary --requirement requirements-tests.txt + + - name: Run linter and software tests + run: | + pytest diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..64a7365 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.idea +__pycache__ diff --git a/mqttdecode b/mqttdecode index 9b6fbc3..c2dce60 100755 --- a/mqttdecode +++ b/mqttdecode @@ -1102,6 +1102,11 @@ def get_args(): epilog="https://github.com/mqtt-tools/mqttdecode" ) + parser.add_argument( + "-v", "--version", action="version", + version = f"{parser.prog} version 0.0.1" + ) + parser.add_argument( '-s', "--force-spec", action="store", type=int, default=None, choices=[4, 5], diff --git a/requirements-tests.txt b/requirements-tests.txt new file mode 100644 index 0000000..204981a --- /dev/null +++ b/requirements-tests.txt @@ -0,0 +1,2 @@ +pytest +runs diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..df37477 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,44 @@ +import subprocess + +import runs + + +def run_mqttdecode(arguments): + """ + Utility function to invoke `mqttdecode`. + + :param arguments: + :return: + """ + command = f"python mqttdecode {arguments}" + response = runs.run(command) + return response + + +def test_cli_version(capfd): + """ + Verify `mqttdecode --version` succeeds. + """ + run_mqttdecode("--version") + out, err = capfd.readouterr() + assert out.strip() == "mqttdecode version 0.0.1" + + +def test_cli_mqttdecode(capfd): + """ + Verify basic `mqttdecode` incantation. + """ + output = subprocess.check_output(["python", "mqttdecode", "50020001"]) + assert output == b""" +Hex: 50 02 0001 +Command: PUBREC v3.1.1 +Length: 2 +Packet ID: 1 + +Hex: 50 02 0001 +Command: PUBREC v5.0 +Length: 2 +Packet ID: 1 +Reason code: 0 (success) + +""".lstrip()