Skip to content

Commit 1c65ebf

Browse files
Added mock-upload tests (#1358)
Co-authored-by: Silvano Cerza <[email protected]>
1 parent e7d4eaa commit 1c65ebf

File tree

2 files changed

+88
-9
lines changed

2 files changed

+88
-9
lines changed

Diff for: test/conftest.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,7 @@
2929
from .common import Board
3030

3131

32-
@pytest.fixture(scope="function")
33-
def data_dir(tmpdir_factory):
34-
"""
35-
A tmp folder will be created before running
36-
each test and deleted at the end, this way all the
37-
tests work in isolation.
38-
"""
39-
32+
def create_data_dir(tmpdir_factory):
4033
# it seems that paths generated by pytest's tmpdir_factory are too
4134
# long and may lead to arduino-cli compile failures due to the
4235
# combination of (some or all of) the following reasons:
@@ -65,6 +58,21 @@ def data_dir(tmpdir_factory):
6558
shutil.rmtree(data, ignore_errors=True)
6659

6760

61+
@pytest.fixture(scope="session")
62+
def session_data_dir(tmpdir_factory):
63+
yield from create_data_dir(tmpdir_factory)
64+
65+
66+
@pytest.fixture(scope="function")
67+
def data_dir(tmpdir_factory):
68+
"""
69+
A tmp folder will be created before running
70+
each test and deleted at the end, this way all the
71+
tests work in isolation.
72+
"""
73+
yield from create_data_dir(tmpdir_factory)
74+
75+
6876
@pytest.fixture(scope="session")
6977
def downloads_dir(tmpdir_factory, worker_id):
7078
"""
@@ -114,7 +122,7 @@ def run_command(pytestconfig, data_dir, downloads_dir, working_dir):
114122
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
115123
"ARDUINO_SKETCHBOOK_DIR": data_dir,
116124
}
117-
(Path(data_dir) / "packages").mkdir()
125+
(Path(data_dir) / "packages").mkdir(exist_ok=True)
118126

119127
def _run(cmd_string, custom_working_dir=None, custom_env=None):
120128

Diff for: test/test_upload_mock.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# This file is part of arduino-cli.
2+
#
3+
# Copyright 2020 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+
16+
import tempfile
17+
import hashlib
18+
import pytest
19+
from pathlib import Path
20+
21+
22+
def generate_build_dir(sketch_path):
23+
sketch_path_md5 = hashlib.md5(bytes(sketch_path)).hexdigest().upper()
24+
build_dir = Path(tempfile.gettempdir(), f"arduino-sketch-{sketch_path_md5}")
25+
build_dir.mkdir(parents=True, exist_ok=True)
26+
return build_dir.resolve()
27+
28+
29+
testdata = [
30+
(
31+
"arduino:avr:uno",
32+
"arduino:[email protected]",
33+
"/dev/ttyACM0",
34+
'"{data_dir}/packages/arduino/tools/avrdude/6.3.0-arduino17/bin/avrdude" '
35+
+ '"-C{data_dir}/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf" '
36+
+ '-v -V -patmega328p -carduino "-P{upload_port}" -b115200 -D "-Uflash:w:{build_dir}/{sketch_name}.ino.hex:i"',
37+
),
38+
(
39+
"arduino:avr:leonardo",
40+
"arduino:[email protected]",
41+
"/dev/ttyACM999",
42+
'"{data_dir}/packages/arduino/tools/avrdude/6.3.0-arduino17/bin/avrdude" '
43+
+ '"-C{data_dir}/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf" '
44+
+ '-v -V -patmega32u4 -cavr109 "-P{upload_port}0" -b57600 -D "-Uflash:w:{build_dir}/{sketch_name}.ino.hex:i"',
45+
),
46+
]
47+
48+
49+
@pytest.mark.parametrize("fqbn, core, upload_port, expected_output", testdata)
50+
def test_upload_sketch(run_command, session_data_dir, downloads_dir, fqbn, core, upload_port, expected_output):
51+
env = {
52+
"ARDUINO_DATA_DIR": session_data_dir,
53+
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
54+
"ARDUINO_SKETCHBOOK_DIR": session_data_dir,
55+
}
56+
assert run_command(f"core install {core}", custom_env=env)
57+
58+
# Create a sketch
59+
sketch_name = "TestSketchForUpload"
60+
sketch_path = Path(session_data_dir, sketch_name)
61+
assert run_command(f'sketch new "{sketch_path}"', custom_env=env)
62+
63+
# Fake compilation, we just need the folder to exist
64+
build_dir = generate_build_dir(sketch_path)
65+
66+
res = run_command(f'upload -p {upload_port} -b {fqbn} "{sketch_path}" --dry-run -v', custom_env=env)
67+
assert res.ok
68+
69+
assert expected_output.format(
70+
data_dir=session_data_dir, upload_port=upload_port, build_dir=build_dir, sketch_name=sketch_name
71+
).replace("\\", "/") in res.stdout.replace("\\", "/")

0 commit comments

Comments
 (0)