Skip to content

Commit

Permalink
Add script to run tests for all modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitrisJim committed Sep 26, 2023
1 parent c4d6fe8 commit 3d72f7a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
15 changes: 2 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
45 changes: 45 additions & 0 deletions scripts/go-test-all.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit 3d72f7a

Please sign in to comment.