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