diff --git a/.circleci/config.yml b/.circleci/config.yml index cf99b9b95..bae0b61dc 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -414,6 +414,26 @@ jobs: - checkout - run: *run_docs_pragma_min_version + t_pyscripts_ubu: + docker: + - image: << pipeline.parameters.ubuntu-2004-docker-image >> + steps: + - checkout + - run: + name: Python unit tests + command: python3 test/pyscriptTests.py + + t_pyscripts_win: + executor: + name: win/default + shell: powershell.exe + steps: + - run: git config --global core.autocrlf false + - checkout + - run: + name: Python unit tests + command: python.exe test/pyscriptTests.py + b_ubu_clang: &build_ubuntu2004_clang resource_class: xlarge docker: @@ -991,6 +1011,8 @@ workflows: - chk_errorcodes: *workflow_trigger_on_tags - chk_antlr_grammar: *workflow_trigger_on_tags - chk_docs_pragma_min_version: *workflow_trigger_on_tags + - t_pyscripts_ubu: *workflow_trigger_on_tags + - t_pyscripts_win: *workflow_trigger_on_tags # build-only - b_docs: *workflow_trigger_on_tags diff --git a/scripts/tests.sh b/scripts/tests.sh index 2882af067..e9354479a 100755 --- a/scripts/tests.sh +++ b/scripts/tests.sh @@ -62,6 +62,9 @@ else log_directory="" fi +printTask "Testing Python scripts..." +"$REPO_ROOT/test/pyscriptTests.py" + printTask "Running commandline tests..." # Only run in parallel if this is run on CI infrastructure if [[ -n "$CI" ]] diff --git a/test/pyscriptTests.py b/test/pyscriptTests.py new file mode 100755 index 000000000..17b095d64 --- /dev/null +++ b/test/pyscriptTests.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +# Runs all tests for Python-based scripts. +# Any Python test suites located in test/scripts/ will be executed automatically by this script. + +import sys +from pathlib import Path +from unittest import TestLoader, TextTestRunner + +SCRIPTS_DIR = Path(__file__).parent.parent / "scripts" +TEST_DIR = Path(__file__).parent.parent / "test/scripts/" + + +if __name__ == '__main__': + # Add the directory containing scripts to be tested to PYTHONPATH. This means that these + # scripts can be imported from anywhere (in particular from the test suites) as if they were + # installed globally. This is necessary because scripts and their tests are in separate + # directories not recognized by Python as a part of the same package (i.e. their common parent + # directory does not have an __init__.py file). + # NOTE: This does not play well with relative imports from test suites so the suites must be + # placed directly in TEST_DIR and not in its subdirectories. Relative imports from scripts + # themselves work fine though. + sys.path.insert(0, str(SCRIPTS_DIR)) + + # This is equivalent to `python -m unittest discover --start-directory $TEST_DIR` + test_suite = TestLoader().discover(start_dir=TEST_DIR) + result = TextTestRunner().run(test_suite) + + if len(result.errors) > 0 or len(result.failures) > 0: + sys.exit(1)