Skip to content

Commit

Permalink
Added first mocked integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Oct 19, 2023
1 parent 3914c89 commit 01e95f0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
27 changes: 27 additions & 0 deletions internal/integrationtest/arduino-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 38 additions & 0 deletions internal/integrationtest/board/board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 01e95f0

Please sign in to comment.