github.com/jfrog/jfrog-client-go@v1.40.2/utils/io/fileutils/files_test.go (about) 1 package fileutils 2 3 import ( 4 biutils "github.com/jfrog/build-info-go/utils" 5 "github.com/jfrog/jfrog-client-go/utils/io" 6 "os" 7 "path/filepath" 8 "reflect" 9 "regexp" 10 "strings" 11 "testing" 12 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestIsSsh(t *testing.T) { 17 testRuns := []struct { 18 url string 19 expected bool 20 }{ 21 {"http://some.url", false}, 22 {"https://some.url", false}, 23 {"sshd://wrong.url", false}, 24 {"assh://wrong.url", false}, 25 {"ssh://some.url", true}, 26 {"sSh://some.url/some/api", true}, 27 {"SSH://some.url/some/api", true}, 28 } 29 for _, test := range testRuns { 30 t.Run(test.url, func(t *testing.T) { 31 assert.Equal(t, test.expected, IsSshUrl(test.url), "Wrong ssh for URL: "+test.url) 32 }) 33 } 34 } 35 36 func TestFindUpstreamFile(t *testing.T) { 37 wd, err := os.Getwd() 38 if err != nil { 39 assert.Error(t, err) 40 return 41 } 42 defer func() { 43 assert.NoError(t, os.Chdir(wd)) 44 }() 45 46 // CD into a directory with a goDotMod.test file. 47 projectRoot := filepath.Join("testdata", "project") 48 err = os.Chdir(projectRoot) 49 if err != nil { 50 assert.Error(t, err) 51 return 52 } 53 54 // Make projectRoot an absolute path. 55 projectRoot, err = os.Getwd() 56 if err != nil { 57 assert.Error(t, err) 58 return 59 } 60 61 // Get the project root. 62 if err = assertFindUpstreamExistsAndEqual(t, "goDotMod.test", projectRoot, File); err != nil { 63 return 64 } 65 66 // Assert with Any too. 67 if err = assertFindUpstreamExistsAndEqual(t, "goDotMod.test", projectRoot, Any); err != nil { 68 return 69 } 70 71 // CD back to the original directory. 72 if err := os.Chdir(wd); err != nil { 73 assert.Error(t, err) 74 return 75 } 76 77 // CD into a subdirectory in the same project, and expect to get the same project root. 78 assert.NoError(t, os.Chdir(wd)) 79 projectSubDirectory := filepath.Join("testdata", "project", "dir") 80 err = os.Chdir(projectSubDirectory) 81 if err != nil { 82 assert.Error(t, err) 83 return 84 } 85 86 if err = assertFindUpstreamExistsAndEqual(t, "goDotMod.test", projectRoot, File); err != nil { 87 return 88 } 89 90 root, exists, err := FindUpstream("go-missing.mod", File) 91 if err != nil { 92 assert.Error(t, err) 93 return 94 } 95 assert.False(t, exists, "File go-missing.mod found but shouldn't.") 96 assert.Empty(t, root, "File go-missing.mod shouldn't be found") 97 98 // CD back to the original directory. 99 if err := os.Chdir(wd); err != nil { 100 assert.Error(t, err) 101 return 102 } 103 104 // Now CD into a directory outside the project, and expect to get a different project root. 105 noProjectRoot := filepath.Join("testdata", "noproject") 106 err = os.Chdir(noProjectRoot) 107 if err != nil { 108 assert.Error(t, err) 109 return 110 } 111 root, exists, err = FindUpstream("goDotMod.test", File) 112 if err != nil { 113 assert.Error(t, err) 114 return 115 } 116 assert.True(t, exists, "File goDotMod.test is missing.") 117 assert.NotEqual(t, projectRoot, root) 118 } 119 120 func TestFindUpstreamFolder(t *testing.T) { 121 wd, err := os.Getwd() 122 if err != nil { 123 assert.Error(t, err) 124 return 125 } 126 defer func() { 127 assert.NoError(t, os.Chdir(wd)) 128 }() 129 130 // Create path to directory to find. 131 dirPath := "testdata" 132 err = os.Chdir(dirPath) 133 if err != nil { 134 assert.Error(t, err) 135 return 136 } 137 // Get absolute path. 138 dirPath, err = os.Getwd() 139 if err != nil { 140 assert.Error(t, err) 141 return 142 } 143 // CD back to the original directory. 144 if err := os.Chdir(wd); err != nil { 145 assert.Error(t, err) 146 return 147 } 148 149 // Go to starting dir to search from. 150 searchFromDir := filepath.Join("testdata", "project", "dir") 151 err = os.Chdir(searchFromDir) 152 if err != nil { 153 assert.Error(t, err) 154 return 155 } 156 157 // Get the directory path. 158 if err = assertFindUpstreamExistsAndEqual(t, "noproject", dirPath, Dir); err != nil { 159 return 160 } 161 162 // Assert with Any too. 163 if err = assertFindUpstreamExistsAndEqual(t, "noproject", dirPath, Any); err != nil { 164 return 165 } 166 } 167 168 func assertFindUpstreamExistsAndEqual(t *testing.T, path, expectedPath string, itemType ItemType) error { 169 foundPath, exists, err := FindUpstream(path, itemType) 170 if err != nil { 171 assert.Error(t, err) 172 return err 173 } 174 assert.True(t, exists) 175 assert.Equal(t, expectedPath, foundPath) 176 return nil 177 } 178 179 func TestIsEqualToLocalFile(t *testing.T) { 180 localFilePath := filepath.Join("testdata", "files", "comparisonFile") 181 182 // Get file actual details. 183 localFileDetails, err := GetFileDetails(localFilePath, true) 184 if err != nil { 185 assert.NoError(t, err) 186 return 187 } 188 189 actualMd5 := localFileDetails.Checksum.Md5 190 actualSha1 := localFileDetails.Checksum.Sha1 191 tests := []struct { 192 name string 193 localPath string 194 remoteMd5 string 195 remoteSha1 string 196 expectedResult bool 197 }{ 198 {"realEquality", localFilePath, actualMd5, actualSha1, true}, 199 {"unequalPath", "non/existing/path", actualMd5, actualSha1, false}, 200 {"unequalChecksum", localFilePath, "wrongMd5", "wrongSha1", false}, 201 {"unequalMd5", localFilePath, "wrongMd5", actualSha1, false}, 202 {"unequalSha1", localFilePath, actualMd5, "wrongSha1", false}, 203 } 204 205 for _, test := range tests { 206 t.Run(test.name, func(t *testing.T) { 207 isEqual, err := IsEqualToLocalFile(test.localPath, test.remoteMd5, test.remoteSha1) 208 if err != nil { 209 assert.NoError(t, err) 210 return 211 } 212 assert.Equal(t, test.expectedResult, isEqual) 213 }) 214 } 215 } 216 217 func TestListFilesByFilterFunc(t *testing.T) { 218 testDir := filepath.Join("testdata", "listextension") 219 expected := []string{filepath.Join(testDir, "a.proj"), 220 filepath.Join(testDir, "b.csproj"), 221 filepath.Join(testDir, "someproj.csproj")} 222 223 // List files with extension that satisfy the filter function. 224 filterFunc := func(filePath string) (bool, error) { 225 ext := strings.TrimLeft(filepath.Ext(filePath), ".") 226 return regexp.MatchString(`.*proj$`, ext) 227 } 228 files, err := ListFilesByFilterFunc(testDir, filterFunc) 229 if err != nil { 230 assert.NoError(t, err) 231 return 232 } 233 assert.ElementsMatch(t, expected, files) 234 } 235 236 func TestGetFileAndDirFromPath(t *testing.T) { 237 testRuns := []struct { 238 path string 239 expectedFile string 240 expectedDir string 241 }{ 242 {"a\\\\b\\\\c.in", "c.in", "a\\\\b"}, 243 {"a\\b\\c.in", "c.in", "a\\b"}, 244 {"a/b/c.in", "c.in", "a/b"}, 245 {"a\\\\b\\\\", "", "a\\\\b"}, 246 {"", "", ""}, 247 {"a\\\\b\\c.in", "c.in", "a\\\\b"}, 248 {"a\\b\\\\c.in", "c.in", "a\\b"}, 249 {"\\c.in", "c.in", ""}, 250 {"\\\\c.in", "c.in", ""}, 251 } 252 for _, test := range testRuns { 253 File, Dir := GetFileAndDirFromPath(test.path) 254 assert.Equal(t, test.expectedFile, File, "Wrong file name for path: "+test.path) 255 assert.Equal(t, test.expectedDir, Dir, "Wrong dir for path: "+test.path) 256 } 257 } 258 259 func TestRemoveDirContents(t *testing.T) { 260 // Prepare the test environment in a temporary directory 261 tmpDirPath, err := CreateTempDir() 262 assert.NoError(t, err) 263 defer func() { 264 assert.NoError(t, RemoveTempDir(tmpDirPath)) 265 }() 266 err = biutils.CopyDir(filepath.Join("testdata", "removedircontents"), tmpDirPath, true, nil) 267 assert.NoError(t, err) 268 269 // Run the function 270 dirToEmptyPath := filepath.Join(tmpDirPath, "dirtoempty") 271 err = RemoveDirContents(dirToEmptyPath) 272 assert.NoError(t, err) 273 274 // Assert the directories contents: dirtoempty should be empty and dirtoremain should contain one file. 275 emptyDirFiles, err := os.ReadDir(dirToEmptyPath) 276 assert.NoError(t, err) 277 assert.Empty(t, emptyDirFiles) 278 remainedDirPath := filepath.Join(tmpDirPath, "dirtoremain") 279 remainedDirFiles, err := os.ReadDir(remainedDirPath) 280 assert.NoError(t, err) 281 assert.Len(t, remainedDirFiles, 1) 282 } 283 284 func TestListFilesRecursiveWalkIntoDirSymlink(t *testing.T) { 285 if io.IsWindows() { 286 t.Skip("Running on windows, skipping...") 287 } 288 expectedFileList := []string{ 289 "testdata/dirsymlinks", 290 "testdata/dirsymlinks/d1", 291 "testdata/dirsymlinks/d1/File_F1", 292 "testdata/dirsymlinks/d1/linktoparent", 293 "testdata/dirsymlinks/d1/linktoparent/d1", 294 "testdata/dirsymlinks/d1/linktoparent/d1/File_F1", 295 "testdata/dirsymlinks/d1/linktoparent/d2", 296 "testdata/dirsymlinks/d1/linktoparent/d2/d1link", 297 "testdata/dirsymlinks/d1/linktoparent/d2/d1link/File_F1", 298 "testdata/dirsymlinks/d2", 299 } 300 301 // This directory and its subdirectories contain a symlink to a parent directory and a symlink to a sibling directory. 302 testDirPath := filepath.Join("testdata", "dirsymlinks") 303 filesList, err := ListFilesRecursiveWalkIntoDirSymlink(testDirPath, true) 304 assert.NoError(t, err) 305 assert.True(t, reflect.DeepEqual(expectedFileList, filesList)) 306 }