yq/pkg/yqlib/operators_test.go

242 lines
5.7 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
import (
"bufio"
"bytes"
2020-11-06 03:37:01 +00:00
"container/list"
2020-11-03 23:48:43 +00:00
"fmt"
2020-11-13 09:58:01 +00:00
"io"
2020-11-03 23:48:43 +00:00
"os"
2020-11-06 01:11:38 +00:00
"strings"
2020-11-03 23:48:43 +00:00
"testing"
"github.com/mikefarah/yq/v4/test"
2020-12-01 03:06:49 +00:00
yaml "gopkg.in/yaml.v3"
2020-11-03 23:48:43 +00:00
)
type expressionScenario struct {
2020-11-14 23:50:30 +00:00
description string
2020-11-17 23:32:30 +00:00
subdescription string
2020-11-14 23:50:30 +00:00
document string
2021-01-01 23:27:32 +00:00
document2 string
2020-11-14 23:50:30 +00:00
expression string
expected []string
skipDoc bool
dontFormatInputForDoc bool // dont format input doc for documentation generation
2020-11-03 23:48:43 +00:00
}
func testScenario(t *testing.T, s *expressionScenario) {
2020-11-06 03:37:01 +00:00
var results *list.List
var err error
2020-11-13 02:19:54 +00:00
node, err := treeCreator.ParsePath(s.expression)
if err != nil {
2020-11-22 02:50:32 +00:00
t.Error(fmt.Errorf("Error parsing expression %v of %v: %v", s.expression, s.description, err))
2020-11-13 02:19:54 +00:00
return
}
inputs := list.New()
2020-11-06 03:37:01 +00:00
if s.document != "" {
2020-11-20 11:57:32 +00:00
inputs, err = readDocuments(strings.NewReader(s.document), "sample.yml", 0)
2020-11-13 02:19:54 +00:00
if err != nil {
2020-12-26 10:37:08 +00:00
t.Error(err, s.document, s.expression)
2020-11-13 02:19:54 +00:00
return
}
2021-01-01 23:27:32 +00:00
if s.document2 != "" {
moreInputs, err := readDocuments(strings.NewReader(s.document2), "another.yml", 1)
if err != nil {
t.Error(err, s.document, s.expression)
return
}
inputs.PushBackList(moreInputs)
}
2020-12-01 03:06:49 +00:00
} else {
candidateNode := &CandidateNode{
Document: 0,
Filename: "",
Node: &yaml.Node{Tag: "!!null"},
FileIndex: 0,
}
inputs.PushBack(candidateNode)
2020-11-03 23:48:43 +00:00
}
2020-11-13 02:19:54 +00:00
results, err = treeNavigator.GetMatchingNodes(inputs, node)
2020-11-06 03:37:01 +00:00
if err != nil {
2020-12-26 10:37:08 +00:00
t.Error(fmt.Errorf("%v: %v", err, s.expression))
2020-11-03 23:48:43 +00:00
return
}
test.AssertResultComplexWithContext(t, s.expected, resultsToString(results), fmt.Sprintf("exp: %v\ndoc: %v", s.expression, s.document))
}
2020-11-16 01:09:57 +00:00
func resultsToString(results *list.List) []string {
var pretty []string = make([]string, 0)
for el := results.Front(); el != nil; el = el.Next() {
n := el.Value.(*CandidateNode)
pretty = append(pretty, NodeToString(n))
}
return pretty
}
2020-11-13 03:07:11 +00:00
func writeOrPanic(w *bufio.Writer, text string) {
_, err := w.WriteString(text)
if err != nil {
panic(err)
}
}
2020-11-13 10:22:05 +00:00
func copyFromHeader(title string, out *os.File) error {
2020-11-13 09:58:01 +00:00
source := fmt.Sprintf("doc/headers/%v.md", title)
_, err := os.Stat(source)
if os.IsNotExist(err) {
2020-11-13 10:22:05 +00:00
return nil
2020-11-13 09:58:01 +00:00
}
in, err := os.Open(source) // nolint gosec
if err != nil {
2020-11-13 10:22:05 +00:00
return err
2020-11-13 09:58:01 +00:00
}
defer safelyCloseFile(in)
_, err = io.Copy(out, in)
2020-11-13 10:22:05 +00:00
return err
}
2021-01-01 23:27:32 +00:00
func formatYaml(yaml string, filename string) string {
2020-11-13 10:22:05 +00:00
var output bytes.Buffer
printer := NewPrinter(bufio.NewWriter(&output), false, true, false, 2, true)
node, err := treeCreator.ParsePath(".. style= \"\"")
if err != nil {
panic(err)
}
2020-11-22 00:56:28 +00:00
streamEvaluator := NewStreamEvaluator()
2021-01-01 23:27:32 +00:00
err = streamEvaluator.Evaluate(filename, strings.NewReader(yaml), node, printer)
2020-11-13 10:22:05 +00:00
if err != nil {
panic(err)
}
return output.String()
2020-11-13 09:58:01 +00:00
}
2020-11-03 23:48:43 +00:00
func documentScenarios(t *testing.T, title string, scenarios []expressionScenario) {
f, err := os.Create(fmt.Sprintf("doc/%v.md", title))
if err != nil {
2020-11-13 02:19:54 +00:00
t.Error(err)
2020-11-13 09:58:01 +00:00
return
2020-11-03 23:48:43 +00:00
}
defer f.Close()
2020-11-13 09:58:01 +00:00
2020-11-13 10:22:05 +00:00
err = copyFromHeader(title, f)
2020-11-13 09:58:01 +00:00
if err != nil {
t.Error(err)
return
}
2020-11-03 23:48:43 +00:00
w := bufio.NewWriter(f)
2020-11-22 02:16:54 +00:00
writeOrPanic(w, "\n")
2020-11-13 09:58:01 +00:00
2020-11-22 02:16:54 +00:00
for _, s := range scenarios {
2020-11-06 00:45:18 +00:00
if !s.skipDoc {
2020-11-03 23:48:43 +00:00
2020-11-22 02:16:54 +00:00
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
2020-11-17 23:32:30 +00:00
if s.subdescription != "" {
writeOrPanic(w, s.subdescription)
writeOrPanic(w, "\n\n")
}
2020-11-14 23:50:30 +00:00
formattedDoc := ""
2021-01-01 23:27:32 +00:00
formattedDoc2 := ""
command := "eval"
2020-11-06 00:45:18 +00:00
if s.document != "" {
2020-11-14 23:50:30 +00:00
if s.dontFormatInputForDoc {
2020-11-22 02:16:54 +00:00
formattedDoc = s.document + "\n"
2020-11-14 23:50:30 +00:00
} else {
2021-01-01 23:27:32 +00:00
formattedDoc = formatYaml(s.document, "sample.yml")
2020-11-14 23:50:30 +00:00
}
2020-11-13 10:22:05 +00:00
2021-01-01 23:27:32 +00:00
writeOrPanic(w, "Given a sample.yml file of:\n")
2020-11-14 23:50:30 +00:00
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n", formattedDoc))
2021-01-01 23:27:32 +00:00
files := "sample.yml"
if s.document2 != "" {
if s.dontFormatInputForDoc {
formattedDoc2 = s.document2 + "\n"
} else {
formattedDoc2 = formatYaml(s.document2, "another.yml")
}
writeOrPanic(w, "And another sample another.yml file of:\n")
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n", formattedDoc2))
files = "sample.yml another.yml"
command = "eval-all"
}
2020-11-13 09:58:01 +00:00
writeOrPanic(w, "then\n")
2020-11-13 10:22:05 +00:00
if s.expression != "" {
2021-01-01 23:27:32 +00:00
writeOrPanic(w, fmt.Sprintf("```bash\nyq %v '%v' %v\n```\n", command, s.expression, files))
2020-11-13 10:22:05 +00:00
} else {
2021-01-01 23:27:32 +00:00
writeOrPanic(w, fmt.Sprintf("```bash\nyq %v %v\n```\n", command, files))
2020-11-13 10:22:05 +00:00
}
} else {
writeOrPanic(w, "Running\n")
2021-01-01 23:27:32 +00:00
writeOrPanic(w, fmt.Sprintf("```bash\nyq %v --null-input '%v'\n```\n", command, s.expression))
2020-11-06 00:45:18 +00:00
}
2020-11-03 23:48:43 +00:00
2020-11-13 09:58:01 +00:00
writeOrPanic(w, "will output\n")
2020-11-06 00:45:18 +00:00
var output bytes.Buffer
2020-11-06 03:37:01 +00:00
var err error
2020-11-13 02:19:54 +00:00
printer := NewPrinter(bufio.NewWriter(&output), false, true, false, 2, true)
2021-01-01 23:27:32 +00:00
node, err := treeCreator.ParsePath(s.expression)
if err != nil {
t.Error(fmt.Errorf("Error parsing expression %v of %v: %v", s.expression, s.description, err))
return
}
inputs := list.New()
2020-11-13 02:19:54 +00:00
2020-11-06 03:37:01 +00:00
if s.document != "" {
2021-01-01 23:27:32 +00:00
inputs, err = readDocuments(strings.NewReader(formattedDoc), "sample.yml", 0)
2020-11-13 02:19:54 +00:00
if err != nil {
2021-01-01 23:27:32 +00:00
t.Error(err, s.document, s.expression)
return
2020-11-13 02:19:54 +00:00
}
2021-01-01 23:27:32 +00:00
if s.document2 != "" {
moreInputs, err := readDocuments(strings.NewReader(formattedDoc2), "another.yml", 1)
if err != nil {
t.Error(err, s.document, s.expression)
return
}
inputs.PushBackList(moreInputs)
2020-11-13 03:07:11 +00:00
}
2020-11-06 03:37:01 +00:00
} else {
2021-01-01 23:27:32 +00:00
candidateNode := &CandidateNode{
Document: 0,
Filename: "",
Node: &yaml.Node{Tag: "!!null"},
FileIndex: 0,
2020-11-13 03:07:11 +00:00
}
2021-01-01 23:27:32 +00:00
inputs.PushBack(candidateNode)
}
results, err := treeNavigator.GetMatchingNodes(inputs, node)
if err != nil {
t.Error(err, s.expression)
}
err = printer.PrintResults(results)
if err != nil {
t.Error(err, s.expression)
2020-11-06 03:37:01 +00:00
}
2020-11-13 10:22:05 +00:00
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", output.String()))
2020-11-03 23:48:43 +00:00
}
}
w.Flush()
}