Skip to content

Commit 84058f4

Browse files
committed
make tests independent from each other
1 parent d7643ae commit 84058f4

9 files changed

+251
-103
lines changed

Diff for: test/.flake8

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
max-line-length = 120

Diff for: test/README.md

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
1-
## Integration tests
1+
# Integration tests
22

33
This dir contains integration tests, the aim is to test the Command Line Interface and its output
44
from a pure user point of view.
55

6-
### Installation
6+
## Installation
77

8-
cd test
9-
virtualenv --python=python3 venv
10-
source venv/bin/activate
11-
pip install -r requirements.txt
12-
13-
### Running tests
8+
See also [Contributing][0].
149

15-
pytest
10+
```shell
11+
cd test
12+
virtualenv --python=python3 venv
13+
source venv/bin/activate
14+
pip install -r requirements.txt
15+
```
16+
17+
## Running tests
18+
19+
To run all the tests:
20+
21+
```shell
22+
pytest
23+
```
24+
25+
To run specific modules:
26+
27+
```shell
28+
pytest test_lib.py
29+
```
30+
31+
To run very specific test functions:
32+
33+
```shell
34+
pytest test_lib.py::test_list
35+
```
36+
37+
[0]: ../CONTRIBUTING.md

Diff for: test/__init__.py

Whitespace-only changes.

Diff for: test/common.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This file is part of arduino-cli.
2+
3+
# Copyright 2019 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-cli.
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 modify or
12+
# otherwise use the software for commercial activities involving the Arduino
13+
# software without disclosing the source code of your own applications. To purchase
14+
# a commercial license, send an email to [email protected].
15+
import os
16+
17+
18+
def running_on_ci():
19+
"""
20+
Returns whether the program is running on a CI environment
21+
"""
22+
val = os.getenv("APPVEYOR") or os.getenv("DRONE")
23+
return val is not None

Diff for: test/conftest.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,39 @@
1818
from invoke import run
1919

2020

21-
@pytest.fixture(scope="session")
21+
@pytest.fixture(scope="function")
2222
def data_dir(tmpdir_factory):
2323
"""
2424
A tmp folder will be created before running
25-
the tests and deleted at the end.
25+
each test and deleted at the end, this way all the
26+
tests work in isolation.
2627
"""
27-
return str(tmpdir_factory.mktemp('ArduinoTest'))
28+
return str(tmpdir_factory.mktemp("ArduinoTest"))
2829

2930

3031
@pytest.fixture(scope="session")
31-
def run_command(data_dir):
32+
def downloads_dir(tmpdir_factory):
33+
"""
34+
To save time and bandwidth, all the tests will access
35+
the same download cache folder.
36+
"""
37+
return str(tmpdir_factory.mktemp("ArduinoTest"))
38+
39+
40+
@pytest.fixture(scope="function")
41+
def run_command(data_dir, downloads_dir):
3242
"""
3343
Provide a wrapper around invoke's `run` API so that every test
3444
will work in the same temporary folder.
3545
3646
Useful reference:
3747
http://docs.pyinvoke.org/en/1.2/api/runners.html#invoke.runners.Result
3848
"""
39-
cli_path = os.path.join(pytest.config.rootdir, '..', 'arduino-cli')
49+
cli_path = os.path.join(pytest.config.rootdir, "..", "arduino-cli")
4050
env = {
41-
"ARDUINO_DATA_DIR": data_dir,
42-
"ARDUINO_DOWNLOADS_DIR": data_dir,
43-
"ARDUINO_SKETCHBOOK_DIR": data_dir
51+
"ARDUINO_DATA_DIR": data_dir,
52+
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
53+
"ARDUINO_SKETCHBOOK_DIR": data_dir,
4454
}
4555

4656
def _run(cmd_string):

Diff for: test/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pylint==2.3.1
1515
pyparsing==2.4.0
1616
pytest==5.0.1
1717
semver==2.8.1
18+
simplejson==3.16.0
1819
six==1.12.0
1920
typed-ast==1.4.0
2021
wcwidth==0.1.7

