Always print new line so wc works properly

This commit is contained in:
Mike Farah 2020-02-21 10:29:37 +11:00
parent a46386e093
commit 0347516d82
6 changed files with 31 additions and 39 deletions

View File

@ -91,7 +91,7 @@ func TestReadCmd(t *testing.T) {
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
test.AssertResult(t, "2", result.Output) test.AssertResult(t, "2\n", result.Output)
} }
func TestCompareCmd(t *testing.T) { func TestCompareCmd(t *testing.T) {
@ -157,7 +157,7 @@ func TestReadWithAdvancedFilterCmd(t *testing.T) {
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
test.AssertResult(t, "4", result.Output) test.AssertResult(t, "4\n", result.Output)
} }
func TestReadWithAdvancedFilterMapCmd(t *testing.T) { func TestReadWithAdvancedFilterMapCmd(t *testing.T) {
@ -226,7 +226,7 @@ func TestReadWithKeyCmd(t *testing.T) {
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
test.AssertResult(t, "b.c", result.Output) test.AssertResult(t, "b.c\n", result.Output)
} }
func TestReadAnchorsCmd(t *testing.T) { func TestReadAnchorsCmd(t *testing.T) {
@ -235,7 +235,7 @@ func TestReadAnchorsCmd(t *testing.T) {
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
test.AssertResult(t, "1", result.Output) test.AssertResult(t, "1\n", result.Output)
} }
func TestReadAnchorsWithKeyAndValueCmd(t *testing.T) { func TestReadAnchorsWithKeyAndValueCmd(t *testing.T) {
@ -279,7 +279,7 @@ func TestReadMergeAnchorsOriginalCmd(t *testing.T) {
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
test.AssertResult(t, "original", result.Output) test.AssertResult(t, "original\n", result.Output)
} }
func TestReadMergeAnchorsExplodeJsonCmd(t *testing.T) { func TestReadMergeAnchorsExplodeJsonCmd(t *testing.T) {
@ -318,7 +318,7 @@ pointer: *value-pointer`
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
expectedOutput := `the value` expectedOutput := "the value\n"
test.AssertResult(t, expectedOutput, result.Output) test.AssertResult(t, expectedOutput, result.Output)
} }
@ -378,7 +378,7 @@ pointer: *value-pointer`
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
expectedOutput := `the value` expectedOutput := "the value\n"
test.AssertResult(t, expectedOutput, result.Output) test.AssertResult(t, expectedOutput, result.Output)
} }
@ -433,7 +433,7 @@ func TestReadMergeAnchorsOverrideCmd(t *testing.T) {
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
test.AssertResult(t, "ice", result.Output) test.AssertResult(t, "ice\n", result.Output)
} }
func TestReadMergeAnchorsPrefixMatchCmd(t *testing.T) { func TestReadMergeAnchorsPrefixMatchCmd(t *testing.T) {
@ -455,7 +455,7 @@ func TestReadMergeAnchorsListOriginalCmd(t *testing.T) {
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
test.AssertResult(t, "original", result.Output) test.AssertResult(t, "original\n", result.Output)
} }
func TestReadMergeAnchorsListOverrideInListCmd(t *testing.T) { func TestReadMergeAnchorsListOverrideInListCmd(t *testing.T) {
@ -464,7 +464,7 @@ func TestReadMergeAnchorsListOverrideInListCmd(t *testing.T) {
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
test.AssertResult(t, "coconut", result.Output) test.AssertResult(t, "coconut\n", result.Output)
} }
func TestReadMergeAnchorsListOverrideCmd(t *testing.T) { func TestReadMergeAnchorsListOverrideCmd(t *testing.T) {
@ -473,7 +473,7 @@ func TestReadMergeAnchorsListOverrideCmd(t *testing.T) {
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
test.AssertResult(t, "newbar", result.Output) test.AssertResult(t, "newbar\n", result.Output)
} }
func TestReadInvalidDocumentIndexCmd(t *testing.T) { func TestReadInvalidDocumentIndexCmd(t *testing.T) {
@ -515,7 +515,7 @@ func TestReadMultiCmd(t *testing.T) {
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
test.AssertResult(t, "here", result.Output) test.AssertResult(t, "here\n", result.Output)
} }
func TestReadMultiWithKeyAndValueCmd(t *testing.T) { func TestReadMultiWithKeyAndValueCmd(t *testing.T) {
@ -536,7 +536,8 @@ func TestReadMultiAllCmd(t *testing.T) {
test.AssertResult(t, test.AssertResult(t,
`first document `first document
second document second document
third document`, result.Output) third document
`, result.Output)
} }
func TestReadMultiAllWithKeyAndValueCmd(t *testing.T) { func TestReadMultiAllWithKeyAndValueCmd(t *testing.T) {
@ -558,7 +559,7 @@ func TestReadCmd_ArrayYaml(t *testing.T) {
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
test.AssertResult(t, "false", result.Output) test.AssertResult(t, "false\n", result.Output)
} }
func TestReadEmptyContentCmd(t *testing.T) { func TestReadEmptyContentCmd(t *testing.T) {
@ -740,7 +741,8 @@ func TestReadCmd_ArrayYaml_SplatWithKeyCmd(t *testing.T) {
t.Error(result.Error) t.Error(result.Error)
} }
expectedOutput := `[0] expectedOutput := `[0]
[1]` [1]
`
test.AssertResult(t, expectedOutput, result.Output) test.AssertResult(t, expectedOutput, result.Output)
} }
@ -751,7 +753,8 @@ func TestReadCmd_ArrayYaml_SplatKey(t *testing.T) {
t.Error(result.Error) t.Error(result.Error)
} }
expectedOutput := `false expectedOutput := `false
true` true
`
test.AssertResult(t, expectedOutput, result.Output) test.AssertResult(t, expectedOutput, result.Output)
} }
@ -950,7 +953,8 @@ b:
} }
expectedOutput := `more things expectedOutput := `more things
more things also` more things also
`
test.AssertResult(t, expectedOutput, result.Output) test.AssertResult(t, expectedOutput, result.Output)
} }
@ -1005,7 +1009,8 @@ b:
} }
expectedOutput := `b.there.c expectedOutput := `b.there.c
b.there2.c` b.there2.c
`
test.AssertResult(t, expectedOutput, result.Output) test.AssertResult(t, expectedOutput, result.Output)
} }

