Skip to content

Commit 8fbdf10

Browse files
authored
Merge pull request #2776 from afbjorklund/yqutil-empty
Avoid calling yq when the expression is empty
2 parents ad5b945 + bcdbabd commit 8fbdf10

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

pkg/yqutil/yqutil.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ func ValidateContent(content []byte) error {
3838

3939
// EvaluateExpression evaluates the yq expression, and returns the modified yaml.
4040
func EvaluateExpression(expression string, content []byte) ([]byte, error) {
41+
if expression == "" {
42+
return content, nil
43+
}
4144
logrus.Debugf("Evaluating yq expression: %q", expression)
4245
formatter, err := yamlfmtBasicFormatter()
4346
if err != nil {

pkg/yqutil/yqutil_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ func TestValidateContentError(t *testing.T) {
2525
assert.ErrorContains(t, err, "could not find expected")
2626
}
2727

28+
func TestEvaluateExpressionEmpty(t *testing.T) {
29+
expression := ""
30+
content := `
31+
foo: bar
32+
`
33+
expected := content
34+
out, err := EvaluateExpression(expression, []byte(content))
35+
assert.NilError(t, err)
36+
assert.Equal(t, expected, string(out))
37+
}
38+
2839
func TestEvaluateExpressionSimple(t *testing.T) {
2940
expression := `.cpus = 2 | .memory = "2GiB"`
3041
content := `

0 commit comments

Comments
 (0)