mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-11 13:02:14 +00:00
Moving filesystem mock to pkg/util, and added some functionality
This commit is contained in:
parent
b8fde17fc2
commit
fde9541c80
@ -19,12 +19,15 @@ package filesystem
|
|||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DefaultFs implements Filesystem using same-named functions from "os" and "io/ioutil"
|
// DefaultFs implements Filesystem using same-named functions from "os" and "io/ioutil"
|
||||||
type DefaultFs struct{}
|
type DefaultFs struct{}
|
||||||
|
|
||||||
|
var _ Filesystem = DefaultFs{}
|
||||||
|
|
||||||
// Stat via os.Stat
|
// Stat via os.Stat
|
||||||
func (DefaultFs) Stat(name string) (os.FileInfo, error) {
|
func (DefaultFs) Stat(name string) (os.FileInfo, error) {
|
||||||
return os.Stat(name)
|
return os.Stat(name)
|
||||||
@ -54,12 +57,17 @@ func (DefaultFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
|
|||||||
return os.Chtimes(name, atime, mtime)
|
return os.Chtimes(name, atime, mtime)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadFile via os.ReadFile
|
// RemoveAll via os.RemoveAll
|
||||||
|
func (DefaultFs) RemoveAll(path string) error {
|
||||||
|
return os.RemoveAll(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadFile via ioutil.ReadFile
|
||||||
func (DefaultFs) ReadFile(filename string) ([]byte, error) {
|
func (DefaultFs) ReadFile(filename string) ([]byte, error) {
|
||||||
return ioutil.ReadFile(filename)
|
return ioutil.ReadFile(filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TempFile via os.TempFile
|
// TempFile via ioutil.TempFile
|
||||||
func (DefaultFs) TempFile(dir, prefix string) (File, error) {
|
func (DefaultFs) TempFile(dir, prefix string) (File, error) {
|
||||||
file, err := ioutil.TempFile(dir, prefix)
|
file, err := ioutil.TempFile(dir, prefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -68,6 +76,16 @@ func (DefaultFs) TempFile(dir, prefix string) (File, error) {
|
|||||||
return &defaultFile{file}, nil
|
return &defaultFile{file}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadDir via ioutil.ReadDir
|
||||||
|
func (DefaultFs) ReadDir(dirname string) ([]os.FileInfo, error) {
|
||||||
|
return ioutil.ReadDir(dirname)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Walk via filepath.Walk
|
||||||
|
func (DefaultFs) Walk(root string, walkFn filepath.WalkFunc) error {
|
||||||
|
return filepath.Walk(root, walkFn)
|
||||||
|
}
|
||||||
|
|
||||||
// defaultFile implements File using same-named functions from "os"
|
// defaultFile implements File using same-named functions from "os"
|
||||||
type defaultFile struct {
|
type defaultFile struct {
|
||||||
file *os.File
|
file *os.File
|
@ -21,6 +21,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
// fakeFs is implemented in terms of afero
|
// fakeFs is implemented in terms of afero
|
||||||
@ -62,12 +63,12 @@ func (fs *fakeFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
|
|||||||
return fs.a.Fs.Chtimes(name, atime, mtime)
|
return fs.a.Fs.Chtimes(name, atime, mtime)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadFile via afero.Fs.ReadFile
|
// ReadFile via afero.ReadFile
|
||||||
func (fs *fakeFs) ReadFile(filename string) ([]byte, error) {
|
func (fs *fakeFs) ReadFile(filename string) ([]byte, error) {
|
||||||
return fs.a.ReadFile(filename)
|
return fs.a.ReadFile(filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TempFile via afero.Fs.TempFile
|
// TempFile via afero.TempFile
|
||||||
func (fs *fakeFs) TempFile(dir, prefix string) (File, error) {
|
func (fs *fakeFs) TempFile(dir, prefix string) (File, error) {
|
||||||
file, err := fs.a.TempFile(dir, prefix)
|
file, err := fs.a.TempFile(dir, prefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -76,6 +77,21 @@ func (fs *fakeFs) TempFile(dir, prefix string) (File, error) {
|
|||||||
return &fakeFile{file}, nil
|
return &fakeFile{file}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadDir via afero.ReadDir
|
||||||
|
func (fs *fakeFs) ReadDir(dirname string) ([]os.FileInfo, error) {
|
||||||
|
return fs.a.ReadDir(dirname)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Walk via afero.Walk
|
||||||
|
func (fs *fakeFs) Walk(root string, walkFn filepath.WalkFunc) error {
|
||||||
|
return fs.a.Walk(root, walkFn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveAll via afero.RemoveAll
|
||||||
|
func (fs *fakeFs) RemoveAll(path string) error {
|
||||||
|
return fs.a.RemoveAll(path)
|
||||||
|
}
|
||||||
|
|
||||||
// fakeFile implements File; for use with fakeFs
|
// fakeFile implements File; for use with fakeFs
|
||||||
type fakeFile struct {
|
type fakeFile struct {
|
||||||
file afero.File
|
file afero.File
|
@ -18,6 +18,7 @@ package filesystem
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,10 +30,13 @@ type Filesystem interface {
|
|||||||
Rename(oldpath, newpath string) error
|
Rename(oldpath, newpath string) error
|
||||||
MkdirAll(path string, perm os.FileMode) error
|
MkdirAll(path string, perm os.FileMode) error
|
||||||
Chtimes(name string, atime time.Time, mtime time.Time) error
|
Chtimes(name string, atime time.Time, mtime time.Time) error
|
||||||
|
RemoveAll(path string) error
|
||||||
|
|
||||||
// from "io/ioutil"
|
// from "io/ioutil"
|
||||||
ReadFile(filename string) ([]byte, error)
|
ReadFile(filename string) ([]byte, error)
|
||||||
TempFile(dir, prefix string) (File, error)
|
TempFile(dir, prefix string) (File, error)
|
||||||
|
ReadDir(dirname string) ([]os.FileInfo, error)
|
||||||
|
Walk(root string, walkFn filepath.WalkFunc) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// File is an interface that we can use to mock various filesystem operations typically
|
// File is an interface that we can use to mock various filesystem operations typically
|
Loading…
Reference in New Issue
Block a user