test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2020 Google Inc. All rights reserved.
  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 fs
  15. import (
  16. "io/ioutil"
  17. "path/filepath"
  18. "reflect"
  19. "sort"
  20. "testing"
  21. "time"
  22. )
  23. func Write(t *testing.T, path string, content string, filesystem *MockFs) {
  24. parent := filepath.Dir(path)
  25. filesystem.MkDirs(parent)
  26. err := filesystem.WriteFile(path, []byte(content), 0777)
  27. if err != nil {
  28. t.Fatal(err.Error())
  29. }
  30. }
  31. func Create(t *testing.T, path string, filesystem *MockFs) {
  32. Write(t, path, "hi", filesystem)
  33. }
  34. func Delete(t *testing.T, path string, filesystem *MockFs) {
  35. err := filesystem.Remove(path)
  36. if err != nil {
  37. t.Fatal(err.Error())
  38. }
  39. }
  40. func RemoveAll(t *testing.T, path string, filesystem *MockFs) {
  41. err := filesystem.RemoveAll(path)
  42. if err != nil {
  43. t.Fatal(err.Error())
  44. }
  45. }
  46. func Move(t *testing.T, oldPath string, newPath string, filesystem *MockFs) {
  47. err := filesystem.Rename(oldPath, newPath)
  48. if err != nil {
  49. t.Fatal(err.Error())
  50. }
  51. }
  52. func Link(t *testing.T, newPath string, oldPath string, filesystem *MockFs) {
  53. parentPath := filepath.Dir(newPath)
  54. err := filesystem.MkDirs(parentPath)
  55. if err != nil {
  56. t.Fatal(err.Error())
  57. }
  58. err = filesystem.Symlink(oldPath, newPath)
  59. if err != nil {
  60. t.Fatal(err.Error())
  61. }
  62. }
  63. func Read(t *testing.T, path string, filesystem *MockFs) string {
  64. reader, err := filesystem.Open(path)
  65. if err != nil {
  66. t.Fatalf(err.Error())
  67. }
  68. defer reader.Close()
  69. bytes, err := ioutil.ReadAll(reader)
  70. if err != nil {
  71. t.Fatal(err.Error())
  72. }
  73. return string(bytes)
  74. }
  75. func ModTime(t *testing.T, path string, filesystem *MockFs) time.Time {
  76. stats, err := filesystem.Lstat(path)
  77. if err != nil {
  78. t.Fatal(err.Error())
  79. }
  80. return stats.ModTime()
  81. }
  82. func SetReadable(t *testing.T, path string, readable bool, filesystem *MockFs) {
  83. err := filesystem.SetReadable(path, readable)
  84. if err != nil {
  85. t.Fatal(err.Error())
  86. }
  87. }
  88. func SetReadErr(t *testing.T, path string, readErr error, filesystem *MockFs) {
  89. err := filesystem.SetReadErr(path, readErr)
  90. if err != nil {
  91. t.Fatal(err.Error())
  92. }
  93. }
  94. func AssertSameResponse(t *testing.T, actual []string, expected []string) {
  95. t.Helper()
  96. sort.Strings(actual)
  97. sort.Strings(expected)
  98. if !reflect.DeepEqual(actual, expected) {
  99. t.Fatalf("Expected Finder to return these %v paths:\n %v,\ninstead returned these %v paths: %v\n",
  100. len(expected), expected, len(actual), actual)
  101. }
  102. }
  103. func AssertSameStatCalls(t *testing.T, actual []string, expected []string) {
  104. t.Helper()
  105. sort.Strings(actual)
  106. sort.Strings(expected)
  107. if !reflect.DeepEqual(actual, expected) {
  108. t.Fatalf("Finder made incorrect Stat calls.\n"+
  109. "Actual:\n"+
  110. "%v\n"+
  111. "Expected:\n"+
  112. "%v\n"+
  113. "\n",
  114. actual, expected)
  115. }
  116. }
  117. func AssertSameReadDirCalls(t *testing.T, actual []string, expected []string) {
  118. t.Helper()
  119. sort.Strings(actual)
  120. sort.Strings(expected)
  121. if !reflect.DeepEqual(actual, expected) {
  122. t.Fatalf("Finder made incorrect ReadDir calls.\n"+
  123. "Actual:\n"+
  124. "%v\n"+
  125. "Expected:\n"+
  126. "%v\n"+
  127. "\n",
  128. actual, expected)
  129. }
  130. }