mirror of
https://github.com/helmfile/helmfile.git
synced 2026-10-18 05:54:40 +02:00
Introduce Helmfile's own filesystem abstraction to correctly unit test some components (#307)
Use abstracted FS Signed-off-by: Arkaitz Jimenez <arkaitzj@gmail.com> Signed-off-by: Arkaitz Jimenez <arkaitzj@gmail.com>
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
package filesystem
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type FileSystem struct {
|
||||
ReadFile func(string) ([]byte, error)
|
||||
ReadDir func(string) ([]fs.DirEntry, error)
|
||||
DeleteFile func(string) error
|
||||
FileExists func(string) (bool, error)
|
||||
Glob func(string) ([]string, error)
|
||||
FileExistsAt func(string) bool
|
||||
DirectoryExistsAt func(string) bool
|
||||
Stat func(string) (os.FileInfo, error)
|
||||
Getwd func() (string, error)
|
||||
Chdir func(string) error
|
||||
Abs func(string) (string, error)
|
||||
}
|
||||
|
||||
func DefaultFileSystem() *FileSystem {
|
||||
dfs := FileSystem{
|
||||
ReadFile: os.ReadFile,
|
||||
ReadDir: os.ReadDir,
|
||||
DeleteFile: os.Remove,
|
||||
Stat: os.Stat,
|
||||
Glob: filepath.Glob,
|
||||
Getwd: os.Getwd,
|
||||
Chdir: os.Chdir,
|
||||
Abs: filepath.Abs,
|
||||
}
|
||||
|
||||
dfs.FileExistsAt = dfs.fileExistsAtDefault
|
||||
dfs.DirectoryExistsAt = dfs.directoryExistsDefault
|
||||
dfs.FileExists = dfs.fileExistsDefault
|
||||
return &dfs
|
||||
}
|
||||
|
||||
func FromFileSystem(params FileSystem) *FileSystem {
|
||||
dfs := DefaultFileSystem()
|
||||
|
||||
if params.ReadFile != nil {
|
||||
dfs.ReadFile = params.ReadFile
|
||||
}
|
||||
if params.ReadDir != nil {
|
||||
dfs.ReadDir = params.ReadDir
|
||||
}
|
||||
if params.DeleteFile != nil {
|
||||
dfs.DeleteFile = params.DeleteFile
|
||||
}
|
||||
if params.FileExists != nil {
|
||||
dfs.FileExists = params.FileExists
|
||||
}
|
||||
if params.Glob != nil {
|
||||
dfs.Glob = params.Glob
|
||||
}
|
||||
if params.FileExistsAt != nil {
|
||||
dfs.FileExistsAt = params.FileExistsAt
|
||||
}
|
||||
if params.DirectoryExistsAt != nil {
|
||||
dfs.DirectoryExistsAt = params.DirectoryExistsAt
|
||||
}
|
||||
if params.Stat != nil {
|
||||
dfs.Stat = params.Stat
|
||||
}
|
||||
if params.Getwd != nil {
|
||||
dfs.Getwd = params.Getwd
|
||||
}
|
||||
if params.Chdir != nil {
|
||||
dfs.Chdir = params.Chdir
|
||||
}
|
||||
if params.Abs != nil {
|
||||
dfs.Abs = params.Abs
|
||||
}
|
||||
|
||||
return dfs
|
||||
}
|
||||
|
||||
func (filesystem *FileSystem) fileExistsAtDefault(path string) bool {
|
||||
fileInfo, err := filesystem.Stat(path)
|
||||
return err == nil && fileInfo.Mode().IsRegular()
|
||||
}
|
||||
|
||||
func (filesystem *FileSystem) fileExistsDefault(path string) (bool, error) {
|
||||
_, err := filesystem.Stat(path)
|
||||
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (filesystem *FileSystem) directoryExistsDefault(path string) bool {
|
||||
fileInfo, err := filesystem.Stat(path)
|
||||
return err == nil && fileInfo.Mode().IsDir()
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package filesystem
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TestFileInfo struct {
|
||||
mode fs.FileMode
|
||||
}
|
||||
|
||||
func (tfi TestFileInfo) Name() string { return "" }
|
||||
func (tfi TestFileInfo) Size() int64 { return 0 }
|
||||
func (tfi TestFileInfo) Mode() fs.FileMode { return tfi.mode }
|
||||
func (tfi TestFileInfo) ModTime() time.Time { return time.Time{} }
|
||||
func (tfi TestFileInfo) IsDir() bool { return tfi.mode.IsDir() }
|
||||
func (tfi TestFileInfo) Sys() any { return nil }
|
||||
|
||||
func NewTestFileSystem() FileSystem {
|
||||
replaceffs := FileSystem{
|
||||
Stat: func(s string) (os.FileInfo, error) {
|
||||
if strings.HasPrefix(s, "existing_file") {
|
||||
return TestFileInfo{mode: 0}, nil
|
||||
}
|
||||
if strings.HasPrefix(s, "existing_dir") {
|
||||
return TestFileInfo{mode: fs.ModeDir}, nil
|
||||
}
|
||||
return nil, errors.New("Error")
|
||||
},
|
||||
}
|
||||
return *FromFileSystem(replaceffs)
|
||||
}
|
||||
|
||||
func TestFs_fileExistsDefault(t *testing.T) {
|
||||
ffs := NewTestFileSystem()
|
||||
var exists, _ = ffs.FileExists("existing_file.txt")
|
||||
if !exists {
|
||||
t.Errorf("Expected file %s, not found", "existing_file.txt")
|
||||
}
|
||||
|
||||
exists, _ = ffs.FileExists("non_existing_file.txt")
|
||||
if exists {
|
||||
t.Errorf("Not expected file %s, found", "non_existing_file.txt")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFs_fileExistsAtDefault(t *testing.T) {
|
||||
ffs := NewTestFileSystem()
|
||||
|
||||
var exists = ffs.FileExistsAt("existing_file.txt")
|
||||
if !exists {
|
||||
t.Errorf("Expected file %s, not found", "existing_file.txt")
|
||||
}
|
||||
|
||||
exists = ffs.FileExistsAt("non_existing_file.txt")
|
||||
if exists {
|
||||
t.Errorf("Not expected file %s, found", "non_existing_file.txt")
|
||||
}
|
||||
|
||||
exists = ffs.FileExistsAt("existing_dir")
|
||||
if exists {
|
||||
t.Errorf("Not expected file %s, found", "existing_dir")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFs_directoryExistsDefault(t *testing.T) {
|
||||
ffs := NewTestFileSystem()
|
||||
var exists = ffs.DirectoryExistsAt("existing_dir")
|
||||
if !exists {
|
||||
t.Errorf("Expected file %s, not found", "existing_dir")
|
||||
}
|
||||
|
||||
exists = ffs.DirectoryExistsAt("not_existing_dir")
|
||||
if exists {
|
||||
t.Errorf("Not expected file %s, found", "existing_dir")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFs_DefaultBuilder(t *testing.T) {
|
||||
ffs := DefaultFileSystem()
|
||||
if ffs.ReadFile == nil ||
|
||||
ffs.ReadDir == nil ||
|
||||
ffs.DeleteFile == nil ||
|
||||
ffs.FileExists == nil ||
|
||||
ffs.Glob == nil ||
|
||||
ffs.FileExistsAt == nil ||
|
||||
ffs.DirectoryExistsAt == nil ||
|
||||
ffs.Stat == nil ||
|
||||
ffs.Getwd == nil ||
|
||||
ffs.Chdir == nil ||
|
||||
ffs.Abs == nil {
|
||||
t.Errorf("Missing functions in DefaultFileSystem")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user