-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathconftest.py
127 lines (103 loc) · 4.08 KB
/
conftest.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# This file is part of arduino-cli.
#
# Copyright 2019 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-cli.
# 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 json
import os
import pytest
from .common import build_runner, Board
@pytest.fixture(scope="function")
def data_dir(tmpdir_factory):
"""
A tmp folder will be created before running
each test and deleted at the end, this way all the
tests work in isolation.
"""
return str(tmpdir_factory.mktemp("ArduinoTest"))
@pytest.fixture(scope="session")
def downloads_dir(tmpdir_factory):
"""
To save time and bandwidth, all the tests will access
the same download cache folder.
"""
return str(tmpdir_factory.mktemp("ArduinoTest"))
@pytest.fixture(scope="function")
def working_dir(tmpdir_factory):
"""
A tmp folder to work in
will be created before running each test and deleted
at the end, this way all the tests work in isolation.
"""
return str(tmpdir_factory.mktemp("ArduinoTestWork"))
@pytest.fixture(scope="function")
def run_command(pytestconfig, data_dir, downloads_dir, working_dir):
"""
Run the ``arduino-cli`` command to perform a the real test on the CLI.
"""
cli_path = os.path.join(pytestconfig.rootdir, "..", "arduino-cli")
env = {
"ARDUINO_DATA_DIR": data_dir,
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
"ARDUINO_SKETCHBOOK_DIR": data_dir,
}
return build_runner(cli_path, env, working_dir)
@pytest.fixture(scope="session")
def _run_session_command(pytestconfig, tmpdir_factory, downloads_dir):
"""
Run the ``arduino-cli`` command to collect general metadata and store it in
a `session` scope for the tests.
"""
cli_path = os.path.join(pytestconfig.rootdir, "..", "arduino-cli")
data_dir = tmpdir_factory.mktemp("SessionDataDir")
env = {
"ARDUINO_DATA_DIR": data_dir,
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
"ARDUINO_SKETCHBOOK_DIR": data_dir,
}
# it looks like the pyinvoke library has a few problems in dealing with the path
# object, so to avoid this issue we can convert them to str.
# Take a look at https://github.com/pyinvoke/invoke/issues/454 for more details.
working_dir = str(tmpdir_factory.mktemp("SessionTestWork"))
return build_runner(cli_path, env, working_dir)
@pytest.fixture(scope="session")
def detected_boards(_run_session_command):
"""This fixture provides a list of all the boards attached to the host.
This fixture will parse the JSON output of the ``arduino-cli board list --format json``
command to extract all the connected boards data.
:returns a list ``Board`` objects.
"""
result = _run_session_command("core update-index")
assert result.ok
result = _run_session_command("board list --format json")
assert result.ok
detected_boards = []
ports = json.loads(result.stdout)
assert isinstance(ports, list)
for port in ports:
boards = port.get('boards', [])
assert isinstance(boards, list)
for board in boards:
fqbn = board.get('FQBN')
package, architecture, _id = fqbn.split(":")
detected_boards.append(
Board(
address=port.get('address'),
fqbn=fqbn,
package=package,
architecture=architecture,
id=_id,
core="{}:{}".format(package, architecture)
)
)
assert len(detected_boards) >= 1, "There are no boards available for testing"
return detected_boards