-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
testutil.go
68 lines (57 loc) · 1.36 KB
/
testutil.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
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package testutil
import (
"bytes"
"runtime"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/yaml"
"testing"
goerrors "github.com/go-errors/errors"
"github.com/stretchr/testify/assert"
)
func UpdateYamlString(doc string, functions ...yaml.Filter) (string, error) {
b, err := UpdateYamlBytes([]byte(doc), functions...)
return string(b), err
}
func UpdateYamlBytes(b []byte, function ...yaml.Filter) ([]byte, error) {
var out bytes.Buffer
rw := kio.ByteReadWriter{
Reader: bytes.NewBuffer(b),
Writer: &out,
}
err := kio.Pipeline{
Inputs: []kio.Reader{&rw},
Filters: []kio.Filter{
kio.FilterAll(yaml.FilterFunc(
func(node *yaml.RNode) (*yaml.RNode, error) {
return node.Pipe(function...)
}),
),
},
Outputs: []kio.Writer{&rw},
}.Execute()
return out.Bytes(), err
}
func AssertErrorContains(t *testing.T, err error, value string, msg ...string) {
if !assert.Error(t, err, msg) {
t.FailNow()
}
if !assert.Contains(t, err.Error(), value, msg) {
t.FailNow()
}
}
func AssertNoError(t *testing.T, err error, msg ...string) {
if !assert.NoError(t, err, msg) {
gerr, ok := err.(*goerrors.Error)
if ok {
t.Fatal(string(gerr.Stack()))
}
t.FailNow()
}
}
func SkipWindows(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip()
}
}