github.com/nak3/source-to-image@v1.1.10-0.20180319140719-2ed55639898d/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  }
    67  
    68  // FakeReadCloser provider a fake ReadCloser
    69  type FakeReadCloser struct {
    70  	*bytes.Buffer
    71  	CloseCalled bool
    72  	CloseError  error
    73  }
    74  
    75  // Close closes the fake ReadCloser
    76  func (f *FakeReadCloser) Close() error {
    77  	f.CloseCalled = true
    78  	return f.CloseError
    79  }
    80  
    81  // FakeWriteCloser provider a fake ReadCloser
    82  type FakeWriteCloser struct {
    83  	bytes.Buffer
    84  }
    85  
    86  // Close closes the fake ReadCloser
    87  func (f *FakeWriteCloser) Close() error {
    88  	return nil
    89  }
    90  
    91  // ReadDir reads the files in specified directory
    92  func (f *FakeFileSystem) ReadDir(p string) ([]os.FileInfo, error) {
    93  	return f.Files, nil
    94  }
    95  
    96  // Lstat provides stats about a single file  (not following symlinks)
    97  func (f *FakeFileSystem) Lstat(p string) (os.FileInfo, error) {
    98  	for _, f := range f.Files {
    99  		if strings.HasSuffix(p, string(filepath.Separator)+f.Name()) {
   100  			return f, nil
   101  		}
   102  	}
   103  	return nil, &os.PathError{Path: p, Err: os.ErrNotExist}
   104  }
   105  
   106  // Stat returns a FileInfo describing the named file
   107  func (f *FakeFileSystem) Stat(p string) (os.FileInfo, error) {
   108  	fi, err := f.Lstat(p)
   109  	if err != nil || fi.Mode()&os.ModeSymlink == 0 {
   110  		return fi, err
   111  	}
   112  	p, err = f.Readlink(p)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  	return f.Lstat(p)
   117  }
   118  
   119  // Chmod manipulates permissions on the fake filesystem
   120  func (f *FakeFileSystem) Chmod(file string, mode os.FileMode) error {
   121  	f.mutex.Lock()
   122  	defer f.mutex.Unlock()
   123  	f.ChmodFile = append(f.ChmodFile, file)
   124  	f.ChmodMode = mode
   125  
   126  	return f.ChmodError[file]
   127  }
   128  
   129  // Rename renames files on the fake filesystem
   130  func (f *FakeFileSystem) Rename(from, to string) error {
   131  	f.RenameFrom = from
   132  	f.RenameTo = to
   133  	return f.RenameError
   134  }
   135  
   136  // MkdirAll creates a new directories on the fake filesystem
   137  func (f *FakeFileSystem) MkdirAll(dirname string) error {
   138  	f.MkdirAllDir = append(f.MkdirAllDir, dirname)
   139  	return f.MkdirAllError
   140  }
   141  
   142  // MkdirAllWithPermissions creates a new directories on the fake filesystem
   143  func (f *FakeFileSystem) MkdirAllWithPermissions(dirname string, perm os.FileMode) error {
   144  	f.MkdirAllDir = append(f.MkdirAllDir, dirname)
   145  	return f.MkdirAllError
   146  }
   147  
   148  // Mkdir creates a new directory on the fake filesystem
   149  func (f *FakeFileSystem) Mkdir(dirname string) error {
   150  	f.MkdirDir = dirname
   151  	return f.MkdirError
   152  }
   153  
   154  // Exists checks if the file exists in fake filesystem
   155  func (f *FakeFileSystem) Exists(file string) bool {
   156  	f.ExistsFile = append(f.ExistsFile, file)
   157  	return f.ExistsResult[file]
   158  }
   159  
   160  // Copy copies files on the fake filesystem
   161  func (f *FakeFileSystem) Copy(sourcePath, targetPath string) error {
   162  	f.CopySource = sourcePath
   163  	f.CopyDest = targetPath
   164  	return f.CopyError
   165  }
   166  
   167  // CopyContents copies directory contents on the fake filesystem
   168  func (f *FakeFileSystem) CopyContents(sourcePath, targetPath string) error {
   169  	f.CopySource = sourcePath
   170  	f.CopyDest = targetPath
   171  	return f.CopyError
   172  }
   173  
   174  // RemoveDirectory removes a directory in the fake filesystem
   175  func (f *FakeFileSystem) RemoveDirectory(dir string) error {
   176  	f.RemoveDirName = dir
   177  	return f.RemoveDirError
   178  }
   179  
   180  // CreateWorkingDirectory creates a fake working directory
   181  func (f *FakeFileSystem) CreateWorkingDirectory() (string, error) {
   182  	f.WorkingDirCalled = true
   183  	return f.WorkingDirResult, f.WorkingDirError
   184  }
   185  
   186  // Open opens a file
   187  func (f *FakeFileSystem) Open(file string) (io.ReadCloser, error) {
   188  	f.OpenFile = file
   189  	buf := bytes.NewBufferString(f.OpenContent)
   190  	f.OpenFileResult = &FakeReadCloser{
   191  		Buffer:     buf,
   192  		CloseError: f.OpenCloseError,
   193  	}
   194  	return f.OpenFileResult, f.OpenError
   195  }
   196  
   197  // Create creates a file
   198  func (f *FakeFileSystem) Create(file string) (io.WriteCloser, error) {
   199  	f.CreateFile = file
   200  	return &f.CreateContent, f.CreateError
   201  }
   202  
   203  // WriteFile writes a file
   204  func (f *FakeFileSystem) WriteFile(file string, data []byte) error {
   205  	f.WriteFileName = file
   206  	f.WriteFileContent = string(data)
   207  	return f.WriteFileError
   208  }
   209  
   210  // Walk walks the file tree rooted at root, calling walkFn for each file or
   211  // directory in the tree, including root.
   212  func (f *FakeFileSystem) Walk(root string, walkFn filepath.WalkFunc) error {
   213  	return filepath.Walk(root, walkFn)
   214  }
   215  
   216  // Readlink reads the destination of a symlink
   217  func (f *FakeFileSystem) Readlink(name string) (string, error) {
   218  	return f.ReadlinkName, f.ReadlinkError
   219  }
   220  
   221  // Symlink creates a symlink at newname, pointing to oldname
   222  func (f *FakeFileSystem) Symlink(oldname, newname string) error {
   223  	f.SymlinkOldname, f.SymlinkNewname = oldname, newname
   224  	return f.SymlinkError
   225  }