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