forked from kubernetes-sigs/kustomize
/
testing_test.go
47 lines (40 loc) · 1.06 KB
/
testing_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
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package kio
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
// Setup creates directories and files for testing
type Setup struct {
// root is the tmp directory
Root string
}
// SetupDirectories creates directories for reading test configuration from
func SetupDirectories(t *testing.T, dirs ...string) Setup {
t.Helper()
d, err := ioutil.TempDir("", "kyaml-test")
require.NoError(t, err)
err = os.Chdir(d)
require.NoError(t, err)
for _, s := range dirs {
err = os.MkdirAll(s, 0700)
require.NoError(t, err)
}
return Setup{Root: d}
}
// WriteFile writes a file under the test directory
func (s Setup) WriteFile(t *testing.T, path string, value []byte) {
t.Helper()
err := os.MkdirAll(filepath.Dir(filepath.Join(s.Root, path)), 0700)
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(s.Root, path), value, 0600)
require.NoError(t, err)
}
// Clean deletes the test config
func (s Setup) Clean() {
os.RemoveAll(s.Root)
}