|
| 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 | +import tempfile |
| 16 | +import hashlib |
| 17 | +import shutil |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | + |
| 21 | +def generate_build_dir(sketch_path): |
| 22 | + sketch_path_md5 = hashlib.md5(bytes(sketch_path)).hexdigest().upper() |
| 23 | + build_dir = Path(tempfile.gettempdir(), f"arduino-sketch-{sketch_path_md5}") |
| 24 | + build_dir.mkdir(parents=True, exist_ok=True) |
| 25 | + return build_dir |
| 26 | + |
| 27 | + |
| 28 | +def install_fake_platform(data_dir, platform_name): |
| 29 | + platform_install_dir = Path(data_dir, "packages", "arduino", "hardware", platform_name, "4.2.0") |
| 30 | + # Install fake platform |
| 31 | + shutil.copytree( |
| 32 | + Path(__file__).parent / "testdata" / platform_name, platform_install_dir, dirs_exist_ok=True, |
| 33 | + ) |
| 34 | + return platform_install_dir |
| 35 | + |
| 36 | + |
| 37 | +def install_fake_avrdude(data_dir): |
| 38 | + avrdude_install_dir = Path(data_dir, "packages", "arduino", "tools", "avrdude", "1.0.0") |
| 39 | + (avrdude_install_dir / "bin").mkdir(parents=True, exist_ok=True) |
| 40 | + (avrdude_install_dir / "res").mkdir(parents=True, exist_ok=True) |
| 41 | + (avrdude_install_dir / "bin" / "avrdude").touch() |
| 42 | + (avrdude_install_dir / "res" / "avrdude.conf").touch() |
| 43 | + return avrdude_install_dir |
| 44 | + |
| 45 | + |
| 46 | +def test_upload_sketch_without_pluggable_discovery(run_command, data_dir): |
| 47 | + assert run_command("update") |
| 48 | + |
| 49 | + test_platform_arch = "platform_without_pluggable_discovery" |
| 50 | + install_fake_platform(data_dir, test_platform_arch) |
| 51 | + avrdude_install_dir = install_fake_avrdude(data_dir) |
| 52 | + avrdude_binary_path = avrdude_install_dir / "bin" / "avrdude" |
| 53 | + avrdude_config_path = avrdude_install_dir / "etc" / "avrdude.conf" |
| 54 | + |
| 55 | + # Create a sketch |
| 56 | + sketch_name = "UploadSketchWithoutPluggableDiscovery" |
| 57 | + sketch_path = Path(data_dir, sketch_name) |
| 58 | + assert run_command(f"sketch new {sketch_path}") |
| 59 | + |
| 60 | + # Fake compilation, we just need the folder to exist |
| 61 | + build_dir = generate_build_dir(sketch_path) |
| 62 | + |
| 63 | + fqbn = "arduino:platform_without_pluggable_discovery:nessuno" |
| 64 | + |
| 65 | + upload_port = "/dev/ttyACM1" |
| 66 | + res = run_command(f"upload -p {upload_port} -b {fqbn} {sketch_path} --dry-run -v --log-level info") |
| 67 | + assert res.ok |
| 68 | + |
| 69 | + compiled_binary_path = build_dir / f"{sketch_name}.ino.hex" |
| 70 | + build_mcu = "atmega328p" |
| 71 | + upload_protocol = "arduino" |
| 72 | + upload_speed = "115200" |
| 73 | + |
| 74 | + expected_output = ( |
| 75 | + f'"{avrdude_binary_path}" ' |
| 76 | + + f'"-C{avrdude_config_path}" ' |
| 77 | + + "-v -V " |
| 78 | + + f"-p{build_mcu} " |
| 79 | + + f"-c{upload_protocol} " |
| 80 | + + f'"-P{upload_port}" ' |
| 81 | + + f"-b{upload_speed} " |
| 82 | + + f'-D "-Uflash:w:{compiled_binary_path}:i"' |
| 83 | + ) |
| 84 | + |
| 85 | + assert expected_output in res.stdout |
| 86 | + |
| 87 | + # Upload to board that uses a 1200bps touch |
| 88 | + fqbn = "arduino:platform_without_pluggable_discovery:altra" |
| 89 | + upload_port = "/dev/ttyACM999" |
| 90 | + res = run_command(f"upload -p {upload_port} -b {fqbn} {sketch_path} --dry-run -v --log-level info") |
| 91 | + assert res.ok |
| 92 | + |
| 93 | + # Port changes after touch |
| 94 | + upload_port = "/dev/ttyACM9990" |
| 95 | + expected_output = ( |
| 96 | + f'"{avrdude_binary_path}" ' |
| 97 | + + f'"-C{avrdude_config_path}" ' |
| 98 | + + "-v -V " |
| 99 | + + f"-p{build_mcu} " |
| 100 | + + f"-c{upload_protocol} " |
| 101 | + + f'"-P{upload_port}" ' |
| 102 | + + f"-b{upload_speed} " |
| 103 | + + f'-D "-Uflash:w:{compiled_binary_path}:i"' |
| 104 | + ) |
| 105 | + |
| 106 | + assert expected_output in res.stdout |
0 commit comments