forked from mikefarah/yq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
82 lines (68 loc) · 1.79 KB
/
utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
"testing"
yaml "github.com/mikefarah/yaml/v2"
"github.com/spf13/cobra"
)
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}
}
func parseData(rawData string) yaml.MapSlice {
var parsedData yaml.MapSlice
err := yaml.Unmarshal([]byte(rawData), &parsedData)
if err != nil {
fmt.Printf("Error parsing yaml: %v\n", err)
os.Exit(1)
}
return parsedData
}
func assertResult(t *testing.T, expectedValue interface{}, actualValue interface{}) {
t.Helper()
if expectedValue != actualValue {
t.Error("Expected <", expectedValue, "> but got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
}
}
func assertResultComplex(t *testing.T, expectedValue interface{}, actualValue interface{}) {
t.Helper()
if !reflect.DeepEqual(expectedValue, actualValue) {
t.Error("Expected <", expectedValue, "> but got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
}
}
func assertResultWithContext(t *testing.T, expectedValue interface{}, actualValue interface{}, context interface{}) {
t.Helper()
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)
}