|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2025 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package daemon |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/arduino/arduino-cli/internal/integrationtest" |
| 24 | + "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +func TestBoardListMock(t *testing.T) { |
| 29 | + env, cli := integrationtest.CreateEnvForDaemon(t) |
| 30 | + defer env.CleanUp() |
| 31 | + |
| 32 | + _, _, err := cli.Run("core", "update-index") |
| 33 | + require.NoError(t, err) |
| 34 | + |
| 35 | + cli.InstallMockedSerialDiscovery(t) |
| 36 | + |
| 37 | + var tmp1, tmp2 string |
| 38 | + |
| 39 | + { |
| 40 | + // Create a new instance of the daemon |
| 41 | + grpcInst := cli.Create() |
| 42 | + require.NoError(t, grpcInst.Init("", "", func(ir *commands.InitResponse) { |
| 43 | + fmt.Printf("INIT> %v\n", ir.GetMessage()) |
| 44 | + })) |
| 45 | + |
| 46 | + // Run a BoardList |
| 47 | + resp, err := grpcInst.BoardList(time.Second) |
| 48 | + require.NoError(t, err) |
| 49 | + require.NotEmpty(t, resp.Ports) |
| 50 | + for _, port := range resp.Ports { |
| 51 | + if port.GetPort().GetProtocol() == "serial" { |
| 52 | + tmp1 = port.Port.GetProperties()["discovery_tmp"] |
| 53 | + } |
| 54 | + } |
| 55 | + require.NotEmpty(t, tmp1) |
| 56 | + |
| 57 | + // Close instance |
| 58 | + require.NoError(t, grpcInst.Destroy(t.Context())) |
| 59 | + } |
| 60 | + |
| 61 | + { |
| 62 | + // Create a second instance of the daemon |
| 63 | + grpcInst := cli.Create() |
| 64 | + require.NoError(t, grpcInst.Init("", "", func(ir *commands.InitResponse) { |
| 65 | + fmt.Printf("INIT> %v\n", ir.GetMessage()) |
| 66 | + })) |
| 67 | + |
| 68 | + // Run a BoardList |
| 69 | + resp, err := grpcInst.BoardList(time.Second) |
| 70 | + require.NoError(t, err) |
| 71 | + require.NotEmpty(t, resp.Ports) |
| 72 | + for _, port := range resp.Ports { |
| 73 | + if port.GetPort().GetProtocol() == "serial" { |
| 74 | + tmp2 = port.Port.GetProperties()["discovery_tmp"] |
| 75 | + } |
| 76 | + } |
| 77 | + require.NotEmpty(t, tmp2) |
| 78 | + |
| 79 | + // Close instance |
| 80 | + require.NoError(t, grpcInst.Destroy(t.Context())) |
| 81 | + } |
| 82 | + |
| 83 | + // Check if the discoveries have been successfully close |
| 84 | + require.NoFileExists(t, tmp1, "discovery has not been closed") |
| 85 | + require.NoFileExists(t, tmp2, "discovery has not been closed") |
| 86 | +} |
0 commit comments