Debian Codegraph @

api/testutils/kusttest/ondisk.go

raw
1 // Copyright 2023 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package kusttest_test
5
6 import (
7 "path/filepath"
8 "testing"
9
10 "github.com/stretchr/testify/require"
11 "sigs.k8s.io/kustomize/kyaml/filesys"
12 )
13
14 // Setup sets up a file system on disk and directory that is cleaned after
15 // test completion.
16 func Setup(t *testing.T) (filesys.FileSystem, filesys.ConfirmedDir) {
17 t.Helper()
18
19 fSys := filesys.MakeFsOnDisk()
20 dir, err := filesys.NewTmpConfirmedDir()
21 require.NoError(t, err)
22 t.Cleanup(func() {
23 _ = fSys.RemoveAll(dir.String())
24 })
25 return fSys, dir
26 }
27
28 // CreateKustDir creates a file system on disk and a new directory
29 // that holds a kustomization file with content. The directory is removed on
30 // test completion.
31 func CreateKustDir(t *testing.T, content string) (filesys.FileSystem, filesys.ConfirmedDir) {
32 t.Helper()
33
34 fSys, tmpDir := Setup(t)
35 require.NoError(t, fSys.WriteFile(filepath.Join(tmpDir.String(), "kustomization.yaml"), []byte(content)))
36 return fSys, tmpDir
37 }