pkg/fileutils: remove gotest.tools as dependency, use t.TempDir()
In preparation of moving this package separate.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 0440ca07ba
)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
70c3d7783f
commit
a8b1fec072
1 changed files with 71 additions and 83 deletions
|
@ -8,19 +8,11 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gotest.tools/v3/assert"
|
|
||||||
is "gotest.tools/v3/assert/cmp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// CopyFile with invalid src
|
// CopyFile with invalid src
|
||||||
func TestCopyFileWithInvalidSrc(t *testing.T) {
|
func TestCopyFileWithInvalidSrc(t *testing.T) {
|
||||||
tempFolder, err := os.MkdirTemp("", "docker-fileutils-test") // #nosec G303
|
bytes, err := CopyFile("/invalid/file/path", path.Join(t.TempDir(), "dest"))
|
||||||
defer os.RemoveAll(tempFolder)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
bytes, err := CopyFile("/invalid/file/path", path.Join(tempFolder, "dest"))
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("Should have fail to copy an invalid src file")
|
t.Fatal("Should have fail to copy an invalid src file")
|
||||||
}
|
}
|
||||||
|
@ -32,13 +24,9 @@ func TestCopyFileWithInvalidSrc(t *testing.T) {
|
||||||
|
|
||||||
// CopyFile with invalid dest
|
// CopyFile with invalid dest
|
||||||
func TestCopyFileWithInvalidDest(t *testing.T) {
|
func TestCopyFileWithInvalidDest(t *testing.T) {
|
||||||
tempFolder, err := os.MkdirTemp("", "docker-fileutils-test")
|
tempFolder := t.TempDir()
|
||||||
defer os.RemoveAll(tempFolder)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
src := path.Join(tempFolder, "file")
|
src := path.Join(tempFolder, "file")
|
||||||
err = os.WriteFile(src, []byte("content"), 0740)
|
err := os.WriteFile(src, []byte("content"), 0o740)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -54,13 +42,8 @@ func TestCopyFileWithInvalidDest(t *testing.T) {
|
||||||
|
|
||||||
// CopyFile with same src and dest
|
// CopyFile with same src and dest
|
||||||
func TestCopyFileWithSameSrcAndDest(t *testing.T) {
|
func TestCopyFileWithSameSrcAndDest(t *testing.T) {
|
||||||
tempFolder, err := os.MkdirTemp("", "docker-fileutils-test")
|
file := path.Join(t.TempDir(), "file")
|
||||||
defer os.RemoveAll(tempFolder)
|
err := os.WriteFile(file, []byte("content"), 0o740)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
file := path.Join(tempFolder, "file")
|
|
||||||
err = os.WriteFile(file, []byte("content"), 0740)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -75,19 +58,14 @@ func TestCopyFileWithSameSrcAndDest(t *testing.T) {
|
||||||
|
|
||||||
// CopyFile with same src and dest but path is different and not clean
|
// CopyFile with same src and dest but path is different and not clean
|
||||||
func TestCopyFileWithSameSrcAndDestWithPathNameDifferent(t *testing.T) {
|
func TestCopyFileWithSameSrcAndDestWithPathNameDifferent(t *testing.T) {
|
||||||
tempFolder, err := os.MkdirTemp("", "docker-fileutils-test")
|
testFolder := path.Join(t.TempDir(), "test")
|
||||||
defer os.RemoveAll(tempFolder)
|
err := os.Mkdir(testFolder, 0o740)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
testFolder := path.Join(tempFolder, "test")
|
|
||||||
err = os.MkdirAll(testFolder, 0740)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
file := path.Join(testFolder, "file")
|
file := path.Join(testFolder, "file")
|
||||||
sameFile := testFolder + "/../test/file"
|
sameFile := testFolder + "/../test/file"
|
||||||
err = os.WriteFile(file, []byte("content"), 0740)
|
err = os.WriteFile(file, []byte("content"), 0o740)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -101,15 +79,17 @@ func TestCopyFileWithSameSrcAndDestWithPathNameDifferent(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCopyFile(t *testing.T) {
|
func TestCopyFile(t *testing.T) {
|
||||||
tempFolder, err := os.MkdirTemp("", "docker-fileutils-test")
|
tempFolder := t.TempDir()
|
||||||
defer os.RemoveAll(tempFolder)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
src := path.Join(tempFolder, "src")
|
src := path.Join(tempFolder, "src")
|
||||||
dest := path.Join(tempFolder, "dest")
|
dest := path.Join(tempFolder, "dest")
|
||||||
os.WriteFile(src, []byte("content"), 0777)
|
err := os.WriteFile(src, []byte("content"), 0o777)
|
||||||
os.WriteFile(dest, []byte("destContent"), 0777)
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
err = os.WriteFile(dest, []byte("destContent"), 0o777)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
bytes, err := CopyFile(src, dest)
|
bytes, err := CopyFile(src, dest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -133,7 +113,7 @@ func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) {
|
||||||
t.Skip("Needs porting to Windows")
|
t.Skip("Needs porting to Windows")
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
if err = os.Mkdir("/tmp/testReadSymlinkToExistingDirectory", 0777); err != nil {
|
if err = os.Mkdir("/tmp/testReadSymlinkToExistingDirectory", 0o777); err != nil {
|
||||||
t.Errorf("failed to create directory: %s", err)
|
t.Errorf("failed to create directory: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,13 +121,13 @@ func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) {
|
||||||
t.Errorf("failed to create symlink: %s", err)
|
t.Errorf("failed to create symlink: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var path string
|
var symlinkedPath string
|
||||||
if path, err = ReadSymlinkedDirectory("/tmp/dirLinkTest"); err != nil {
|
if symlinkedPath, err = ReadSymlinkedDirectory("/tmp/dirLinkTest"); err != nil {
|
||||||
t.Fatalf("failed to read symlink to directory: %s", err)
|
t.Fatalf("failed to read symlink to directory: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if path != "/tmp/testReadSymlinkToExistingDirectory" {
|
if symlinkedPath != "/tmp/testReadSymlinkToExistingDirectory" {
|
||||||
t.Fatalf("symlink returned unexpected directory: %s", path)
|
t.Fatalf("symlink returned unexpected directory: %s", symlinkedPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = os.Remove("/tmp/testReadSymlinkToExistingDirectory"); err != nil {
|
if err = os.Remove("/tmp/testReadSymlinkToExistingDirectory"); err != nil {
|
||||||
|
@ -161,14 +141,13 @@ func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) {
|
||||||
|
|
||||||
// Reading a non-existing symlink must fail
|
// Reading a non-existing symlink must fail
|
||||||
func TestReadSymlinkedDirectoryNonExistingSymlink(t *testing.T) {
|
func TestReadSymlinkedDirectoryNonExistingSymlink(t *testing.T) {
|
||||||
var path string
|
symLinkedPath, err := ReadSymlinkedDirectory("/tmp/test/foo/Non/ExistingPath")
|
||||||
var err error
|
if err == nil {
|
||||||
if path, err = ReadSymlinkedDirectory("/tmp/test/foo/Non/ExistingPath"); err == nil {
|
|
||||||
t.Fatalf("error expected for non-existing symlink")
|
t.Fatalf("error expected for non-existing symlink")
|
||||||
}
|
}
|
||||||
|
|
||||||
if path != "" {
|
if symLinkedPath != "" {
|
||||||
t.Fatalf("expected empty path, but '%s' was returned", path)
|
t.Fatalf("expected empty path, but '%s' was returned", symLinkedPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,13 +171,13 @@ func TestReadSymlinkedDirectoryToFile(t *testing.T) {
|
||||||
t.Errorf("failed to create symlink: %s", err)
|
t.Errorf("failed to create symlink: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var path string
|
symlinkedPath, err := ReadSymlinkedDirectory("/tmp/fileLinkTest")
|
||||||
if path, err = ReadSymlinkedDirectory("/tmp/fileLinkTest"); err == nil {
|
if err == nil {
|
||||||
t.Fatalf("ReadSymlinkedDirectory on a symlink to a file should've failed")
|
t.Fatalf("ReadSymlinkedDirectory on a symlink to a file should've failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
if path != "" {
|
if symlinkedPath != "" {
|
||||||
t.Fatalf("path should've been empty: %s", path)
|
t.Fatalf("path should've been empty: %s", symlinkedPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = os.Remove("/tmp/testReadSymlinkToFile"); err != nil {
|
if err = os.Remove("/tmp/testReadSymlinkToFile"); err != nil {
|
||||||
|
@ -400,27 +379,34 @@ func TestMatches(t *testing.T) {
|
||||||
|
|
||||||
t.Run("MatchesOrParentMatches", func(t *testing.T) {
|
t.Run("MatchesOrParentMatches", func(t *testing.T) {
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
desc := fmt.Sprintf("pattern=%q text=%q", test.pattern, test.text)
|
|
||||||
pm, err := NewPatternMatcher([]string{test.pattern})
|
pm, err := NewPatternMatcher([]string{test.pattern})
|
||||||
assert.NilError(t, err, desc)
|
if err != nil {
|
||||||
|
t.Fatalf("%v (pattern=%q, text=%q)", err, test.pattern, test.text)
|
||||||
|
}
|
||||||
res, _ := pm.MatchesOrParentMatches(test.text)
|
res, _ := pm.MatchesOrParentMatches(test.text)
|
||||||
assert.Check(t, is.Equal(test.pass, res), desc)
|
if test.pass != res {
|
||||||
|
t.Fatalf("%v (pattern=%q, text=%q)", err, test.pattern, test.text)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range multiPatternTests {
|
for _, test := range multiPatternTests {
|
||||||
desc := fmt.Sprintf("patterns=%q text=%q", test.patterns, test.text)
|
|
||||||
pm, err := NewPatternMatcher(test.patterns)
|
pm, err := NewPatternMatcher(test.patterns)
|
||||||
assert.NilError(t, err, desc)
|
if err != nil {
|
||||||
|
t.Fatalf("%v (patterns=%q, text=%q)", err, test.patterns, test.text)
|
||||||
|
}
|
||||||
res, _ := pm.MatchesOrParentMatches(test.text)
|
res, _ := pm.MatchesOrParentMatches(test.text)
|
||||||
assert.Check(t, is.Equal(test.pass, res), desc)
|
if test.pass != res {
|
||||||
|
t.Errorf("expected: %v, got: %v (patterns=%q, text=%q)", test.pass, res, test.patterns, test.text)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("MatchesUsingParentResult", func(t *testing.T) {
|
t.Run("MatchesUsingParentResult", func(t *testing.T) {
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
desc := fmt.Sprintf("pattern=%q text=%q", test.pattern, test.text)
|
|
||||||
pm, err := NewPatternMatcher([]string{test.pattern})
|
pm, err := NewPatternMatcher([]string{test.pattern})
|
||||||
assert.NilError(t, err, desc)
|
if err != nil {
|
||||||
|
t.Fatalf("%v (pattern=%q, text=%q)", err, test.pattern, test.text)
|
||||||
|
}
|
||||||
|
|
||||||
parentPath := filepath.Dir(filepath.FromSlash(test.text))
|
parentPath := filepath.Dir(filepath.FromSlash(test.text))
|
||||||
parentPathDirs := strings.Split(parentPath, string(os.PathSeparator))
|
parentPathDirs := strings.Split(parentPath, string(os.PathSeparator))
|
||||||
|
@ -433,7 +419,9 @@ func TestMatches(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
res, _ := pm.MatchesUsingParentResult(test.text, parentMatched)
|
res, _ := pm.MatchesUsingParentResult(test.text, parentMatched)
|
||||||
assert.Check(t, is.Equal(test.pass, res), desc)
|
if test.pass != res {
|
||||||
|
t.Errorf("expected: %v, got: %v (pattern=%q, text=%q)", test.pass, res, test.pattern, test.text)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -450,13 +438,17 @@ func TestMatches(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
res, _, _ := pm.MatchesUsingParentResults(text, parentMatchInfo)
|
res, _, _ := pm.MatchesUsingParentResults(text, parentMatchInfo)
|
||||||
assert.Check(t, is.Equal(pass, res), desc)
|
if pass != res {
|
||||||
|
t.Errorf("expected: %v, got: %v %s", pass, res, desc)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
desc := fmt.Sprintf("pattern=%q text=%q", test.pattern, test.text)
|
desc := fmt.Sprintf("(pattern=%q text=%q)", test.pattern, test.text)
|
||||||
pm, err := NewPatternMatcher([]string{test.pattern})
|
pm, err := NewPatternMatcher([]string{test.pattern})
|
||||||
assert.NilError(t, err, desc)
|
if err != nil {
|
||||||
|
t.Fatal(err, desc)
|
||||||
|
}
|
||||||
|
|
||||||
check(pm, test.text, test.pass, desc)
|
check(pm, test.text, test.pass, desc)
|
||||||
}
|
}
|
||||||
|
@ -464,7 +456,9 @@ func TestMatches(t *testing.T) {
|
||||||
for _, test := range multiPatternTests {
|
for _, test := range multiPatternTests {
|
||||||
desc := fmt.Sprintf("pattern=%q text=%q", test.patterns, test.text)
|
desc := fmt.Sprintf("pattern=%q text=%q", test.patterns, test.text)
|
||||||
pm, err := NewPatternMatcher(test.patterns)
|
pm, err := NewPatternMatcher(test.patterns)
|
||||||
assert.NilError(t, err, desc)
|
if err != nil {
|
||||||
|
t.Fatal(err, desc)
|
||||||
|
}
|
||||||
|
|
||||||
check(pm, test.text, test.pass, desc)
|
check(pm, test.text, test.pass, desc)
|
||||||
}
|
}
|
||||||
|
@ -473,21 +467,27 @@ func TestMatches(t *testing.T) {
|
||||||
t.Run("MatchesUsingParentResultsNoContext", func(t *testing.T) {
|
t.Run("MatchesUsingParentResultsNoContext", func(t *testing.T) {
|
||||||
check := func(pm *PatternMatcher, text string, pass bool, desc string) {
|
check := func(pm *PatternMatcher, text string, pass bool, desc string) {
|
||||||
res, _, _ := pm.MatchesUsingParentResults(text, MatchInfo{})
|
res, _, _ := pm.MatchesUsingParentResults(text, MatchInfo{})
|
||||||
assert.Check(t, is.Equal(pass, res), desc)
|
if pass != res {
|
||||||
|
t.Errorf("expected: %v, got: %v %s", pass, res, desc)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
desc := fmt.Sprintf("pattern=%q text=%q", test.pattern, test.text)
|
desc := fmt.Sprintf("(pattern=%q text=%q)", test.pattern, test.text)
|
||||||
pm, err := NewPatternMatcher([]string{test.pattern})
|
pm, err := NewPatternMatcher([]string{test.pattern})
|
||||||
assert.NilError(t, err, desc)
|
if err != nil {
|
||||||
|
t.Fatal(err, desc)
|
||||||
|
}
|
||||||
|
|
||||||
check(pm, test.text, test.pass, desc)
|
check(pm, test.text, test.pass, desc)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range multiPatternTests {
|
for _, test := range multiPatternTests {
|
||||||
desc := fmt.Sprintf("pattern=%q text=%q", test.patterns, test.text)
|
desc := fmt.Sprintf("(pattern=%q text=%q)", test.patterns, test.text)
|
||||||
pm, err := NewPatternMatcher(test.patterns)
|
pm, err := NewPatternMatcher(test.patterns)
|
||||||
assert.NilError(t, err, desc)
|
if err != nil {
|
||||||
|
t.Fatal(err, desc)
|
||||||
|
}
|
||||||
|
|
||||||
check(pm, test.text, test.pass, desc)
|
check(pm, test.text, test.pass, desc)
|
||||||
}
|
}
|
||||||
|
@ -561,13 +561,7 @@ func TestCleanPatternsErrorSingleException(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateIfNotExistsDir(t *testing.T) {
|
func TestCreateIfNotExistsDir(t *testing.T) {
|
||||||
tempFolder, err := os.MkdirTemp("", "docker-fileutils-test")
|
folderToCreate := filepath.Join(t.TempDir(), "tocreate")
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempFolder)
|
|
||||||
|
|
||||||
folderToCreate := filepath.Join(tempFolder, "tocreate")
|
|
||||||
|
|
||||||
if err := CreateIfNotExists(folderToCreate, true); err != nil {
|
if err := CreateIfNotExists(folderToCreate, true); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -583,13 +577,7 @@ func TestCreateIfNotExistsDir(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateIfNotExistsFile(t *testing.T) {
|
func TestCreateIfNotExistsFile(t *testing.T) {
|
||||||
tempFolder, err := os.MkdirTemp("", "docker-fileutils-test")
|
fileToCreate := filepath.Join(t.TempDir(), "file/to/create")
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempFolder)
|
|
||||||
|
|
||||||
fileToCreate := filepath.Join(tempFolder, "file/to/create")
|
|
||||||
|
|
||||||
if err := CreateIfNotExists(fileToCreate, false); err != nil {
|
if err := CreateIfNotExists(fileToCreate, false); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
Loading…
Reference in a new issue