Skip to content

Commit dae3242

Browse files
committed
testsuite: Added helper functions to run arduino-cli
1 parent 874d2e2 commit dae3242

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

testsuite/arduino-cli.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2022 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 license@arduino.cc.
15+
16+
package integrationtest
17+
18+
import (
19+
"bytes"
20+
"context"
21+
"fmt"
22+
"io"
23+
"testing"
24+
25+
"github.com/arduino/arduino-cli/executils"
26+
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
27+
"github.com/arduino/go-paths-helper"
28+
"github.com/stretchr/testify/require"
29+
"google.golang.org/grpc"
30+
)
31+
32+
// ArduinoCLI is an Arduino CLI client.
33+
type ArduinoCLI struct {
34+
path *paths.Path
35+
t *require.Assertions
36+
proc *executils.Process
37+
cliConfigPath *paths.Path
38+
daemonAddr string
39+
daemonConn *grpc.ClientConn
40+
daemonClient commands.ArduinoCoreServiceClient
41+
}
42+
43+
// NewArduinoCliWithinEnvironment creates a new Arduino CLI client inside the given environment.
44+
func NewArduinoCliWithinEnvironment(t *testing.T, cliPath *paths.Path, env *Environment) *ArduinoCLI {
45+
cli := NewArduinoCli(t, cliPath)
46+
cli.cliConfigPath = env.Root().Join("arduino-cli.yaml")
47+
config := fmt.Sprintf(`
48+
directories:
49+
data: %s
50+
downloads: %s
51+
user: %s
52+
`,
53+
env.Root().Join("arduino15"),
54+
env.Root().Join("arduino15/staging"),
55+
env.Root().Join("Arduino"))
56+
require.NoError(t, cli.cliConfigPath.WriteFile([]byte(config)))
57+
return cli
58+
}
59+
60+
// NewArduinoCli creates a new Arduino CLI client.
61+
func NewArduinoCli(t *testing.T, cliPath *paths.Path) *ArduinoCLI {
62+
return &ArduinoCLI{
63+
path: cliPath,
64+
t: require.New(t),
65+
}
66+
}
67+
68+
// CleanUp closes the Arduino CLI client.
69+
func (cli *ArduinoCLI) CleanUp() {
70+
if cli.proc != nil {
71+
cli.proc.Kill()
72+
cli.proc.Wait()
73+
}
74+
}
75+
76+
// Run executes the given arduino-cli command and returns the output.
77+
func (cli *ArduinoCLI) Run(args ...string) ([]byte, []byte, error) {
78+
if cli.cliConfigPath != nil {
79+
args = append([]string{"--config-file", cli.cliConfigPath.String()}, args...)
80+
}
81+
cliProc, err := executils.NewProcessFromPath(nil, cli.path, args...)
82+
cli.t.NoError(err)
83+
stdout, err := cliProc.StdoutPipe()
84+
cli.t.NoError(err)
85+
stderr, err := cliProc.StderrPipe()
86+
cli.t.NoError(err)
87+
_, err = cliProc.StdinPipe()
88+
cli.t.NoError(err)
89+
90+
cli.t.NoError(cliProc.Start())
91+
92+
var stdoutBuf, stderrBuf bytes.Buffer
93+
stdoutCtx, stdoutCancel := context.WithCancel(context.Background())
94+
stderrCtx, stderrCancel := context.WithCancel(context.Background())
95+
go func() {
96+
io.Copy(&stdoutBuf, stdout)
97+
stdoutCancel()
98+
}()
99+
go func() {
100+
io.Copy(&stderrBuf, stderr)
101+
stderrCancel()
102+
}()
103+
cliErr := cliProc.Wait()
104+
<-stdoutCtx.Done()
105+
<-stderrCtx.Done()
106+
return stdoutBuf.Bytes(), stderrBuf.Bytes(), cliErr
107+
}

0 commit comments

Comments
 (0)