| 1 |
// Copyright 2022 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package localizertest |
| 5 |
|
| 6 |
import ( |
| 7 |
"io/fs" |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
"sigs.k8s.io/kustomize/kyaml/filesys" |
| 14 |
) |
| 15 |
|
| 16 |
// PrepareFs returns an in-memory and the actual file system, both with the test |
| 17 |
// directory with the file content mapping in files. |
| 18 |
// dirs are the directory paths that need to be created to write the files. |
| 19 |
func PrepareFs(t *testing.T, dirs []string, files map[string]string) ( |
| 20 |
memory, actual filesys.FileSystem, test filesys.ConfirmedDir) { |
| 21 |
t.Helper() |
| 22 |
|
| 23 |
memory = filesys.MakeFsInMemory() |
| 24 |
actual = filesys.MakeFsOnDisk() |
| 25 |
|
| 26 |
testDir, err := filesys.NewTmpConfirmedDir() |
| 27 |
require.NoError(t, err) |
| 28 |
|
| 29 |
SetupDir(t, memory, testDir.String(), files) |
| 30 |
for _, dirPath := range dirs { |
| 31 |
require.NoError(t, actual.MkdirAll(testDir.Join(dirPath))) |
| 32 |
} |
| 33 |
SetupDir(t, actual, testDir.String(), files) |
| 34 |
|
| 35 |
t.Cleanup(func() { |
| 36 |
_ = actual.RemoveAll(testDir.String()) |
| 37 |
}) |
| 38 |
|
| 39 |
return memory, actual, testDir |
| 40 |
} |
| 41 |
|
| 42 |
// SetupDir creates each file, specified by the file name to content mapping in |
| 43 |
// files, under dir on fSys |
| 44 |
func SetupDir(t *testing.T, fSys filesys.FileSystem, dir string, |
| 45 |
files map[string]string) { |
| 46 |
t.Helper() |
| 47 |
|
| 48 |
for file, content := range files { |
| 49 |
require.NoError(t, fSys.WriteFile(filepath.Join(dir, file), []byte(content))) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// SetWorkingDir sets the working directory to workingDir and restores the |
| 54 |
// original working directory after test completion. |
| 55 |
func SetWorkingDir(t *testing.T, workingDir string) { |
| 56 |
t.Helper() |
| 57 |
|
| 58 |
wd, err := os.Getwd() |
| 59 |
require.NoError(t, err) |
| 60 |
t.Cleanup(func() { |
| 61 |
require.NoError(t, os.Chdir(wd)) |
| 62 |
}) |
| 63 |
|
| 64 |
err = os.Chdir(workingDir) |
| 65 |
require.NoError(t, err) |
| 66 |
} |
| 67 |
|
| 68 |
// CheckFs checks actual, the real file system, against expected, a file |
| 69 |
// system in memory, for contents in directory dir. |
| 70 |
// CheckFs does not allow symlinks. |
| 71 |
func CheckFs(t *testing.T, dir string, expected, actual filesys.FileSystem) { |
| 72 |
t.Helper() |
| 73 |
|
| 74 |
err := actual.Walk(dir, func(path string, info fs.FileInfo, err error) error { |
| 75 |
require.NoError(t, err) |
| 76 |
|
| 77 |
require.NotEqual(t, os.ModeSymlink, info.Mode()&os.ModeSymlink) |
| 78 |
require.True(t, expected.Exists(path), "unexpected file %q", path) |
| 79 |
return nil |
| 80 |
}) |
| 81 |
require.NoError(t, err) |
| 82 |
|
| 83 |
err = expected.Walk(dir, func(path string, info fs.FileInfo, err error) error { |
| 84 |
require.NoError(t, err) |
| 85 |
|
| 86 |
if info.IsDir() { |
| 87 |
require.DirExists(t, path) |
| 88 |
} else { |
| 89 |
require.FileExists(t, path) |
| 90 |
|
| 91 |
expectedContent, err := expected.ReadFile(path) |
| 92 |
require.NoError(t, err) |
| 93 |
actualContent, err := actual.ReadFile(path) |
| 94 |
require.NoError(t, err) |
| 95 |
require.Equal(t, string(expectedContent), string(actualContent)) |
| 96 |
} |
| 97 |
return nil |
| 98 |
}) |
| 99 |
require.NoError(t, err) |
| 100 |
} |