yq/utils_test.go

83 lines
1.8 KiB
Go
Raw Normal View History

2015-10-10 08:18:54 +00:00
package main
import (
"bytes"
2015-10-10 08:18:54 +00:00
"fmt"
"io/ioutil"
2015-10-10 08:18:54 +00:00
"os"
"reflect"
"strings"
2015-10-10 08:18:54 +00:00
"testing"
"github.com/spf13/cobra"
yaml "gopkg.in/mikefarah/yaml.v2"
2015-10-10 08:18:54 +00:00
)
type resulter struct {
Error error
Output string
Command *cobra.Command
}
func runCmd(c *cobra.Command, input string) resulter {
buf := new(bytes.Buffer)
c.SetOutput(buf)
c.SetArgs(strings.Split(input, " "))
err := c.Execute()
output := buf.String()
return resulter{err, output, c}
}
2017-02-26 22:01:52 +00:00
func parseData(rawData string) yaml.MapSlice {
var parsedData yaml.MapSlice
2015-10-10 08:18:54 +00:00
err := yaml.Unmarshal([]byte(rawData), &parsedData)
if err != nil {
fmt.Printf("Error parsing yaml: %v\n", err)
2015-10-10 08:18:54 +00:00
os.Exit(1)
}
return parsedData
}
func assertResult(t *testing.T, expectedValue interface{}, actualValue interface{}) {
2018-05-04 15:47:08 +00:00
t.Helper()
2015-10-10 08:18:54 +00:00
if expectedValue != actualValue {
t.Error("Expected <", expectedValue, "> but got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
}
}
func assertResultComplex(t *testing.T, expectedValue interface{}, actualValue interface{}) {
2018-05-04 15:47:08 +00:00
t.Helper()
if !reflect.DeepEqual(expectedValue, actualValue) {
t.Error("Expected <", expectedValue, "> but got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
}
}
2015-10-10 08:18:54 +00:00
func assertResultWithContext(t *testing.T, expectedValue interface{}, actualValue interface{}, context interface{}) {
2018-05-04 15:47:08 +00:00
t.Helper()
2015-10-10 08:18:54 +00:00
if expectedValue != actualValue {
t.Error(context)
t.Error(": expected <", expectedValue, "> but got <", actualValue, ">")
}
}
func writeTempYamlFile(content string) string {
tmpfile, _ := ioutil.TempFile("", "testyaml")
defer func() {
_ = tmpfile.Close()
}()
_, _ = tmpfile.Write([]byte(content))
return tmpfile.Name()
}
func readTempYamlFile(name string) string {
content, _ := ioutil.ReadFile(name)
return string(content)
}
func removeTempYamlFile(name string) {
_ = os.Remove(name)
}