| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package filesys |
| 5 |
|
| 6 |
import ( |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
"sigs.k8s.io/kustomize/kyaml/errors" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
disk = "MakeFsOnDisk" |
| 18 |
memoryAbs = "MakeFsInMemory" |
| 19 |
memoryEmpty = "MakeEmptyDirInMemory" |
| 20 |
|
| 21 |
existMsg = "expected '%s' to exist" |
| 22 |
) |
| 23 |
|
| 24 |
var filesysBuilders = map[string]func() FileSystem{ |
| 25 |
memoryAbs: MakeFsInMemory, |
| 26 |
disk: MakeFsOnDisk, |
| 27 |
memoryEmpty: func() FileSystem { return MakeEmptyDirInMemory() }, |
| 28 |
} |
| 29 |
|
| 30 |
func TestNotExistErr(t *testing.T) { |
| 31 |
for name, builder := range filesysBuilders { |
| 32 |
t.Run(name, func(t *testing.T) { |
| 33 |
testNotExistErr(t, builder()) |
| 34 |
}) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
func testNotExistErr(t *testing.T, fs FileSystem) { |
| 39 |
t.Helper() |
| 40 |
const path = "bad-dir/file.txt" |
| 41 |
|
| 42 |
err := fs.RemoveAll(path) |
| 43 |
assert.Falsef(t, errors.Is(err, os.ErrNotExist), "RemoveAll should not return ErrNotExist, got %v", err) |
| 44 |
_, err = fs.Open(path) |
| 45 |
assert.Truef(t, errors.Is(err, os.ErrNotExist), "Open should return ErrNotExist, got %v", err) |
| 46 |
_, err = fs.ReadDir(path) |
| 47 |
assert.Truef(t, errors.Is(err, os.ErrNotExist), "ReadDir should return ErrNotExist, got %v", err) |
| 48 |
_, _, err = fs.CleanedAbs(path) |
| 49 |
assert.Truef(t, errors.Is(err, os.ErrNotExist), "CleanedAbs should return ErrNotExist, got %v", err) |
| 50 |
_, err = fs.ReadFile(path) |
| 51 |
assert.Truef(t, errors.Is(err, os.ErrNotExist), "ReadFile should return ErrNotExist, got %v", err) |
| 52 |
err = fs.Walk(path, func(_ string, _ os.FileInfo, err error) error { return err }) |
| 53 |
assert.Truef(t, errors.Is(err, os.ErrNotExist), "Walk should return ErrNotExist, got %v", err) |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
func setupFileSys(t *testing.T, name string) (FileSystem, string, string) { |
| 58 |
t.Helper() |
| 59 |
|
| 60 |
switch name { |
| 61 |
case disk: |
| 62 |
fSys, testDir := makeTestDir(t) |
| 63 |
return fSys, testDir, cleanWd(t) |
| 64 |
case memoryAbs: |
| 65 |
return filesysBuilders[name](), Separator, Separator |
| 66 |
case memoryEmpty: |
| 67 |
return filesysBuilders[name](), "", "" |
| 68 |
default: |
| 69 |
t.Fatalf("unexpected FileSystem implementation '%s'", name) |
| 70 |
panic("unreachable point of execution") |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func TestConfirmDir(t *testing.T) { |
| 75 |
for name := range filesysBuilders { |
| 76 |
name := name |
| 77 |
t.Run(name, func(t *testing.T) { |
| 78 |
fSys, prefixPath, wd := setupFileSys(t, name) |
| 79 |
|
| 80 |
d1Path := filepath.Join(prefixPath, "d1") |
| 81 |
d2Path := filepath.Join(d1Path, ".d2") |
| 82 |
err := fSys.MkdirAll(d2Path) |
| 83 |
require.NoError(t, err) |
| 84 |
require.Truef(t, fSys.Exists(d2Path), existMsg, d2Path) |
| 85 |
|
| 86 |
tests := map[string]*struct { |
| 87 |
dir string |
| 88 |
expected string |
| 89 |
}{ |
| 90 |
"Simple": { |
| 91 |
d1Path, |
| 92 |
d1Path, |
| 93 |
}, |
| 94 |
"Hidden": { |
| 95 |
d2Path, |
| 96 |
d2Path, |
| 97 |
}, |
| 98 |
"Relative": { |
| 99 |
SelfDir, |
| 100 |
wd, |
| 101 |
}, |
| 102 |
} |
| 103 |
for subName, test := range tests { |
| 104 |
test := test |
| 105 |
t.Run(subName, func(t *testing.T) { |
| 106 |
cleanDir, err := ConfirmDir(fSys, test.dir) |
| 107 |
require.NoError(t, err) |
| 108 |
require.Equal(t, test.expected, cleanDir.String()) |
| 109 |
}) |
| 110 |
} |
| 111 |
}) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func TestConfirmDirErr(t *testing.T) { |
| 116 |
for name := range filesysBuilders { |
| 117 |
thisName := name |
| 118 |
t.Run(thisName, func(t *testing.T) { |
| 119 |
fSys, prefixPath, _ := setupFileSys(t, thisName) |
| 120 |
|
| 121 |
fPath := filepath.Join(prefixPath, "foo") |
| 122 |
err := fSys.WriteFile(fPath, []byte(`foo`)) |
| 123 |
require.NoError(t, err) |
| 124 |
require.Truef(t, fSys.Exists(fPath), existMsg, fPath) |
| 125 |
|
| 126 |
tests := map[string]string{ |
| 127 |
"Empty": "", |
| 128 |
"File": fPath, |
| 129 |
"Non-existent": filepath.Join(prefixPath, "bar"), |
| 130 |
} |
| 131 |
for subName, invalidPath := range tests { |
| 132 |
invalidPath := invalidPath |
| 133 |
t.Run(subName, func(t *testing.T) { |
| 134 |
_, err := ConfirmDir(fSys, invalidPath) |
| 135 |
require.Error(t, err) |
| 136 |
}) |
| 137 |
} |
| 138 |
}) |
| 139 |
} |
| 140 |
} |