Skip to content

Commit 363fcc8

Browse files
Migrate TestCompileAndUploadCombo from test_upload.py to upload_test.go
1 parent 41b1e62 commit 363fcc8

File tree

2 files changed

+73
-40
lines changed

2 files changed

+73
-40
lines changed

internal/integrationtest/upload/upload_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strconv"
2222
"strings"
2323
"testing"
24+
"time"
2425

2526
"github.com/arduino/arduino-cli/internal/integrationtest"
2627
"github.com/stretchr/testify/require"
@@ -62,6 +63,27 @@ func detectedBoards(t *testing.T, cli *integrationtest.ArduinoCLI) []board {
6263
return boards
6364
}
6465

66+
func waitForBoard(t *testing.T, cli *integrationtest.ArduinoCLI) {
67+
timeEnd := time.Now().Unix() + 10
68+
for time.Now().Unix() < timeEnd {
69+
stdout, _, err := cli.Run("board", "list", "--format", "json")
70+
require.NoError(t, err)
71+
len, err := strconv.Atoi(requirejson.Parse(t, stdout).Query("length").String())
72+
require.NoError(t, err)
73+
numBoards := 0
74+
for i := 0; i < len; i++ {
75+
numBoards, err = strconv.Atoi(requirejson.Parse(t, stdout).Query(".[] | .matching_boards | length").String())
76+
require.NoError(t, err)
77+
if numBoards > 0 {
78+
break
79+
}
80+
}
81+
if numBoards > 0 {
82+
break
83+
}
84+
}
85+
}
86+
6587
func TestUpload(t *testing.T) {
6688
if os.Getenv("CI") != "" {
6789
t.Skip("VMs have no serial ports")
@@ -177,3 +199,54 @@ func TestUploadWithInputFileFlag(t *testing.T) {
177199
require.NoError(t, err)
178200
}
179201
}
202+
203+
func TestCompileAndUploadCombo(t *testing.T) {
204+
if os.Getenv("CI") != "" {
205+
t.Skip("VMs have no serial ports")
206+
}
207+
208+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
209+
defer env.CleanUp()
210+
211+
// Init the environment explicitly
212+
_, _, err := cli.Run("core", "update-index")
213+
require.NoError(t, err)
214+
215+
// Create a test sketch
216+
sketchName := "CompileAndUploadIntegrationTest"
217+
sketchPath := cli.SketchbookDir().Join(sketchName)
218+
sketchMainFile := sketchPath.Join(sketchName + ".ino")
219+
stdout, _, err := cli.Run("sketch", "new", sketchPath.String())
220+
require.NoError(t, err)
221+
require.Contains(t, string(stdout), "Sketch created in: "+sketchPath.String())
222+
223+
// Build sketch for each detected board
224+
for _, board := range detectedBoards(t, cli) {
225+
logFileName := strings.ReplaceAll(board.fqbn, ":", "-") + "-compile.log"
226+
logFilePath := cli.SketchbookDir().Join(logFileName)
227+
228+
_, _, err = cli.Run("core", "install", board.core)
229+
require.NoError(t, err)
230+
231+
runTest := func(s string) {
232+
waitForBoard(t, cli)
233+
_, _, err := cli.Run("compile", "-b", board.fqbn, "--upload", "-p", board.address, s,
234+
"--log-format", "json", "--log-file", logFilePath.String(), "--log-level", "trace")
235+
require.NoError(t, err)
236+
logJson, err := logFilePath.ReadFile()
237+
require.NoError(t, err)
238+
239+
// check from the logs if the bin file were uploaded on the current board
240+
logJson = []byte("[" + strings.ReplaceAll(strings.TrimSuffix(string(logJson), "\n"), "\n", ",") + "]")
241+
traces := requirejson.Parse(t, logJson).Query("[ .[] | select(.level==\"trace\") | .msg ]").String()
242+
traces = strings.ReplaceAll(traces, "\\\\", "\\")
243+
require.Contains(t, traces, "Compile "+sketchPath.String()+" for "+board.fqbn+" started")
244+
require.Contains(t, traces, "Compile "+sketchName+" for "+board.fqbn+" successful")
245+
require.Contains(t, traces, "Upload "+sketchPath.String()+" on "+board.fqbn+" started")
246+
require.Contains(t, traces, "Upload successful")
247+
}
248+
249+
runTest(sketchPath.String())
250+
runTest(sketchMainFile.String())
251+
}
252+
}

test/test_upload.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -42,46 +42,6 @@ def test_upload_after_attach(run_command, data_dir, detected_boards):
4242
assert run_command(["upload", sketch_path])
4343

4444

45-
def test_compile_and_upload_combo(run_command, data_dir, detected_boards, wait_for_board):
46-
# Init the environment explicitly
47-
run_command(["core", "update-index"])
48-
49-
# Install required core(s)
50-
run_command(["core", "install", "arduino:avr@1.8.3"])
51-
run_command(["core", "install", "arduino:samd@1.8.6"])
52-
53-
# Create a test sketch
54-
sketch_name = "CompileAndUploadIntegrationTest"
55-
sketch_path = os.path.join(data_dir, sketch_name)
56-
sketch_main_file = os.path.join(sketch_path, sketch_name + ".ino")
57-
result = run_command(["sketch", "new", sketch_path])
58-
assert result.ok
59-
assert "Sketch created in: {}".format(sketch_path) in result.stdout
60-
61-
# Build sketch for each detected board
62-
for board in detected_boards:
63-
log_file_name = "{fqbn}-compile.log".format(fqbn=board.fqbn.replace(":", "-"))
64-
log_file_path = os.path.join(data_dir, log_file_name)
65-
command_log_flags = ["--log-format", "json", "--log-file", log_file_path, "--log-level", "trace"]
66-
67-
def run_test(s):
68-
wait_for_board()
69-
result = run_command(["compile", "-b", board.fqbn, "--upload", "-p", board.address, s] + command_log_flags)
70-
print(result.stderr)
71-
assert result.ok
72-
73-
# check from the logs if the bin file were uploaded on the current board
74-
log_json = open(log_file_path, "r")
75-
traces = parse_json_traces(log_json.readlines())
76-
assert f"Compile {sketch_path} for {board.fqbn} started" in traces
77-
assert f"Compile {sketch_name} for {board.fqbn} successful" in traces
78-
assert f"Upload {sketch_path} on {board.fqbn} started" in traces
79-
assert "Upload successful" in traces
80-
81-
run_test(sketch_path)
82-
run_test(sketch_main_file)
83-
84-
8545
def test_compile_and_upload_combo_with_custom_build_path(run_command, data_dir, detected_boards, wait_for_board):
8646
# Init the environment explicitly
8747
run_command(["core", "update-index"])

0 commit comments

Comments
 (0)