mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-24 14:45:39 +00:00
Front matter processor seems to be working!
This commit is contained in:
parent
555ad0762c
commit
9c8253b582
@ -89,6 +89,25 @@ func evaluateAll(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
printer := yqlib.NewPrinter(out, outputToJSON, unwrapScalar, colorsEnabled, indent, !noDocSeparators)
|
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()
|
allAtOnceEvaluator := yqlib.NewAllAtOnceEvaluator()
|
||||||
switch len(args) {
|
switch len(args) {
|
||||||
case 0:
|
case 0:
|
||||||
|
@ -120,7 +120,6 @@ func evaluateSequence(cmd *cobra.Command, args []string) error {
|
|||||||
defer yqlib.SafelyCloseReader(reader)
|
defer yqlib.SafelyCloseReader(reader)
|
||||||
}
|
}
|
||||||
defer frontMatterHandler.CleanUp()
|
defer frontMatterHandler.CleanUp()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch len(args) {
|
switch len(args) {
|
||||||
|
133
pkg/yqlib/front_matter_test.go
Normal file
133
pkg/yqlib/front_matter_test.go
Normal file
@ -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()
|
||||||
|
}
|
@ -127,12 +127,13 @@ if [[ $? != 0 ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# run expression against empty file
|
# run expression against empty file
|
||||||
|
rm -f temp.yaml
|
||||||
touch temp.yaml
|
touch temp.yaml
|
||||||
expected="apple: tree"
|
expected="apple: tree"
|
||||||
|
|
||||||
./yq e '.apple = "tree"' temp.yaml -i
|
./yq e '.apple = "tree"' temp.yaml -i
|
||||||
X=$(cat temp.yaml)
|
X=$(cat temp.yaml)
|
||||||
rm temp.yaml
|
rm -f temp.yaml
|
||||||
if [[ $X != $expected ]]; then
|
if [[ $X != $expected ]]; then
|
||||||
echo "Write empty doc"
|
echo "Write empty doc"
|
||||||
echo "Expected $expected but was $X"
|
echo "Expected $expected but was $X"
|
||||||
@ -143,7 +144,7 @@ touch temp.yaml
|
|||||||
|
|
||||||
./yq ea '.apple = "tree"' temp.yaml -i
|
./yq ea '.apple = "tree"' temp.yaml -i
|
||||||
X=$(cat temp.yaml)
|
X=$(cat temp.yaml)
|
||||||
rm temp.yaml
|
rm -f temp.yaml
|
||||||
if [[ $X != $expected ]]; then
|
if [[ $X != $expected ]]; then
|
||||||
echo "Write all empty doc"
|
echo "Write all empty doc"
|
||||||
echo "Expected $expected but was $X"
|
echo "Expected $expected but was $X"
|
||||||
@ -151,6 +152,7 @@ if [[ $X != $expected ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Test: handle empty files with just comments"
|
echo "Test: handle empty files with just comments"
|
||||||
|
rm -f temp.yaml
|
||||||
echo "# comment" > temp.yaml
|
echo "# comment" > temp.yaml
|
||||||
read -r -d '' expected << EOM
|
read -r -d '' expected << EOM
|
||||||
# comment
|
# comment
|
||||||
@ -159,7 +161,7 @@ EOM
|
|||||||
|
|
||||||
./yq e '.apple = "tree"' temp.yaml -i
|
./yq e '.apple = "tree"' temp.yaml -i
|
||||||
X=$(cat temp.yaml)
|
X=$(cat temp.yaml)
|
||||||
rm temp.yaml
|
rm -f temp.yaml
|
||||||
if [[ $X != $expected ]]; then
|
if [[ $X != $expected ]]; then
|
||||||
echo "Write empty doc"
|
echo "Write empty doc"
|
||||||
echo "Expected $expected but was $X"
|
echo "Expected $expected but was $X"
|
||||||
@ -170,13 +172,95 @@ echo "# comment" > temp.yaml
|
|||||||
|
|
||||||
./yq ea '.apple = "tree"' temp.yaml -i
|
./yq ea '.apple = "tree"' temp.yaml -i
|
||||||
X=$(cat temp.yaml)
|
X=$(cat temp.yaml)
|
||||||
rm temp.yaml
|
rm -f temp.yaml
|
||||||
if [[ $X != $expected ]]; then
|
if [[ $X != $expected ]]; then
|
||||||
echo "Write all empty doc"
|
echo "Write all empty doc"
|
||||||
echo "Expected $expected but was $X"
|
echo "Expected $expected but was $X"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
echo "Test: eval front matter process"
|
||||||
|
|
||||||
|
cat >temp.yaml <<EOL
|
||||||
|
---
|
||||||
|
a: apple
|
||||||
|
b: cat
|
||||||
|
---
|
||||||
|
not yaml
|
||||||
|
c: at
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
---
|
||||||
|
a: apple
|
||||||
|
b: dog
|
||||||
|
---
|
||||||
|
not yaml
|
||||||
|
c: at
|
||||||
|
EOM
|
||||||
|
|
||||||
|
./yq e --front-matter="process" '.b = "dog"' temp.yaml -i
|
||||||
|
X=$(cat temp.yaml)
|
||||||
|
rm -f temp.yaml
|
||||||
|
if [[ $X != $expected ]]; then
|
||||||
|
echo "eval fail"
|
||||||
|
echo "Expected $expected but was $X"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Test: eval front matter extract"
|
||||||
|
|
||||||
|
cat >temp.yaml <<EOL
|
||||||
|
a: apple
|
||||||
|
b: cat
|
||||||
|
---
|
||||||
|
not yaml
|
||||||
|
c: at
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
a: apple
|
||||||
|
b: dog
|
||||||
|
EOM
|
||||||
|
|
||||||
|
./yq e --front-matter="extract" '.b = "dog"' temp.yaml -i
|
||||||
|
X=$(cat temp.yaml)
|
||||||
|
rm -f temp.yaml
|
||||||
|
if [[ $X != $expected ]]; then
|
||||||
|
echo "eval fail"
|
||||||
|
echo "Expected $expected but was $X"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Test: eval-all front matter process"
|
||||||
|
|
||||||
|
cat >temp.yaml <<EOL
|
||||||
|
---
|
||||||
|
a: apple
|
||||||
|
b: cat
|
||||||
|
---
|
||||||
|
not yaml
|
||||||
|
c: at
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
---
|
||||||
|
a: apple
|
||||||
|
b: dog
|
||||||
|
---
|
||||||
|
not yaml
|
||||||
|
c: at
|
||||||
|
EOM
|
||||||
|
|
||||||
|
./yq ea --front-matter="process" '.b = "dog"' temp.yaml -i
|
||||||
|
X=$(cat temp.yaml)
|
||||||
|
rm -f temp.yaml
|
||||||
|
if [[ $X != $expected ]]; then
|
||||||
|
echo "eval fail"
|
||||||
|
echo "Expected $expected but was $X"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
echo "--success"
|
echo "--success"
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
Loading…
Reference in New Issue
Block a user