github.com/cobalt77/jfrog-client-go@v0.14.5/utils/io/fileutils/files_test.go (about) 1 package fileutils 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "os" 6 "path/filepath" 7 "testing" 8 ) 9 10 func TestIsSsh(t *testing.T) { 11 tests := []struct { 12 url string 13 expected bool 14 }{ 15 {"http://some.url", false}, 16 {"https://some.url", false}, 17 {"sshd://wrong.url", false}, 18 {"assh://wrong.url", false}, 19 {"ssh://some.url", true}, 20 {"sSh://some.url/some/api", true}, 21 {"SSH://some.url/some/api", true}, 22 } 23 for _, test := range tests { 24 t.Run(test.url, func(t *testing.T) { 25 assert.Equal(t, test.expected, IsSshUrl(test.url), "Wrong ssh for URL: " + test.url) 26 }) 27 } 28 } 29 30 func TestGetFileOrDirPathFile(t *testing.T) { 31 wd, err := os.Getwd() 32 if err != nil { 33 assert.Error(t, err) 34 return 35 } 36 defer os.Chdir(wd) 37 38 // CD into a directory with a go.mod file. 39 projectRoot := filepath.Join("testdata", "project") 40 err = os.Chdir(projectRoot) 41 if err != nil { 42 assert.Error(t, err) 43 return 44 } 45 46 // Make projectRoot an absolute path. 47 projectRoot, err = os.Getwd() 48 if err != nil { 49 assert.Error(t, err) 50 return 51 } 52 53 // Get the project root. 54 root, exists, err := FindUpstream("go.mod", File) 55 if err != nil { 56 assert.Error(t, err) 57 return 58 } 59 assert.True(t, exists, "File go.mod is missing.") 60 assert.Equal(t, projectRoot, root) 61 62 // CD back to the original directory. 63 if err := os.Chdir(wd); err != nil { 64 assert.Error(t, err) 65 return 66 } 67 68 // CD into a sub directory in the same project, and expect to get the same project root. 69 os.Chdir(wd) 70 projectSubDirectory := filepath.Join("testdata", "project", "dir") 71 err = os.Chdir(projectSubDirectory) 72 if err != nil { 73 assert.Error(t, err) 74 return 75 } 76 root, exists, err = FindUpstream("go.mod", File) 77 if err != nil { 78 assert.Error(t, err) 79 return 80 } 81 assert.True(t, exists, "File go.mod is missing.") 82 assert.Equal(t, projectRoot, root) 83 84 root, exists, err = FindUpstream("go-missing.mod", File) 85 if err != nil { 86 assert.Error(t, err) 87 return 88 } 89 assert.False(t, exists, "File go-missing.mod found but shouldn't.") 90 assert.Empty(t, root, "File go-missing.mod shouldn't be found") 91 92 // CD back to the original directory. 93 if err := os.Chdir(wd); err != nil { 94 assert.Error(t, err) 95 return 96 } 97 98 // Now CD into a directory outside the project, and expect to get a different project root. 99 noProjectRoot := filepath.Join("testdata", "noproject") 100 err = os.Chdir(noProjectRoot) 101 if err != nil { 102 assert.Error(t, err) 103 return 104 } 105 root, exists, err = FindUpstream("go.mod", File) 106 if err != nil { 107 assert.Error(t, err) 108 return 109 } 110 assert.True(t, exists, "File go.mod is missing.") 111 assert.NotEqual(t, projectRoot, root) 112 } 113 114 func TestGetFileOrDirPathFolder(t *testing.T) { 115 wd, err := os.Getwd() 116 if err != nil { 117 assert.Error(t, err) 118 return 119 } 120 defer os.Chdir(wd) 121 122 // Create path to directory to find. 123 dirPath := filepath.Join("testdata") 124 err = os.Chdir(dirPath) 125 if err != nil { 126 assert.Error(t, err) 127 return 128 } 129 // Get absolute path. 130 dirPath, err = os.Getwd() 131 if err != nil { 132 assert.Error(t, err) 133 return 134 } 135 // CD back to the original directory. 136 if err := os.Chdir(wd); err != nil { 137 assert.Error(t, err) 138 return 139 } 140 141 // Go to starting dir to search from. 142 searchFromDir := filepath.Join("testdata", "project", "dir") 143 err = os.Chdir(searchFromDir) 144 if err != nil { 145 assert.Error(t, err) 146 return 147 } 148 149 // Get the directory path. 150 root, exists, err := FindUpstream("noproject", Dir) 151 if err != nil { 152 assert.Error(t, err) 153 return 154 } 155 assert.True(t, exists, "Dir noproject is missing.") 156 assert.Equal(t, dirPath, root) 157 } 158 159 func TestIsEqualToLocalFile(t *testing.T) { 160 localFilePath := filepath.Join("testdata", "files", "comparisonFile") 161 162 // Get file actual details. 163 localFileDetails, err := GetFileDetails(localFilePath) 164 if err != nil { 165 assert.NoError(t, err) 166 return 167 } 168 169 actualMd5 := localFileDetails.Checksum.Md5 170 actualSha1 := localFileDetails.Checksum.Sha1 171 tests := []struct { 172 name string 173 localPath string 174 remoteMd5 string 175 remoteSha1 string 176 expectedResult bool 177 }{ 178 {"realEquality", localFilePath, actualMd5, actualSha1, true}, 179 {"unequalPath", "non/existing/path", actualMd5, actualSha1, false}, 180 {"unequalChecksum", localFilePath, "wrongMd5", "wrongSha1", false}, 181 {"unequalMd5", localFilePath, "wrongMd5", actualSha1, false}, 182 {"unequalSha1", localFilePath, actualMd5, "wrongSha1", false}, 183 } 184 185 for _, test := range tests { 186 t.Run(test.name, func(t *testing.T) { 187 isEqual, err := IsEqualToLocalFile(test.localPath, test.remoteMd5, test.remoteSha1) 188 if err != nil { 189 assert.NoError(t, err) 190 return 191 } 192 assert.Equal(t, test.expectedResult, isEqual) 193 }) 194 } 195 }