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