yq/pkg/yqlib/formatting_expressions_test.go

92 lines
3.6 KiB
Go
Raw Normal View History

2024-02-09 02:54:27 +00:00
package yqlib
import (
"bufio"
"fmt"
2024-02-16 00:03:35 +00:00
"strings"
2024-02-09 02:54:27 +00:00
"testing"
"github.com/mikefarah/yq/v4/test"
)
var formattingExpressionScenarios = []formatScenario{
{
description: "Using expression files and comments",
2024-02-15 23:56:18 +00:00
skipDoc: true,
2024-02-09 02:54:27 +00:00
input: "a:\n b: old",
2024-02-15 23:56:18 +00:00
expression: "#! yq\n\n# This is a yq expression that updates the map\n# for several great reasons outlined here.\n\n.a.b = \"new\" # line comment here\n| .a.c = \"frog\"\n\n# Now good things will happen.\n",
2024-02-09 02:54:27 +00:00
expected: "a:\n b: new\n c: frog\n",
},
2024-02-15 23:56:18 +00:00
{
description: "Using expression files and comments",
subdescription: "Note that you can execute the file directly - but make sure you make the expression file executable.",
input: "a:\n b: old",
expression: "#! yq\n\n# This is a yq expression that updates the map\n# for several great reasons outlined here.\n\n.a.b = \"new\" # line comment here\n| .a.c = \"frog\"\n\n# Now good things will happen.\n",
expected: "a:\n b: new\n c: frog\n",
scenarioType: "shebang",
},
2024-02-16 00:03:35 +00:00
{
description: "Flags in expression files",
subdescription: "You can specify flags on the shebang line, this only works when executing the file directly.",
input: "a:\n b: old",
expression: "#! yq -oj\n\n# This is a yq expression that updates the map\n# for several great reasons outlined here.\n\n.a.b = \"new\" # line comment here\n| .a.c = \"frog\"\n\n# Now good things will happen.\n",
expected: "a:\n b: new\n c: frog\n",
scenarioType: "shebang-json",
},
2024-02-09 02:58:59 +00:00
{
description: "Commenting out yq expressions",
2024-02-15 23:56:18 +00:00
subdescription: "Note that `c` is no longer set to 'frog'. In this example we're calling yq directly and passing the expression file into `--from-file`, this is no different from executing the expression file directly.",
2024-02-09 02:58:59 +00:00
input: "a:\n b: old",
2024-02-15 23:56:18 +00:00
expression: "#! yq\n# This is a yq expression that updates the map\n# for several great reasons outlined here.\n\n.a.b = \"new\" # line comment here\n# | .a.c = \"frog\"\n\n# Now good things will happen.\n",
2024-02-09 02:58:59 +00:00
expected: "a:\n b: new\n",
},
2024-02-09 02:54:27 +00:00
}
func documentExpressionScenario(_ *testing.T, w *bufio.Writer, i interface{}) {
s := i.(formatScenario)
if s.skipDoc {
return
}
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
if s.subdescription != "" {
writeOrPanic(w, s.subdescription)
writeOrPanic(w, "\n\n")
}
writeOrPanic(w, "Given a sample.yaml file of:\n")
writeOrPanic(w, fmt.Sprintf("```yaml\n%v\n```\n", s.input))
writeOrPanic(w, "And an 'update.yq' expression file of:\n")
2024-02-15 23:56:18 +00:00
writeOrPanic(w, fmt.Sprintf("```bash\n%v```\n", s.expression))
2024-02-09 02:54:27 +00:00
writeOrPanic(w, "then\n")
2024-02-16 00:03:35 +00:00
if strings.HasPrefix(s.scenarioType, "shebang") {
2024-02-15 23:56:18 +00:00
writeOrPanic(w, "```bash\n./update.yq sample.yaml\n```\n")
} else {
writeOrPanic(w, "```bash\nyq --from-file update.yq sample.yml\n```\n")
}
2024-02-09 02:54:27 +00:00
writeOrPanic(w, "will output\n")
2024-02-24 04:36:16 +00:00
encoder := NewYamlEncoder(ConfiguredYamlPreferences)
2024-02-16 00:03:35 +00:00
if s.scenarioType == "shebang-json" {
2024-02-24 04:59:12 +00:00
encoder = NewJSONEncoder(ConfiguredJSONPreferences)
2024-02-16 00:03:35 +00:00
}
2024-02-09 02:54:27 +00:00
2024-02-16 00:03:35 +00:00
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), encoder)))
2024-02-09 02:54:27 +00:00
}
func TestExpressionCommentScenarios(t *testing.T) {
for _, tt := range formattingExpressionScenarios {
test.AssertResultComplexWithContext(t, tt.expected,
2024-02-24 04:36:16 +00:00
mustProcessFormatScenario(tt, NewYamlDecoder(ConfiguredYamlPreferences), NewYamlEncoder(ConfiguredYamlPreferences)),
2024-02-09 02:54:27 +00:00
tt.description)
}
genericScenarios := make([]interface{}, len(formattingExpressionScenarios))
for i, s := range formattingExpressionScenarios {
genericScenarios[i] = s
}
documentScenarios(t, "usage", "formatting-expressions", genericScenarios, documentExpressionScenario)
}