File: testutil.go

package info (click to toggle)
golang-k8s-sigs-kustomize-kyaml 0.20.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,180 kB
  • sloc: makefile: 220; sh: 68
file content (70 lines) | stat: -rw-r--r-- 1,428 bytes parent folder | download | duplicates (2)
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
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package testutil

import (
	"bytes"
	"runtime"
	"testing"

	"sigs.k8s.io/kustomize/kyaml/kio"
	"sigs.k8s.io/kustomize/kyaml/yaml"

	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) {
	t.Helper()
	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) {
	t.Helper()
	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) {
	t.Helper()
	if runtime.GOOS == "windows" {
		t.Skip()
	}
}