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