Skip to content

Commit 2c2a5cc

Browse files
authored
Fix preprocess output (do not print stats after the preprocessed source) (#2152)
* Added test * Hide compile stats when using preprocess flag
1 parent 7b774e1 commit 2c2a5cc

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

Diff for: internal/cli/compile/compile.go

+6
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
330330
ProfileOut: profileOut,
331331
Success: compileError == nil,
332332
showPropertiesMode: showProperties,
333+
hideStats: preprocess,
333334
}
334335

335336
if compileError != nil {
@@ -394,6 +395,7 @@ type compileResult struct {
394395
Error string `json:"error,omitempty"`
395396

396397
showPropertiesMode arguments.ShowPropertiesMode
398+
hideStats bool
397399
}
398400

399401
func (r *compileResult) Data() interface{} {
@@ -405,6 +407,10 @@ func (r *compileResult) String() string {
405407
return strings.Join(r.BuilderResult.GetBuildProperties(), fmt.Sprintln())
406408
}
407409

410+
if r.hideStats {
411+
return ""
412+
}
413+
408414
titleColor := color.New(color.FgHiGreen)
409415
nameColor := color.New(color.FgHiYellow)
410416
pathColor := color.New(color.FgHiBlack)

Diff for: internal/integrationtest/compile_1/compile_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"testing"
2626
"time"
2727

28+
"github.com/arduino/arduino-cli/arduino/builder"
2829
"github.com/arduino/arduino-cli/internal/integrationtest"
2930
"github.com/arduino/go-paths-helper"
3031
"github.com/stretchr/testify/require"
@@ -71,6 +72,7 @@ func TestCompile(t *testing.T) {
7172
{"WithInvalidBuildOptionJson", compileWithInvalidBuildOptionJson},
7273
{"WithRelativeBuildPath", compileWithRelativeBuildPath},
7374
{"WithFakeSecureBootCore", compileWithFakeSecureBootCore},
75+
{"PreprocessFlagDoNotMessUpWithOutput", preprocessFlagDoNotMessUpWithOutput},
7476
}.Run(t, env, cli)
7577
}
7678

@@ -1162,3 +1164,44 @@ func compileWithFakeSecureBootCore(t *testing.T, env *integrationtest.Environmen
11621164
require.Contains(t, string(stdout), "my-sign-key.pem")
11631165
require.Contains(t, string(stdout), "my-encrypt-key.pem")
11641166
}
1167+
1168+
func preprocessFlagDoNotMessUpWithOutput(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
1169+
// https://github.com/arduino/arduino-cli/issues/2150
1170+
1171+
// go test -v ./internal/integrationtest/compile_1 --run=TestCompile$/PreprocessFlagDoNotMessUpWithOutput
1172+
1173+
sketchPath := cli.SketchbookDir().Join("SketchSimple")
1174+
defer sketchPath.RemoveAll()
1175+
fqbn := "arduino:avr:uno"
1176+
_, _, err := cli.Run("sketch", "new", sketchPath.String())
1177+
require.NoError(t, err)
1178+
1179+
expected := `#include <Arduino.h>
1180+
#line 1 %SKETCH_PATH%
1181+
1182+
#line 2 %SKETCH_PATH%
1183+
void setup();
1184+
#line 5 %SKETCH_PATH%
1185+
void loop();
1186+
#line 2 %SKETCH_PATH%
1187+
void setup() {
1188+
}
1189+
1190+
void loop() {
1191+
}
1192+
1193+
`
1194+
expected = strings.ReplaceAll(expected, "%SKETCH_PATH%", builder.QuoteCppString(sketchPath.Join("SketchSimple.ino").String()))
1195+
1196+
jsonOut, _, err := cli.Run("compile", "-b", fqbn, "--preprocess", sketchPath.String(), "--format", "json")
1197+
require.NoError(t, err)
1198+
var ex struct {
1199+
CompilerOut string `json:"compiler_out"`
1200+
}
1201+
require.NoError(t, json.Unmarshal(jsonOut, &ex))
1202+
require.Equal(t, expected, ex.CompilerOut)
1203+
1204+
output, _, err := cli.Run("compile", "-b", fqbn, "--preprocess", sketchPath.String())
1205+
require.NoError(t, err)
1206+
require.Equal(t, expected, string(output))
1207+
}

0 commit comments

Comments
 (0)