diff --git a/cmd/evaluate_all_command.go b/cmd/evaluate_all_command.go index 68edf62d..220517a0 100644 --- a/cmd/evaluate_all_command.go +++ b/cmd/evaluate_all_command.go @@ -89,6 +89,25 @@ func evaluateAll(cmd *cobra.Command, args []string) error { printer := yqlib.NewPrinter(out, outputToJSON, unwrapScalar, colorsEnabled, indent, !noDocSeparators) + if frontMatter != "" { + frontMatterHandler := yqlib.NewFrontMatterHandler(args[firstFileIndex]) + err = frontMatterHandler.Split() + if err != nil { + return err + } + args[firstFileIndex] = frontMatterHandler.GetYamlFrontMatterFilename() + + if frontMatter == "process" { + reader, err := os.Open(frontMatterHandler.GetContentFilename()) // #nosec + if err != nil { + return err + } + printer.SetAppendix(reader) + defer yqlib.SafelyCloseReader(reader) + } + defer frontMatterHandler.CleanUp() + } + allAtOnceEvaluator := yqlib.NewAllAtOnceEvaluator() switch len(args) { case 0: diff --git a/cmd/evalute_sequence_command.go b/cmd/evalute_sequence_command.go index dcc82f10..69aae477 100644 --- a/cmd/evalute_sequence_command.go +++ b/cmd/evalute_sequence_command.go @@ -120,7 +120,6 @@ func evaluateSequence(cmd *cobra.Command, args []string) error { defer yqlib.SafelyCloseReader(reader) } defer frontMatterHandler.CleanUp() - } switch len(args) { diff --git a/pkg/yqlib/front_matter_test.go b/pkg/yqlib/front_matter_test.go new file mode 100644 index 00000000..bc2f952e --- /dev/null +++ b/pkg/yqlib/front_matter_test.go @@ -0,0 +1,133 @@ +package yqlib + +import ( + "io/ioutil" + "testing" + + "github.com/mikefarah/yq/v4/test" +) + +func createTestFile(content string) string { + tempFile, err := createTempFile() + if err != nil { + panic(err) + } + + _, err = tempFile.Write([]byte(content)) + if err != nil { + panic(err) + } + + safelyCloseFile(tempFile) + + return tempFile.Name() +} + +func readFile(filename string) string { + bytes, err := ioutil.ReadFile(filename) + if err != nil { + panic(err) + } + return string(bytes) +} + +func TestFrontMatterSplitWithLeadingSep(t *testing.T) { + file := createTestFile(`--- +a: apple +b: banana +--- +not a +yaml: doc +`) + + expectedYamlFm := `--- +a: apple +b: banana +` + + expectedContent := `--- +not a +yaml: doc +` + + fmHandler := NewFrontMatterHandler(file) + err := fmHandler.Split() + if err != nil { + panic(err) + } + + yamlFm := readFile(fmHandler.GetYamlFrontMatterFilename()) + + test.AssertResult(t, expectedYamlFm, yamlFm) + + content := readFile(fmHandler.GetContentFilename()) + test.AssertResult(t, expectedContent, content) + + tryRemoveFile(file) + fmHandler.CleanUp() +} + +func TestFrontMatterSplitWithNoLeadingSep(t *testing.T) { + file := createTestFile(`a: apple +b: banana +--- +not a +yaml: doc +`) + + expectedYamlFm := `a: apple +b: banana +` + + expectedContent := `--- +not a +yaml: doc +` + + fmHandler := NewFrontMatterHandler(file) + err := fmHandler.Split() + if err != nil { + panic(err) + } + + yamlFm := readFile(fmHandler.GetYamlFrontMatterFilename()) + + test.AssertResult(t, expectedYamlFm, yamlFm) + + content := readFile(fmHandler.GetContentFilename()) + test.AssertResult(t, expectedContent, content) + + tryRemoveFile(file) + fmHandler.CleanUp() +} + +func TestFrontMatterSplitWithArray(t *testing.T) { + file := createTestFile(`[1,2,3] +--- +not a +yaml: doc +`) + + expectedYamlFm := "[1,2,3]\n" + + expectedContent := `--- +not a +yaml: doc +` + + fmHandler := NewFrontMatterHandler(file) + err := fmHandler.Split() + if err != nil { + panic(err) + } + + yamlFm := readFile(fmHandler.GetYamlFrontMatterFilename()) + + test.AssertResult(t, expectedYamlFm, yamlFm) + + content := readFile(fmHandler.GetContentFilename()) + test.AssertResult(t, expectedContent, content) + + tryRemoveFile(file) + fmHandler.CleanUp() +} diff --git a/scripts/acceptance.sh b/scripts/acceptance.sh index 2ea7902e..0929f871 100755 --- a/scripts/acceptance.sh +++ b/scripts/acceptance.sh @@ -127,12 +127,13 @@ if [[ $? != 0 ]]; then fi # run expression against empty file +rm -f temp.yaml touch temp.yaml expected="apple: tree" ./yq e '.apple = "tree"' temp.yaml -i X=$(cat temp.yaml) -rm temp.yaml +rm -f temp.yaml if [[ $X != $expected ]]; then echo "Write empty doc" echo "Expected $expected but was $X" @@ -143,7 +144,7 @@ touch temp.yaml ./yq ea '.apple = "tree"' temp.yaml -i X=$(cat temp.yaml) -rm temp.yaml +rm -f temp.yaml if [[ $X != $expected ]]; then echo "Write all empty doc" echo "Expected $expected but was $X" @@ -151,6 +152,7 @@ if [[ $X != $expected ]]; then fi echo "Test: handle empty files with just comments" +rm -f temp.yaml echo "# comment" > temp.yaml read -r -d '' expected << EOM # comment @@ -159,7 +161,7 @@ EOM ./yq e '.apple = "tree"' temp.yaml -i X=$(cat temp.yaml) -rm temp.yaml +rm -f temp.yaml if [[ $X != $expected ]]; then echo "Write empty doc" echo "Expected $expected but was $X" @@ -170,13 +172,95 @@ echo "# comment" > temp.yaml ./yq ea '.apple = "tree"' temp.yaml -i X=$(cat temp.yaml) -rm temp.yaml +rm -f temp.yaml if [[ $X != $expected ]]; then echo "Write all empty doc" echo "Expected $expected but was $X" exit 1 fi +echo "Test: eval front matter process" + +cat >temp.yaml <temp.yaml <temp.yaml <