yq/yq_test.go

95 lines
2.2 KiB
Go
Raw Permalink Normal View History

2015-10-03 07:25:13 +00:00
package main
import (
2020-01-10 08:34:31 +00:00
"bytes"
2017-02-26 22:01:52 +00:00
"fmt"
"runtime"
2015-10-03 07:25:13 +00:00
"testing"
2019-12-01 20:10:42 +00:00
2019-12-04 05:10:28 +00:00
"github.com/mikefarah/yq/v2/pkg/marshal"
"github.com/mikefarah/yq/v2/test"
2020-01-10 08:34:31 +00:00
"github.com/spf13/cobra"
2015-10-03 07:25:13 +00:00
)
func TestMultilineString(t *testing.T) {
testString := `
abcd
efg`
formattedResult, _ := marshal.NewYamlConverter().YamlToString(testString, false)
test.AssertResult(t, testString, formattedResult)
}
func TestNewYaml(t *testing.T) {
result, _ := newYaml([]string{"b.c", "3"})
formattedResult := fmt.Sprintf("%v", result)
test.AssertResult(t,
"[{b [{c 3}]}]",
formattedResult)
}
2017-08-08 07:04:30 +00:00
func TestNewYamlArray(t *testing.T) {
result, _ := newYaml([]string{"[0].cat", "meow"})
2017-08-08 07:04:30 +00:00
formattedResult := fmt.Sprintf("%v", result)
test.AssertResult(t,
2017-08-08 07:04:30 +00:00
"[[{cat meow}]]",
formattedResult)
}
func TestNewYamlBigInt(t *testing.T) {
result, _ := newYaml([]string{"b", "1212121"})
formattedResult := fmt.Sprintf("%v", result)
test.AssertResult(t,
"[{b 1212121}]",
formattedResult)
}
func TestNewYaml_WithScript(t *testing.T) {
writeScript = "examples/instruction_sample.yaml"
expectedResult := `b:
c: cat
e:
- name: Mike Farah`
result, _ := newYaml([]string{""})
actualResult, _ := marshal.NewYamlConverter().YamlToString(result, true)
test.AssertResult(t, expectedResult, actualResult)
}
func TestNewYaml_WithUnknownScript(t *testing.T) {
writeScript = "fake-unknown"
_, err := newYaml([]string{""})
if err == nil {
t.Error("Expected error 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, err.Error())
}
2020-01-10 08:34:31 +00:00
func TestReadWithKeyOnly(t *testing.T) {
readCmd := createReadCmd()
expectedResult := `b
d
f
`
actualResult, err := executeTestCommand(readCmd, "test/fixture/keyonly.yaml", "a", "-k")
if err != nil {
t.Error(err.Error())
}
test.AssertResult(t, expectedResult, actualResult)
}
func executeTestCommand(command *cobra.Command, args ...string) (output string, err error) {
buf := new(bytes.Buffer)
command.SetOutput(buf)
command.SetArgs(args)
_, err = command.ExecuteC()
return buf.String(), err
}