mirror of https://github.com/portainer/portainer
refactor: unit tests (#6367)
parent
8e45076f35
commit
bc54d687be
|
@ -1,7 +1,6 @@
|
||||||
package filesystem
|
package filesystem
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -9,41 +8,65 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
// temporary function until upgrade to 1.16
|
func Test_movePath_shouldFailIfSourceDirDoesNotExist(t *testing.T) {
|
||||||
func tempDir(t *testing.T) string {
|
sourceDir := "missing"
|
||||||
tmpDir, err := os.MkdirTemp("", "dir")
|
destinationDir := t.TempDir()
|
||||||
assert.NoError(t, err, "MkdirTemp should not fail")
|
file1 := addFile(destinationDir, "dir", "file")
|
||||||
|
file2 := addFile(destinationDir, "file")
|
||||||
|
|
||||||
return tmpDir
|
err := MoveDirectory(sourceDir, destinationDir)
|
||||||
|
assert.Error(t, err, "move directory should fail when source path is missing")
|
||||||
|
assert.FileExists(t, file1, "destination dir contents should remain")
|
||||||
|
assert.FileExists(t, file2, "destination dir contents should remain")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_movePath_shouldFailIfOriginalPathDoesntExist(t *testing.T) {
|
func Test_movePath_shouldFailIfDestinationDirExists(t *testing.T) {
|
||||||
tmpDir := tempDir(t)
|
sourceDir := t.TempDir()
|
||||||
missingPath := path.Join(tmpDir, "missing")
|
file1 := addFile(sourceDir, "dir", "file")
|
||||||
targetPath := path.Join(tmpDir, "target")
|
file2 := addFile(sourceDir, "file")
|
||||||
|
destinationDir := t.TempDir()
|
||||||
|
file3 := addFile(destinationDir, "dir", "file")
|
||||||
|
file4 := addFile(destinationDir, "file")
|
||||||
|
|
||||||
defer os.RemoveAll(tmpDir)
|
err := MoveDirectory(sourceDir, destinationDir)
|
||||||
|
assert.Error(t, err, "move directory should fail when destination directory already exists")
|
||||||
err := MoveDirectory(missingPath, targetPath)
|
assert.FileExists(t, file1, "source dir contents should remain")
|
||||||
assert.Error(t, err, "move directory should fail when target path exists")
|
assert.FileExists(t, file2, "source dir contents should remain")
|
||||||
|
assert.FileExists(t, file3, "destination dir contents should remain")
|
||||||
|
assert.FileExists(t, file4, "destination dir contents should remain")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_movePath_shouldFailIfTargetPathDoesExist(t *testing.T) {
|
func Test_movePath_successWhenSourceExistsAndDestinationIsMissing(t *testing.T) {
|
||||||
originalPath := tempDir(t)
|
tmp := t.TempDir()
|
||||||
missingPath := tempDir(t)
|
sourceDir := path.Join(tmp, "source")
|
||||||
|
os.Mkdir(sourceDir, 0766)
|
||||||
|
file1 := addFile(sourceDir, "dir", "file")
|
||||||
|
file2 := addFile(sourceDir, "file")
|
||||||
|
destinationDir := path.Join(tmp, "destination")
|
||||||
|
|
||||||
defer os.RemoveAll(originalPath)
|
err := MoveDirectory(sourceDir, destinationDir)
|
||||||
defer os.RemoveAll(missingPath)
|
|
||||||
|
|
||||||
err := MoveDirectory(originalPath, missingPath)
|
|
||||||
assert.Error(t, err, "move directory should fail when target path exists")
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_movePath_success(t *testing.T) {
|
|
||||||
originalPath := tempDir(t)
|
|
||||||
|
|
||||||
defer os.RemoveAll(originalPath)
|
|
||||||
|
|
||||||
err := MoveDirectory(originalPath, fmt.Sprintf("%s-old", originalPath))
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
assert.NoFileExists(t, file1, "source dir contents should be moved")
|
||||||
|
assert.NoFileExists(t, file2, "source dir contents should be moved")
|
||||||
|
assertFileContent(t, path.Join(destinationDir, "file"))
|
||||||
|
assertFileContent(t, path.Join(destinationDir, "dir", "file"))
|
||||||
|
}
|
||||||
|
|
||||||
|
var content []byte = []byte("content")
|
||||||
|
|
||||||
|
func addFile(fileParts ...string) (filepath string) {
|
||||||
|
if len(fileParts) > 2 {
|
||||||
|
dir := path.Join(fileParts[:len(fileParts)-1]...)
|
||||||
|
os.MkdirAll(dir, 0766)
|
||||||
|
}
|
||||||
|
|
||||||
|
p := path.Join(fileParts...)
|
||||||
|
os.WriteFile(p, content, 0766)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertFileContent(t *testing.T, filePath string) {
|
||||||
|
actualContent, err := os.ReadFile(filePath)
|
||||||
|
assert.NoErrorf(t, err, "failed to read file %s", filePath)
|
||||||
|
assert.Equal(t, content, actualContent, "file %s content doesn't match", filePath)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue