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 15package fs 16 17import ( 18 "io/ioutil" 19 "path/filepath" 20 "reflect" 21 "sort" 22 "testing" 23 "time" 24) 25 26func 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 35func Create(t *testing.T, path string, filesystem *MockFs) { 36 Write(t, path, "hi", filesystem) 37} 38 39func 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 46func 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 53func 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 60func 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 72func 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 } 77 bytes, err := ioutil.ReadAll(reader) 78 if err != nil { 79 t.Fatal(err.Error()) 80 } 81 return string(bytes) 82} 83 84func ModTime(t *testing.T, path string, filesystem *MockFs) time.Time { 85 stats, err := filesystem.Lstat(path) 86 if err != nil { 87 t.Fatal(err.Error()) 88 } 89 return stats.ModTime() 90} 91 92func SetReadable(t *testing.T, path string, readable bool, filesystem *MockFs) { 93 err := filesystem.SetReadable(path, readable) 94 if err != nil { 95 t.Fatal(err.Error()) 96 } 97} 98 99func SetReadErr(t *testing.T, path string, readErr error, filesystem *MockFs) { 100 err := filesystem.SetReadErr(path, readErr) 101 if err != nil { 102 t.Fatal(err.Error()) 103 } 104} 105 106func AssertSameResponse(t *testing.T, actual []string, expected []string) { 107 t.Helper() 108 sort.Strings(actual) 109 sort.Strings(expected) 110 if !reflect.DeepEqual(actual, expected) { 111 t.Fatalf("Expected Finder to return these %v paths:\n %v,\ninstead returned these %v paths: %v\n", 112 len(expected), expected, len(actual), actual) 113 } 114} 115 116func AssertSameStatCalls(t *testing.T, actual []string, expected []string) { 117 t.Helper() 118 sort.Strings(actual) 119 sort.Strings(expected) 120 121 if !reflect.DeepEqual(actual, expected) { 122 t.Fatalf("Finder made incorrect Stat calls.\n"+ 123 "Actual:\n"+ 124 "%v\n"+ 125 "Expected:\n"+ 126 "%v\n"+ 127 "\n", 128 actual, expected) 129 } 130} 131 132func AssertSameReadDirCalls(t *testing.T, actual []string, expected []string) { 133 t.Helper() 134 sort.Strings(actual) 135 sort.Strings(expected) 136 137 if !reflect.DeepEqual(actual, expected) { 138 t.Fatalf("Finder made incorrect ReadDir calls.\n"+ 139 "Actual:\n"+ 140 "%v\n"+ 141 "Expected:\n"+ 142 "%v\n"+ 143 "\n", 144 actual, expected) 145 } 146} 147