Improve self-consistency of file system mock (#1815)

This commit is contained in:
Stephan Aßmus
2020-07-20 14:23:33 +02:00
committed by GitHub
parent 9027c4ccda
commit 61fed83475
3 changed files with 267 additions and 73 deletions
+105 -50
View File
@@ -4,9 +4,9 @@ package mock
import (
"fmt"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/bmatcuk/doublestar"
"os"
"path"
"path/filepath"
"sort"
"strings"
@@ -15,6 +15,11 @@ import (
var dirContent []byte
const (
defaultFileMode os.FileMode = 0644
defaultDirMode os.FileMode = 0755
)
type fileInfoMock struct {
name string
mode os.FileMode
@@ -31,7 +36,12 @@ func (fInfo fileInfoMock) Sys() interface{} { return nil }
type fileProperties struct {
content *[]byte
mode *os.FileMode
mode os.FileMode
}
// isDir returns true when the properties describe a directory entry.
func (p *fileProperties) isDir() bool {
return p.content == &dirContent
}
//FilesMock implements the functions from piperutils.Files with an in-memory file system.
@@ -52,40 +62,51 @@ func (f *FilesMock) init() {
}
}
// toAbsPath checks if the given path is relative, and if so converts it to an absolute path considering the
// current directory of the FilesMock.
// Relative segments such as "../" are currently NOT supported.
func (f *FilesMock) toAbsPath(path string) string {
if path == "." {
return f.Separator + f.currentDir
}
if !strings.HasPrefix(path, f.Separator) {
path = f.Separator + filepath.Join(f.currentDir, path)
}
return path
}
// AddFile establishes the existence of a virtual file. The file is
// added with mode 644
// AddFile establishes the existence of a virtual file.
// The file is added with mode 644.
func (f *FilesMock) AddFile(path string, contents []byte) {
f.AddFileWithMode(path, contents, 0644)
f.AddFileWithMode(path, contents, defaultFileMode)
}
// AddFileWithMode establishes the existence of a virtual file.
func (f *FilesMock) AddFileWithMode(path string, contents []byte, mode os.FileMode) {
f.associateContent(path, &contents, &mode)
f.associateContent(path, &contents, mode)
}
// AddDir establishes the existence of a virtual directory. The directory
// is add with default mode 755
// AddDir establishes the existence of a virtual directory.
// The directory is add with default mode 755.
func (f *FilesMock) AddDir(path string) {
f.AddDirWithMode(path, 0755)
f.AddDirWithMode(path, defaultDirMode)
}
// AddDirWithMode establishes the existence of a virtual directory.
func (f *FilesMock) AddDirWithMode(path string, mode os.FileMode) {
f.associateContent(path, &dirContent, &mode)
f.associateContent(path, &dirContent, mode)
}
func (f *FilesMock) associateContent(path string, content *[]byte, mode *os.FileMode) {
func (f *FilesMock) associateContent(path string, content *[]byte, mode os.FileMode) {
f.init()
path = f.toAbsPath(path)
f.associateContentAbs(path, content, mode)
}
func (f *FilesMock) associateContentAbs(path string, content *[]byte, mode os.FileMode) {
f.init()
path = strings.ReplaceAll(path, "/", f.Separator)
path = strings.ReplaceAll(path, "\\", f.Separator)
path = f.toAbsPath(path)
if _, ok := f.files[path]; !ok {
f.files[path] = &fileProperties{}
}
@@ -103,22 +124,13 @@ func (f *FilesMock) HasFile(path string) bool {
// HasRemovedFile returns true if the virtual file system at one point contained an entry for the given path,
// and it was removed via FileRemove().
func (f *FilesMock) HasRemovedFile(path string) bool {
return contains(f.removedFiles, f.toAbsPath(path))
return piperutils.ContainsString(f.removedFiles, f.toAbsPath(path))
}
// HasWrittenFile returns true if the virtual file system at one point contained an entry for the given path,
// and it was written via FileWrite().
func (f *FilesMock) HasWrittenFile(path string) bool {
return contains(f.writtenFiles, f.toAbsPath(path))
}
func contains(collection []string, name string) bool {
for _, entry := range collection {
if entry == name {
return true
}
}
return false
return piperutils.ContainsString(f.writtenFiles, f.toAbsPath(path))
}
// FileExists returns true if file content has been associated with the given path, false otherwise.
@@ -138,9 +150,13 @@ func (f *FilesMock) FileExists(path string) (bool, error) {
// previously added files.
func (f *FilesMock) DirExists(path string) (bool, error) {
path = f.toAbsPath(path)
if path == "." || path == "."+f.Separator || path == f.Separator {
// The current folder, or the root folder always exist
return true, nil
}
for entry, props := range f.files {
var dirComponents []string
if props.content == &dirContent {
if props.isDir() {
dirComponents = strings.Split(entry, f.Separator)
} else {
dirComponents = strings.Split(filepath.Dir(entry), f.Separator)
@@ -166,10 +182,10 @@ func (f *FilesMock) DirExists(path string) (bool, error) {
func (f *FilesMock) Copy(src, dst string) (int64, error) {
f.init()
props, exists := f.files[f.toAbsPath(src)]
if !exists || props.content == &dirContent {
if !exists || props.isDir() {
return 0, fmt.Errorf("cannot copy '%s': %w", src, os.ErrNotExist)
}
f.AddFileWithMode(dst, *props.content, *props.mode)
f.AddFileWithMode(dst, *props.content, props.mode)
return int64(len(*props.content)), nil
}
@@ -182,7 +198,7 @@ func (f *FilesMock) FileRead(path string) ([]byte, error) {
return nil, fmt.Errorf("could not read '%s'", path)
}
// check if trying to open a directory for reading
if props.content == &dirContent {
if props.isDir() {
return nil, fmt.Errorf("could not read '%s': %w", path, os.ErrInvalid)
}
return *props.content, nil
@@ -206,12 +222,37 @@ func (f *FilesMock) FileRemove(path string) error {
return fmt.Errorf("the file '%s' does not exist: %w", path, os.ErrNotExist)
}
absPath := f.toAbsPath(path)
_, exists := f.files[absPath]
props, exists := f.files[absPath]
// If there is no leaf-entry in the map, path may be a directory, but implicitly it cannot be empty
if !exists {
dirExists, _ := f.DirExists(path)
if dirExists {
return fmt.Errorf("the directory '%s' is not empty", path)
}
return fmt.Errorf("the file '%s' does not exist: %w", path, os.ErrNotExist)
} else if props.isDir() {
// Check if the directory is not empty re-using the Glob() implementation
entries, _ := f.Glob(path + f.Separator + "*")
if len(entries) > 0 {
return fmt.Errorf("the directory '%s' is not empty", path)
}
}
delete(f.files, absPath)
f.removedFiles = append(f.removedFiles, absPath)
// Make sure the parent directory still exists, if it only existed via this one entry
leaf := filepath.Base(absPath)
absPath = strings.TrimSuffix(absPath, f.Separator+leaf)
if absPath != f.Separator {
relPath := strings.TrimPrefix(absPath, f.Separator+f.currentDir+f.Separator)
dirExists, _ := f.DirExists(relPath)
if !dirExists {
f.AddDir(relPath)
}
}
return nil
}
@@ -248,7 +289,7 @@ func (f *FilesMock) Getwd() (string, error) {
return f.toAbsPath(""), nil
}
// Chdir changes virtually in to the given directory.
// Chdir changes virtually into the given directory.
// The directory needs to exist according to the files and directories via AddFile() and AddDirectory().
// The implementation does not support relative path components such as "..".
func (f *FilesMock) Chdir(path string) error {
@@ -267,47 +308,61 @@ func (f *FilesMock) Chdir(path string) error {
return nil
}
// Stat ...
func (f *FilesMock) Stat(name string) (os.FileInfo, error) {
props, exists := f.files[f.toAbsPath(name)]
// Stat returns an approximated os.FileInfo. For files, it returns properties that have been associated
// via the setup methods. For directories it depends. If a directory exists only implicitly, because
// it is the parent of an added file, default values will be reflected in the file info.
func (f *FilesMock) Stat(path string) (os.FileInfo, error) {
props, exists := f.files[f.toAbsPath(path)]
if !exists {
isDir, err := f.DirExists(f.toAbsPath(name))
// Check if this folder exists implicitly
isDir, err := f.DirExists(path)
if err != nil {
return nil, fmt.Errorf("Internal error inside mock: %w", err)
return nil, fmt.Errorf("internal error inside mock: %w", err)
}
if !isDir {
return nil, &os.PathError{
Op: "stat",
Path: name,
Path: path,
Err: fmt.Errorf("no such file or directory"),
}
}
// we assume some default // in the free wild wia umask
var mode os.FileMode = 0755
props = &fileProperties{}
props.mode = &mode
props.content = &dirContent
// we claim default umask, as no properties are stored for implicit folders
props = &fileProperties{
mode: defaultDirMode,
content: &dirContent,
}
}
return fileInfoMock{
name: path.Base(name),
mode: *props.mode,
name: filepath.Base(path),
mode: props.mode,
size: int64(len(*props.content)),
isDir: props.content == &dirContent,
isDir: props.isDir(),
}, nil
}
//Chmod ...
// Chmod changes the file mode for the entry at the given path
func (f *FilesMock) Chmod(path string, mode os.FileMode) error {
props, exists := f.files[f.toAbsPath(path)]
if !exists {
if exists {
props.mode = mode
return nil
}
// Check if the dir exists implicitly
isDir, err := f.DirExists(path)
if err != nil {
return fmt.Errorf("internal error inside mock: %w", err)
}
if !isDir {
return fmt.Errorf("chmod: %s: No such file or directory", path)
}
props.mode = &mode
if mode != defaultDirMode {
// we need to create properties to store the mode
f.AddDirWithMode(path, mode)
}
return nil
}
+140 -23
View File
@@ -63,6 +63,12 @@ func TestFilesMockDirExists(t *testing.T) {
assert.NoError(t, err)
assert.False(t, exists)
})
t.Run("root folder always exists", func(t *testing.T) {
files := FilesMock{}
exists, err := files.DirExists(string(os.PathSeparator))
assert.NoError(t, err)
assert.False(t, exists)
})
t.Run("dir exists after AddDir()", func(t *testing.T) {
files := FilesMock{}
path := filepath.Join("some", "path")
@@ -112,6 +118,17 @@ func TestFilesMockDirExists(t *testing.T) {
assert.False(t, exists, "Should not exist: '%s'", dir)
}
})
t.Run("dir still exists after removing last file", func(t *testing.T) {
files := FilesMock{}
dir := filepath.Join("path", "to")
file := filepath.Join(dir, "file")
files.AddFile(file, []byte("dummy content"))
err := files.FileRemove(file)
assert.NoError(t, err)
exists, err := files.DirExists(dir)
assert.NoError(t, err)
assert.True(t, exists)
})
}
func TestFilesMockCopy(t *testing.T) {
@@ -154,6 +171,53 @@ func TestFilesMockFileRemove(t *testing.T) {
assert.EqualError(t, err, "the file '"+path+"' does not exist: file does not exist")
assert.False(t, files.HasRemovedFile(path))
})
t.Run("fail to remove non-empty directory", func(t *testing.T) {
files := FilesMock{}
path := filepath.Join("dir", "file")
files.AddFile(path, []byte("dummy content"))
err := files.FileRemove("dir")
assert.Error(t, err)
})
t.Run("fail to remove non-empty directory also when it was explicitly added", func(t *testing.T) {
files := FilesMock{}
path := filepath.Join("dir", "file")
files.AddFile(path, []byte("dummy content"))
files.AddDir("dir")
err := files.FileRemove("dir")
assert.Error(t, err)
})
t.Run("succeed to remove empty directory when it was explicitly added", func(t *testing.T) {
files := FilesMock{}
files.AddDir("dir")
err := files.FileRemove("dir")
assert.NoError(t, err)
})
t.Run("removing chain of entries works", func(t *testing.T) {
files := FilesMock{}
path := filepath.Join("path", "to", "file")
files.AddFile(path, []byte("dummy content"))
assert.NoError(t, files.FileRemove(filepath.Join("path", "to", "file")))
assert.NoError(t, files.FileRemove(filepath.Join("path", "to")))
assert.NoError(t, files.FileRemove(filepath.Join("path")))
assert.Len(t, files.files, 0)
})
t.Run("removing entry from current dir works", func(t *testing.T) {
files := FilesMock{}
path := filepath.Join("path", "to", "file")
files.AddFile(path, []byte("dummy content"))
err := files.Chdir("path")
assert.NoError(t, err)
assert.NoError(t, files.FileRemove(filepath.Join("to", "file")))
assert.NoError(t, files.FileRemove(filepath.Join("to")))
err = files.Chdir("/")
assert.NoError(t, err)
assert.NoError(t, files.FileRemove(filepath.Join("path")))
assert.Len(t, files.files, 0)
})
t.Run("track removing a file", func(t *testing.T) {
files := FilesMock{}
path := filepath.Join("some", "file")
@@ -243,24 +307,16 @@ func TestFilesMockGlob(t *testing.T) {
})
}
var (
onlyMe os.FileMode = 0700
othersCanRead os.FileMode = 0644
othersCanReadAndExecute os.FileMode = 0755
everybodyCanReadAndExecute os.FileMode = 0777
)
func TestStat(t *testing.T) {
files := FilesMock{}
files.AddFile("tmp/dummy.txt", []byte("Hello SAP"))
files.AddDirWithMode("bin", 0700)
explicitMode := os.FileMode(0700)
files.AddDirWithMode("bin", explicitMode)
t.Run("non existing file", func(t *testing.T) {
_, err := files.Stat("doesNotExist.txt")
assert.EqualError(t, err, "stat doesNotExist.txt: no such file or directory")
})
t.Run("check file info", func(t *testing.T) {
info, err := files.Stat("tmp/dummy.txt")
@@ -269,54 +325,115 @@ func TestStat(t *testing.T) {
assert.Equal(t, "dummy.txt", info.Name())
assert.False(t, info.IsDir())
// if not specified otherwise the 644 file mode is used.
assert.Equal(t, othersCanRead, info.Mode())
assert.Equal(t, defaultFileMode, info.Mode())
}
})
t.Run("check implicit dir", func(t *testing.T) {
info, err := files.Stat("tmp")
if assert.NoError(t, err) {
assert.True(t, info.IsDir())
assert.Equal(t, othersCanReadAndExecute, info.Mode())
assert.Equal(t, defaultDirMode, info.Mode())
}
})
t.Run("check explicit dir", func(t *testing.T) {
info, err := files.Stat("bin")
if assert.NoError(t, err) {
assert.True(t, info.IsDir())
assert.Equal(t, onlyMe, info.Mode())
assert.Equal(t, explicitMode, info.Mode())
}
})
}
func TestGetChod(t *testing.T) {
func TestChmod(t *testing.T) {
files := FilesMock{}
files.AddDirWithMode("tmp", 0777)
files.AddFileWithMode("tmp/log.txt", []byte("build failed"), 0777)
t.Run("non existing file", func(t *testing.T) {
err := files.Chmod("does/not/exist", 0400)
assert.EqualError(t, err, "chmod: does/not/exist: No such file or directory")
})
t.Run("chmod for file", func(t *testing.T) {
err := files.Chmod("tmp/log.txt", 0644)
err := files.Chmod("tmp/log.txt", 0645)
if assert.NoError(t, err) {
info, e := files.Stat("tmp/log.txt")
if assert.NoError(t, e) {
assert.Equal(t, othersCanRead, info.Mode())
assert.Equal(t, os.FileMode(0645), info.Mode())
}
}
})
t.Run("chmod for directory", func(t *testing.T) {
err := files.Chmod("tmp", 0755)
err := files.Chmod("tmp", 0766)
if assert.NoError(t, err) {
info, e := files.Stat("tmp")
if assert.NoError(t, e) {
assert.Equal(t, othersCanReadAndExecute, info.Mode())
assert.Equal(t, os.FileMode(0766), info.Mode())
}
}
})
}
func TestRelativePaths(t *testing.T) {
t.Parallel()
t.Run("files are not mixed up", func(t *testing.T) {
files := FilesMock{}
files.AddFile("path/to/file", []byte("content"))
files.AddFile("file", []byte("root-content"))
err := files.Chdir("path")
if assert.NoError(t, err) {
exists, _ := files.FileExists("file")
assert.False(t, exists)
err := files.Chdir("to")
if assert.NoError(t, err) {
content, err := files.FileRead("file")
if assert.NoError(t, err) {
assert.Equal(t, []byte("content"), content, "should not read root file")
}
}
}
})
t.Run("root folder exists after change dir", func(t *testing.T) {
files := FilesMock{}
files.AddFile("path/to/file", []byte("content"))
errChdirInto := files.Chdir("path")
assert.NoError(t, errChdirInto)
exists, err := files.DirExists("/")
assert.NoError(t, err)
assert.True(t, exists, "the root folder should exist no matter the current dir")
})
t.Run("current folder always exists", func(t *testing.T) {
files := FilesMock{}
files.AddFile("path/to/file", []byte("content"))
exists, err := files.DirExists(".")
assert.NoError(t, err)
assert.True(t, exists, "the current folder should exist")
errChdirInto := files.Chdir("path")
assert.NoError(t, errChdirInto)
exists, err = files.DirExists("./")
assert.NoError(t, err)
assert.True(t, exists, "the current folder should exist after changing into it")
})
t.Run("chmod works on implicit, relative dir", func(t *testing.T) {
files := FilesMock{}
files.AddFile("path/to/file", []byte("content"))
errChdirInto := files.Chdir("path")
errChmod := files.Chmod("to", 0700)
errChdirBack := files.Chdir("/")
assert.NoError(t, errChdirInto)
assert.NoError(t, errChmod)
assert.NoError(t, errChdirBack)
fileInfo, err := files.Stat("path/to")
if assert.NoError(t, err) {
assert.Equal(t, os.FileMode(0700), fileInfo.Mode())
}
})
}
+22
View File
@@ -27,6 +27,28 @@ func TestFileExists(t *testing.T) {
})
}
func TestDirExists(t *testing.T) {
runInTempDir(t, "testing dir exists", "dir-exists", func(t *testing.T) {
err := os.Mkdir("test", 0777)
if err != nil {
t.Fatal("failed to create test dir in temporary dir")
}
files := Files{}
result, err := files.DirExists("test")
assert.NoError(t, err)
assert.True(t, result, "created folder should exist")
result, err = files.DirExists(".")
assert.NoError(t, err)
assert.True(t, result, "current directory should exist")
result, err = files.DirExists(string(os.PathSeparator))
assert.NoError(t, err)
assert.True(t, result, "root directory should exist")
})
}
func TestCopy(t *testing.T) {
runInTempDir(t, "copying file succeeds", "dir2", func(t *testing.T) {
file := "testFile"