This commit is contained in:
Mike Farah 2021-07-19 19:58:47 +10:00
parent 3a6f73e836
commit 519cf1dcd7
3 changed files with 20 additions and 32 deletions

View File

@ -114,7 +114,7 @@ func formatYaml(yaml string, filename string) string {
panic(err) panic(err)
} }
streamEvaluator := NewStreamEvaluator() streamEvaluator := NewStreamEvaluator()
_, err = streamEvaluator.Evaluate(filename, strings.NewReader(yaml), node, printer) _, err = streamEvaluator.Evaluate(filename, strings.NewReader(yaml), node, printer, "")
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -101,7 +101,9 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List, leadingContent s
defer p.safelyFlush(bufferedWriter) defer p.safelyFlush(bufferedWriter)
if matchingNodes.Len() == 0 { if matchingNodes.Len() == 0 {
p.writeString(bufferedWriter, leadingContent) if err := p.writeString(bufferedWriter, leadingContent); err != nil {
return err
}
log.Debug("no matching results, nothing to print") log.Debug("no matching results, nothing to print")
return nil return nil
} }
@ -127,7 +129,9 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List, leadingContent s
if !printedLead { if !printedLead {
// we want to print this after the seperator logic // we want to print this after the seperator logic
p.writeString(bufferedWriter, leadingContent) if err := p.writeString(bufferedWriter, leadingContent); err != nil {
return err
}
printedLead = true printedLead = true
} }

View File

@ -17,34 +17,18 @@ a: apple
a: coconut a: coconut
` `
var leadingSeperatorSample = `---
a: good doc
`
func nodeToList(candidate *CandidateNode) *list.List { func nodeToList(candidate *CandidateNode) *list.List {
elMap := list.New() elMap := list.New()
elMap.PushBack(candidate) elMap.PushBack(candidate)
return elMap return elMap
} }
func TestPrinterWithLeadingSeperator(t *testing.T) { func TestPrinterWithLeadingContentEvalStyle(t *testing.T) {
var output bytes.Buffer test.AssertResult(t, "todo", "")
var writer = bufio.NewWriter(&output) }
printer := NewPrinter(writer, false, true, false, 2, true)
inputs, err := readDocuments(strings.NewReader(leadingSeperatorSample), "sample.yml", 0) func TestPrinterWithLeadingContentEvalAllStyle(t *testing.T) {
if err != nil { test.AssertResult(t, "todo", "")
panic(err)
}
printer.SetPrintLeadingSeperator(true)
err = printer.PrintResults(inputs)
if err != nil {
panic(err)
}
writer.Flush()
test.AssertResult(t, leadingSeperatorSample, output.String())
} }
func TestPrinterMultipleDocsInSequence(t *testing.T) { func TestPrinterMultipleDocsInSequence(t *testing.T) {
@ -66,17 +50,17 @@ func TestPrinterMultipleDocsInSequence(t *testing.T) {
el = el.Next() el = el.Next()
sample3 := nodeToList(el.Value.(*CandidateNode)) sample3 := nodeToList(el.Value.(*CandidateNode))
err = printer.PrintResults(sample1) err = printer.PrintResults(sample1, "")
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = printer.PrintResults(sample2) err = printer.PrintResults(sample2, "")
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = printer.PrintResults(sample3) err = printer.PrintResults(sample3, "")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -113,17 +97,17 @@ func TestPrinterMultipleFilesInSequence(t *testing.T) {
elNode.FileIndex = 2 elNode.FileIndex = 2
sample3 := nodeToList(elNode) sample3 := nodeToList(elNode)
err = printer.PrintResults(sample1) err = printer.PrintResults(sample1, "")
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = printer.PrintResults(sample2) err = printer.PrintResults(sample2, "")
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = printer.PrintResults(sample3) err = printer.PrintResults(sample3, "")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -142,7 +126,7 @@ func TestPrinterMultipleDocsInSinglePrint(t *testing.T) {
panic(err) panic(err)
} }
err = printer.PrintResults(inputs) err = printer.PrintResults(inputs, "")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -163,7 +147,7 @@ func TestPrinterMultipleDocsJson(t *testing.T) {
panic(err) panic(err)
} }
err = printer.PrintResults(inputs) err = printer.PrintResults(inputs, "")
if err != nil { if err != nil {
panic(err) panic(err)
} }