Skip to content

Commit

Permalink
Fix preprocess output (do not print stats after the preprocessed sour…
Browse files Browse the repository at this point in the history
…ce) (arduino#2152)

* Added test

* Hide compile stats when using preprocess flag
  • Loading branch information
cmaglie authored Apr 19, 2023
1 parent 7b774e1 commit 2c2a5cc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
ProfileOut: profileOut,
Success: compileError == nil,
showPropertiesMode: showProperties,
hideStats: preprocess,
}

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

showPropertiesMode arguments.ShowPropertiesMode
hideStats bool
}

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

if r.hideStats {
return ""
}

titleColor := color.New(color.FgHiGreen)
nameColor := color.New(color.FgHiYellow)
pathColor := color.New(color.FgHiBlack)
Expand Down
43 changes: 43 additions & 0 deletions internal/integrationtest/compile_1/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"testing"
"time"

"github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/internal/integrationtest"
"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -71,6 +72,7 @@ func TestCompile(t *testing.T) {
{"WithInvalidBuildOptionJson", compileWithInvalidBuildOptionJson},
{"WithRelativeBuildPath", compileWithRelativeBuildPath},
{"WithFakeSecureBootCore", compileWithFakeSecureBootCore},
{"PreprocessFlagDoNotMessUpWithOutput", preprocessFlagDoNotMessUpWithOutput},
}.Run(t, env, cli)
}

Expand Down Expand Up @@ -1162,3 +1164,44 @@ func compileWithFakeSecureBootCore(t *testing.T, env *integrationtest.Environmen
require.Contains(t, string(stdout), "my-sign-key.pem")
require.Contains(t, string(stdout), "my-encrypt-key.pem")
}

func preprocessFlagDoNotMessUpWithOutput(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
// https://github.com/arduino/arduino-cli/issues/2150

// go test -v ./internal/integrationtest/compile_1 --run=TestCompile$/PreprocessFlagDoNotMessUpWithOutput

sketchPath := cli.SketchbookDir().Join("SketchSimple")
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)

expected := `#include <Arduino.h>
#line 1 %SKETCH_PATH%
#line 2 %SKETCH_PATH%
void setup();
#line 5 %SKETCH_PATH%
void loop();
#line 2 %SKETCH_PATH%
void setup() {
}
void loop() {
}
`
expected = strings.ReplaceAll(expected, "%SKETCH_PATH%", builder.QuoteCppString(sketchPath.Join("SketchSimple.ino").String()))

jsonOut, _, err := cli.Run("compile", "-b", fqbn, "--preprocess", sketchPath.String(), "--format", "json")
require.NoError(t, err)
var ex struct {
CompilerOut string `json:"compiler_out"`
}
require.NoError(t, json.Unmarshal(jsonOut, &ex))
require.Equal(t, expected, ex.CompilerOut)

output, _, err := cli.Run("compile", "-b", fqbn, "--preprocess", sketchPath.String())
require.NoError(t, err)
require.Equal(t, expected, string(output))
}

0 comments on commit 2c2a5cc

Please sign in to comment.