Diff for: test/test_board.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# This file is part of arduino-cli.
2+
3+
# Copyright 2019 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-cli.
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 modify or
12+
# otherwise use the software for commercial activities involving the Arduino
13+
# software without disclosing the source code of your own applications. To purchase
14+
# a commercial license, send an email to [email protected].
15+
import pytest
16+
import simplejson as json
17+
18+
from .common import running_on_ci
19+
20+
21+
@pytest.mark.skipif(running_on_ci(), reason="VMs have no serial ports")
22+
def test_board_list(run_command):
23+
result = run_command("core update-index")
24+
assert result.ok
25+
result = run_command("board list --format json")
26+
assert result.ok
27+
# check is a valid json and contains a list of ports
28+
ports = json.loads(result.stdout).get("ports")
29+
assert isinstance(ports, list)
30+
for port in ports:
31+
assert "protocol" in port
32+
assert "protocol_label" in port
33+
34+
35+
@pytest.mark.skipif(running_on_ci(), reason="VMs have no serial ports")
36+
def test_board_listall(run_command):
37+
assert run_command("core update-index")
38+
result = run_command("board listall")
39+
print(result.stderr, result.stdout)
40+
assert result.ok
41+
assert ["Board", "Name", "FQBN"] == result.stdout.splitlines()[0].strip().split()

Diff for: test/test_lib.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# This file is part of arduino-cli.
2+
3+
# Copyright 2019 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-cli.
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 modify or
12+
# otherwise use the software for commercial activities involving the Arduino
13+
# software without disclosing the source code of your own applications. To purchase
14+
# a commercial license, send an email to [email protected].
15+
import pytest
16+
import simplejson as json
17+
18+
19+
def test_list(run_command):
20+
# Init the environment explicitly
21+
assert run_command("core update-index")
22+
23+
# When ouput is empty, nothing is printed out, no matter the output format
24+
result = run_command("lib list")
25+
assert result.ok
26+
assert "" == result.stderr
27+
assert "" == result.stdout
28+
result = run_command("lib list --format json")
29+
assert result.ok
30+
assert "" == result.stderr
31+
assert "" == result.stdout
32+
33+
# Install something we can list at a version older than latest
34+
result = run_command("lib install [email protected]")
35+
assert result.ok
36+
37+
# Look at the plain text output
38+
result = run_command("lib list")
39+
assert result.ok
40+
assert "" == result.stderr
41+
lines = result.stdout.strip().splitlines()
42+
assert 2 == len(lines)
43+
toks = lines[1].split("\t")
44+
# be sure line contain the current version AND the available version
45+
assert "" != toks[1]
46+
assert "" != toks[2]
47+
48+
# Look at the JSON output
49+
result = run_command("lib list --format json")
50+
assert result.ok
51+
assert "" == result.stderr
52+
data = json.loads(result.stdout)
53+
assert 1 == len(data)
54+
# be sure data contains the available version
55+
assert "" != data[0]["release"]["version"]
56+
57+
58+
def test_install(run_command):
59+
libs = ['"AzureIoTProtocol_MQTT"', '"CMMC MQTT Connector"', '"WiFiNINA"']
60+
# Should be safe to run install multiple times
61+
assert run_command("lib install {}".format(" ".join(libs)))
62+
assert run_command("lib install {}".format(" ".join(libs)))
63+
64+
65+
def test_update_index(run_command):
66+
result = run_command("lib update-index")
67+
assert result.ok
68+
assert (
69+
"Updating index: library_index.json downloaded"
70+
== result.stdout.splitlines()[-1].strip()
71+
)
72+
73+
74+
def test_remove(run_command):
75+
libs = ['"AzureIoTProtocol_MQTT"', '"CMMC MQTT Connector"', '"WiFiNINA"']
76+
assert run_command("lib install {}".format(" ".join(libs)))
77+
78+
result = run_command("lib uninstall {}".format(" ".join(libs)))
79+
assert result.ok
80+
81+
82+
@pytest.mark.slow
83+
def test_search(run_command):
84+
result = run_command("lib search")
85+
assert result.ok
86+
out_lines = result.stdout.splitlines()
87+
# Create an array with just the name of the vars
88+
libs = []
89+
for line in out_lines:
90+
if line.startswith("Name: "):
91+
start = line.find('"') + 1
92+
libs.append(line[start:-1])
93+
94+
expected = {"WiFi101", "WiFi101OTA", "Firebase Arduino based on WiFi101"}
95+
assert expected == {lib for lib in libs if "WiFi101" in lib}
96+
97+
result = run_command("lib search --format json")
98+
assert result.ok
99+
libs_json = json.loads(result.stdout)
100+
assert len(libs) == len(libs_json.get("libraries"))
101+
102+
result = run_command("lib search")
103+
assert result.ok
104+
105+
# Search for a specific target
106+
result = run_command("lib search ArduinoJson --format json")
107+
assert result.ok
108+
libs_json = json.loads(result.stdout)
109+
assert 1 == len(libs_json.get("libraries"))

0 commit comments

Comments
 (0)