mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 05:38:04 +00:00
e347090571
* refactor: move from io/ioutil to io and os packages The io/ioutil package has been deprecated as of Go 1.16, see https://golang.org/doc/go1.16#ioutil. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> * build: update snapcraft `go-channel` to 1.17 Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
144 lines
2.3 KiB
Go
144 lines
2.3 KiB
Go
package yqlib
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"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 := os.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)
|
|
|
|
contentBytes, err := io.ReadAll(fmHandler.GetContentReader())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
test.AssertResult(t, expectedContent, string(contentBytes))
|
|
|
|
tryRemoveTempFile(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)
|
|
|
|
contentBytes, err := io.ReadAll(fmHandler.GetContentReader())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
test.AssertResult(t, expectedContent, string(contentBytes))
|
|
|
|
tryRemoveTempFile(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)
|
|
|
|
contentBytes, err := io.ReadAll(fmHandler.GetContentReader())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
test.AssertResult(t, expectedContent, string(contentBytes))
|
|
|
|
tryRemoveTempFile(file)
|
|
fmHandler.CleanUp()
|
|
}
|