filesystem_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package test
  15. import (
  16. "errors"
  17. "os"
  18. "reflect"
  19. "testing"
  20. )
  21. func TestReadFile(t *testing.T) {
  22. tests := []struct {
  23. filesystem MockFilesystem
  24. filename string
  25. contents string
  26. err error
  27. }{
  28. {
  29. filename: "dne",
  30. err: os.ErrNotExist,
  31. },
  32. {
  33. filesystem: MockFilesystem{
  34. "exists": File{Contents: "hi"},
  35. },
  36. filename: "exists",
  37. contents: "hi",
  38. },
  39. {
  40. filesystem: MockFilesystem{
  41. "dir": File{Directory: true},
  42. },
  43. filename: "dir",
  44. err: errors.New("read dir: is a directory"),
  45. },
  46. }
  47. for i, tt := range tests {
  48. contents, err := tt.filesystem.ReadFile(tt.filename)
  49. if tt.contents != string(contents) {
  50. t.Errorf("bad contents (test %d): want %q, got %q", i, tt.contents, string(contents))
  51. }
  52. if !reflect.DeepEqual(tt.err, err) {
  53. t.Errorf("bad error (test %d): want %v, got %v", i, tt.err, err)
  54. }
  55. }
  56. }
  57. func TestNewMockFilesystem(t *testing.T) {
  58. tests := []struct {
  59. files []File
  60. filesystem MockFilesystem
  61. }{
  62. {
  63. filesystem: MockFilesystem{},
  64. },
  65. {
  66. files: []File{{Path: "file"}},
  67. filesystem: MockFilesystem{
  68. "file": File{Path: "file"},
  69. },
  70. },
  71. {
  72. files: []File{{Path: "/file"}},
  73. filesystem: MockFilesystem{
  74. "/file": File{Path: "/file"},
  75. },
  76. },
  77. {
  78. files: []File{{Path: "/dir/file"}},
  79. filesystem: MockFilesystem{
  80. "/dir": File{Path: "/dir", Directory: true},
  81. "/dir/file": File{Path: "/dir/file"},
  82. },
  83. },
  84. {
  85. files: []File{{Path: "/dir/dir/file"}},
  86. filesystem: MockFilesystem{
  87. "/dir": File{Path: "/dir", Directory: true},
  88. "/dir/dir": File{Path: "/dir/dir", Directory: true},
  89. "/dir/dir/file": File{Path: "/dir/dir/file"},
  90. },
  91. },
  92. {
  93. files: []File{{Path: "/dir/dir/dir", Directory: true}},
  94. filesystem: MockFilesystem{
  95. "/dir": File{Path: "/dir", Directory: true},
  96. "/dir/dir": File{Path: "/dir/dir", Directory: true},
  97. "/dir/dir/dir": File{Path: "/dir/dir/dir", Directory: true},
  98. },
  99. },
  100. }
  101. for i, tt := range tests {
  102. filesystem := NewMockFilesystem(tt.files...)
  103. if !reflect.DeepEqual(tt.filesystem, filesystem) {
  104. t.Errorf("bad filesystem (test %d): want %#v, got %#v", i, tt.filesystem, filesystem)
  105. }
  106. }
  107. }