Skip to content

Commit e6a82a1

Browse files
RoboticPrismfacebook-github-bot
authored andcommitted
TTPForge StepVars Bugfix: Fix newline appended to stdout (#544)
Summary: Pull Request resolved: #544 In inline blocks, when we capture stdout to a variable, we include the newline character used to move the cursor to a fresh line. While this is technically correct, most users would expect something like `echo "asdf"` to load the variable with `asdf`, not `asdf\n`. To fix this, I've opted just to remove a single newline from the end of stdout before loading it into the variable. Reviewed By: d0n601 Differential Revision: D78497399 fbshipit-source-id: 41ab4c783f2d6ec0ec8d7754a58c6feb4f1f7c7e
1 parent e43fa2c commit e6a82a1

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

pkg/blocks/basicstep.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"errors"
2525
"fmt"
2626
"os/exec"
27+
"strings"
2728
"time"
2829

2930
"github.com/facebookincubator/ttpforge/pkg/logging"
@@ -120,7 +121,7 @@ func (b *BasicStep) Execute(execCtx TTPExecutionContext) (*ActResult, error) {
120121
}
121122
// Send stdout to the output variable
122123
if b.OutputVar != "" {
123-
execCtx.Vars.StepVars[b.OutputVar] = result.Stdout
124+
execCtx.Vars.StepVars[b.OutputVar] = strings.TrimSuffix(result.Stdout, "\n")
124125
}
125126
return result, nil
126127
}

pkg/blocks/basicstep_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ outputs:
9999

100100
func TestBasicStepExecuteWithTemplate(t *testing.T) {
101101
content := `name: test_basic_step
102-
inline: echo -n "this is {[{.StepVars.foo}]}"`
102+
inline: echo "this is {[{.StepVars.foo}]}"`
103103
var s BasicStep
104104
execCtx := NewTTPExecutionContext()
105105
execCtx.Vars.StepVars = map[string]string{
@@ -113,7 +113,7 @@ inline: echo -n "this is {[{.StepVars.foo}]}"`
113113
require.NoError(t, err)
114114
result, err := s.Execute(execCtx)
115115
require.NoError(t, err)
116-
assert.Equal(t, "this is successfully templated", result.Stdout, "stdout should be templated")
116+
assert.Equal(t, "this is successfully templated\n", result.Stdout, "stdout should be templated")
117117
}
118118

119119
func TestBasicStepRaisesErrorWithMissingTemplateVariable(t *testing.T) {
@@ -131,7 +131,7 @@ inline: echo "this is {[{.StepVars.foo}]}"`
131131

132132
func TestBasicStepExecuteOutputsToOutputVar(t *testing.T) {
133133
content := `name: test_basic_step
134-
inline: echo -n "bar"
134+
inline: echo "bar"
135135
outputvar: foo`
136136
var s BasicStep
137137
execCtx := NewTTPExecutionContext()
@@ -146,9 +146,26 @@ outputvar: foo`
146146
assert.Equal(t, "bar", execCtx.Vars.StepVars["foo"], "outputvar should be set")
147147
}
148148

149+
func TestBasicStepExecuteOutputsMultilineToOutputVar(t *testing.T) {
150+
content := `name: test_basic_step
151+
inline: echo -e "line1\nline2\n\nline4\n"
152+
outputvar: foo`
153+
var s BasicStep
154+
execCtx := NewTTPExecutionContext()
155+
err := yaml.Unmarshal([]byte(content), &s)
156+
require.NoError(t, err)
157+
err = s.Validate(execCtx)
158+
require.NoError(t, err)
159+
err = s.Template(execCtx)
160+
require.NoError(t, err)
161+
_, err = s.Execute(execCtx)
162+
require.NoError(t, err)
163+
assert.Equal(t, "line1\nline2\n\nline4\n", execCtx.Vars.StepVars["foo"], "outputvar should be set")
164+
}
165+
149166
func TestBasicStepExecuteOutputsToAndOverwritesOutputVar(t *testing.T) {
150167
content := `name: test_basic_step
151-
inline: echo -n "bar"
168+
inline: echo "bar"
152169
outputvar: foo`
153170
var s BasicStep
154171
execCtx := NewTTPExecutionContext()

0 commit comments

Comments
 (0)