Skip to content

Commit 89b039a

Browse files
committed
Add tests
1 parent e176a59 commit 89b039a

File tree

3 files changed

+803
-1
lines changed

3 files changed

+803
-1
lines changed

discovery_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 := `{
61+
"eventType": "quit",
62+
"message": "OK"
63+
}
64+
`
65+
require.Equal(t, expectedOutput, string(output[:n]))
66+
}

go.mod

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ module github.com/arduino/pluggable-discovery-protocol-handler/v2
22

33
go 1.16
44

5-
require github.com/arduino/go-properties-orderedmap v1.4.0
5+
require (
6+
github.com/arduino/arduino-cli v0.0.0-20211123110800-c756a0f1305d // indirect
7+
github.com/arduino/go-properties-orderedmap v1.6.0
8+
github.com/stretchr/testify v1.7.0 // indirect
9+
)

0 commit comments

Comments
 (0)