Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Migrate TestCompileAndUploadCombo from test_upload.py to upload_test.go
  • Loading branch information
MatteoPologruto committed Dec 13, 2022
commit 88f854a03095c352040090a59f8f301d55decf3d
73 changes: 73 additions & 0 deletions internal/integrationtest/upload/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strconv"
"strings"
"testing"
"time"

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

func waitForBoard(t *testing.T, cli *integrationtest.ArduinoCLI) {
timeEnd := time.Now().Unix() + 10
for time.Now().Unix() < timeEnd {
stdout, _, err := cli.Run("board", "list", "--format", "json")
require.NoError(t, err)
len, err := strconv.Atoi(requirejson.Parse(t, stdout).Query("length").String())
require.NoError(t, err)
numBoards := 0
for i := 0; i < len; i++ {
numBoards, err = strconv.Atoi(requirejson.Parse(t, stdout).Query(".[] | .matching_boards | length").String())
require.NoError(t, err)
if numBoards > 0 {
break
}
}
if numBoards > 0 {
break
}
}
}

func TestUpload(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip("VMs have no serial ports")
Expand Down Expand Up @@ -177,3 +199,54 @@ func TestUploadWithInputFileFlag(t *testing.T) {
require.NoError(t, err)
}
}

func TestCompileAndUploadCombo(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip("VMs have no serial ports")
}

env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

// Init the environment explicitly
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)

// Create a test sketch
sketchName := "CompileAndUploadIntegrationTest"
sketchPath := cli.SketchbookDir().Join(sketchName)
sketchMainFile := sketchPath.Join(sketchName + ".ino")
stdout, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
require.Contains(t, string(stdout), "Sketch created in: "+sketchPath.String())

// Build sketch for each detected board
for _, board := range detectedBoards(t, cli) {
logFileName := strings.ReplaceAll(board.fqbn, ":", "-") + "-compile.log"
logFilePath := cli.SketchbookDir().Join(logFileName)

_, _, err = cli.Run("core", "install", board.core)
require.NoError(t, err)

runTest := func(s string) {
waitForBoard(t, cli)
_, _, err := cli.Run("compile", "-b", board.fqbn, "--upload", "-p", board.address, s,
"--log-format", "json", "--log-file", logFilePath.String(), "--log-level", "trace")
require.NoError(t, err)
logJson, err := logFilePath.ReadFile()
require.NoError(t, err)

// check from the logs if the bin file were uploaded on the current board
logJson = []byte("[" + strings.ReplaceAll(strings.TrimSuffix(string(logJson), "\n"), "\n", ",") + "]")
traces := requirejson.Parse(t, logJson).Query("[ .[] | select(.level==\"trace\") | .msg ]").String()
traces = strings.ReplaceAll(traces, "\\\\", "\\")
require.Contains(t, traces, "Compile "+sketchPath.String()+" for "+board.fqbn+" started")
require.Contains(t, traces, "Compile "+sketchName+" for "+board.fqbn+" successful")
require.Contains(t, traces, "Upload "+sketchPath.String()+" on "+board.fqbn+" started")
require.Contains(t, traces, "Upload successful")
}

runTest(sketchPath.String())
runTest(sketchMainFile.String())
}
}
40 changes: 0 additions & 40 deletions test/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,46 +42,6 @@ def test_upload_after_attach(run_command, data_dir, detected_boards):
assert run_command(["upload", sketch_path])


def test_compile_and_upload_combo(run_command, data_dir, detected_boards, wait_for_board):
# Init the environment explicitly
run_command(["core", "update-index"])

# Install required core(s)
run_command(["core", "install", "arduino:avr@1.8.3"])
run_command(["core", "install", "arduino:samd@1.8.6"])

# Create a test sketch
sketch_name = "CompileAndUploadIntegrationTest"
sketch_path = os.path.join(data_dir, sketch_name)
sketch_main_file = os.path.join(sketch_path, sketch_name + ".ino")
result = run_command(["sketch", "new", sketch_path])
assert result.ok
assert "Sketch created in: {}".format(sketch_path) in result.stdout

# Build sketch for each detected board
for board in detected_boards:
log_file_name = "{fqbn}-compile.log".format(fqbn=board.fqbn.replace(":", "-"))
log_file_path = os.path.join(data_dir, log_file_name)
command_log_flags = ["--log-format", "json", "--log-file", log_file_path, "--log-level", "trace"]

def run_test(s):
wait_for_board()
result = run_command(["compile", "-b", board.fqbn, "--upload", "-p", board.address, s] + command_log_flags)
print(result.stderr)
assert result.ok

# check from the logs if the bin file were uploaded on the current board
log_json = open(log_file_path, "r")
traces = parse_json_traces(log_json.readlines())
assert f"Compile {sketch_path} for {board.fqbn} started" in traces
assert f"Compile {sketch_name} for {board.fqbn} successful" in traces
assert f"Upload {sketch_path} on {board.fqbn} started" in traces
assert "Upload successful" in traces

run_test(sketch_path)
run_test(sketch_main_file)


def test_compile_and_upload_combo_with_custom_build_path(run_command, data_dir, detected_boards, wait_for_board):
# Init the environment explicitly
run_command(["core", "update-index"])
Expand Down