| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package testutil |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"runtime" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"sigs.k8s.io/kustomize/kyaml/kio" |
| 12 |
"sigs.k8s.io/kustomize/kyaml/yaml" |
| 13 |
|
| 14 |
goerrors "github.com/go-errors/errors" |
| 15 |
|
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
) |
| 18 |
|
| 19 |
func UpdateYamlString(doc string, functions ...yaml.Filter) (string, error) { |
| 20 |
b, err := UpdateYamlBytes([]byte(doc), functions...) |
| 21 |
return string(b), err |
| 22 |
} |
| 23 |
|
| 24 |
func UpdateYamlBytes(b []byte, function ...yaml.Filter) ([]byte, error) { |
| 25 |
var out bytes.Buffer |
| 26 |
rw := kio.ByteReadWriter{ |
| 27 |
Reader: bytes.NewBuffer(b), |
| 28 |
Writer: &out, |
| 29 |
} |
| 30 |
err := kio.Pipeline{ |
| 31 |
Inputs: []kio.Reader{&rw}, |
| 32 |
Filters: []kio.Filter{ |
| 33 |
kio.FilterAll(yaml.FilterFunc( |
| 34 |
func(node *yaml.RNode) (*yaml.RNode, error) { |
| 35 |
return node.Pipe(function...) |
| 36 |
}), |
| 37 |
), |
| 38 |
}, |
| 39 |
Outputs: []kio.Writer{&rw}, |
| 40 |
}.Execute() |
| 41 |
return out.Bytes(), err |
| 42 |
} |
| 43 |
|
| 44 |
func AssertErrorContains(t *testing.T, err error, value string, msg ...string) { |
| 45 |
t.Helper() |
| 46 |
if !assert.Error(t, err, msg) { |
| 47 |
t.FailNow() |
| 48 |
} |
| 49 |
if !assert.Contains(t, err.Error(), value, msg) { |
| 50 |
t.FailNow() |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
func AssertNoError(t *testing.T, err error, msg ...string) { |
| 55 |
t.Helper() |
| 56 |
if !assert.NoError(t, err, msg) { |
| 57 |
gerr, ok := err.(*goerrors.Error) |
| 58 |
if ok { |
| 59 |
t.Fatal(string(gerr.Stack())) |
| 60 |
} |
| 61 |
t.FailNow() |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func SkipWindows(t *testing.T) { |
| 66 |
t.Helper() |
| 67 |
if runtime.GOOS == "windows" { |
| 68 |
t.Skip() |
| 69 |
} |
| 70 |
} |