Skip to content

Commit 5edef26

Browse files
committed
Added integration test
1 parent 5a39dec commit 5edef26

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

Diff for: internal/integrationtest/arduino-cli.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ func (cli *ArduinoCLI) Run(args ...string) ([]byte, []byte, error) {
190190
return cli.RunWithCustomEnv(cli.cliEnvVars, args...)
191191
}
192192

193+
// RunWithContext executes the given arduino-cli command with the given context and returns the output.
194+
// If the context is canceled, the command is killed.
195+
func (cli *ArduinoCLI) RunWithContext(ctx context.Context, args ...string) ([]byte, []byte, error) {
196+
return cli.RunWithCustomEnvContext(ctx, cli.cliEnvVars, args...)
197+
}
198+
193199
// GetDefaultEnv returns a copy of the default execution env used with the Run method.
194200
func (cli *ArduinoCLI) GetDefaultEnv() map[string]string {
195201
res := map[string]string{}
@@ -324,8 +330,13 @@ func (cli *ArduinoCLI) InstallMockedAvrdude(t *testing.T) {
324330

325331
// RunWithCustomEnv executes the given arduino-cli command with the given custom env and returns the output.
326332
func (cli *ArduinoCLI) RunWithCustomEnv(env map[string]string, args ...string) ([]byte, []byte, error) {
333+
return cli.RunWithCustomEnvContext(context.Background(), env, args...)
334+
}
335+
336+
// RunWithCustomEnv executes the given arduino-cli command with the given custom env and returns the output.
337+
func (cli *ArduinoCLI) RunWithCustomEnvContext(ctx context.Context, env map[string]string, args ...string) ([]byte, []byte, error) {
327338
var stdoutBuf, stderrBuf bytes.Buffer
328-
err := cli.run(&stdoutBuf, &stderrBuf, nil, env, args...)
339+
err := cli.run(ctx, &stdoutBuf, &stderrBuf, nil, env, args...)
329340

330341
errBuf := stderrBuf.Bytes()
331342
cli.t.NotContains(string(errBuf), "panic: runtime error:", "arduino-cli panicked")
@@ -336,15 +347,15 @@ func (cli *ArduinoCLI) RunWithCustomEnv(env map[string]string, args ...string) (
336347
// RunWithCustomInput executes the given arduino-cli command pushing the given input stream and returns the output.
337348
func (cli *ArduinoCLI) RunWithCustomInput(in io.Reader, args ...string) ([]byte, []byte, error) {
338349
var stdoutBuf, stderrBuf bytes.Buffer
339-
err := cli.run(&stdoutBuf, &stderrBuf, in, cli.cliEnvVars, args...)
350+
err := cli.run(context.Background(), &stdoutBuf, &stderrBuf, in, cli.cliEnvVars, args...)
340351

341352
errBuf := stderrBuf.Bytes()
342353
cli.t.NotContains(string(errBuf), "panic: runtime error:", "arduino-cli panicked")
343354

344355
return stdoutBuf.Bytes(), errBuf, err
345356
}
346357

347-
func (cli *ArduinoCLI) run(stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader, env map[string]string, args ...string) error {
358+
func (cli *ArduinoCLI) run(ctx context.Context, stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader, env map[string]string, args ...string) error {
348359
if cli.cliConfigPath != nil {
349360
args = append([]string{"--config-file", cli.cliConfigPath.String()}, args...)
350361
}
@@ -402,8 +413,8 @@ func (cli *ArduinoCLI) run(stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader
402413
}
403414
}()
404415
}
416+
cliErr := cliProc.WaitWithinContext(ctx)
405417
wg.Wait()
406-
cliErr := cliProc.Wait()
407418
fmt.Fprintln(terminalOut, color.HiBlackString("<<< Run completed (err = %v)", cliErr))
408419

409420
return cliErr
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 compile_test
17+
18+
import (
19+
"context"
20+
"testing"
21+
"time"
22+
23+
"github.com/arduino/arduino-cli/internal/integrationtest"
24+
"github.com/arduino/go-paths-helper"
25+
"github.com/stretchr/testify/require"
26+
)
27+
28+
func TestCompileWithInfiniteMultipleIncludeRecursion(t *testing.T) {
29+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
30+
t.Cleanup(env.CleanUp)
31+
32+
// Install Arduino AVR Boards
33+
_, _, err := cli.Run("core", "install", "arduino:[email protected]")
34+
require.NoError(t, err)
35+
36+
sketch, err := paths.New("testdata", "SketchWithRecursiveIncludes").Abs()
37+
require.NoError(t, err)
38+
39+
// Time-limited test to prevent OOM
40+
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
41+
t.Cleanup(cancel)
42+
_, _, _ = cli.RunWithContext(ctx, "compile", "-b", "arduino:avr:uno", sketch.String())
43+
require.NotErrorIs(t, ctx.Err(), context.DeadlineExceeded, "compilation should not hang")
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "a.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include "a.h"
2+
#include "a.h"

0 commit comments

Comments
 (0)