github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/utils/argsutils_test.go (about) 1 package utils 2 3 import ( 4 "os" 5 "reflect" 6 "testing" 7 8 "github.com/jfrog/jfrog-cli-go/utils/cliutils" 9 ) 10 11 func TestFindAndRemoveFlagFromCommand(t *testing.T) { 12 args := [][]string{ 13 {"-X", "GET", "/api/build/test1", "--server-id", "test1", "--foo", "bar"}, 14 {"-X", "GET", "/api/build/test2", "--server-idea", "foo", "--server-id=test2"}, 15 {"-X", "GET", "api/build/test3", "--server-id", "test3", "--foo", "bar"}, 16 {"-X", "GET", "api/build/test3", "--build-name", "name", "--foo", "bar"}, 17 {"-X", "GET", "api/build/test3", "--build-number", "3", "--foo", "bar"}, 18 } 19 20 expected := []struct { 21 key string 22 value string 23 command []string 24 }{ 25 {"--server-id", "test1", []string{"-X", "GET", "/api/build/test1", "--foo", "bar"}}, 26 {"--server-id", "test2", []string{"-X", "GET", "/api/build/test2", "--server-idea", "foo"}}, 27 {"--server-id", "test3", []string{"-X", "GET", "api/build/test3", "--foo", "bar"}}, 28 {"--build-name", "name", []string{"-X", "GET", "api/build/test3", "--foo", "bar"}}, 29 {"--build-number", "3", []string{"-X", "GET", "api/build/test3", "--foo", "bar"}}, 30 } 31 32 for index, test := range args { 33 flagIndex, valueIndex, keyValue, err := FindFlag(expected[index].key, test) 34 if err != nil { 35 t.Error(err) 36 } 37 if keyValue != expected[index].value { 38 t.Errorf("Expected %s value: %s, got: %s.", expected[index].key, expected[index].value, keyValue) 39 } 40 RemoveFlagFromCommand(&test, flagIndex, valueIndex) 41 if !reflect.DeepEqual(test, expected[index].command) { 42 t.Errorf("Expected command arguments: %v, got: %v.", expected[index].command, test) 43 } 44 } 45 } 46 47 func TestFindFlag(t *testing.T) { 48 tests := getFlagTestCases() 49 for _, test := range tests { 50 t.Run(test.name, func(t *testing.T) { 51 actualIndex, actualValueIndex, actualValue, err := FindFlag(test.flagName, test.arguments) 52 53 // Check errors. 54 if err != nil && !test.expectErr { 55 t.Error(err) 56 } 57 if err == nil && test.expectErr { 58 t.Errorf("Expecting: error, Got: nil") 59 } 60 61 if err == nil { 62 // Validate results. 63 if actualValue != test.flagValue { 64 t.Errorf("Expected flag value of: %s, got: %s.", test.flagValue, actualValue) 65 } 66 if actualValueIndex != test.flagValueIndex { 67 t.Errorf("Expected flag value index of: %d, got: %d.", test.flagValueIndex, actualValueIndex) 68 } 69 if actualIndex != test.flagIndex { 70 t.Errorf("Expected flag index of: %d, got: %d.", test.flagIndex, actualIndex) 71 } 72 } 73 }) 74 } 75 } 76 77 func TestGetFlagValueAndValueIndex(t *testing.T) { 78 tests := getFlagTestCases() 79 for _, test := range tests { 80 t.Run(test.name, func(t *testing.T) { 81 actualValue, actualIndex, err := getFlagValueAndValueIndex(test.flagName, test.arguments, test.flagIndex) 82 83 // Validate errors. 84 if err != nil && !test.expectErr { 85 t.Error(err) 86 } 87 if err == nil && test.expectErr { 88 t.Errorf("Expecting: error, Got: nil") 89 } 90 91 // Validate results. 92 if actualValue != test.flagValue { 93 t.Errorf("Expected value of: %s, got: %s.", test.flagValue, actualValue) 94 } 95 if actualIndex != test.flagValueIndex { 96 t.Errorf("Expected value of: %d, got: %d.", test.flagValueIndex, actualIndex) 97 } 98 }) 99 } 100 } 101 102 func TestFindBooleanFlag(t *testing.T) { 103 tests := []struct { 104 flagName string 105 command []string 106 expectedIndex int 107 expectedValue bool 108 shouldFail bool 109 }{ 110 {"--foo", []string{"-X", "--GET", "--foo/api/build/test1", "--foo", "bar"}, 3, true, false}, 111 {"--server-id", []string{"-X", "GET", "/api/build/test2", "--server-idea", "foo"}, -1, false, false}, 112 {"--bar", []string{"-X", "GET", "api/build/test3", "--foo", "bar"}, -1, false, false}, 113 {"-X", []string{"-X=true", "GET", "api/build/test3", "--foo", "bar"}, 0, true, false}, 114 {"--json", []string{"-X=true", "GET", "api/build/test3", "--foo", "--json=false"}, 4, false, false}, 115 {"--dry-run", []string{"-X=falsee", "GET", "api/build/test3", "--dry-run=falsee", "--json"}, 3, false, true}, 116 } 117 118 for testIndex, test := range tests { 119 actualIndex, actualValue, err := FindBooleanFlag(test.flagName, test.command) 120 if test.shouldFail && err == nil { 121 t.Errorf("Test #%d: Should fail to parse the boolean value, but ended with nil error.", testIndex) 122 } 123 if actualIndex != test.expectedIndex { 124 t.Errorf("Test #%d: Expected index value: %d, got: %d.", testIndex, test.expectedIndex, actualIndex) 125 } 126 if actualValue != test.expectedValue { 127 t.Errorf("Test #%d: Expected value: %t, got: %t.", testIndex, test.expectedValue, actualValue) 128 } 129 } 130 } 131 132 func TestExtractBuildDetailsFromArgs(t *testing.T) { 133 tests := []struct { 134 command []string 135 expectedArgs []string 136 expectedBuildConfig *BuildConfiguration 137 }{ 138 {[]string{"-test", "--build-name", "test1", "--foo", "--build-number", "1", "--module", "module1"}, []string{"-test", "--foo"}, &BuildConfiguration{"test1", "1", "module1"}}, 139 {[]string{"--module=module2", "--build-name", "test2", "--foo", "bar", "--build-number=2"}, []string{"--foo", "bar"}, &BuildConfiguration{"test2", "2", "module2"}}, 140 {[]string{"foo", "-X", "123", "--build-name", "test3", "--bar", "--build-number=3", "--foox"}, []string{"foo", "-X", "123", "--bar", "--foox"}, &BuildConfiguration{"test3", "3", ""}}, 141 } 142 143 for _, test := range tests { 144 actualArgs, actualBuildConfig, err := ExtractBuildDetailsFromArgs(test.command) 145 if err != nil { 146 t.Error(err) 147 } 148 if !reflect.DeepEqual(actualArgs, test.expectedArgs) { 149 t.Errorf("Expected value: %v, got: %v.", test.expectedArgs, actualArgs) 150 } 151 if !reflect.DeepEqual(actualBuildConfig, test.expectedBuildConfig) { 152 t.Errorf("Expected value: %v, got: %v.", test.expectedBuildConfig, actualBuildConfig) 153 } 154 } 155 } 156 157 func TestExtractBuildDetailsFromEnv(t *testing.T) { 158 const buildNameEnv = "envBuildName" 159 const buildNumberEnv = "777" 160 tests := []struct { 161 command []string 162 expectedArgs []string 163 expectedBuildConfig *BuildConfiguration 164 }{ 165 {[]string{"-test", "--build-name", "test1", "--foo", "--build-number", "1", "--module", "module1"}, []string{"-test", "--foo"}, &BuildConfiguration{"test1", "1", "module1"}}, 166 {[]string{"foo", "-X", "123", "--bar", "--build-name=test3", "--build-number=3", "--foox"}, []string{"foo", "-X", "123", "--bar", "--foox"}, &BuildConfiguration{"test3", "3", ""}}, 167 {[]string{"foo", "-X", "123", "--bar", "--build-name=test1", "--build-number=1", "--foox"}, []string{"foo", "-X", "123", "--bar", "--foox"}, &BuildConfiguration{"test1", "1", ""}}, 168 {[]string{"foo", "-X", "123", "--bar", "--foox"}, []string{"foo", "-X", "123", "--bar", "--foox"}, &BuildConfiguration{buildNameEnv, buildNumberEnv, ""}}, 169 } 170 171 os.Setenv(cliutils.BuildName, buildNameEnv) 172 os.Setenv(cliutils.BuildNumber, buildNumberEnv) 173 defer os.Unsetenv(cliutils.BuildName) 174 defer os.Unsetenv(cliutils.BuildNumber) 175 for _, test := range tests { 176 actualArgs, actualBuildConfig, err := ExtractBuildDetailsFromArgs(test.command) 177 if err != nil { 178 t.Error(err) 179 } 180 if !reflect.DeepEqual(actualArgs, test.expectedArgs) { 181 t.Errorf("Expected value: %v, got: %v.", test.expectedArgs, actualArgs) 182 } 183 if !reflect.DeepEqual(actualBuildConfig, test.expectedBuildConfig) { 184 t.Errorf("Expected value: %v, got: %v.", test.expectedBuildConfig, actualBuildConfig) 185 } 186 } 187 } 188 189 func TestFindFlagFirstMatch(t *testing.T) { 190 tests := []struct { 191 command []string 192 flags []string 193 expectedFlagIndex int 194 expectedValueIndex int 195 expectedValue string 196 }{ 197 {[]string{"-test", "--build-name", "test1", "--foo", "--build-number", "1", "--module", "module1"}, []string{"--build", "--build-name"}, 1, 2, "test1"}, 198 {[]string{"--module=module2", "--build-name", "test2", "--foo", "bar", "--build-number=2"}, []string{"--build-name", "--module"}, 1, 2, "test2"}, 199 {[]string{"foo", "-X", "123", "--bar", "--build-number=3", "--foox=barx"}, []string{"-Y", "--foo", "--foox"}, 5, 5, "barx"}, 200 } 201 202 for _, test := range tests { 203 actualFlagIndex, actualValueIndex, actualValue, err := FindFlagFirstMatch(test.flags, test.command) 204 if err != nil { 205 t.Error(err) 206 } 207 // Validate results. 208 if actualValue != test.expectedValue { 209 t.Errorf("Expected flag value of: %s, got: %s.", test.expectedValue, actualValue) 210 } 211 if actualValueIndex != test.expectedValueIndex { 212 t.Errorf("Expected flag value index of: %d, got: %d.", test.expectedValueIndex, actualValueIndex) 213 } 214 if actualFlagIndex != test.expectedFlagIndex { 215 t.Errorf("Expected flag index of: %d, got: %d.", test.expectedFlagIndex, actualFlagIndex) 216 } 217 } 218 } 219 220 func getFlagTestCases() []testCase { 221 return []testCase{ 222 {"test1", []string{"-X", "GET", "/api/build/test1", "--server-id", "test1", "--foo", "bar"}, "--server-id", 3, "test1", 4, false}, 223 {"test2", []string{"-X", "GET", "/api/build/test2", "--server-idea", "foo", "--server-id=test2"}, "--server-id", 5, "test2", 5, false}, 224 {"test3", []string{"-XGET", "/api/build/test3", "--server-id="}, "--server-id", 2, "", -1, true}, 225 {"test4", []string{"-XGET", "/api/build/test4", "--build-name", "--foo", "bar"}, "--build-name", 2, "", -1, true}, 226 {"test5", []string{"-X", "GET", "api/build/test5", "--build-number", "4", "--foo", "bar"}, "--build-number", 3, "4", 4, false}, 227 } 228 } 229 230 type testCase struct { 231 name string 232 arguments []string 233 flagName string 234 flagIndex int 235 flagValue string 236 flagValueIndex int 237 expectErr bool 238 }