|
| 1 | +// |
| 2 | +// This file is part of dummy-discovery. |
| 3 | +// |
| 4 | +// Copyright 2021 ARDUINO SA (http://www.arduino.cc/) |
| 5 | +// |
| 6 | +// This software is released under the GNU General Public License version 3, |
| 7 | +// which covers the main part of arduino-cli. |
| 8 | +// The terms of this license can be found at: |
| 9 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 10 | +// |
| 11 | +// You can be released from the requirements of the above licenses by purchasing |
| 12 | +// a commercial license. Buying such a license is mandatory if you want to modify or |
| 13 | +// otherwise use the software for commercial activities involving the Arduino |
| 14 | +// software without disclosing the source code of your own applications. To purchase |
| 15 | +// a commercial license, send an email to [email protected]. |
| 16 | +// |
| 17 | + |
| 18 | +package discovery |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/arduino/arduino-cli/executils" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | +) |
| 26 | + |
| 27 | +func runDummyDiscovery(t *testing.T) *executils.Process { |
| 28 | + discoveryDir := "dummy-discovery" |
| 29 | + // Build dummy-discovery for testing |
| 30 | + builder, err := executils.NewProcess("go", "build") |
| 31 | + require.NoError(t, err) |
| 32 | + builder.SetDir(discoveryDir) |
| 33 | + require.NoError(t, builder.Run()) |
| 34 | + discovery, err := executils.NewProcess("./dummy-discovery") |
| 35 | + require.NoError(t, err) |
| 36 | + discovery.SetDir(discoveryDir) |
| 37 | + return discovery |
| 38 | +} |
| 39 | + |
| 40 | +func TestDiscoveryStdioHandling(t *testing.T) { |
| 41 | + discovery := runDummyDiscovery(t) |
| 42 | + |
| 43 | + stdout, err := discovery.StdoutPipe() |
| 44 | + require.NoError(t, err) |
| 45 | + stdin, err := discovery.StdinPipe() |
| 46 | + require.NoError(t, err) |
| 47 | + |
| 48 | + require.NoError(t, discovery.Start()) |
| 49 | + defer discovery.Kill() |
| 50 | + |
| 51 | + n, err := stdin.Write([]byte("quit\n")) |
| 52 | + require.Greater(t, n, 0) |
| 53 | + require.NoError(t, err) |
| 54 | + output := [1024]byte{} |
| 55 | + // require.NoError(t, discovery.Wait()) |
| 56 | + n, err = stdout.Read(output[:]) |
| 57 | + require.Greater(t, n, 0) |
| 58 | + require.NoError(t, err) |
| 59 | + |
| 60 | + expectedOutput := "{\n \"eventType\": \"quit\",\n \"message\": \"OK\"\n}\n" |
| 61 | + require.Equal(t, expectedOutput, string(output[:n])) |
| 62 | +} |
0 commit comments