Debian Codegraph @

kyaml/kio/testing_test.go

raw symbols SCIP
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package kio
5
6 import (
7 "os"
8 "path/filepath"
9 "testing"
10
11 "github.com/stretchr/testify/require"
12 )
13
14 // Setup creates directories and files for testing
15 type Setup struct {
16 // root is the tmp directory
17 Root string
18 }
19
20 // SetupDirectories creates directories for reading test configuration from
21 func SetupDirectories(t *testing.T, dirs ...string) Setup {
22 t.Helper()
23 d, err := os.MkdirTemp("", "kyaml-test")
24 require.NoError(t, err)
25 err = os.Chdir(d)
26 require.NoError(t, err)
27 for _, s := range dirs {
28 err = os.MkdirAll(s, 0700)
29 require.NoError(t, err)
30 }
31 return Setup{Root: d}
32 }
33
34 // WriteFile writes a file under the test directory
35 func (s Setup) WriteFile(t *testing.T, path string, value []byte) {
36 t.Helper()
37 err := os.MkdirAll(filepath.Dir(filepath.Join(s.Root, path)), 0700)
38 require.NoError(t, err)
39 err = os.WriteFile(filepath.Join(s.Root, path), value, 0600)
40 require.NoError(t, err)
41 }
42
43 // Clean deletes the test config
44 func (s Setup) Clean() {
45 os.RemoveAll(s.Root)
46 }