Skip to content

Commit b677d7c

Browse files
committed
Upload command now works with sketches and builds from .pde files
1 parent 71b39f9 commit b677d7c

File tree

3 files changed

+162
-7
lines changed

3 files changed

+162
-7
lines changed

cli/upload/upload.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"os"
2121

22+
"github.com/arduino/arduino-cli/arduino/sketches"
2223
"github.com/arduino/arduino-cli/cli/errorcodes"
2324
"github.com/arduino/arduino-cli/cli/feedback"
2425
"github.com/arduino/arduino-cli/cli/instance"
@@ -83,6 +84,14 @@ func run(command *cobra.Command, args []string) {
8384
}
8485
sketchPath := initSketchPath(path)
8586

87+
// .pde files are still supported but deprecated, this warning urges the user to rename them
88+
if files := sketches.CheckForPdeFiles(sketchPath); len(files) > 0 {
89+
feedback.Error("Sketches with .pde extension are deprecated, please rename the following files to .ino:")
90+
for _, f := range files {
91+
feedback.Error(f)
92+
}
93+
}
94+
8695
if _, err := upload.Upload(context.Background(), &rpc.UploadReq{
8796
Instance: instance,
8897
Fqbn: fqbn,

commands/upload/upload.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
bldr "github.com/arduino/arduino-cli/arduino/builder"
2727
"github.com/arduino/arduino-cli/arduino/cores"
2828
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
29+
"github.com/arduino/arduino-cli/arduino/globals"
2930
"github.com/arduino/arduino-cli/arduino/serialutils"
3031
"github.com/arduino/arduino-cli/arduino/sketches"
3132
"github.com/arduino/arduino-cli/commands"
@@ -452,7 +453,7 @@ func determineBuildPathAndSketchName(importFile, importDir string, sketch *sketc
452453

453454
// Case 4: only sketch specified. In this case we use the generated build path
454455
// and the given sketch name.
455-
return bldr.GenBuildPath(sketch.FullPath), sketch.Name + ".ino", nil
456+
return bldr.GenBuildPath(sketch.FullPath), sketch.Name + sketch.MainFileExtension, nil
456457
}
457458

458459
func detectSketchNameFromBuildPath(buildPath *paths.Path) (string, error) {
@@ -462,11 +463,13 @@ func detectSketchNameFromBuildPath(buildPath *paths.Path) (string, error) {
462463
}
463464

464465
if absBuildPath, err := buildPath.Abs(); err == nil {
465-
candidateName := absBuildPath.Base() + ".ino"
466-
f := files.Clone()
467-
f.FilterPrefix(candidateName + ".")
468-
if f.Len() > 0 {
469-
return candidateName, nil
466+
for ext := range globals.MainFileValidExtensions {
467+
candidateName := absBuildPath.Base() + ext
468+
f := files.Clone()
469+
f.FilterPrefix(candidateName + ".")
470+
if f.Len() > 0 {
471+
return candidateName, nil
472+
}
470473
}
471474
}
472475

@@ -479,7 +482,7 @@ func detectSketchNameFromBuildPath(buildPath *paths.Path) (string, error) {
479482

480483
// Sometimes we may have particular files like:
481484
// Blink.ino.with_bootloader.bin
482-
if filepath.Ext(name) != ".ino" {
485+
if _, ok := globals.MainFileValidExtensions[filepath.Ext(name)]; !ok {
483486
// just ignore those files
484487
continue
485488
}

test/test_upload.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
# software without disclosing the source code of your own applications. To purchase
1414
# a commercial license, send an email to [email protected].
1515
import os
16+
import hashlib
17+
import tempfile
18+
import shutil
1619
from pathlib import Path
1720

1821
import pytest
@@ -195,3 +198,143 @@ def test_compile_and_upload_combo_with_custom_build_path(run_command, data_dir,
195198
assert f"Compile {sketch_name} for {board.fqbn} successful" in traces
196199
assert f"Upload {sketch_path} on {board.fqbn} started" in traces
197200
assert "Upload successful" in traces
201+
202+
203+
def test_compile_and_upload_combo_sketch_with_pde_extension(run_command, data_dir, detected_boards, wait_for_board):
204+
assert run_command("update")
205+
206+
sketch_name = "CompileAndUploadPdeSketch"
207+
sketch_path = Path(data_dir, sketch_name)
208+
209+
# Create a test sketch
210+
assert run_command(f"sketch new {sketch_path}")
211+
212+
# Renames sketch file to pde
213+
sketch_file = Path(sketch_path, f"{sketch_name}.ino").rename(sketch_path / f"{sketch_name}.pde")
214+
215+
for board in detected_boards:
216+
# Install core
217+
core = ":".join(board.fqbn.split(":")[:2])
218+
assert run_command(f"core install {core}")
219+
220+
# Build sketch and upload from folder
221+
wait_for_board()
222+
res = run_command(f"compile --clean -b {board.fqbn} -u -p {board.address} {sketch_path}")
223+
assert res.ok
224+
assert "Sketches with .pde extension are deprecated, please rename the following files to .ino" in res.stderr
225+
assert str(sketch_file) in res.stderr
226+
227+
# Build sketch and upload from file
228+
wait_for_board()
229+
res = run_command(f"compile --clean -b {board.fqbn} -u -p {board.address} {sketch_file}")
230+
assert res.ok
231+
assert "Sketches with .pde extension are deprecated, please rename the following files to .ino" in res.stderr
232+
assert str(sketch_file) in res.stderr
233+
234+
235+
def test_upload_sketch_with_pde_extension(run_command, data_dir, detected_boards, wait_for_board):
236+
assert run_command("update")
237+
238+
sketch_name = "UploadPdeSketch"
239+
sketch_path = Path(data_dir, sketch_name)
240+
241+
# Create a test sketch
242+
assert run_command(f"sketch new {sketch_path}")
243+
244+
# Renames sketch file to pde
245+
sketch_file = Path(sketch_path, f"{sketch_name}.ino").rename(sketch_path / f"{sketch_name}.pde")
246+
247+
for board in detected_boards:
248+
# Install core
249+
core = ":".join(board.fqbn.split(":")[:2])
250+
assert run_command(f"core install {core}")
251+
252+
# Compile sketch first
253+
assert run_command(f"compile --clean -b {board.fqbn} {sketch_path}")
254+
255+
# Upload from sketch folder
256+
wait_for_board()
257+
assert run_command(f"upload -b {board.fqbn} -p {board.address} {sketch_path}")
258+
259+
# Upload from sketch file
260+
wait_for_board()
261+
assert run_command(f"upload -b {board.fqbn} -p {board.address} {sketch_file}")
262+
263+
# Upload from build folder
264+
sketch_path_md5 = hashlib.md5(bytes(sketch_path)).hexdigest().upper()
265+
build_dir = Path(tempfile.gettempdir(), f"arduino-sketch-{sketch_path_md5}")
266+
wait_for_board()
267+
res = run_command(f"upload -b {board.fqbn} -p {board.address} --input-dir {build_dir}")
268+
assert (
269+
"Sketches with .pde extension are deprecated, please rename the following files to .ino:" not in res.stderr
270+
)
271+
272+
# Upload from binary file
273+
wait_for_board()
274+
# We don't need a specific file when using the --input-file flag to upload since
275+
# it's just used to calculate the directory, so it's enough to get a random file
276+
# that's inside that directory
277+
binary_file = next(build_dir.glob(f"{sketch_name}.pde.*"))
278+
res = run_command(f"upload -b {board.fqbn} -p {board.address} --input-file {binary_file}")
279+
assert (
280+
"Sketches with .pde extension are deprecated, please rename the following files to .ino:" not in res.stderr
281+
)
282+
283+
284+
def test_upload_with_input_dir_containing_multiple_binaries(run_command, data_dir, detected_boards, wait_for_board):
285+
# This tests verifies the behaviour outlined in this issue:
286+
# https://github.com/arduino/arduino-cli/issues/765#issuecomment-699678646
287+
assert run_command("update")
288+
289+
# Create a two different sketches
290+
sketch_one_name = "UploadMultipleBinariesSketchOne"
291+
sketch_one_path = Path(data_dir, sketch_one_name)
292+
assert run_command(f"sketch new {sketch_one_path}")
293+
294+
sketch_two_name = "UploadMultipleBinariesSketchTwo"
295+
sketch_two_path = Path(data_dir, sketch_two_name)
296+
assert run_command(f"sketch new {sketch_two_path}")
297+
298+
for board in detected_boards:
299+
# Install core
300+
core = ":".join(board.fqbn.split(":")[:2])
301+
assert run_command(f"core install {core}")
302+
303+
# Compile both sketches and copy binaries in the same directory same build directory
304+
assert run_command(f"compile --clean -b {board.fqbn} {sketch_one_path}")
305+
assert run_command(f"compile --clean -b {board.fqbn} {sketch_two_path}")
306+
307+
sketch_path_md5 = hashlib.md5(bytes(sketch_one_path)).hexdigest().upper()
308+
build_dir_one = Path(tempfile.gettempdir(), f"arduino-sketch-{sketch_path_md5}")
309+
310+
sketch_path_md5 = hashlib.md5(bytes(sketch_two_path)).hexdigest().upper()
311+
build_dir_two = Path(tempfile.gettempdir(), f"arduino-sketch-{sketch_path_md5}")
312+
313+
# Copy binaries to same folder
314+
binaries_dir = Path(data_dir, "build", "BuiltBinaries")
315+
shutil.copytree(build_dir_one, binaries_dir, dirs_exist_ok=True)
316+
shutil.copytree(build_dir_two, binaries_dir, dirs_exist_ok=True)
317+
318+
wait_for_board()
319+
# Verifies upload fails because multiple binaries are found
320+
res = run_command(f"upload -b {board.fqbn} -p {board.address} --input-dir {binaries_dir}")
321+
assert res.failed
322+
assert (
323+
"Error during Upload: "
324+
+ "retrieving build artifacts: "
325+
+ "autodetect build artifact: "
326+
+ "multiple build artifacts found:"
327+
in res.stderr
328+
)
329+
330+
# Copy binaries to folder with same name of a sketch
331+
binaries_dir = Path(data_dir, "build", "UploadMultipleBinariesSketchOne")
332+
shutil.copytree(build_dir_one, binaries_dir, dirs_exist_ok=True)
333+
shutil.copytree(build_dir_two, binaries_dir, dirs_exist_ok=True)
334+
335+
wait_for_board()
336+
# Verifies upload is successful using the binaries with the same name of the containing folder
337+
res = run_command(f"upload -b {board.fqbn} -p {board.address} --input-dir {binaries_dir}")
338+
assert (
339+
"Sketches with .pde extension are deprecated, please rename the following files to .ino:" not in res.stderr
340+
)

0 commit comments

Comments
 (0)