github.com/jfrog/jfrog-cli-core/v2@v2.51.0/utils/coreutils/cmdutils_test.go (about) 1 package coreutils 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestFindAndRemoveFlagFromCommand(t *testing.T) { 9 argsTable := [][]string{ 10 {"-X", "GET", "/api/build/test1", "--server-id", "test1", "--foo", "bar"}, 11 {"-X", "GET", "/api/build/test2", "--server-idea", "foo", "--server-id=test2"}, 12 {"-X", "GET", "api/build/test3", "--server-id", "test3", "--foo", "bar"}, 13 {"-X", "GET", "api/build/test3", "--build-name", "name", "--foo", "bar"}, 14 {"-X", "GET", "api/build/test3", "--build-number", "3", "--foo", "bar"}, 15 } 16 17 expected := []struct { 18 key string 19 value string 20 command []string 21 }{ 22 {"--server-id", "test1", []string{"-X", "GET", "/api/build/test1", "--foo", "bar"}}, 23 {"--server-id", "test2", []string{"-X", "GET", "/api/build/test2", "--server-idea", "foo"}}, 24 {"--server-id", "test3", []string{"-X", "GET", "api/build/test3", "--foo", "bar"}}, 25 {"--build-name", "name", []string{"-X", "GET", "api/build/test3", "--foo", "bar"}}, 26 {"--build-number", "3", []string{"-X", "GET", "api/build/test3", "--foo", "bar"}}, 27 } 28 29 for index := range argsTable { 30 curTestArgs := argsTable[index] 31 flagIndex, valueIndex, keyValue, err := FindFlag(expected[index].key, curTestArgs) 32 if err != nil { 33 t.Error(err) 34 } 35 if keyValue != expected[index].value { 36 t.Errorf("Expected %s value: %s, got: %s.", expected[index].key, expected[index].value, keyValue) 37 } 38 RemoveFlagFromCommand(&curTestArgs, flagIndex, valueIndex) 39 if !reflect.DeepEqual(curTestArgs, expected[index].command) { 40 t.Errorf("Expected command arguments: %v, got: %v.", expected[index].command, curTestArgs) 41 } 42 } 43 } 44 45 func TestFindFlag(t *testing.T) { 46 tests := getFlagTestCases() 47 for _, test := range tests { 48 t.Run(test.name, func(t *testing.T) { 49 actualIndex, actualValueIndex, actualValue, err := FindFlag(test.flagName, test.arguments) 50 51 // Check errors. 52 if err != nil && !test.expectErr { 53 t.Error(err) 54 } 55 if err == nil && test.expectErr { 56 t.Errorf("Expecting: error, Got: nil") 57 } 58 59 if err == nil { 60 // Validate results. 61 if actualValue != test.flagValue { 62 t.Errorf("Expected flag value of: %s, got: %s.", test.flagValue, actualValue) 63 } 64 if actualValueIndex != test.flagValueIndex { 65 t.Errorf("Expected flag value index of: %d, got: %d.", test.flagValueIndex, actualValueIndex) 66 } 67 if actualIndex != test.flagIndex { 68 t.Errorf("Expected flag index of: %d, got: %d.", test.flagIndex, actualIndex) 69 } 70 } 71 }) 72 } 73 } 74 75 func TestGetFlagValueAndValueIndex(t *testing.T) { 76 tests := getFlagTestCases() 77 for _, test := range tests { 78 t.Run(test.name, func(t *testing.T) { 79 actualValue, actualIndex, err := getFlagValueAndValueIndex(test.flagName, test.arguments, test.flagIndex) 80 81 // Validate errors. 82 if err != nil && !test.expectErr { 83 t.Error(err) 84 } 85 if err == nil && test.expectErr { 86 t.Errorf("Expecting: error, Got: nil") 87 } 88 89 // Validate results. 90 if actualValue != test.flagValue { 91 t.Errorf("Expected value of: %s, got: %s.", test.flagValue, actualValue) 92 } 93 if actualIndex != test.flagValueIndex { 94 t.Errorf("Expected value of: %d, got: %d.", test.flagValueIndex, actualIndex) 95 } 96 }) 97 } 98 } 99 100 func getFlagTestCases() []testCase { 101 return []testCase{ 102 {"test1", []string{"-X", "GET", "/api/build/test1", "--server-id", "test1", "--foo", "bar"}, "--server-id", 3, "test1", 4, false}, 103 {"test2", []string{"-X", "GET", "/api/build/test2", "--server-idea", "foo", "--server-id=test2"}, "--server-id", 5, "test2", 5, false}, 104 {"test3", []string{"-XGET", "/api/build/test3", "--server-id="}, "--server-id", 2, "", -1, true}, 105 {"test4", []string{"-XGET", "/api/build/test4", "--build-name", "--foo", "bar"}, "--build-name", 2, "", -1, true}, 106 {"test5", []string{"-X", "GET", "api/build/test5", "--build-number", "4", "--foo", "bar"}, "--build-number", 3, "4", 4, false}, 107 } 108 } 109 110 type testCase struct { 111 name string 112 arguments []string 113 flagName string 114 flagIndex int 115 flagValue string 116 flagValueIndex int 117 expectErr bool 118 } 119 120 func TestFindBooleanFlag(t *testing.T) { 121 tests := []struct { 122 flagName string 123 command []string 124 expectedIndex int 125 expectedValue bool 126 shouldFail bool 127 }{ 128 {"--foo", []string{"-X", "--GET", "--foo/api/build/test1", "--foo", "bar"}, 3, true, false}, 129 {"--server-id", []string{"-X", "GET", "/api/build/test2", "--server-idea", "foo"}, -1, false, false}, 130 {"--bar", []string{"-X", "GET", "api/build/test3", "--foo", "bar"}, -1, false, false}, 131 {"-X", []string{"-X=true", "GET", "api/build/test3", "--foo", "bar"}, 0, true, false}, 132 {"--json", []string{"-X=true", "GET", "api/build/test3", "--foo", "--json=false"}, 4, false, false}, 133 {"--dry-run", []string{"-X=falsee", "GET", "api/build/test3", "--dry-run=falsee", "--json"}, 3, false, true}, 134 } 135 136 for testIndex, test := range tests { 137 actualIndex, actualValue, err := FindBooleanFlag(test.flagName, test.command) 138 if test.shouldFail && err == nil { 139 t.Errorf("Test #%d: Should fail to parse the boolean value, but ended with nil error.", testIndex) 140 } 141 if actualIndex != test.expectedIndex { 142 t.Errorf("Test #%d: Expected index value: %d, got: %d.", testIndex, test.expectedIndex, actualIndex) 143 } 144 if actualValue != test.expectedValue { 145 t.Errorf("Test #%d: Expected value: %t, got: %t.", testIndex, test.expectedValue, actualValue) 146 } 147 } 148 } 149 150 func TestFindFlagFirstMatch(t *testing.T) { 151 tests := []struct { 152 command []string 153 flags []string 154 expectedFlagIndex int 155 expectedValueIndex int 156 expectedValue string 157 }{ 158 {[]string{"-test", "--build-name", "test1", "--foo", "--build-number", "1", "--module", "module1"}, []string{"--build", "--build-name"}, 1, 2, "test1"}, 159 {[]string{"--module=module2", "--build-name", "test2", "--foo", "bar", "--build-number=2"}, []string{"--build-name", "--module"}, 1, 2, "test2"}, 160 {[]string{"foo", "-X", "123", "--bar", "--build-number=3", "--foox=barx"}, []string{"-Y", "--foo", "--foox"}, 5, 5, "barx"}, 161 } 162 163 for _, test := range tests { 164 actualFlagIndex, actualValueIndex, actualValue, err := FindFlagFirstMatch(test.flags, test.command) 165 if err != nil { 166 t.Error(err) 167 } 168 // Validate results. 169 if actualValue != test.expectedValue { 170 t.Errorf("Expected flag value of: %s, got: %s.", test.expectedValue, actualValue) 171 } 172 if actualValueIndex != test.expectedValueIndex { 173 t.Errorf("Expected flag value index of: %d, got: %d.", test.expectedValueIndex, actualValueIndex) 174 } 175 if actualFlagIndex != test.expectedFlagIndex { 176 t.Errorf("Expected flag index of: %d, got: %d.", test.expectedFlagIndex, actualFlagIndex) 177 } 178 } 179 }