From 01e95f09c4b1b80a5ba8ac9893e344415f0549b4 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 19 Oct 2023 17:22:40 +0200 Subject: [PATCH] Added first mocked integration test --- internal/integrationtest/arduino-cli.go | 27 ++++++++++++++ internal/integrationtest/board/board_test.go | 38 ++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/internal/integrationtest/arduino-cli.go b/internal/integrationtest/arduino-cli.go index 45620ee4c71..dd1855577aa 100644 --- a/internal/integrationtest/arduino-cli.go +++ b/internal/integrationtest/arduino-cli.go @@ -201,6 +201,33 @@ func (cli *ArduinoCLI) convertEnvForExecutils(env map[string]string) []string { return envVars } +// InstallMockedSerialDiscovery will replace the already installed serial-discovery +// with a mocked one. +func (cli *ArduinoCLI) InstallMockedSerialDiscovery(t *testing.T) { + // Build mocked serial-discovery + mockDir := FindRepositoryRootPath(t).Join("internal", "integrationtest", "mock_serial_discovery") + gobuild, err := executils.NewProcess(nil, "go", "build") + require.NoError(t, err) + gobuild.SetDirFromPath(mockDir) + require.NoError(t, gobuild.Run(), "Building mocked serial-discovery") + + // Install it replacing the current serial discovery + mockBin := mockDir.Join("mock_serial_discovery") + dataDir := cli.DataDir() + require.NotNil(t, dataDir, "data dir missing") + serialDiscoveries, err := dataDir.Join("packages", "builtin", "tools", "serial-discovery").ReadDirRecursiveFiltered( + nil, paths.AndFilter( + paths.FilterNames("serial-discovery"), + paths.FilterOutDirectories(), + ), + ) + require.NoError(t, err, "scanning data dir for serial-discoveries") + require.NotEmpty(t, serialDiscoveries, "no serial-discoveries found in data dir") + for _, serialDiscovery := range serialDiscoveries { + require.NoError(t, mockBin.CopyTo(serialDiscovery), "installing mocked serial discovery to %s", serialDiscovery) + } +} + // RunWithCustomEnv executes the given arduino-cli command with the given custom env and returns the output. func (cli *ArduinoCLI) RunWithCustomEnv(env map[string]string, args ...string) ([]byte, []byte, error) { if cli.cliConfigPath != nil { diff --git a/internal/integrationtest/board/board_test.go b/internal/integrationtest/board/board_test.go index a74434c5d97..70c53de9039 100644 --- a/internal/integrationtest/board/board_test.go +++ b/internal/integrationtest/board/board_test.go @@ -91,6 +91,44 @@ func TestBoardList(t *testing.T) { MustBeEmpty() } +func TestBoardListMock(t *testing.T) { + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) + defer env.CleanUp() + + _, _, err := cli.Run("core", "update-index") + require.NoError(t, err) + + cli.InstallMockedSerialDiscovery(t) + + stdout, _, err := cli.Run("board", "list", "--format", "json") + require.NoError(t, err) + + // check is a valid json and contains a list of ports + requirejson.Contains(t, stdout, `[ + { + "matching_boards": [ + { + "name": "Arduino Yún", + "fqbn": "arduino:avr:yun" + } + ], + "port": { + "address": "/dev/ttyCIAO", + "label": "Mocked Serial port", + "protocol": "serial", + "protocol_label": "Serial", + "properties": { + "pid": "0x0041", + "serial": "123456", + "vid": "0x2341" + }, + "hardware_id": "123456" + } + } + ] + `) +} + func TestBoardListWithFqbnFilter(t *testing.T) { if os.Getenv("CI") != "" { t.Skip("VMs have no serial ports")