yq/commands_test.go

1552 lines
37 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"os"
"runtime"
"strings"
"testing"
2019-12-09 02:44:53 +00:00
"github.com/mikefarah/yq/v3/test"
2019-12-01 20:10:42 +00:00
"github.com/spf13/cobra"
)
func getRootCommand() *cobra.Command {
return newCommandCLI()
}
func TestRootCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "")
if result.Error != nil {
t.Error(result.Error)
}
if !strings.Contains(result.Output, "Usage:") {
t.Error("Expected usage message to be printed out, but the usage message was not found.")
}
2019-05-14 17:28:06 +00:00
}
func TestRootCmd_Help(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "--help")
2019-05-14 17:28:06 +00:00
if result.Error != nil {
t.Error(result.Error)
}
2019-05-14 17:28:06 +00:00
if !strings.Contains(result.Output, "yq is a lightweight and portable command-line YAML processor. It aims to be the jq or sed of yaml files.") {
t.Error("Expected usage message to be printed out, but the usage message was not found.")
}
}
func TestRootCmd_VerboseLong(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "--verbose")
if result.Error != nil {
t.Error(result.Error)
}
if !verbose {
t.Error("Expected verbose to be true")
}
}
func TestRootCmd_VerboseShort(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "-v")
if result.Error != nil {
t.Error(result.Error)
}
if !verbose {
t.Error("Expected verbose to be true")
}
}
func TestRootCmd_VersionShort(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "-V")
if result.Error != nil {
t.Error(result.Error)
}
2018-04-09 05:43:22 +00:00
if !strings.Contains(result.Output, "yq version") {
t.Error("expected version message to be printed out, but the message was not found.")
}
}
func TestRootCmd_VersionLong(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "--version")
if result.Error != nil {
t.Error(result.Error)
}
2018-04-09 05:43:22 +00:00
if !strings.Contains(result.Output, "yq version") {
t.Error("expected version message to be printed out, but the message was not found.")
}
}
func TestReadCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read examples/sample.yaml b.c")
if result.Error != nil {
t.Error(result.Error)
}
2019-12-23 23:35:57 +00:00
test.AssertResult(t, "2", result.Output)
}
2020-01-11 07:52:15 +00:00
func TestReadWithAdvancedFilterCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read examples/sample.yaml b.e(name == sam).value")
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "4", result.Output)
}
2019-12-23 23:46:21 +00:00
func TestReadWithKeyAndValueCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -p pv examples/sample.yaml b.c")
2019-12-23 23:46:21 +00:00
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "b.c: 2\n", result.Output)
}
2019-12-29 22:21:21 +00:00
func TestReadArrayCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -p pv examples/sample.yaml b.e.1.name")
2019-12-29 22:21:21 +00:00
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "b.e.1.name: sam\n", result.Output)
}
2019-12-30 03:51:07 +00:00
func TestReadDeepSplatCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -p pv examples/sample.yaml b.**")
2019-12-30 03:51:07 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b.c: 2
b.d.[0]: 3
b.d.[1]: 4
b.d.[2]: 5
b.e.[0].name: fred
b.e.[0].value: 3
b.e.[1].name: sam
b.e.[1].value: 4
`
test.AssertResult(t, expectedOutput, result.Output)
}
2019-12-31 02:21:39 +00:00
func TestReadDeepSplatWithSuffixCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -p pv examples/sample.yaml b.**.name")
2019-12-31 02:21:39 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b.e.[0].name: fred
b.e.[1].name: sam
`
test.AssertResult(t, expectedOutput, result.Output)
}
2019-12-23 23:46:21 +00:00
func TestReadWithKeyCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -p p examples/sample.yaml b.c")
2019-12-23 23:46:21 +00:00
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "b.c", result.Output)
}
2019-12-16 09:38:55 +00:00
func TestReadAnchorsCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read examples/simple-anchor.yaml foobar.a")
if result.Error != nil {
t.Error(result.Error)
}
2019-12-23 23:35:57 +00:00
test.AssertResult(t, "1", result.Output)
2019-12-16 09:38:55 +00:00
}
2019-12-23 23:46:21 +00:00
func TestReadAnchorsWithKeyAndValueCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -p pv examples/simple-anchor.yaml foobar.a")
2019-12-23 23:46:21 +00:00
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "foobar.a: 1\n", result.Output)
}
2019-12-22 04:15:15 +00:00
func TestReadMergeAnchorsOriginalCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read examples/merge-anchor.yaml foobar.a")
if result.Error != nil {
t.Error(result.Error)
}
2019-12-23 23:35:57 +00:00
test.AssertResult(t, "original", result.Output)
2019-12-22 04:15:15 +00:00
}
func TestReadMergeAnchorsOverrideCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read examples/merge-anchor.yaml foobar.thing")
if result.Error != nil {
t.Error(result.Error)
}
2019-12-23 23:35:57 +00:00
test.AssertResult(t, "ice", result.Output)
2019-12-22 04:15:15 +00:00
}
2019-12-25 01:11:04 +00:00
func TestReadMergeAnchorsPrefixMatchCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "r -p pv examples/merge-anchor.yaml foobar.th*")
2019-12-25 01:11:04 +00:00
if result.Error != nil {
t.Error(result.Error)
}
2019-12-27 08:06:08 +00:00
expectedOutput := `foobar.thirty: well beyond
foobar.thing: ice
foobar.thirsty: yep
`
2019-12-25 01:11:04 +00:00
test.AssertResult(t, expectedOutput, result.Output)
}
2019-12-22 04:15:15 +00:00
func TestReadMergeAnchorsListOriginalCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read examples/merge-anchor.yaml foobarList.a")
if result.Error != nil {
t.Error(result.Error)
}
2019-12-23 23:35:57 +00:00
test.AssertResult(t, "original", result.Output)
2019-12-22 04:15:15 +00:00
}
func TestReadMergeAnchorsListOverrideInListCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read examples/merge-anchor.yaml foobarList.thing")
if result.Error != nil {
t.Error(result.Error)
}
2019-12-23 23:35:57 +00:00
test.AssertResult(t, "coconut", result.Output)
2019-12-22 04:15:15 +00:00
}
func TestReadMergeAnchorsListOverrideCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read examples/merge-anchor.yaml foobarList.c")
if result.Error != nil {
t.Error(result.Error)
}
2019-12-23 23:35:57 +00:00
test.AssertResult(t, "newbar", result.Output)
2019-12-22 04:15:15 +00:00
}
2018-07-08 12:04:31 +00:00
func TestReadInvalidDocumentIndexCmd(t *testing.T) {
2018-07-08 11:57:56 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -df examples/sample.yaml b.c")
2018-07-08 11:57:56 +00:00
if result.Error == nil {
t.Error("Expected command to fail due to invalid path")
}
expectedOutput := `Document index f is not a integer or *: strconv.ParseInt: parsing "f": invalid syntax`
test.AssertResult(t, expectedOutput, result.Error.Error())
2018-07-08 11:57:56 +00:00
}
2018-07-08 12:04:31 +00:00
func TestReadBadDocumentIndexCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -d1 examples/sample.yaml b.c")
2018-07-08 12:04:31 +00:00
if result.Error == nil {
t.Error("Expected command to fail due to invalid path")
}
2020-01-06 03:22:24 +00:00
expectedOutput := `Could not process document index 1 as there are only 1 document(s)`
test.AssertResult(t, expectedOutput, result.Error.Error())
2018-07-08 12:04:31 +00:00
}
2018-07-08 11:47:01 +00:00
func TestReadOrderCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read examples/order.yaml")
2018-07-08 11:47:01 +00:00
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t,
2018-07-08 11:47:01 +00:00
`version: 3
application: MyApp
`,
result.Output)
}
2018-06-14 22:38:30 +00:00
func TestReadMultiCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -d 1 examples/multiple_docs.yaml another.document")
2018-06-14 22:38:30 +00:00
if result.Error != nil {
t.Error(result.Error)
}
2019-12-23 23:35:57 +00:00
test.AssertResult(t, "here", result.Output)
2018-06-14 22:38:30 +00:00
}
2019-12-23 23:46:21 +00:00
func TestReadMultiWithKeyAndValueCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -p vp -d 1 examples/multiple_docs.yaml another.document")
2019-12-23 23:46:21 +00:00
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "another.document: here\n", result.Output)
}
2018-07-08 11:47:01 +00:00
func TestReadMultiAllCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -d* examples/multiple_docs.yaml commonKey")
2018-07-08 11:47:01 +00:00
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t,
2019-12-23 23:35:57 +00:00
`first document
second document
third document`, result.Output)
2018-07-08 11:47:01 +00:00
}
2019-12-23 23:46:21 +00:00
func TestReadMultiAllWithKeyAndValueCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -p pv -d* examples/multiple_docs.yaml commonKey")
2019-12-23 23:46:21 +00:00
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t,
`commonKey: first document
commonKey: second document
commonKey: third document
`, result.Output)
}
func TestReadCmd_ArrayYaml(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read examples/array.yaml [0].gather_facts")
if result.Error != nil {
t.Error(result.Error)
}
2019-12-23 23:35:57 +00:00
test.AssertResult(t, "false", result.Output)
}
func TestReadCmd_ArrayYaml_NoPath(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read examples/array.yaml")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `- become: true
gather_facts: false
hosts: lalaland
2019-12-09 02:44:53 +00:00
name: "Apply smth"
roles:
- lala
- land
serial: 1
2019-12-15 07:24:23 +00:00
- become: false
gather_facts: true
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadCmd_ArrayYaml_OneElement(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read examples/array.yaml [0]")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `become: true
gather_facts: false
hosts: lalaland
2019-12-09 02:44:53 +00:00
name: "Apply smth"
roles:
- lala
- land
serial: 1
`
test.AssertResult(t, expectedOutput, result.Output)
}
2019-12-23 23:46:21 +00:00
func TestReadCmd_ArrayYaml_SplatCmd(t *testing.T) {
cmd := getRootCommand()
2019-12-23 23:35:57 +00:00
result := test.RunCmd(cmd, "read examples/array.yaml [*]")
if result.Error != nil {
t.Error(result.Error)
}
2019-12-23 23:35:57 +00:00
expectedOutput := `become: true
gather_facts: false
hosts: lalaland
name: "Apply smth"
roles:
- lala
- land
serial: 1
become: false
gather_facts: true
`
test.AssertResult(t, expectedOutput, result.Output)
}
2019-12-23 23:46:21 +00:00
func TestReadCmd_ArrayYaml_SplatWithKeyAndValueCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -p pv examples/array.yaml [*]")
2019-12-23 23:46:21 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `'[0]':
become: true
gather_facts: false
hosts: lalaland
name: "Apply smth"
roles:
- lala
- land
serial: 1
'[1]':
become: false
gather_facts: true
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadCmd_ArrayYaml_SplatWithKeyCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -p p examples/array.yaml [*]")
2019-12-23 23:46:21 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `[0]
[1]`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadCmd_ArrayYaml_SplatKey(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read examples/array.yaml [*].gather_facts")
if result.Error != nil {
t.Error(result.Error)
}
2019-12-23 23:35:57 +00:00
expectedOutput := `false
true`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadCmd_ArrayYaml_ErrorBadPath(t *testing.T) {
cmd := getRootCommand()
2019-12-23 23:35:57 +00:00
result := test.RunCmd(cmd, "read examples/array.yaml [x].gather_facts")
2019-12-25 01:11:04 +00:00
if result.Error == nil {
t.Error("Expected command to fail due to missing arg")
}
2019-12-31 02:21:39 +00:00
expectedOutput := `Error reading path in document index 0: Error parsing array index 'x' for '': strconv.ParseInt: parsing "x": invalid syntax`
2019-12-25 01:11:04 +00:00
test.AssertResult(t, expectedOutput, result.Error.Error())
}
2019-12-15 07:38:40 +00:00
func TestReadCmd_ArrayYaml_Splat_ErrorBadPath(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read examples/array.yaml [*].roles[x]")
2019-12-31 02:21:39 +00:00
if result.Error == nil {
t.Error("Expected command to fail due to missing arg")
}
expectedOutput := `Error reading path in document index 0: Error parsing array index 'x' for '[0].roles': strconv.ParseInt: parsing "x": invalid syntax`
test.AssertResult(t, expectedOutput, result.Error.Error())
2019-12-15 07:38:40 +00:00
}
func TestReadCmd_Error(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read")
if result.Error == nil {
t.Error("Expected command to fail due to missing arg")
}
expectedOutput := `Must provide filename`
test.AssertResult(t, expectedOutput, result.Error.Error())
}
func TestReadCmd_ErrorEmptyFilename(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read ")
if result.Error == nil {
t.Error("Expected command to fail due to missing arg")
}
expectedOutput := `Must provide filename`
test.AssertResult(t, expectedOutput, result.Error.Error())
}
func TestReadCmd_ErrorUnreadableFile(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read fake-unknown")
if result.Error == nil {
t.Error("Expected command to fail due to unknown file")
}
var expectedOutput string
if runtime.GOOS == "windows" {
expectedOutput = `open fake-unknown: The system cannot find the file specified.`
} else {
expectedOutput = `open fake-unknown: no such file or directory`
}
test.AssertResult(t, expectedOutput, result.Error.Error())
}
2019-12-15 07:38:40 +00:00
func TestReadCmd_ErrorBadPath(t *testing.T) {
content := `b:
d:
e:
- 3
- 4
f:
- 1
- 2
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2019-12-09 02:44:53 +00:00
2019-12-15 07:38:40 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read %s b.d.*.[x]", filename))
2019-12-31 02:21:39 +00:00
expectedOutput := `Error reading path in document index 0: Error parsing array index 'x' for 'b.d.e': strconv.ParseInt: parsing "x": invalid syntax`
test.AssertResult(t, expectedOutput, result.Error.Error())
2019-12-15 07:38:40 +00:00
}
func TestReadCmd_Verbose(t *testing.T) {
cmd := getRootCommand()
2019-12-23 23:46:21 +00:00
result := test.RunCmd(cmd, "read -v examples/sample.yaml b.c")
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "2", result.Output)
}
2019-12-09 02:44:53 +00:00
// func TestReadCmd_ToJson(t *testing.T) {
// cmd := getRootCommand()
// result := test.RunCmd(cmd, "read -j examples/sample.yaml b.c")
// if result.Error != nil {
// t.Error(result.Error)
// }
// test.AssertResult(t, "2\n", result.Output)
// }
// func TestReadCmd_ToJsonLong(t *testing.T) {
// cmd := getRootCommand()
// result := test.RunCmd(cmd, "read --tojson examples/sample.yaml b.c")
// if result.Error != nil {
// t.Error(result.Error)
// }
// test.AssertResult(t, "2\n", result.Output)
// }
2019-12-23 23:46:21 +00:00
func TestReadSplatPrefixCmd(t *testing.T) {
2019-12-15 07:24:23 +00:00
content := `a: 2
b:
hi:
c: things
d: something else
there:
c: more things
d: more something else
there2:
c: more things also
d: more something else also
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
2019-12-23 23:35:57 +00:00
result := test.RunCmd(cmd, fmt.Sprintf("read %s b.there*.c", filename))
2019-12-15 07:24:23 +00:00
if result.Error != nil {
t.Error(result.Error)
}
2019-12-23 23:35:57 +00:00
expectedOutput := `more things
more things also`
2019-12-15 07:24:23 +00:00
test.AssertResult(t, expectedOutput, result.Output)
}
2019-12-23 23:46:21 +00:00
func TestReadSplatPrefixWithKeyAndValueCmd(t *testing.T) {
content := `a: 2
b:
hi:
c: things
d: something else
there:
c: more things
d: more something else
there2:
c: more things also
d: more something else also
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -p pv %s b.there*.c", filename))
2019-12-23 23:46:21 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b.there.c: more things
b.there2.c: more things also
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadSplatPrefixWithKeyCmd(t *testing.T) {
content := `a: 2
b:
hi:
c: things
d: something else
there:
c: more things
d: more something else
there2:
c: more things also
d: more something else also
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -p p %s b.there*.c", filename))
2019-12-23 23:46:21 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b.there.c
b.there2.c`
test.AssertResult(t, expectedOutput, result.Output)
}
2018-11-18 15:35:28 +00:00
func TestPrefixCmd(t *testing.T) {
content := `b:
c: 3
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-11-18 15:35:28 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("prefix %s d", filename))
2018-11-18 15:35:28 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `d:
b:
c: 3
`
test.AssertResult(t, expectedOutput, result.Output)
2018-11-18 15:35:28 +00:00
}
2018-11-19 22:47:17 +00:00
func TestPrefixCmdArray(t *testing.T) {
content := `b:
c: 3
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-11-19 22:47:17 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("prefix %s [+].d.[+]", filename))
2018-11-19 22:47:17 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `- d:
- b:
c: 3
`
test.AssertResult(t, expectedOutput, result.Output)
2018-11-19 22:47:17 +00:00
}
2018-11-18 15:35:28 +00:00
func TestPrefixCmd_MultiLayer(t *testing.T) {
content := `b:
c: 3
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-11-18 15:35:28 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("prefix %s d.e.f", filename))
2018-11-18 15:35:28 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `d:
e:
f:
b:
c: 3
`
test.AssertResult(t, expectedOutput, result.Output)
2018-11-18 15:35:28 +00:00
}
func TestPrefixMultiCmd(t *testing.T) {
content := `b:
c: 3
---
apples: great
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-11-18 15:35:28 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("prefix %s -d 1 d", filename))
2018-11-18 15:35:28 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
c: 3
---
d:
apples: great
`
test.AssertResult(t, expectedOutput, result.Output)
2018-11-18 15:35:28 +00:00
}
func TestPrefixInvalidDocumentIndexCmd(t *testing.T) {
content := `b:
c: 3
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-11-18 15:35:28 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("prefix %s -df d", filename))
2018-11-18 15:35:28 +00:00
if result.Error == nil {
t.Error("Expected command to fail due to invalid path")
}
expectedOutput := `Document index f is not a integer or *: strconv.ParseInt: parsing "f": invalid syntax`
test.AssertResult(t, expectedOutput, result.Error.Error())
2018-11-18 15:35:28 +00:00
}
func TestPrefixBadDocumentIndexCmd(t *testing.T) {
content := `b:
c: 3
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-11-18 15:35:28 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("prefix %s -d 1 d", filename))
2018-11-18 15:35:28 +00:00
if result.Error == nil {
t.Error("Expected command to fail due to invalid path")
}
2019-01-20 22:33:14 +00:00
expectedOutput := `asked to process document index 1 but there are only 1 document(s)`
test.AssertResult(t, expectedOutput, result.Error.Error())
2018-11-18 15:35:28 +00:00
}
func TestPrefixMultiAllCmd(t *testing.T) {
content := `b:
c: 3
---
apples: great
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-11-18 15:35:28 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("prefix %s -d * d", filename))
2018-11-18 15:35:28 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `d:
b:
c: 3
---
d:
apples: great`
test.AssertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
2018-11-18 15:35:28 +00:00
}
func TestPrefixCmd_Error(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "prefix")
2018-11-18 15:35:28 +00:00
if result.Error == nil {
t.Error("Expected command to fail due to missing arg")
}
expectedOutput := `Must provide <filename> <prefixed_path>`
test.AssertResult(t, expectedOutput, result.Error.Error())
2018-11-18 15:35:28 +00:00
}
func TestPrefixCmd_ErrorUnreadableFile(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "prefix fake-unknown a.b")
2018-11-18 15:35:28 +00:00
if result.Error == nil {
t.Error("Expected command to fail due to unknown file")
}
var expectedOutput string
if runtime.GOOS == "windows" {
expectedOutput = `open fake-unknown: The system cannot find the file specified.`
} else {
expectedOutput = `open fake-unknown: no such file or directory`
}
test.AssertResult(t, expectedOutput, result.Error.Error())
2018-11-18 15:35:28 +00:00
}
func TestPrefixCmd_Verbose(t *testing.T) {
content := `b:
c: 3
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-11-18 15:35:28 +00:00
cmd := getRootCommand()
2019-12-23 23:35:57 +00:00
result := test.RunCmd(cmd, fmt.Sprintf("prefix %s x", filename))
2018-11-18 15:35:28 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `x:
b:
c: 3
`
test.AssertResult(t, expectedOutput, result.Output)
2018-11-18 15:35:28 +00:00
}
func TestPrefixCmd_Inplace(t *testing.T) {
content := `b:
c: 3
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-11-18 15:35:28 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("prefix -i %s d", filename))
2018-11-18 15:35:28 +00:00
if result.Error != nil {
t.Error(result.Error)
}
gotOutput := test.ReadTempYamlFile(filename)
2018-11-18 15:35:28 +00:00
expectedOutput := `d:
b:
c: 3`
test.AssertResult(t, expectedOutput, strings.Trim(gotOutput, "\n "))
2018-11-18 15:35:28 +00:00
}
func TestNewCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "new b.c 3")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
c: 3
`
test.AssertResult(t, expectedOutput, result.Output)
}
2020-01-05 21:12:30 +00:00
func TestNewArrayCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "new b[0] 3")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
- 3
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestNewCmd_Error(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "new b.c")
if result.Error == nil {
t.Error("Expected command to fail due to missing arg")
}
expectedOutput := `Must provide <path_to_update> <value>`
test.AssertResult(t, expectedOutput, result.Error.Error())
}
func TestWriteCmd(t *testing.T) {
content := `b:
c: 3
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("write %s b.c 7", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
c: 7
`
test.AssertResult(t, expectedOutput, result.Output)
}
2019-12-16 05:17:01 +00:00
func TestWriteCmdScript(t *testing.T) {
content := `b:
c: 3
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2019-12-23 23:35:57 +00:00
updateScript := `- command: update
path: b.c
value: 7`
scriptFilename := test.WriteTempYamlFile(updateScript)
defer test.RemoveTempYamlFile(scriptFilename)
2019-12-16 05:17:01 +00:00
cmd := getRootCommand()
2019-12-23 23:35:57 +00:00
result := test.RunCmd(cmd, fmt.Sprintf("write --script %s %s", scriptFilename, filename))
2019-12-16 05:17:01 +00:00
if result.Error != nil {
t.Error(result.Error)
}
2019-12-23 23:35:57 +00:00
expectedOutput := `b:
c: 7
`
2019-12-16 05:17:01 +00:00
test.AssertResult(t, expectedOutput, result.Output)
}
func TestWriteCmdEmptyScript(t *testing.T) {
content := `b:
c: 3
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
updateScript := ``
scriptFilename := test.WriteTempYamlFile(updateScript)
defer test.RemoveTempYamlFile(scriptFilename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("write --script %s %s", scriptFilename, filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
c: 3
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestWriteMultiCmd(t *testing.T) {
content := `b:
c: 3
---
apples: great
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("write %s -d 1 apples ok", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
c: 3
---
apples: ok
`
test.AssertResult(t, expectedOutput, result.Output)
}
2018-07-08 12:04:31 +00:00
func TestWriteInvalidDocumentIndexCmd(t *testing.T) {
content := `b:
c: 3
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-07-08 12:04:31 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("write %s -df apples ok", filename))
2018-07-08 12:04:31 +00:00
if result.Error == nil {
t.Error("Expected command to fail due to invalid path")
}
expectedOutput := `Document index f is not a integer or *: strconv.ParseInt: parsing "f": invalid syntax`
test.AssertResult(t, expectedOutput, result.Error.Error())
2018-07-08 12:04:31 +00:00
}
func TestWriteBadDocumentIndexCmd(t *testing.T) {
content := `b:
c: 3
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-07-08 12:04:31 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("write %s -d 1 apples ok", filename))
2018-07-08 12:04:31 +00:00
if result.Error == nil {
t.Error("Expected command to fail due to invalid path")
}
2019-01-20 22:33:14 +00:00
expectedOutput := `asked to process document index 1 but there are only 1 document(s)`
test.AssertResult(t, expectedOutput, result.Error.Error())
2018-07-08 12:04:31 +00:00
}
2018-06-20 01:45:51 +00:00
func TestWriteMultiAllCmd(t *testing.T) {
content := `b:
c: 3
---
apples: great
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-06-20 01:45:51 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("write %s -d * apples ok", filename))
2018-06-20 01:45:51 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
c: 3
apples: ok
---
apples: ok`
test.AssertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
2018-06-20 01:45:51 +00:00
}
func TestWriteCmd_EmptyArray(t *testing.T) {
content := `b: 3`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("write %s a []", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b: 3
a: []
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestWriteCmd_Error(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "write")
if result.Error == nil {
t.Error("Expected command to fail due to missing arg")
}
expectedOutput := `Must provide <filename> <path_to_update> <value>`
test.AssertResult(t, expectedOutput, result.Error.Error())
}
func TestWriteCmd_ErrorUnreadableFile(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "write fake-unknown a.b 3")
if result.Error == nil {
t.Error("Expected command to fail due to unknown file")
}
var expectedOutput string
if runtime.GOOS == "windows" {
expectedOutput = `open fake-unknown: The system cannot find the file specified.`
} else {
expectedOutput = `open fake-unknown: no such file or directory`
}
test.AssertResult(t, expectedOutput, result.Error.Error())
}
func TestWriteCmd_Inplace(t *testing.T) {
content := `b:
c: 3
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("write -i %s b.c 7", filename))
if result.Error != nil {
t.Error(result.Error)
}
gotOutput := test.ReadTempYamlFile(filename)
expectedOutput := `b:
c: 7`
test.AssertResult(t, expectedOutput, strings.Trim(gotOutput, "\n "))
}
func TestWriteCmd_Append(t *testing.T) {
content := `b:
- foo
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("write %s b[+] 7", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
- foo
- 7
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestWriteCmd_AppendEmptyArray(t *testing.T) {
content := `a: 2
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
2019-12-23 23:35:57 +00:00
result := test.RunCmd(cmd, fmt.Sprintf("write %s b[+] v", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a: 2
b:
- v
`
test.AssertResult(t, expectedOutput, result.Output)
}
2019-04-30 22:49:50 +00:00
func TestWriteCmd_SplatArray(t *testing.T) {
content := `b:
- c: thing
- c: another thing
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2019-04-30 22:49:50 +00:00
cmd := getRootCommand()
2019-12-23 23:35:57 +00:00
result := test.RunCmd(cmd, fmt.Sprintf("write %s b[*].c new", filename))
2019-04-30 22:49:50 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
- c: new
- c: new
`
test.AssertResult(t, expectedOutput, result.Output)
2019-04-30 22:49:50 +00:00
}
2019-05-12 23:13:45 +00:00
func TestWriteCmd_SplatMap(t *testing.T) {
content := `b:
c: thing
d: another thing
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2019-05-12 23:13:45 +00:00
cmd := getRootCommand()
2019-12-23 23:35:57 +00:00
result := test.RunCmd(cmd, fmt.Sprintf("write %s b.* new", filename))
2019-05-12 23:13:45 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
c: new
d: new
`
test.AssertResult(t, expectedOutput, result.Output)
2019-05-12 23:13:45 +00:00
}
func TestWriteCmd_SplatMapEmpty(t *testing.T) {
content := `b:
c: thing
d: another thing
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2019-05-12 23:13:45 +00:00
cmd := getRootCommand()
2019-12-23 23:35:57 +00:00
result := test.RunCmd(cmd, fmt.Sprintf("write %s b.c.* new", filename))
2019-05-12 23:13:45 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
2020-01-05 03:22:18 +00:00
c: thing
2019-05-12 23:13:45 +00:00
d: another thing
`
test.AssertResult(t, expectedOutput, result.Output)
2019-05-12 23:13:45 +00:00
}
2019-12-27 21:51:54 +00:00
func TestDeleteYamlCmd(t *testing.T) {
2018-06-14 23:43:20 +00:00
content := `a: 2
b:
c: things
d: something else
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-06-14 23:43:20 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("delete %s b.c", filename))
2018-06-14 23:43:20 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a: 2
b:
d: something else
`
test.AssertResult(t, expectedOutput, result.Output)
2018-06-14 23:43:20 +00:00
}
func TestDeleteSplatYaml(t *testing.T) {
2019-12-15 07:38:40 +00:00
content := `a: other
b: [3, 4]
c:
toast: leave
test: 1
tell: 1
taco: cool
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
2019-12-23 23:35:57 +00:00
result := test.RunCmd(cmd, fmt.Sprintf("delete %s c.te*", filename))
if result.Error != nil {
t.Error(result.Error)
}
2019-12-15 07:38:40 +00:00
expectedOutput := `a: other
b: [3, 4]
c:
toast: leave
taco: cool
`
test.AssertResult(t, expectedOutput, result.Output)
}
2019-05-14 01:20:41 +00:00
func TestDeleteSplatArrayYaml(t *testing.T) {
content := `a: 2
b:
hi:
2019-05-14 17:28:06 +00:00
- thing: item1
2019-05-14 01:20:41 +00:00
name: fred
2019-05-14 17:28:06 +00:00
- thing: item2
2019-05-14 01:20:41 +00:00
name: sam
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2019-05-14 01:20:41 +00:00
cmd := getRootCommand()
2020-01-06 03:22:24 +00:00
result := test.RunCmd(cmd, fmt.Sprintf("delete %s b.hi[*].thing", filename))
2019-05-14 01:20:41 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a: 2
b:
hi:
- name: fred
- name: sam
`
test.AssertResult(t, expectedOutput, result.Output)
2019-05-14 01:20:41 +00:00
}
func TestDeleteSplatPrefixYaml(t *testing.T) {
content := `a: 2
b:
hi:
c: things
d: something else
there:
c: more things
d: more something else
there2:
c: more things also
d: more something else also
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
2020-01-06 03:22:24 +00:00
result := test.RunCmd(cmd, fmt.Sprintf("delete %s b.there*.c", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a: 2
b:
hi:
c: things
d: something else
there:
d: more something else
there2:
d: more something else also
`
test.AssertResult(t, expectedOutput, result.Output)
}
2018-06-14 23:43:20 +00:00
func TestDeleteYamlArray(t *testing.T) {
content := `- 1
- 2
- 3
2018-06-14 23:43:20 +00:00
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-06-14 23:43:20 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("delete %s [1]", filename))
2018-06-14 23:43:20 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `- 1
- 3
`
test.AssertResult(t, expectedOutput, result.Output)
2018-06-14 23:43:20 +00:00
}
func TestDeleteYamlMulti(t *testing.T) {
content := `apples: great
---
- 1
- 2
- 3
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-06-14 23:43:20 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("delete -d 1 %s [1]", filename))
2018-06-14 23:43:20 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `apples: great
---
- 1
- 3
`
test.AssertResult(t, expectedOutput, result.Output)
2018-06-14 23:43:20 +00:00
}
2018-06-20 01:45:51 +00:00
func TestDeleteYamlMultiAllCmd(t *testing.T) {
content := `b:
c: 3
apples: great
---
apples: great
something: else
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-06-20 01:45:51 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("delete %s -d * apples", filename))
2018-06-20 01:45:51 +00:00
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
c: 3
---
something: else`
test.AssertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
2018-06-20 01:45:51 +00:00
}
2020-01-05 04:14:14 +00:00
func TestMergeCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge examples/data1.yaml examples/data2.yaml")
if result.Error != nil {
t.Error(result.Error)
}
2020-01-06 03:22:24 +00:00
expectedOutput := `a: simple # just the best
2020-01-05 04:14:14 +00:00
b: [1, 2]
c:
test: 1
2020-01-05 04:14:14 +00:00
toast: leave
tell: 1
taco: cool
`
test.AssertResult(t, expectedOutput, result.Output)
}
2020-01-05 04:14:14 +00:00
func TestMergeNoAutoCreateCmd(t *testing.T) {
2018-06-20 01:45:51 +00:00
cmd := getRootCommand()
2020-01-05 04:14:14 +00:00
result := test.RunCmd(cmd, "merge -c=false examples/data1.yaml examples/data2.yaml")
if result.Error != nil {
t.Error(result.Error)
}
2020-01-06 03:22:24 +00:00
expectedOutput := `a: simple # just the best
2020-01-05 04:14:14 +00:00
b: [1, 2]
c:
test: 1
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestMergeOverwriteCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge -c=false --overwrite examples/data1.yaml examples/data2.yaml")
2018-06-20 01:45:51 +00:00
if result.Error != nil {
t.Error(result.Error)
}
2020-01-05 21:12:30 +00:00
expectedOutput := `a: other # better than the original
2020-01-05 04:14:14 +00:00
b: [3, 4]
2018-07-07 05:26:56 +00:00
c:
test: 1
`
test.AssertResult(t, expectedOutput, result.Output)
2018-07-07 05:26:56 +00:00
}
2020-01-05 04:28:24 +00:00
func TestMergeAppendCmd(t *testing.T) {
2018-07-07 05:26:56 +00:00
cmd := getRootCommand()
2020-01-05 04:28:24 +00:00
result := test.RunCmd(cmd, "merge --autocreate=false --append examples/data1.yaml examples/data2.yaml")
2018-07-07 05:26:56 +00:00
if result.Error != nil {
t.Error(result.Error)
}
2020-01-05 21:12:30 +00:00
expectedOutput := `a: simple # just the best
2020-01-05 04:28:24 +00:00
b: [1, 2, 3, 4]
2018-06-20 01:45:51 +00:00
c:
test: 1
`
test.AssertResult(t, expectedOutput, result.Output)
2018-06-20 01:45:51 +00:00
}
2020-01-06 03:27:00 +00:00
func TestMergeOverwriteAndAppendCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge --autocreate=false --append --overwrite examples/data1.yaml examples/data2.yaml")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a: other # better than the original
b: [1, 2, 3, 4]
c:
test: 1
`
test.AssertResult(t, expectedOutput, result.Output)
}
2020-01-05 04:28:24 +00:00
func TestMergeArraysCmd(t *testing.T) {
2018-07-07 05:26:56 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge --append examples/sample_array.yaml examples/sample_array_2.yaml")
2018-07-07 05:26:56 +00:00
if result.Error != nil {
t.Error(result.Error)
}
2020-01-05 04:28:24 +00:00
expectedOutput := `[1, 2, 3, 4, 5]
2018-07-07 05:26:56 +00:00
`
test.AssertResult(t, expectedOutput, result.Output)
2018-07-07 05:26:56 +00:00
}
2018-06-20 01:45:51 +00:00
2020-01-05 21:12:30 +00:00
func TestMergeCmd_Multi(t *testing.T) {
cmd := getRootCommand()
2020-01-05 21:12:30 +00:00
result := test.RunCmd(cmd, "merge -d1 examples/multiple_docs_small.yaml examples/data1.yaml")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a: Easy! as one two three
---
another:
document: here
2020-01-05 21:12:30 +00:00
a: simple # just the best
2018-07-07 05:26:56 +00:00
b:
2020-01-05 21:12:30 +00:00
- 1
- 2
c:
test: 1
---
- 1
2020-01-05 21:12:30 +00:00
- 2
`
test.AssertResult(t, expectedOutput, result.Output)
}
2020-01-06 03:22:24 +00:00
func TestMergeYamlMultiAllCmd(t *testing.T) {
2018-06-20 01:45:51 +00:00
content := `b:
c: 3
apples: green
---
something: else`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-06-20 01:45:51 +00:00
mergeContent := `apples: red
something: good`
mergeFilename := test.WriteTempYamlFile(mergeContent)
defer test.RemoveTempYamlFile(mergeFilename)
2018-06-20 01:45:51 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("merge -d* %s %s", filename, mergeFilename))
2018-06-20 01:45:51 +00:00
if result.Error != nil {
t.Error(result.Error)
}
2020-01-06 03:22:24 +00:00
expectedOutput := `b:
2018-06-20 01:45:51 +00:00
c: 3
2020-01-06 03:22:24 +00:00
apples: green
2018-06-20 01:45:51 +00:00
something: good
---
2020-01-06 03:22:24 +00:00
something: else
2018-06-20 01:45:51 +00:00
apples: red
2020-01-06 03:22:24 +00:00
`
test.AssertResult(t, expectedOutput, result.Output)
2018-06-20 01:45:51 +00:00
}
2020-01-06 03:22:24 +00:00
func TestMergeYamlMultiAllOverwriteCmd(t *testing.T) {
2018-06-20 01:45:51 +00:00
content := `b:
c: 3
apples: green
---
something: else`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
2018-06-20 01:45:51 +00:00
mergeContent := `apples: red
something: good`
mergeFilename := test.WriteTempYamlFile(mergeContent)
defer test.RemoveTempYamlFile(mergeFilename)
2018-06-20 01:45:51 +00:00
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("merge --overwrite -d* %s %s", filename, mergeFilename))
2018-06-20 01:45:51 +00:00
if result.Error != nil {
t.Error(result.Error)
}
2020-01-06 03:22:24 +00:00
expectedOutput := `b:
2018-06-20 01:45:51 +00:00
c: 3
2020-01-06 03:22:24 +00:00
apples: red
2018-06-20 01:45:51 +00:00
something: good
---
2020-01-06 03:22:24 +00:00
something: good
2018-06-20 01:45:51 +00:00
apples: red
2020-01-06 03:22:24 +00:00
`
test.AssertResult(t, expectedOutput, result.Output)
2018-06-20 01:45:51 +00:00
}
2020-01-06 03:22:24 +00:00
func TestMergeCmd_Error(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge examples/data1.yaml")
if result.Error == nil {
t.Error("Expected command to fail due to missing arg")
}
expectedOutput := `Must provide at least 2 yaml files`
test.AssertResult(t, expectedOutput, result.Error.Error())
}
2020-01-06 03:22:24 +00:00
func TestMergeCmd_ErrorUnreadableFile(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge examples/data1.yaml fake-unknown")
if result.Error == nil {
t.Error("Expected command to fail due to unknown file")
}
var expectedOutput string
if runtime.GOOS == "windows" {
2020-01-06 03:22:24 +00:00
expectedOutput = `open fake-unknown: The system cannot find the file specified.`
} else {
2020-01-06 03:22:24 +00:00
expectedOutput = `open fake-unknown: no such file or directory`
}
test.AssertResult(t, expectedOutput, result.Error.Error())
}
2020-01-06 03:22:24 +00:00
func TestMergeCmd_Inplace(t *testing.T) {
filename := test.WriteTempYamlFile(test.ReadTempYamlFile("examples/data1.yaml"))
2018-10-24 20:04:05 +00:00
err := os.Chmod(filename, os.FileMode(int(0666)))
if err != nil {
t.Error(err)
}
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("merge -i %s examples/data2.yaml", filename))
if result.Error != nil {
t.Error(result.Error)
}
info, _ := os.Stat(filename)
gotOutput := test.ReadTempYamlFile(filename)
2020-01-06 03:22:24 +00:00
expectedOutput := `a: simple # just the best
b: [1, 2]
c:
2020-01-06 03:22:24 +00:00
test: 1
toast: leave
tell: 1
taco: cool
`
test.AssertResult(t, expectedOutput, gotOutput)
test.AssertResult(t, os.FileMode(int(0666)), info.Mode())
}
2020-01-06 03:22:24 +00:00
func TestMergeAllowEmptyCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge --allow-empty examples/data1.yaml examples/empty.yaml")
if result.Error != nil {
t.Error(result.Error)
}
2020-01-06 03:22:24 +00:00
expectedOutput := `a: simple # just the best
b: [1, 2]
c:
test: 1
`
test.AssertResult(t, expectedOutput, result.Output)
}
2020-01-06 03:22:24 +00:00
func TestMergeDontAllowEmptyCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge examples/data1.yaml examples/empty.yaml")
expectedOutput := `Could not process document index 0 as there are only 0 document(s)`
test.AssertResult(t, expectedOutput, result.Error.Error())
}