Skip to content

Commit

Permalink
Use platformio custom actions to run unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
costas-basdekis committed Nov 15, 2020
1 parent 400cc21 commit 6893845
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 112 deletions.
12 changes: 4 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,20 @@ tests-code-single-ci:
# TODO: How can we limit tests with ONLY_TEST with platformio?
tests-code-single-local:
@if ! test -n "$(TEST_TARGET)" ; then echo "***ERROR*** Set TEST_TARGET=<your-module> or use make tests-code-all-local" ; return 1; fi
export PATH=./buildroot/bin/:./buildroot/tests/:${PATH} \
&& export VERBOSE_PLATFORMIO=$(VERBOSE_PLATFORMIO) \
&& exec_pio_test $(TEST_TARGET)
platformio run -t marlin_$(TEST_TARGET)
.PHONY: tests-code-single-local

tests-code-single-local-docker:
@if ! test -n "$(TEST_TARGET)" ; then echo "***ERROR*** Set TEST_TARGET=<your-module> or use make tests-code-all-local-docker" ; return 1; fi
docker-compose run --rm marlin $(MAKE) tests-code-single-local TEST_TARGET=$(TEST_TARGET) VERBOSE_PLATFORMIO=$(VERBOSE_PLATFORMIO) GIT_RESET_HARD=$(GIT_RESET_HARD) ONLY_TEST="$(ONLY_TEST)"
docker-compose run --rm marlin $(MAKE) tests-code-single-local TEST_TARGET=$(TEST_TARGET) ONLY_TEST="$(ONLY_TEST)"
.PHONY: tests-code-single-local-docker

tests-code-all-local:
export PATH=./buildroot/bin/:./buildroot/tests/:${PATH} \
&& export VERBOSE_PLATFORMIO=$(VERBOSE_PLATFORMIO) \
&& exec_all_pio_tests
platformio run -t test-marlin
.PHONY: tests-code-all-local

tests-code-all-local-docker:
docker-compose run --rm marlin $(MAKE) tests-code-all-local VERBOSE_PLATFORMIO=$(VERBOSE_PLATFORMIO) GIT_RESET_HARD=$(GIT_RESET_HARD)
docker-compose run --rm marlin $(MAKE) tests-code-all-local
.PHONY: tests-code-all-local-dockerS

setup-local-docker:
Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,20 @@ Proposed patches should be submitted as a Pull Request against the ([bugfix-2.0.
- If you make significant changes, try to run tests locally if you can
- It's optional: running all the tests on Windows might take a long time, and they will run anyway on GitHub
- If you're running the tests on Linux, or on WSL with the code on a Linux volume, the speed is much faster
- You can use `make tests-all-local`/`make tests-single-local TEST_TARGET=...`
- If you prefer Docker you can use `make tests-all-local-docker`/`make tests-all-local-docker TEST_TARGET=...`
- You can use:
- All: `make tests-config-all-local`
- Only one: `make tests-config-single-local TEST_TARGET=...`
- If you prefer Docker you can use:
- All: `make tests-config-all-local-docker`
- Single: `make tests-config-all-local-docker TEST_TARGET=...`
- To run all unit test suites use:
- Directly: `platformio run -t test-marlin`
- With make: `make tests-code-all-local`
- With Docker & make: `maker tests-code-all-local-docker`
- To run a single unit test suite use:
- Directly: `platformio run -t marlin_<test-suite-name>`
- With make: `make tests-code-single-local TEST_TARGET=<test-suite-name>`
- With Docker & make: `maker tests-code-single-local-docker TEST_TARGET=<test-suite-name>`
- If possible, write some new tests. See [the test documentation](test)

### [RepRap.org Wiki Page](https://reprap.org/wiki/Marlin)
Expand Down
43 changes: 0 additions & 43 deletions buildroot/bin/exec_all_pio_tests

This file was deleted.

59 changes: 0 additions & 59 deletions buildroot/bin/exec_pio_test

This file was deleted.

107 changes: 107 additions & 0 deletions buildroot/share/PlatformIO/scripts/collect-code-tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#
# collect-code-tests.py
# Convenience script to collect all code tests
#

import os

Import("env")
Import("projenv")


os.environ['PATH'] = f"./buildroot/bin/:./buildroot/tests/:{os.environ['PATH']}"


def collect_test_suites():
"""Get all the test suites"""
from pathlib import Path
return list(Path("./test").glob("test_*/test_all.cpp"))


def register_test_suites():
"""Register all the test suites"""
test_suites = collect_test_suites()
for path in test_suites:
name = path.parent.name
configuration = get_configuration(path)
env.AddCustomTarget(
name=f"marlin_{name}",
dependencies=None,
actions=[
f"echo ====== Configuring for marlin_{name} ======",
"restore_configs",
"opt_set MOTHERBOARD BOARD_LINUX_RAMPS",
] + configuration + [
f"echo ====== Finished configuring for marlin_{name} ======",
f"platformio test -e linux_native -f {name}",
f"restore_configs",
],
title="Marlin: {}".format(name.lower().title().replace("_", " ")),
description=(
f"Rin a Marlin test suite, with the appropriate configuration, "
f"that sits in {path}"
),
)
env.AddCustomTarget(
name="test-marlin",
dependencies=None,
actions=[
f"platformio run -t marlin_{path.parent.name}"
for path in test_suites
],
title="Marlin: Test all code test suites",
description=(
f"Run all Marlin code test suites ({len(test_suites)} found), each "
f"with the appropriate configuration"
),
)


def get_configuration(path):
"""Get the configuration from a test suite"""
code_lines = path.read_text().splitlines()

# Find start index
start_indexes = [
index
for index, line in enumerate(code_lines)
if 'START_CONFIGURATION' in line
]
if not start_indexes:
raise Exception(
f"Test suite {path.name} did not contain 'START_CONFIGURATION'")
elif len(start_indexes) > 1:
raise Exception(
f"Test suite {path.name} contained too many instances of "
f"'START_CONFIGURATION'")
start_index, = start_indexes

# Find end index
end_indexes = [
index
for index, line in enumerate(code_lines)
if 'END_CONFIGURATION' in line
]
if not end_indexes:
raise Exception(
f"Test suite {path.name} did not contain 'END_CONFIGURATION'")
elif len(end_indexes) > 1:
raise Exception(
f"Test suite {path.name} contained too many instances of "
f"'END_CONFIGURATION'")
end_index, = end_indexes

# Remove whitespace, empty lines, and commented out lines
configuration_lines = code_lines[start_index + 1:end_index]
configuration_lines = [line.strip() for line in configuration_lines]
configuration_lines = list(filter(None, configuration_lines))
configuration_lines = [
line
for line in configuration_lines
if not line.startswith("//")
]

return configuration_lines


register_test_suites()
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ extra_scripts =
pre:buildroot/share/PlatformIO/scripts/common-dependencies.py
pre:buildroot/share/PlatformIO/scripts/common-cxxflags.py
post:buildroot/share/PlatformIO/scripts/common-dependencies-post.py
post:buildroot/share/PlatformIO/scripts/collect-code-tests.py
build_flags = -fmax-errors=5 -g -D__MARLIN_FIRMWARE__ -fmerge-all-constants
lib_deps =

Expand Down

0 comments on commit 6893845

Please sign in to comment.