fakefs.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package filesystem
  14. import (
  15. "os"
  16. "path/filepath"
  17. "time"
  18. "github.com/spf13/afero"
  19. )
  20. // fakeFs is implemented in terms of afero
  21. type fakeFs struct {
  22. a afero.Afero
  23. }
  24. // NewFakeFs returns a fake Filesystem that exists in-memory, useful for unit tests
  25. func NewFakeFs() Filesystem {
  26. return &fakeFs{a: afero.Afero{Fs: afero.NewMemMapFs()}}
  27. }
  28. // Stat via afero.Fs.Stat
  29. func (fs *fakeFs) Stat(name string) (os.FileInfo, error) {
  30. return fs.a.Fs.Stat(name)
  31. }
  32. // Create via afero.Fs.Create
  33. func (fs *fakeFs) Create(name string) (File, error) {
  34. file, err := fs.a.Fs.Create(name)
  35. if err != nil {
  36. return nil, err
  37. }
  38. return &fakeFile{file}, nil
  39. }
  40. // Rename via afero.Fs.Rename
  41. func (fs *fakeFs) Rename(oldpath, newpath string) error {
  42. return fs.a.Fs.Rename(oldpath, newpath)
  43. }
  44. // MkdirAll via afero.Fs.MkdirAll
  45. func (fs *fakeFs) MkdirAll(path string, perm os.FileMode) error {
  46. return fs.a.Fs.MkdirAll(path, perm)
  47. }
  48. // Chtimes via afero.Fs.Chtimes
  49. func (fs *fakeFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
  50. return fs.a.Fs.Chtimes(name, atime, mtime)
  51. }
  52. // ReadFile via afero.ReadFile
  53. func (fs *fakeFs) ReadFile(filename string) ([]byte, error) {
  54. return fs.a.ReadFile(filename)
  55. }
  56. // TempDir via afero.TempDir
  57. func (fs *fakeFs) TempDir(dir, prefix string) (string, error) {
  58. return fs.a.TempDir(dir, prefix)
  59. }
  60. // TempFile via afero.TempFile
  61. func (fs *fakeFs) TempFile(dir, prefix string) (File, error) {
  62. file, err := fs.a.TempFile(dir, prefix)
  63. if err != nil {
  64. return nil, err
  65. }
  66. return &fakeFile{file}, nil
  67. }
  68. // ReadDir via afero.ReadDir
  69. func (fs *fakeFs) ReadDir(dirname string) ([]os.FileInfo, error) {
  70. return fs.a.ReadDir(dirname)
  71. }
  72. // Walk via afero.Walk
  73. func (fs *fakeFs) Walk(root string, walkFn filepath.WalkFunc) error {
  74. return fs.a.Walk(root, walkFn)
  75. }
  76. // RemoveAll via afero.RemoveAll
  77. func (fs *fakeFs) RemoveAll(path string) error {
  78. return fs.a.RemoveAll(path)
  79. }
  80. // Remove via afero.RemoveAll
  81. func (fs *fakeFs) Remove(name string) error {
  82. return fs.a.Remove(name)
  83. }
  84. // fakeFile implements File; for use with fakeFs
  85. type fakeFile struct {
  86. file afero.File
  87. }
  88. // Name via afero.File.Name
  89. func (file *fakeFile) Name() string {
  90. return file.file.Name()
  91. }
  92. // Write via afero.File.Write
  93. func (file *fakeFile) Write(b []byte) (n int, err error) {
  94. return file.file.Write(b)
  95. }
  96. // Sync via afero.File.Sync
  97. func (file *fakeFile) Sync() error {
  98. return file.file.Sync()
  99. }
  100. // Close via afero.File.Close
  101. func (file *fakeFile) Close() error {
  102. return file.file.Close()
  103. }