2017-09-22 19:58:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-24 20:00:43 +00:00
|
|
|
"os"
|
2019-01-03 13:11:47 +00:00
|
|
|
"runtime"
|
2017-09-22 19:58:12 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-01-20 22:33:14 +00:00
|
|
|
cobra "gopkg.in/spf13/cobra.v0"
|
2017-09-22 19:58:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getRootCommand() *cobra.Command {
|
|
|
|
return newCommandCLI()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootCmd(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := 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.")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootCmd_VerboseLong(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := 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 := runCmd(cmd, "-v")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !verbose {
|
|
|
|
t.Error("Expected verbose to be true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootCmd_TrimLong(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "--trim")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !trimOutput {
|
|
|
|
t.Error("Expected trimOutput to be true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootCmd_TrimShort(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "-t")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !trimOutput {
|
|
|
|
t.Error("Expected trimOutput to be true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-23 04:45:31 +00:00
|
|
|
func TestRootCmd_VersionShort(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := 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") {
|
2017-09-23 04:45:31 +00:00
|
|
|
t.Error("expected version message to be printed out, but the message was not found.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootCmd_VersionLong(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := 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") {
|
2017-09-23 04:45:31 +00:00
|
|
|
t.Error("expected version message to be printed out, but the message was not found.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 19:58:12 +00:00
|
|
|
func TestReadCmd(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read examples/sample.yaml b.c")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
assertResult(t, "2\n", result.Output)
|
|
|
|
}
|
|
|
|
|
2018-07-08 12:04:31 +00:00
|
|
|
func TestReadInvalidDocumentIndexCmd(t *testing.T) {
|
2018-07-08 11:57:56 +00:00
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read -df examples/sample.yaml b.c")
|
|
|
|
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`
|
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
2018-07-08 12:04:31 +00:00
|
|
|
func TestReadBadDocumentIndexCmd(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read -d1 examples/sample.yaml b.c")
|
|
|
|
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)`
|
2018-07-08 12:04:31 +00:00
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
2018-07-08 11:47:01 +00:00
|
|
|
func TestReadOrderCmd(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read examples/order.yaml")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
assertResult(t,
|
|
|
|
`version: 3
|
|
|
|
application: MyApp
|
|
|
|
`,
|
|
|
|
result.Output)
|
|
|
|
}
|
|
|
|
|
2018-06-14 22:38:30 +00:00
|
|
|
func TestReadMultiCmd(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read -d 1 examples/multiple_docs.yaml another.document")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
assertResult(t, "here\n", result.Output)
|
|
|
|
}
|
|
|
|
|
2018-07-08 11:47:01 +00:00
|
|
|
func TestReadMultiAllCmd(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read -d* examples/multiple_docs.yaml commonKey")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
assertResult(t,
|
|
|
|
`- first document
|
|
|
|
- second document
|
|
|
|
- third document
|
|
|
|
`, result.Output)
|
|
|
|
}
|
|
|
|
|
2017-09-23 17:53:04 +00:00
|
|
|
func TestReadCmd_ArrayYaml(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read examples/array.yaml [0].gather_facts")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
assertResult(t, "false\n", result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadCmd_ArrayYaml_NoPath(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read examples/array.yaml")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `- become: true
|
|
|
|
gather_facts: false
|
|
|
|
hosts: lalaland
|
|
|
|
name: Apply smth
|
|
|
|
roles:
|
|
|
|
- lala
|
|
|
|
- land
|
|
|
|
serial: 1
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadCmd_ArrayYaml_OneElement(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read examples/array.yaml [0]")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `become: true
|
|
|
|
gather_facts: false
|
|
|
|
hosts: lalaland
|
|
|
|
name: Apply smth
|
|
|
|
roles:
|
|
|
|
- lala
|
|
|
|
- land
|
|
|
|
serial: 1
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadCmd_ArrayYaml_Splat(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read examples/array.yaml [*]")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `- become: true
|
|
|
|
gather_facts: false
|
|
|
|
hosts: lalaland
|
|
|
|
name: Apply smth
|
|
|
|
roles:
|
|
|
|
- lala
|
|
|
|
- land
|
|
|
|
serial: 1
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadCmd_ArrayYaml_SplatKey(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read examples/array.yaml [*].gather_facts")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := "- false\n"
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadCmd_ArrayYaml_ErrorBadPath(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read examples/array.yaml [x].gather_facts")
|
|
|
|
if result.Error == nil {
|
|
|
|
t.Error("Expected command to fail due to invalid path")
|
|
|
|
}
|
2019-01-20 22:33:14 +00:00
|
|
|
expectedOutput := `Error reading path in document index 0: error accessing array: strconv.ParseInt: parsing "x": invalid syntax`
|
2017-09-23 17:53:04 +00:00
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadCmd_ArrayYaml_Splat_ErrorBadPath(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read examples/array.yaml [*].roles[x]")
|
|
|
|
if result.Error == nil {
|
|
|
|
t.Error("Expected command to fail due to invalid path")
|
|
|
|
}
|
2019-01-20 22:33:14 +00:00
|
|
|
expectedOutput := `Error reading path in document index 0: error accessing array: strconv.ParseInt: parsing "x": invalid syntax`
|
2017-09-23 17:53:04 +00:00
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
2017-09-22 19:58:12 +00:00
|
|
|
func TestReadCmd_Error(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read")
|
|
|
|
if result.Error == nil {
|
|
|
|
t.Error("Expected command to fail due to missing arg")
|
|
|
|
}
|
|
|
|
expectedOutput := `Must provide filename`
|
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadCmd_ErrorEmptyFilename(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read ")
|
|
|
|
if result.Error == nil {
|
|
|
|
t.Error("Expected command to fail due to missing arg")
|
|
|
|
}
|
|
|
|
expectedOutput := `Must provide filename`
|
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadCmd_ErrorUnreadableFile(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read fake-unknown")
|
|
|
|
if result.Error == nil {
|
|
|
|
t.Error("Expected command to fail due to unknown file")
|
|
|
|
}
|
2019-01-03 13:11:47 +00:00
|
|
|
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`
|
|
|
|
}
|
2017-09-22 19:58:12 +00:00
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadCmd_ErrorBadPath(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
d:
|
|
|
|
e:
|
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
f:
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("read %s b.d.*.[x]", filename))
|
|
|
|
if result.Error == nil {
|
|
|
|
t.Fatal("Expected command to fail due to invalid path")
|
|
|
|
}
|
2019-01-20 22:33:14 +00:00
|
|
|
expectedOutput := `Error reading path in document index 0: error accessing array: strconv.ParseInt: parsing "x": invalid syntax`
|
2017-09-22 19:58:12 +00:00
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadCmd_Verbose(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "-v read examples/sample.yaml b.c")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
assertResult(t, "2\n", result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadCmd_NoTrim(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "--trim=false read examples/sample.yaml b.c")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
assertResult(t, "2\n\n", result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadCmd_ToJson(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
2018-06-26 04:09:56 +00:00
|
|
|
result := runCmd(cmd, "read -j examples/sample.yaml b.c")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
assertResult(t, "2\n", result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadCmd_ToJsonLong(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "read --tojson examples/sample.yaml b.c")
|
2017-09-22 19:58:12 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
assertResult(t, "2\n", result.Output)
|
|
|
|
}
|
|
|
|
|
2018-11-18 15:35:28 +00:00
|
|
|
func TestPrefixCmd(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("prefix %s d", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `d:
|
|
|
|
b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
2018-11-19 22:47:17 +00:00
|
|
|
func TestPrefixCmdArray(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("prefix %s [0].d.[1]", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `- d:
|
|
|
|
- null
|
|
|
|
- b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
2018-11-18 15:35:28 +00:00
|
|
|
func TestPrefixCmd_MultiLayer(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("prefix %s d.e.f", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `d:
|
|
|
|
e:
|
|
|
|
f:
|
|
|
|
b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrefixMultiCmd(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
---
|
|
|
|
apples: great
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("prefix %s -d 1 d", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `b:
|
|
|
|
c: 3
|
|
|
|
---
|
|
|
|
d:
|
|
|
|
apples: great
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
func TestPrefixInvalidDocumentIndexCmd(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("prefix %s -df d", filename))
|
|
|
|
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`
|
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrefixBadDocumentIndexCmd(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("prefix %s -d 1 d", filename))
|
|
|
|
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)`
|
2018-11-18 15:35:28 +00:00
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
func TestPrefixMultiAllCmd(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
---
|
|
|
|
apples: great
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("prefix %s -d * d", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `d:
|
|
|
|
b:
|
|
|
|
c: 3
|
|
|
|
---
|
|
|
|
d:
|
|
|
|
apples: great`
|
|
|
|
assertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrefixCmd_Error(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "prefix")
|
|
|
|
if result.Error == nil {
|
|
|
|
t.Error("Expected command to fail due to missing arg")
|
|
|
|
}
|
|
|
|
expectedOutput := `Must provide <filename> <prefixed_path>`
|
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrefixCmd_ErrorUnreadableFile(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "prefix fake-unknown a.b")
|
|
|
|
if result.Error == nil {
|
|
|
|
t.Error("Expected command to fail due to unknown file")
|
|
|
|
}
|
2019-01-03 13:11:47 +00:00
|
|
|
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`
|
|
|
|
}
|
2018-11-18 15:35:28 +00:00
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrefixCmd_Verbose(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("-v prefix %s x", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `x:
|
|
|
|
b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrefixCmd_Inplace(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("prefix -i %s d", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
gotOutput := readTempYamlFile(filename)
|
|
|
|
expectedOutput := `d:
|
|
|
|
b:
|
|
|
|
c: 3`
|
|
|
|
assertResult(t, expectedOutput, strings.Trim(gotOutput, "\n "))
|
|
|
|
}
|
|
|
|
|
2017-09-22 19:58:12 +00:00
|
|
|
func TestNewCmd(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "new b.c 3")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewCmd_Error(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := 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>`
|
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewCmd_Verbose(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "-v new b.c 3")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteCmd(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("write %s b.c 7", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `b:
|
|
|
|
c: 7
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
2018-06-13 04:10:00 +00:00
|
|
|
func TestWriteMultiCmd(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
---
|
|
|
|
apples: great
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := 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
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
2018-07-08 12:04:31 +00:00
|
|
|
func TestWriteInvalidDocumentIndexCmd(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
2018-06-13 04:10:00 +00:00
|
|
|
|
2018-07-08 12:04:31 +00:00
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("write %s -df apples ok", filename))
|
|
|
|
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`
|
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteBadDocumentIndexCmd(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("write %s -d 1 apples ok", filename))
|
|
|
|
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)`
|
2018-07-08 12:04:31 +00:00
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
2018-06-20 01:45:51 +00:00
|
|
|
func TestWriteMultiAllCmd(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
---
|
|
|
|
apples: great
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("write %s -d * apples ok", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `b:
|
|
|
|
c: 3
|
|
|
|
apples: ok
|
|
|
|
---
|
|
|
|
apples: ok`
|
|
|
|
assertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
|
|
|
|
}
|
|
|
|
|
2017-09-23 13:36:17 +00:00
|
|
|
func TestWriteCmd_EmptyArray(t *testing.T) {
|
|
|
|
content := `b: 3`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("write %s a []", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `b: 3
|
|
|
|
a: []
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
2017-09-22 19:58:12 +00:00
|
|
|
func TestWriteCmd_Error(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := 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>`
|
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteCmd_ErrorUnreadableFile(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "write fake-unknown a.b 3")
|
|
|
|
if result.Error == nil {
|
|
|
|
t.Error("Expected command to fail due to unknown file")
|
|
|
|
}
|
2019-01-03 13:11:47 +00:00
|
|
|
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`
|
|
|
|
}
|
2017-09-22 19:58:12 +00:00
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteCmd_Verbose(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("-v write %s b.c 7", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `b:
|
|
|
|
c: 7
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteCmd_Inplace(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("write -i %s b.c 7", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
gotOutput := readTempYamlFile(filename)
|
|
|
|
expectedOutput := `b:
|
|
|
|
c: 7`
|
2018-06-13 04:10:00 +00:00
|
|
|
assertResult(t, expectedOutput, strings.Trim(gotOutput, "\n "))
|
2017-09-22 19:58:12 +00:00
|
|
|
}
|
2017-09-23 03:29:37 +00:00
|
|
|
|
2017-09-24 00:21:16 +00:00
|
|
|
func TestWriteCmd_Append(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
- foo
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("write %s b[+] 7", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `b:
|
|
|
|
- foo
|
|
|
|
- 7
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
2017-09-25 13:31:06 +00:00
|
|
|
func TestWriteCmd_AppendEmptyArray(t *testing.T) {
|
|
|
|
content := `a: 2
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
2019-04-30 22:40:35 +00:00
|
|
|
result := runCmd(cmd, fmt.Sprintf("write -v %s b[+] v", filename))
|
2017-09-25 13:31:06 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `a: 2
|
|
|
|
b:
|
|
|
|
- v
|
|
|
|
`
|
|
|
|
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 := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("write -v %s b[*].c new", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `b:
|
|
|
|
- c: new
|
|
|
|
- c: new
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
2019-05-12 23:13:45 +00:00
|
|
|
func TestWriteCmd_SplatMap(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: thing
|
|
|
|
d: another thing
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("write -v %s b.* new", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `b:
|
|
|
|
c: new
|
|
|
|
d: new
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteCmd_SplatMapEmpty(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: thing
|
|
|
|
d: another thing
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("write -v %s b.c.* new", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `b:
|
|
|
|
c: thing
|
|
|
|
d: another thing
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
2018-06-14 23:43:20 +00:00
|
|
|
func TestDeleteYaml(t *testing.T) {
|
|
|
|
content := `a: 2
|
|
|
|
b:
|
|
|
|
c: things
|
|
|
|
d: something else
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("delete %s b.c", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedOutput := `a: 2
|
|
|
|
b:
|
|
|
|
d: something else
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteYamlArray(t *testing.T) {
|
|
|
|
content := `- 1
|
|
|
|
- 2
|
2018-10-24 20:00:43 +00:00
|
|
|
- 3
|
2018-06-14 23:43:20 +00:00
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("delete %s [1]", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedOutput := `- 1
|
|
|
|
- 3
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteYamlMulti(t *testing.T) {
|
|
|
|
content := `apples: great
|
|
|
|
---
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
- 3
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("delete -d 1 %s [1]", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedOutput := `apples: great
|
|
|
|
---
|
|
|
|
- 1
|
|
|
|
- 3
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
2018-06-20 01:45:51 +00:00
|
|
|
func TestDeleteYamlMultiAllCmd(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
apples: great
|
|
|
|
---
|
|
|
|
apples: great
|
|
|
|
something: else
|
|
|
|
`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("delete %s -d * apples", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `b:
|
|
|
|
c: 3
|
|
|
|
---
|
|
|
|
something: else`
|
|
|
|
assertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
|
|
|
|
}
|
|
|
|
|
2017-09-23 03:29:37 +00:00
|
|
|
func TestMergeCmd(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "merge examples/data1.yaml examples/data2.yaml")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `a: simple
|
|
|
|
b:
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
c:
|
|
|
|
test: 1
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
2018-06-20 01:45:51 +00:00
|
|
|
func TestMergeOverwriteCmd(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "merge --overwrite examples/data1.yaml examples/data2.yaml")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `a: other
|
|
|
|
b:
|
2018-07-07 05:26:56 +00:00
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
c:
|
|
|
|
test: 1
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeAppendCmd(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "merge --append examples/data1.yaml examples/data2.yaml")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `a: simple
|
|
|
|
b:
|
2018-06-20 01:45:51 +00:00
|
|
|
- 1
|
|
|
|
- 2
|
2018-07-07 05:26:56 +00:00
|
|
|
- 3
|
|
|
|
- 4
|
2018-06-20 01:45:51 +00:00
|
|
|
c:
|
|
|
|
test: 1
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
2018-07-07 05:26:56 +00:00
|
|
|
func TestMergeArraysCmd(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "merge --append examples/sample_array.yaml examples/sample_array_2.yaml")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `- 1
|
|
|
|
- 2
|
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
- 5
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
2018-06-20 01:45:51 +00:00
|
|
|
|
2018-06-15 06:48:36 +00:00
|
|
|
func TestMergeCmd_Multi(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "merge -d1 examples/multiple_docs_small.yaml examples/data2.yaml")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `a: Easy! as one two three
|
|
|
|
---
|
|
|
|
a: other
|
|
|
|
another:
|
|
|
|
document: here
|
2018-07-07 05:26:56 +00:00
|
|
|
b:
|
|
|
|
- 3
|
|
|
|
- 4
|
2018-06-15 06:48:36 +00:00
|
|
|
c:
|
|
|
|
test: 1
|
|
|
|
---
|
|
|
|
- 1
|
|
|
|
- 2`
|
|
|
|
assertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
|
|
|
|
}
|
|
|
|
|
2018-06-20 01:45:51 +00:00
|
|
|
func TestMergeYamlMultiAllCmd(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
apples: green
|
|
|
|
---
|
|
|
|
something: else`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
mergeContent := `apples: red
|
|
|
|
something: good`
|
|
|
|
mergeFilename := writeTempYamlFile(mergeContent)
|
|
|
|
defer removeTempYamlFile(mergeFilename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("merge -d* %s %s", filename, mergeFilename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `apples: green
|
|
|
|
b:
|
|
|
|
c: 3
|
|
|
|
something: good
|
|
|
|
---
|
|
|
|
apples: red
|
|
|
|
something: else`
|
|
|
|
assertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeYamlMultiAllOverwriteCmd(t *testing.T) {
|
|
|
|
content := `b:
|
|
|
|
c: 3
|
|
|
|
apples: green
|
|
|
|
---
|
|
|
|
something: else`
|
|
|
|
filename := writeTempYamlFile(content)
|
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
mergeContent := `apples: red
|
|
|
|
something: good`
|
|
|
|
mergeFilename := writeTempYamlFile(mergeContent)
|
|
|
|
defer removeTempYamlFile(mergeFilename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("merge --overwrite -d* %s %s", filename, mergeFilename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `apples: red
|
|
|
|
b:
|
|
|
|
c: 3
|
|
|
|
something: good
|
|
|
|
---
|
|
|
|
apples: red
|
|
|
|
something: good`
|
|
|
|
assertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
|
|
|
|
}
|
|
|
|
|
2017-09-23 03:29:37 +00:00
|
|
|
func TestMergeCmd_Error(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := 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`
|
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeCmd_ErrorUnreadableFile(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "merge examples/data1.yaml fake-unknown")
|
|
|
|
if result.Error == nil {
|
|
|
|
t.Error("Expected command to fail due to unknown file")
|
|
|
|
}
|
2019-01-03 13:11:47 +00:00
|
|
|
var expectedOutput string
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expectedOutput = `Error updating document at index 0: open fake-unknown: The system cannot find the file specified.`
|
|
|
|
} else {
|
|
|
|
expectedOutput = `Error updating document at index 0: open fake-unknown: no such file or directory`
|
|
|
|
}
|
2017-09-23 03:29:37 +00:00
|
|
|
assertResult(t, expectedOutput, result.Error.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeCmd_Verbose(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "-v merge examples/data1.yaml examples/data2.yaml")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `a: simple
|
|
|
|
b:
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
c:
|
|
|
|
test: 1
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeCmd_Inplace(t *testing.T) {
|
|
|
|
filename := writeTempYamlFile(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)
|
|
|
|
}
|
2017-09-23 03:29:37 +00:00
|
|
|
defer removeTempYamlFile(filename)
|
|
|
|
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, fmt.Sprintf("merge -i %s examples/data2.yaml", filename))
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
2018-10-24 20:00:43 +00:00
|
|
|
info, _ := os.Stat(filename)
|
2017-09-23 03:29:37 +00:00
|
|
|
gotOutput := readTempYamlFile(filename)
|
|
|
|
expectedOutput := `a: simple
|
|
|
|
b:
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
c:
|
|
|
|
test: 1`
|
2018-06-15 06:40:52 +00:00
|
|
|
assertResult(t, expectedOutput, strings.Trim(gotOutput, "\n "))
|
2018-10-24 20:00:43 +00:00
|
|
|
assertResult(t, os.FileMode(int(0666)), info.Mode())
|
2017-09-23 03:29:37 +00:00
|
|
|
}
|
2019-03-21 10:08:11 +00:00
|
|
|
|
|
|
|
func TestMergeAllowEmptyCmd(t *testing.T) {
|
|
|
|
cmd := getRootCommand()
|
|
|
|
result := runCmd(cmd, "merge --allow-empty examples/data1.yaml examples/empty.yaml")
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Error(result.Error)
|
|
|
|
}
|
|
|
|
expectedOutput := `a: simple
|
|
|
|
b:
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
`
|
|
|
|
assertResult(t, expectedOutput, result.Output)
|
|
|
|
}
|