...

Source file src/github.com/redhat-developer/odo/pkg/testingutil/filesystem/fake_fs.go

Documentation: github.com/redhat-developer/odo/pkg/testingutil/filesystem

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  /*
    18  	This package is a FORK of https://github.com/kubernetes/kubernetes/blob/master/pkg/util/filesystem/fakefs.go
    19  	See above license
    20  */
    21  
    22  package filesystem
    23  
    24  import (
    25  	"os"
    26  	"path/filepath"
    27  	"time"
    28  
    29  	"github.com/spf13/afero"
    30  )
    31  
    32  // fakeFs is implemented in terms of afero
    33  type fakeFs struct {
    34  	a afero.Afero
    35  }
    36  
    37  var _ Filesystem = (*fakeFs)(nil)
    38  
    39  // NewFakeFs returns a fake Filesystem that exists in-memory, useful for unit tests
    40  func NewFakeFs() Filesystem {
    41  	return &fakeFs{a: afero.Afero{Fs: afero.NewMemMapFs()}}
    42  }
    43  
    44  // Stat via afero.Fs.Stat
    45  func (fs *fakeFs) Stat(name string) (os.FileInfo, error) {
    46  	return fs.a.Fs.Stat(name)
    47  }
    48  
    49  // Create via afero.Fs.Create
    50  func (fs *fakeFs) Create(name string) (File, error) {
    51  	file, err := fs.a.Fs.Create(name)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	return &fakeFile{file}, nil
    56  }
    57  
    58  // Open via afero.Fs.Open
    59  func (fs *fakeFs) Open(name string) (File, error) {
    60  	file, err := fs.a.Fs.Open(name)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	return &fakeFile{file}, nil
    65  }
    66  
    67  // OpenFile via afero.Fs.OpenFile
    68  func (fs *fakeFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
    69  	file, err := fs.a.Fs.OpenFile(name, flag, perm)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	return &fakeFile{file}, nil
    74  }
    75  
    76  // Rename via afero.Fs.Rename
    77  func (fs *fakeFs) Rename(oldpath, newpath string) error {
    78  	return fs.a.Fs.Rename(oldpath, newpath)
    79  }
    80  
    81  // MkdirAll via afero.Fs.MkdirAll
    82  func (fs *fakeFs) MkdirAll(path string, perm os.FileMode) error {
    83  	return fs.a.Fs.MkdirAll(path, perm)
    84  }
    85  
    86  // Chtimes via afero.Fs.Chtimes
    87  func (fs *fakeFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
    88  	return fs.a.Fs.Chtimes(name, atime, mtime)
    89  }
    90  
    91  // ReadFile via afero.ReadFile
    92  func (fs *fakeFs) ReadFile(filename string) ([]byte, error) {
    93  	return fs.a.ReadFile(filename)
    94  }
    95  
    96  // WriteFile via afero.WriteFile
    97  func (fs *fakeFs) WriteFile(filename string, data []byte, perm os.FileMode) error {
    98  	return fs.a.WriteFile(filename, data, perm)
    99  }
   100  
   101  // TempDir via afero.TempDir
   102  func (fs *fakeFs) TempDir(dir, prefix string) (string, error) {
   103  	return fs.a.TempDir(dir, prefix)
   104  }
   105  
   106  // TempFile via afero.TempFile
   107  func (fs *fakeFs) TempFile(dir, prefix string) (File, error) {
   108  	file, err := fs.a.TempFile(dir, prefix)
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  	return &fakeFile{file}, nil
   113  }
   114  
   115  // ReadDir via afero.ReadDir
   116  func (fs *fakeFs) ReadDir(dirname string) ([]os.FileInfo, error) {
   117  	return fs.a.ReadDir(dirname)
   118  }
   119  
   120  // Walk via afero.Walk
   121  func (fs *fakeFs) Walk(root string, walkFn filepath.WalkFunc) error {
   122  	return fs.a.Walk(root, walkFn)
   123  }
   124  
   125  // RemoveAll via afero.RemoveAll
   126  func (fs *fakeFs) RemoveAll(path string) error {
   127  	return fs.a.RemoveAll(path)
   128  }
   129  
   130  func (fs *fakeFs) Getwd() (dir string, err error) {
   131  	return "/", nil
   132  }
   133  
   134  // Remove via afero.RemoveAll
   135  func (fs *fakeFs) Remove(name string) error {
   136  	return fs.a.Remove(name)
   137  }
   138  
   139  // Chmod via afero.Chmod
   140  func (fs *fakeFs) Chmod(name string, mode os.FileMode) error {
   141  	return fs.a.Chmod(name, mode)
   142  }
   143  
   144  // fakeFile implements File; for use with fakeFs
   145  type fakeFile struct {
   146  	file afero.File
   147  }
   148  
   149  var _ File = (*fakeFile)(nil)
   150  
   151  // Name via afero.File.Name
   152  func (file *fakeFile) Name() string {
   153  	return file.file.Name()
   154  }
   155  
   156  // Write via afero.File.Write
   157  func (file *fakeFile) Write(b []byte) (n int, err error) {
   158  	return file.file.Write(b)
   159  }
   160  
   161  // WriteString via afero.File.WriteString
   162  func (file *fakeFile) WriteString(s string) (n int, err error) {
   163  	return file.file.WriteString(s)
   164  }
   165  
   166  // Sync via afero.File.Sync
   167  func (file *fakeFile) Sync() error {
   168  	return file.file.Sync()
   169  }
   170  
   171  // Close via afero.File.Close
   172  func (file *fakeFile) Close() error {
   173  	return file.file.Close()
   174  }
   175  
   176  func (file *fakeFile) Readdir(n int) ([]os.FileInfo, error) {
   177  	return file.file.Readdir(n)
   178  }
   179  
   180  func (file *fakeFile) Read(b []byte) (n int, err error) {
   181  	return file.file.Read(b)
   182  }
   183  

View as plain text