|
| 1 | +# Source: |
| 2 | +# https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/test-integration/test_all.py |
| 3 | +# Copyright 2021 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +# |
| 5 | +# This software is released under the GNU General Public License version 3, |
| 6 | +# which covers the main part of Arduino Lint. |
| 7 | +# The terms of this license can be found at: |
| 8 | +# https: // www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +# |
| 10 | +# You can be released from the requirements of the above licenses by purchasing |
| 11 | +# a commercial license. Buying such a license is mandatory if you want to |
| 12 | +# modify or otherwise use the software for commercial activities involving the |
| 13 | +# Arduino software without disclosing the source code of your own applications. |
| 14 | +# To purchase a commercial license, send an email to [email protected]. |
| 15 | +import pathlib |
| 16 | +import platform |
| 17 | +import typing |
| 18 | + |
| 19 | +import invoke.context |
| 20 | +import pytest |
| 21 | + |
| 22 | +test_data_path = pathlib.Path(__file__).resolve().parent.joinpath("testdata") |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(scope="function") |
| 26 | +def run_command( |
| 27 | + pytestconfig, working_dir |
| 28 | +) -> typing.Callable[..., invoke.runners.Result]: |
| 29 | + """Provide a wrapper around invoke's `run` API so that every test will work in the same temporary folder. |
| 30 | +
|
| 31 | + Useful reference: |
| 32 | + http://docs.pyinvoke.org/en/1.4/api/runners.html#invoke.runners.Result |
| 33 | + """ |
| 34 | + |
| 35 | + arduino_lint_path = pathlib.Path(pytestconfig.rootdir).parent / "arduino-lint" |
| 36 | + |
| 37 | + def _run( |
| 38 | + cmd: list, |
| 39 | + custom_working_dir: typing.Optional[str] = None, |
| 40 | + custom_env: typing.Optional[dict] = None, |
| 41 | + ) -> invoke.runners.Result: |
| 42 | + if cmd is None: |
| 43 | + cmd = [] |
| 44 | + if not custom_working_dir: |
| 45 | + custom_working_dir = working_dir |
| 46 | + quoted_cmd = [] |
| 47 | + for token in cmd: |
| 48 | + quoted_cmd.append(f'"{token}"') |
| 49 | + cli_full_line = '"{}" {}'.format(arduino_lint_path, " ".join(quoted_cmd)) |
| 50 | + run_context = invoke.context.Context() |
| 51 | + # It might happen that we need to change directories between drives on Windows, |
| 52 | + # in that case the "/d" flag must be used otherwise directory wouldn't change |
| 53 | + cd_command = "cd" |
| 54 | + if platform.system() == "Windows": |
| 55 | + cd_command += " /d" |
| 56 | + # Context.cd() is not used since it doesn't work correctly on Windows. |
| 57 | + # It escapes spaces in the path using "\ " but it doesn't always work, |
| 58 | + # wrapping the path in quotation marks is the safest approach |
| 59 | + with run_context.prefix(f'{cd_command} "{custom_working_dir}"'): |
| 60 | + return run_context.run( |
| 61 | + command=cli_full_line, |
| 62 | + echo=False, |
| 63 | + hide=True, |
| 64 | + warn=True, |
| 65 | + env=custom_env, |
| 66 | + encoding="utf-8", |
| 67 | + ) |
| 68 | + |
| 69 | + return _run |
| 70 | + |
| 71 | + |
| 72 | +@pytest.fixture(scope="function") |
| 73 | +def working_dir(tmpdir_factory) -> str: |
| 74 | + """Create a temporary folder for the test to run in. It will be created before running each test and deleted at the |
| 75 | + end. This way all the tests work in isolation. |
| 76 | + """ |
| 77 | + work_dir = tmpdir_factory.mktemp(basename="IntegrationTestWorkingDir") |
| 78 | + yield str(work_dir) |
0 commit comments