View File

@ -79,7 +79,7 @@ func appendDocument(originalMatchingNodes []*yqlib.NodeContext, dataBucket yaml.
func printValue(node *yaml.Node, writer io.Writer) error { func printValue(node *yaml.Node, writer io.Writer) error {
if node.Kind == yaml.ScalarNode { if node.Kind == yaml.ScalarNode {
_, errorWriting := writer.Write([]byte(node.Value)) _, errorWriting := writer.Write([]byte(node.Value + "\n"))
return errorWriting return errorWriting
} }
return printNode(node, writer) return printNode(node, writer)
@ -160,19 +160,13 @@ func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error {
return nil return nil
} }
var errorWriting error var errorWriting error
for index, mappedDoc := range matchingNodes { for _, mappedDoc := range matchingNodes {
switch printMode { switch printMode {
case "p": case "p":
errorWriting = writeString(bufferedWriter, lib.PathStackToString(mappedDoc.PathStack)) errorWriting = writeString(bufferedWriter, lib.PathStackToString(mappedDoc.PathStack)+"\n")
if errorWriting != nil { if errorWriting != nil {
return errorWriting return errorWriting
} }
if index < len(matchingNodes)-1 {
errorWriting = writeString(bufferedWriter, "\n")
if errorWriting != nil {
return errorWriting
}
}
case "pv", "vp": case "pv", "vp":
// put it into a node and print that. // put it into a node and print that.
var parentNode = yaml.Node{Kind: yaml.MappingNode} var parentNode = yaml.Node{Kind: yaml.MappingNode}
@ -186,14 +180,6 @@ func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error {
if err := printValue(mappedDoc.Node, bufferedWriter); err != nil { if err := printValue(mappedDoc.Node, bufferedWriter); err != nil {
return err return err
} }
// Printing our Scalars does not print a new line at the end
// we only want to do that if there are more values (so users can easily script extraction of values in the yaml)
if index < len(matchingNodes)-1 && mappedDoc.Node.Kind == yaml.ScalarNode {
errorWriting = writeString(bufferedWriter, "\n")
if errorWriting != nil {
return errorWriting
}
}
} }
} }

View File

@ -11,7 +11,7 @@ var (
GitDescribe string GitDescribe string
// Version is main version number that is being run at the moment. // Version is main version number that is being run at the moment.
Version = "3.1.1" Version = "3.1.2"
// VersionPrerelease is a pre-release marker for the version. If this is "" (empty string) // VersionPrerelease is a pre-release marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release // then it means that it is a final release. Otherwise, this is a pre-release

3
go.mod
View File

@ -20,8 +20,7 @@ require (
golang.org/x/tools v0.0.0-20191213221258-04c2e8eff935 // indirect golang.org/x/tools v0.0.0-20191213221258-04c2e8eff935 // indirect
gopkg.in/imdario/mergo.v0 v0.3.7 // indirect gopkg.in/imdario/mergo.v0 v0.3.7 // indirect
gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473 gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473
gopkg.in/yaml.v2 v2.2.8 // indirect gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71
gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2
) )
go 1.13 go 1.13

2
go.sum
View File

@ -145,4 +145,6 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2 h1:XZx7nhd5GMaZpmDaEHFVafUZC7ya0fuo7cSJ3UCKYmM= gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2 h1:XZx7nhd5GMaZpmDaEHFVafUZC7ya0fuo7cSJ3UCKYmM=
gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71 h1:Xe2gvTZUJpsvOWUnvmL/tmhVBZUmHSvLbMjRj6NUUKo=
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

View File

@ -1,5 +1,5 @@
name: yq name: yq
version: '3.1.1' version: '3.1.2'
summary: A lightweight and portable command-line YAML processor summary: A lightweight and portable command-line YAML processor
description: | description: |
The aim of the project is to be the jq or sed of yaml files. The aim of the project is to be the jq or sed of yaml files.