Fixed printing of scalars

This commit is contained in:
Mike Farah 2020-03-01 17:13:00 +11:00
parent ed234e37ce
commit 6b07143af7
4 changed files with 77 additions and 2 deletions

View File

@ -397,6 +397,78 @@ func TestReadScalarLengthCmd(t *testing.T) {
test.AssertResult(t, "4\n", result.Output)
}
func TestReadDoubleQuotedStringCmd(t *testing.T) {
content := `name: "meow face"`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read %s name", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "meow face\n", result.Output)
}
func TestReadSingleQuotedStringCmd(t *testing.T) {
content := `name: 'meow face'`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read %s name", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "meow face\n", result.Output)
}
func TestReadQuotedMultinlineStringCmd(t *testing.T) {
content := `test: |
abcdefg
hijklmno
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read %s test", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `abcdefg
hijklmno
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadBooleanCmd(t *testing.T) {
content := `name: true`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read %s name", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "true\n", result.Output)
}
func TestReadNumberCmd(t *testing.T) {
content := `name: 32.13`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read %s name", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "32.13\n", result.Output)
}
func TestReadDeepSplatCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -p pv ../examples/sample.yaml b.**")

View File

@ -103,6 +103,9 @@ func transformNode(node *yaml.Node) *yaml.Node {
func printNode(node *yaml.Node, writer io.Writer) error {
var encoder yqlib.Encoder
if node.Kind == yaml.ScalarNode {
return writeString(writer, node.Value+"\n")
}
if outputToJSON {
encoder = yqlib.NewJsonEncoder(writer, prettyPrint, indent)
} else {

View File

@ -11,7 +11,7 @@ var (
GitDescribe string
// Version is main version number that is being run at the moment.
Version = "3.2.0"
Version = "3.2.1"
// 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

View File

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