1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 2 3package utils 4 5import ( 6 "testing" 7) 8 9// ===== Filesystem and hash functionality tests ===== 10func TestFilesystemCanGetSliceOfFolderContents(t *testing.T) { 11 dirRoot := "../testdata/project1/" 12 13 filePaths, err := GetAllFilePaths(dirRoot, nil) 14 if err != nil { 15 t.Fatalf("expected filePaths, got error: %v", err) 16 } 17 if filePaths == nil { 18 t.Fatalf("expected non-nil filePaths, got nil") 19 } 20 // should only be 5 files 21 // symbolic link in project1/symbolic-link should be ignored 22 if len(filePaths) != 5 { 23 t.Fatalf("expected %v, got %v", 5, len(filePaths)) 24 } 25 26 // should be in alphabetical order, with files prefixed with '/' 27 if filePaths[0] != "/emptyfile.testdata.txt" { 28 t.Errorf("expected %v, got %v", "/emptyfile.testdata.txt", filePaths[0]) 29 } 30 if filePaths[1] != "/file1.testdata.txt" { 31 t.Errorf("expected %v, got %v", "/file1.testdata.txt", filePaths[1]) 32 } 33 if filePaths[2] != "/file3.testdata.txt" { 34 t.Errorf("expected %v, got %v", "/file3.testdata.txt", filePaths[2]) 35 } 36 if filePaths[3] != "/folder1/file4.testdata.txt" { 37 t.Errorf("expected %v, got %v", "/folder1/file4.testdata.txt", filePaths[3]) 38 } 39 if filePaths[4] != "/lastfile.testdata.txt" { 40 t.Errorf("expected %v, got %v", "/lastfile.testdata.txt", filePaths[4]) 41 } 42} 43 44func TestFilesystemGetAllFilePathsFailsForNonExistentDirectory(t *testing.T) { 45 dirRoot := "./does/not/exist/" 46 47 _, err := GetAllFilePaths(dirRoot, nil) 48 if err == nil { 49 t.Errorf("expected non-nil error, got nil") 50 } 51} 52 53func TestFilesystemCanIgnoreFilesWhenGettingFilePaths(t *testing.T) { 54 dirRoot := "../testdata/project3/" 55 pathsIgnored := []string{ 56 "**/ignoredir/", 57 "/excludedir/", 58 "**/ignorefile.txt", 59 "/alsoEXCLUDEthis.txt", 60 } 61 62 filePaths, err := GetAllFilePaths(dirRoot, pathsIgnored) 63 if err != nil { 64 t.Fatalf("expected filePaths, got error: %v", err) 65 } 66 if filePaths == nil { 67 t.Fatalf("expected non-nil filePaths, got nil") 68 } 69 70 // should only be 5 files 71 if len(filePaths) != 5 { 72 t.Fatalf("expected %v, got %v", 5, len(filePaths)) 73 } 74 75 // should be in alphabetical order, with files prefixed with '/' 76 if filePaths[0] != "/dontscan.txt" { 77 t.Errorf("expected %v, got %v", "/dontscan.txt", filePaths[0]) 78 } 79 if filePaths[1] != "/keep/keep.txt" { 80 t.Errorf("expected %v, got %v", "/keep/keep.txt", filePaths[1]) 81 } 82 if filePaths[2] != "/keep.txt" { 83 t.Errorf("expected %v, got %v", "/keep.txt", filePaths[2]) 84 } 85 if filePaths[3] != "/subdir/keep/dontscan.txt" { 86 t.Errorf("expected %v, got %v", "/subdir/keep/dontscan.txt", filePaths[3]) 87 } 88 if filePaths[4] != "/subdir/keep/keep.txt" { 89 t.Errorf("expected %v, got %v", "/subdir/keep/keep.txt", filePaths[4]) 90 } 91 92} 93 94// FIXME add test to make sure we get an error for a directory without 95// FIXME appropriate permissions to read its (sub)contents 96 97func TestFilesystemGetsHashesForFilePath(t *testing.T) { 98 f := "../testdata/project1/file1.testdata.txt" 99 100 ssha1, ssha256, smd5, err := GetHashesForFilePath(f) 101 if err != nil { 102 t.Fatalf("expected nil error, got %v", err) 103 } 104 if ssha1 != "024f870eb6323f532515f7a09d5646a97083b819" { 105 t.Errorf("expected %v, got %v", "024f870eb6323f532515f7a09d5646a97083b819", ssha1) 106 } 107 if ssha256 != "b14e44284ca477b4c0db34b15ca4c454b2947cce7883e22321cf2984050e15bf" { 108 t.Errorf("expected %v, got %v", "b14e44284ca477b4c0db34b15ca4c454b2947cce7883e22321cf2984050e15bf", ssha256) 109 } 110 if smd5 != "37c8208479dfe42d2bb29debd6e32d4a" { 111 t.Errorf("expected %v, got %v", "37c8208479dfe42d2bb29debd6e32d4a", smd5) 112 } 113} 114 115func TestFilesystemGetsErrorWhenRequestingHashesForInvalidFilePath(t *testing.T) { 116 f := "./does/not/exist" 117 118 _, _, _, err := GetHashesForFilePath(f) 119 if err == nil { 120 t.Errorf("expected non-nil error, got nil") 121 } 122} 123 124// FIXME add test to make sure we get an error for hashes for a file without 125// FIXME appropriate permissions to read its contents 126 127func TestFilesystemExcludesForIgnoredPaths(t *testing.T) { 128 // one specific file 129 pathsIgnored := []string{"/file.txt"} 130 fileName := "/file.txt" 131 if !ShouldIgnore(fileName, pathsIgnored) { 132 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 133 } 134 fileName = "/fileNope.txt" 135 if ShouldIgnore(fileName, pathsIgnored) { 136 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 137 } 138 139 // two specific files 140 pathsIgnored = []string{"/file.txt", "/file2.txt"} 141 fileName = "/file.txt" 142 if !ShouldIgnore(fileName, pathsIgnored) { 143 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 144 } 145 fileName = "/fileNope.txt" 146 if ShouldIgnore(fileName, pathsIgnored) { 147 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 148 } 149 fileName = "/file2.txt" 150 if !ShouldIgnore(fileName, pathsIgnored) { 151 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 152 } 153 154 // one specific file in specific subdirectory 155 pathsIgnored = []string{"/subdir/file.txt"} 156 fileName = "/subdir/file.txt" 157 if !ShouldIgnore(fileName, pathsIgnored) { 158 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 159 } 160 fileName = "/file.txt" 161 if ShouldIgnore(fileName, pathsIgnored) { 162 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 163 } 164 fileName = "/something/subdir/file.txt" 165 if ShouldIgnore(fileName, pathsIgnored) { 166 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 167 } 168 169 // one specific file in any directory 170 pathsIgnored = []string{"**/file.txt"} 171 fileName = "/subdir/file.txt" 172 if !ShouldIgnore(fileName, pathsIgnored) { 173 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 174 } 175 fileName = "/file.txt" 176 if !ShouldIgnore(fileName, pathsIgnored) { 177 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 178 } 179 fileName = "/something/subdir/file.txt" 180 if !ShouldIgnore(fileName, pathsIgnored) { 181 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 182 } 183 fileName = "/something/fileNope.txt" 184 if ShouldIgnore(fileName, pathsIgnored) { 185 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 186 } 187 188 // all files in one specific subdirectory (and its subdirectories) 189 pathsIgnored = []string{"/subdir/"} 190 fileName = "/subdir/file.txt" 191 if !ShouldIgnore(fileName, pathsIgnored) { 192 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 193 } 194 fileName = "/file.txt" 195 if ShouldIgnore(fileName, pathsIgnored) { 196 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 197 } 198 fileName = "/subdir/sub2/file.txt" 199 if !ShouldIgnore(fileName, pathsIgnored) { 200 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 201 } 202 fileName = "/nope/subdir/file.txt" 203 if ShouldIgnore(fileName, pathsIgnored) { 204 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 205 } 206 207 // all files in subdirectory with this name, wherever it appears 208 pathsIgnored = []string{"**/subdir/"} 209 fileName = "/subdir/file.txt" 210 if !ShouldIgnore(fileName, pathsIgnored) { 211 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 212 } 213 fileName = "/file.txt" 214 if ShouldIgnore(fileName, pathsIgnored) { 215 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 216 } 217 fileName = "/subdir/sub2/file.txt" 218 if !ShouldIgnore(fileName, pathsIgnored) { 219 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 220 } 221 fileName = "/nope/subdir/file.txt" 222 if !ShouldIgnore(fileName, pathsIgnored) { 223 t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored) 224 } 225 226} 227