Skip to content

Commit b105981

Browse files
committed
[skip changelog] Remove duplicated code and enhance tests
1 parent fcc8482 commit b105981

File tree

3 files changed

+23
-55
lines changed

3 files changed

+23
-55
lines changed

arduino/sketches/sketches.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,10 @@ func CheckForPdeFiles(sketch *paths.Path) []*paths.Path {
138138
sketch = sketch.Parent()
139139
}
140140

141-
// It's not a problem if we can't read files right now
142-
files, _ := sketch.ReadDirRecursive()
143-
files.FilterOutDirs()
144-
res := []*paths.Path{}
145-
for _, f := range files {
146-
if f.Ext() == ".pde" {
147-
res = append(res, f)
148-
}
141+
files, err := sketch.ReadDirRecursive()
142+
if err != nil {
143+
return []*paths.Path{}
149144
}
150-
return res
145+
files.FilterSuffix(".pde")
146+
return files
151147
}

commands/sketch/archive.go

+5-32
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ import (
2323
"path/filepath"
2424
"strings"
2525

26-
"github.com/arduino/arduino-cli/arduino/globals"
26+
"github.com/arduino/arduino-cli/arduino/sketches"
2727
rpc "github.com/arduino/arduino-cli/rpc/commands"
2828
paths "github.com/arduino/go-paths-helper"
29-
"github.com/pkg/errors"
3029
)
3130

3231
// ArchiveSketch FIXMEDOC
@@ -39,39 +38,13 @@ func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchReq) (*rpc.Archive
3938
sketchPath = paths.New(".")
4039
}
4140

42-
sketchPath, err := sketchPath.Clean().Abs()
41+
sketch, err := sketches.NewSketchFromPath(sketchPath)
4342
if err != nil {
44-
return nil, fmt.Errorf("Error getting absolute sketch path %v", err)
43+
return nil, err
4544
}
4645

47-
// Make sketchPath point to the sketch folder
48-
if sketchPath.IsNotDir() {
49-
sketchPath = sketchPath.Parent()
50-
}
51-
52-
sketchName = sketchPath.Base()
53-
54-
// sketchMainFile is the path to the sketch main file
55-
var sketchMainFile *paths.Path
56-
57-
for ext := range globals.MainFileValidExtensions {
58-
candidateSketchMainFile := sketchPath.Join(sketchPath.Base() + ext)
59-
if candidateSketchMainFile.Exist() {
60-
if sketchMainFile == nil {
61-
sketchMainFile = candidateSketchMainFile
62-
} else {
63-
return nil, errors.Errorf("multiple main sketch files found (%v, %v)",
64-
sketchMainFile,
65-
candidateSketchMainFile,
66-
)
67-
}
68-
}
69-
}
70-
71-
// Checks if it's really a sketch
72-
if sketchMainFile == nil {
73-
return nil, fmt.Errorf("specified path is not a sketch: %v", sketchPath.String())
74-
}
46+
sketchPath = sketch.FullPath
47+
sketchName = sketch.Name
7548

7649
archivePath := paths.New(req.ArchivePath)
7750
if archivePath == nil {

test/test_upload.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
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
1816
import shutil
17+
import json
1918
from pathlib import Path
2019

2120
import pytest
@@ -250,7 +249,10 @@ def test_upload_sketch_with_pde_extension(run_command, data_dir, detected_boards
250249
assert run_command(f"core install {core}")
251250

252251
# Compile sketch first
253-
assert run_command(f"compile --clean -b {board.fqbn} {sketch_path}")
252+
res = run_command(f"compile --clean -b {board.fqbn} {sketch_path} --format json")
253+
assert res.ok
254+
data = json.loads(res.stdout)
255+
build_dir = Path(data["builder_result"]["build_path"])
254256

255257
# Upload from sketch folder
256258
wait_for_board()
@@ -260,9 +262,6 @@ def test_upload_sketch_with_pde_extension(run_command, data_dir, detected_boards
260262
wait_for_board()
261263
assert run_command(f"upload -b {board.fqbn} -p {board.address} {sketch_file}")
262264

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}")
266265
wait_for_board()
267266
res = run_command(f"upload -b {board.fqbn} -p {board.address} --input-dir {build_dir}")
268267
assert (
@@ -301,14 +300,14 @@ def test_upload_with_input_dir_containing_multiple_binaries(run_command, data_di
301300
assert run_command(f"core install {core}")
302301

303302
# 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}")
303+
res = run_command(f"compile --clean -b {board.fqbn} {sketch_one_path} --format json")
304+
assert res.ok
305+
data = json.loads(res.stdout)
306+
build_dir_one = Path(data["builder_result"]["build_path"])
307+
res = run_command(f"compile --clean -b {board.fqbn} {sketch_two_path} --format json")
308+
assert res.ok
309+
data = json.loads(res.stdout)
310+
build_dir_two = Path(data["builder_result"]["build_path"])
312311

313312
# Copy binaries to same folder
314313
binaries_dir = Path(data_dir, "build", "BuiltBinaries")

0 commit comments

Comments
 (0)