From 3d72f7a24236487f6e05409e245b40fd5939e5e5 Mon Sep 17 00:00:00 2001 From: DimitrisJim Date: Tue, 26 Sep 2023 11:13:55 +0300 Subject: [PATCH] Add script to run tests for all modules. --- Makefile | 15 ++------------ scripts/go-test-all.py | 45 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 scripts/go-test-all.py diff --git a/Makefile b/Makefile index 020eb1fe065..ecca2fc9f47 100644 --- a/Makefile +++ b/Makefile @@ -193,19 +193,8 @@ $(CHECK_TEST_TARGETS): EXTRA_ARGS=-run=none $(CHECK_TEST_TARGETS): run-tests ARGS += -tags "$(test_tags)" -SUB_MODULES = $(shell find . -type f -name 'go.mod' -print0 | xargs -0 -n1 dirname | sort) -CURRENT_DIR = $(shell pwd) -run-tests: - @echo "Starting unit tests"; \ - finalec=0; \ - for module in $(SUB_MODULES); do \ - cd ${CURRENT_DIR}/$$module; \ - echo "Running unit tests for $$(grep '^module' go.mod)"; \ - go test -mod=readonly $(ARGS) $(EXTRA_ARGS) $(TEST_PACKAGES) ./... ; \ - ec=$$?; \ - if [ "$$ec" -ne '0' ]; then finalec=$$ec; fi; \ - done; \ - exit $$finalec +run-tests: + @ARGS="$(ARGS)" TEST_PACKAGES=$(TEST_PACKAGES) EXTRA_ARGS="$(EXTRA_ARGS)" python ./scripts/go-test-all.py .PHONY: run-tests test test-all $(TEST_TARGETS) diff --git a/scripts/go-test-all.py b/scripts/go-test-all.py new file mode 100644 index 00000000000..10f3c561ff9 --- /dev/null +++ b/scripts/go-test-all.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +import os +import subprocess + +# Get the environment variables given to us by the Makefile +ARGS = os.environ.get('ARGS', '') +EXTRA_ARGS = os.environ.get('EXTRA_ARGS', '') +TEST_PACKAGES = os.environ.get('TEST_PACKAGES', '') +CURRENT_DIR = os.getcwd() + +def find_go_modules(dir='.'): + """ Find all go.mod files in the current directory and subdirectories. """ + go_mod_files = [] + for root, _, files in os.walk(dir): + if 'go.mod' in files: + go_mod_files.append(root) + return go_mod_files + +def run_tests_for_module(module): + """ Run the unit tests for the given module. """ + path = os.path.join(CURRENT_DIR, module) + os.chdir(path) + + print(f"Running unit tests for {path}") + + test_command = f'go test -mod=readonly {ARGS} {EXTRA_ARGS} {TEST_PACKAGES} ./...' + result = subprocess.run(test_command, shell=True) + return result.returncode + + +def run_tests(dir): + """ Run the unit tests for all modules in dir. """ + print("Starting unit tests") + + # Find all go.mod files and get their directory names + go_modules = find_go_modules(dir) + + exit_code = 0 + for gomod in sorted(go_modules): + exit_code = run_tests_for_module(gomod) + exit(exit_code) + +if __name__ == '__main__': + run_tests(os.getcwd()) \ No newline at end of file