|
| 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 | + |
| 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 | + |
| 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