github.com/kubesphere/s2irun@v3.2.1+incompatible/pkg/test/fs/fs.go (about)

     1  package fs
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"sync"
    10  )
    11  
    12  // FakeFileSystem provides a fake filesystem structure for testing
    13  type FakeFileSystem struct {
    14  	ChmodFile  []string
    15  	ChmodMode  os.FileMode
    16  	ChmodError map[string]error
    17  
    18  	RenameFrom  string
    19  	RenameTo    string
    20  	RenameError error
    21  
    22  	MkdirAllDir   []string
    23  	MkdirAllError error
    24  
    25  	MkdirDir   string
    26  	MkdirError error
    27  
    28  	ExistsFile   []string
    29  	ExistsResult map[string]bool
    30  
    31  	CopySource string
    32  	CopyDest   string
    33  	CopyError  error
    34  
    35  	RemoveDirName  string
    36  	RemoveDirError error
    37  
    38  	WorkingDirCalled bool
    39  	WorkingDirResult string
    40  	WorkingDirError  error
    41  
    42  	OpenFile       string
    43  	OpenFileResult *FakeReadCloser
    44  	OpenContent    string
    45  	OpenError      error
    46  	OpenCloseError error
    47  
    48  	CreateFile    string
    49  	CreateContent FakeWriteCloser
    50  	CreateError   error
    51  
    52  	WriteFileName    string
    53  	WriteFileError   error
    54  	WriteFileContent string
    55  
    56  	ReadlinkName  string
    57  	ReadlinkError error
    58  
    59  	SymlinkOldname string
    60  	SymlinkNewname string
    61  	SymlinkError   error
    62  
    63  	Files []os.FileInfo
    64  
    65  	mutex        sync.Mutex
    66  	keepSymlinks bool
    67  }
    68  
    69  // FakeReadCloser provider a fake ReadCloser
    70  type FakeReadCloser struct {
    71  	*bytes.Buffer
    72  	CloseCalled bool
    73  	CloseError  error
    74  }
    75  
    76  // Close closes the fake ReadCloser
    77  func (f *FakeReadCloser) Close() error {
    78  	f.CloseCalled = true
    79  	return f.CloseError
    80  }
    81  
    82  // FakeWriteCloser provider a fake ReadCloser
    83  type FakeWriteCloser struct {
    84  	bytes.Buffer
    85  }
    86  
    87  // Close closes the fake ReadCloser
    88  func (f *FakeWriteCloser) Close() error {
    89  	return nil
    90  }
    91  
    92  // ReadDir reads the files in specified directory
    93  func (f *FakeFileSystem) ReadDir(p string) ([]os.FileInfo, error) {
    94  	return f.Files, nil
    95  }
    96  
    97  // Lstat provides stats about a single file  (not following symlinks)
    98  func (f *FakeFileSystem) Lstat(p string) (os.FileInfo, error) {
    99  	for _, f := range f.Files {
   100  		if strings.HasSuffix(p, string(filepath.Separator)+f.Name()) {
   101  			return f, nil
   102  		}
   103  	}
   104  	return nil, &os.PathError{Path: p, Err: os.ErrNotExist}
   105  }
   106  
   107  // Stat returns a FileInfo describing the named file
   108  func (f *FakeFileSystem) Stat(p string) (os.FileInfo, error) {
   109  	fi, err := f.Lstat(p)
   110  	if err != nil || fi.Mode()&os.ModeSymlink == 0 {
   111  		return fi, err
   112  	}
   113  	p, err = f.Readlink(p)
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  	return f.Lstat(p)
   118  }
   119  
   120  // Chmod manipulates permissions on the fake filesystem
   121  func (f *FakeFileSystem) Chmod(file string, mode os.FileMode) error {
   122  	f.mutex.Lock()
   123  	defer f.mutex.Unlock()
   124  	f.ChmodFile = append(f.ChmodFile, file)
   125  	f.ChmodMode = mode
   126  
   127  	return f.ChmodError[file]
   128  }
   129  
   130  // Rename renames files on the fake filesystem
   131  func (f *FakeFileSystem) Rename(from, to string) error {
   132  	f.RenameFrom = from
   133  	f.RenameTo = to
   134  	return f.RenameError
   135  }
   136  
   137  // MkdirAll creates a new directories on the fake filesystem
   138  func (f *FakeFileSystem) MkdirAll(dirname string) error {
   139  	f.MkdirAllDir = append(f.MkdirAllDir, dirname)
   140  	return f.MkdirAllError
   141  }
   142  
   143  // MkdirAllWithPermissions creates a new directories on the fake filesystem
   144  func (f *FakeFileSystem) MkdirAllWithPermissions(dirname string, perm os.FileMode) error {
   145  	f.MkdirAllDir = append(f.MkdirAllDir, dirname)
   146  	return f.MkdirAllError
   147  }
   148  
   149  // Mkdir creates a new directory on the fake filesystem
   150  func (f *FakeFileSystem) Mkdir(dirname string) error {
   151  	f.MkdirDir = dirname
   152  	return f.MkdirError
   153  }
   154  
   155  // Exists checks if the file exists in fake filesystem
   156  func (f *FakeFileSystem) Exists(file string) bool {
   157  	f.ExistsFile = append(f.ExistsFile, file)
   158  	return f.ExistsResult[file]
   159  }
   160  
   161  // Copy copies files on the fake filesystem
   162  func (f *FakeFileSystem) Copy(sourcePath, targetPath string) error {
   163  	f.CopySource = sourcePath
   164  	f.CopyDest = targetPath
   165  	return f.CopyError
   166  }
   167  
   168  // CopyContents copies directory contents on the fake filesystem
   169  func (f *FakeFileSystem) CopyContents(sourcePath, targetPath string) error {
   170  	f.CopySource = sourcePath
   171  	f.CopyDest = targetPath
   172  	return f.CopyError
   173  }
   174  
   175  // RemoveDirectory removes a directory in the fake filesystem
   176  func (f *FakeFileSystem) RemoveDirectory(dir string) error {
   177  	f.RemoveDirName = dir
   178  	return f.RemoveDirError
   179  }
   180  
   181  // CreateWorkingDirectory creates a fake working directory
   182  func (f *FakeFileSystem) CreateWorkingDirectory() (string, error) {
   183  	f.WorkingDirCalled = true
   184  	return f.WorkingDirResult, f.WorkingDirError
   185  }
   186  
   187  // Open opens a file
   188  func (f *FakeFileSystem) Open(file string) (io.ReadCloser, error) {
   189  	f.OpenFile = file
   190  	buf := bytes.NewBufferString(f.OpenContent)
   191  	f.OpenFileResult = &FakeReadCloser{
   192  		Buffer:     buf,
   193  		CloseError: f.OpenCloseError,
   194  	}
   195  	return f.OpenFileResult, f.OpenError
   196  }
   197  
   198  // Create creates a file
   199  func (f *FakeFileSystem) Create(file string) (io.WriteCloser, error) {
   200  	f.CreateFile = file
   201  	return &f.CreateContent, f.CreateError
   202  }
   203  
   204  // WriteFile writes a file
   205  func (f *FakeFileSystem) WriteFile(file string, data []byte) error {
   206  	f.WriteFileName = file
   207  	f.WriteFileContent = string(data)
   208  	return f.WriteFileError
   209  }
   210  
   211  // Walk walks the file tree rooted at root, calling walkFn for each file or
   212  // directory in the tree, including root.
   213  func (f *FakeFileSystem) Walk(root string, walkFn filepath.WalkFunc) error {
   214  	return filepath.Walk(root, walkFn)
   215  }
   216  
   217  // Readlink reads the destination of a symlink
   218  func (f *FakeFileSystem) Readlink(name string) (string, error) {
   219  	return f.ReadlinkName, f.ReadlinkError
   220  }
   221  
   222  // Symlink creates a symlink at newname, pointing to oldname
   223  func (f *FakeFileSystem) Symlink(oldname, newname string) error {
   224  	f.SymlinkOldname, f.SymlinkNewname = oldname, newname
   225  	return f.SymlinkError
   226  }
   227  
   228  // KeepSymlinks controls whether to handle symlinks as symlinks or follow
   229  // symlinks and copy files with content
   230  func (f *FakeFileSystem) KeepSymlinks(k bool) {
   231  	f.keepSymlinks = k
   232  }
   233  
   234  // ShouldKeepSymlinks informs whether to handle symlinks as symlinks or follow
   235  // symlinks and copy files by content
   236  func (f *FakeFileSystem) ShouldKeepSymlinks() bool {
   237  	return f.keepSymlinks
   238  }