github.com/jfrog/jfrog-cli-core/v2@v2.51.0/utils/coreutils/utils_test.go (about) 1 package coreutils 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "errors" 7 "fmt" 8 "os" 9 "os/exec" 10 "path/filepath" 11 "reflect" 12 "testing" 13 14 "github.com/jfrog/jfrog-client-go/utils/log" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestSpecVarsStringToMap(t *testing.T) { 19 var actual map[string]string 20 actual = SpecVarsStringToMap("") 21 assertSpecVars(nil, actual, t) 22 23 actual = SpecVarsStringToMap("foo=bar") 24 assertSpecVars(map[string]string{"foo": "bar"}, actual, t) 25 26 actual = SpecVarsStringToMap("foo=bar;bar=foo") 27 assertSpecVars(map[string]string{"foo": "bar", "bar": "foo"}, actual, t) 28 29 actual = SpecVarsStringToMap("foo=bar\\;bar=foo") 30 assertSpecVars(map[string]string{"foo": "bar;bar=foo"}, actual, t) 31 32 actual = SpecVarsStringToMap("a=b;foo=foo=bar\\;bar=foo") 33 assertSpecVars(map[string]string{"foo": "foo=bar;bar=foo", "a": "b"}, actual, t) 34 35 actual = SpecVarsStringToMap("foo=bar;foo=bar") 36 assertSpecVars(map[string]string{"foo": "bar"}, actual, t) 37 } 38 39 func assertSpecVars(expected, actual map[string]string, t *testing.T) { 40 if !reflect.DeepEqual(expected, actual) { 41 expectedMap, err := json.Marshal(expected) 42 assert.NoError(t, err) 43 actualMap, err := json.Marshal(actual) 44 assert.NoError(t, err) 45 t.Error("Wrong matching expected: `" + string(expectedMap) + "` Got `" + string(actualMap) + "`") 46 } 47 } 48 49 func TestGetExitCode(t *testing.T) { 50 // No error 51 exitCode := GetExitCode(nil, 0, 0, false) 52 checkExitCode(t, ExitCodeNoError, exitCode) 53 54 // Empty error 55 exitCode = GetExitCode(errors.New(""), 0, 0, false) 56 checkExitCode(t, ExitCodeError, exitCode) 57 58 // Regular error 59 exitCode = GetExitCode(errors.New("Error"), 0, 0, false) 60 checkExitCode(t, ExitCodeError, exitCode) 61 62 // Fail-no-op true without success 63 exitCode = GetExitCode(nil, 0, 0, true) 64 checkExitCode(t, ExitCodeFailNoOp, exitCode) 65 66 // Fail-no-op true with success 67 exitCode = GetExitCode(nil, 1, 0, true) 68 checkExitCode(t, ExitCodeNoError, exitCode) 69 70 // Fail-no-op false 71 exitCode = GetExitCode(nil, 0, 0, false) 72 checkExitCode(t, ExitCodeNoError, exitCode) 73 } 74 75 func checkExitCode(t *testing.T, expected, actual ExitCode) { 76 if expected != actual { 77 t.Errorf("Exit code expected %v, got %v", expected, actual) 78 } 79 } 80 81 func TestReplaceSpecVars(t *testing.T) { 82 log.SetLogger(log.NewLogger(log.INFO, nil)) 83 var actual []byte 84 actual = ReplaceVars([]byte("${foo}aa"), map[string]string{"a": "k", "foo": "bar"}) 85 assertVariablesMap([]byte("baraa"), actual, t) 86 87 actual = ReplaceVars([]byte("a${foo}a"), map[string]string{"foo": "bar"}) 88 assertVariablesMap([]byte("abara"), actual, t) 89 90 actual = ReplaceVars([]byte("aa${foo}"), map[string]string{"foo": "bar"}) 91 assertVariablesMap([]byte("aabar"), actual, t) 92 93 actual = ReplaceVars([]byte("${foo}${foo}${foo}"), map[string]string{"foo": "bar"}) 94 assertVariablesMap([]byte("barbarbar"), actual, t) 95 96 actual = ReplaceVars([]byte("${talk}-${broh}-${foo}"), map[string]string{"foo": "bar", "talk": "speak", "broh": "sroh"}) 97 assertVariablesMap([]byte("speak-sroh-bar"), actual, t) 98 99 actual = ReplaceVars([]byte("a${foo}a"), map[string]string{"foo": ""}) 100 assertVariablesMap([]byte("aa"), actual, t) 101 102 actual = ReplaceVars([]byte("a${foo}a"), map[string]string{"a": "k", "f": "a"}) 103 assertVariablesMap([]byte("a${foo}a"), actual, t) 104 105 actual = ReplaceVars([]byte("a${foo}a"), map[string]string{}) 106 assertVariablesMap([]byte("a${foo}a"), actual, t) 107 108 actual = ReplaceVars(nil, nil) 109 assertVariablesMap([]byte(""), actual, t) 110 } 111 112 func assertVariablesMap(expected, actual []byte, t *testing.T) { 113 if !bytes.Equal(expected, actual) { 114 t.Error("Wrong matching expected: `" + string(expected) + "` Got `" + string(actual) + "`") 115 } 116 } 117 118 type yesNoCases struct { 119 ans string 120 def bool 121 expectedParsed bool 122 expectedValid bool 123 testName string 124 } 125 126 func TestParseYesNo(t *testing.T) { 127 cases := []yesNoCases{ 128 // Positive answer. 129 {"yes", true, true, true, "yes"}, 130 {"y", true, true, true, "y"}, 131 {"Y", true, true, true, "y capital"}, 132 {"YES", true, true, true, "yes capital"}, 133 {"y", false, true, true, "positive with different default"}, 134 135 // Negative answer. 136 {"no", true, false, true, "no"}, 137 {"n", true, false, true, "n"}, 138 {"N", true, false, true, "n capital"}, 139 {"NO", true, false, true, "no capital"}, 140 {"n", false, false, true, "negative with different default"}, 141 142 // Default answer. 143 {"", true, true, true, "empty with positive default"}, 144 {"", false, false, true, "empty with negative default"}, 145 {" ", false, false, true, "empty with space"}, 146 147 // Spaces. 148 {" y", false, true, true, "space before"}, 149 {"yes ", true, true, true, "space after"}, 150 151 // Invalid answer. 152 {"notvalid", false, false, false, "invalid all lower"}, 153 {"yNOVALIDyes", false, false, false, "invalid changing"}, 154 } 155 156 for _, c := range cases { 157 t.Run(c.testName, func(t *testing.T) { 158 actualParsed, actualValid := parseYesNo(c.ans, c.def) 159 assert.Equal(t, actualValid, c.expectedValid) 160 if actualValid { 161 assert.Equal(t, actualParsed, c.expectedParsed) 162 } 163 }) 164 } 165 } 166 167 func TestListToText(t *testing.T) { 168 assert.Equal(t, ListToText([]string{"one"}), "one") 169 assert.Equal(t, ListToText([]string{"one", "two"}), "one and two") 170 assert.Equal(t, ListToText([]string{"one", "two", "three"}), "one, two and three") 171 } 172 173 func TestSplitRepoAndServerId(t *testing.T) { 174 // Test cases 175 tests := []struct { 176 serverAndRepo string 177 remoteEnv string 178 serverID string 179 repoName string 180 err error 181 }{ 182 { 183 serverAndRepo: "myServer/myRepo", 184 remoteEnv: ReleasesRemoteEnv, 185 serverID: "myServer", 186 repoName: "myRepo", 187 err: nil, 188 }, 189 { 190 serverAndRepo: "/myRepo", 191 remoteEnv: DeprecatedExtractorsRemoteEnv, 192 serverID: "", 193 repoName: "", 194 err: fmt.Errorf("'%s' environment variable is '/myRepo' but should be '<server ID>/<repo name>'", DeprecatedExtractorsRemoteEnv), 195 }, 196 { 197 serverAndRepo: "myServer/", 198 remoteEnv: ReleasesRemoteEnv, 199 serverID: "", 200 repoName: "", 201 err: fmt.Errorf("'%s' environment variable is 'myServer/' but should be '<server ID>/<repo name>'", ReleasesRemoteEnv), 202 }, 203 { 204 serverAndRepo: "", 205 remoteEnv: ReleasesRemoteEnv, 206 serverID: "", 207 repoName: "", 208 err: nil, 209 }, 210 { 211 serverAndRepo: "myServer/my/Repo", 212 remoteEnv: ReleasesRemoteEnv, 213 serverID: "myServer", 214 repoName: "my/Repo", 215 err: nil, 216 }, 217 } 218 for _, test := range tests { 219 func() { 220 assert.NoError(t, os.Setenv(test.remoteEnv, test.serverAndRepo)) 221 defer func() { 222 assert.NoError(t, os.Unsetenv(test.remoteEnv)) 223 }() 224 serverID, repoName, err := GetServerIdAndRepo(test.remoteEnv) 225 if err != nil { 226 assert.Equal(t, test.err.Error(), err.Error()) 227 return 228 } 229 // Assert the results 230 assert.Equal(t, test.serverID, serverID) 231 assert.Equal(t, test.repoName, repoName) 232 }() 233 } 234 } 235 236 func TestGetFullPathsWorkingDirs(t *testing.T) { 237 currentDir, err := GetWorkingDirectory() 238 assert.NoError(t, err) 239 dir1, err := filepath.Abs("dir1") 240 assert.NoError(t, err) 241 dir2, err := filepath.Abs("dir2") 242 assert.NoError(t, err) 243 tests := []struct { 244 name string 245 workingDirs []string 246 expectedDirs []string 247 }{ 248 { 249 name: "EmptyWorkingDirs", 250 workingDirs: []string{}, 251 expectedDirs: []string{currentDir}, 252 }, 253 { 254 name: "ValidWorkingDirs", 255 workingDirs: []string{"dir1", "dir2"}, 256 expectedDirs: []string{dir1, dir2}, 257 }, 258 } 259 260 for _, test := range tests { 261 t.Run(test.name, func(t *testing.T) { 262 actualDirs, err := GetFullPathsWorkingDirs(test.workingDirs) 263 assert.NoError(t, err) 264 assert.Equal(t, test.expectedDirs, actualDirs, "Incorrect full paths of working directories") 265 }) 266 } 267 } 268 269 func TestGetMaskedCommandString(t *testing.T) { 270 assert.Equal(t, 271 "pip -i ***@someurl.com/repo", 272 GetMaskedCommandString(exec.Command("pip", "-i", "https://user:pass@someurl.com/repo"))) 273 274 assert.Equal(t, 275 "pip -i ***@someurl.com/repo --password=***", 276 GetMaskedCommandString(exec.Command("pip", "-i", "https://user:pass@someurl.com/repo", "--password=123"))) 277 278 assert.Equal(t, 279 "pip -i ***@someurl.com/repo --access-token=***", 280 GetMaskedCommandString(exec.Command("pip", "-i", "https://user:pass@someurl.com/repo", "--access-token=123"))) 281 }