fakefile.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. Copyright 2018 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 fs
  14. import (
  15. "bytes"
  16. "os"
  17. )
  18. var _ File = &FakeFile{}
  19. // FakeFile implements File in-memory for tests.
  20. type FakeFile struct {
  21. name string
  22. content []byte
  23. dir bool
  24. open bool
  25. }
  26. // makeDir makes a fake directory.
  27. func makeDir(name string) *FakeFile {
  28. return &FakeFile{name: name, dir: true}
  29. }
  30. // Close marks the fake file closed.
  31. func (f *FakeFile) Close() error {
  32. f.open = false
  33. return nil
  34. }
  35. // Read never fails, and doesn't mutate p.
  36. func (f *FakeFile) Read(p []byte) (n int, err error) {
  37. return len(p), nil
  38. }
  39. // Write saves the contents of the argument to memory.
  40. func (f *FakeFile) Write(p []byte) (n int, err error) {
  41. f.content = p
  42. return len(p), nil
  43. }
  44. // ContentMatches returns true if v matches fake file's content.
  45. func (f *FakeFile) ContentMatches(v []byte) bool {
  46. return bytes.Equal(v, f.content)
  47. }
  48. // GetContent the content of a fake file.
  49. func (f *FakeFile) GetContent() []byte {
  50. return f.content
  51. }
  52. // Stat returns nil.
  53. func (f *FakeFile) Stat() (os.FileInfo, error) {
  54. return nil, nil
  55. }