GitLabgitlab-org
cli
main
1//go:build !integration
3package cmdutils
5import (
6 "testing"
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10)
12type testStateValue string
14const (
15 testStateDisabled testStateValue = "disabled"
16 testStateEnabled testStateValue = "enabled"
17 testStateDryRun testStateValue = "dry_run"
18)
20func TestNewEnumValue(t *testing.T) {
21 tests := []struct {
22 name string
23 allowed []string
24 defaultVal string
25 expected string
26 }{
27 {
28 name: "single allowed value",
29 allowed: []string{"option1"},
30 defaultVal: "option1",
31 expected: "option1",
32 },
33 {
34 name: "multiple allowed values",
35 allowed: []string{"option1", "option2", "option3"},
36 defaultVal: "option2",
37 expected: "option2",
38 },
39 {
40 name: "empty allowed values",
41 allowed: []string{},
42 defaultVal: "defaultValue",
43 expected: "defaultValue",
44 },
45 {
46 name: "duplicate allowed values",
47 allowed: []string{"option1", "option1", "option2"},
48 defaultVal: "option1",
49 expected: "option1",
50 },
51 }
53 for _, tt := range tests {
54 t.Run(tt.name, func(t *testing.T) {
55 var value string
56 enumValue := NewEnumValue(tt.allowed, tt.defaultVal, &value)
58 require.NotNil(t, enumValue, "NewEnumValue should not return nil")
59 assert.Equal(t, tt.expected, value, "value should be set to expected default")
60 assert.Equal(t, tt.expected, enumValue.String(), "String() should return expected value")
62 // Check that allowed values are properly stored
63 for _, allowed := range tt.allowed {
64 _, ok := enumValue.allowed[allowed]
65 assert.True(t, ok, "expected %q to be in allowed values", allowed)
66 }
67 })
68 }
69}
71func TestNewEnumValue_PanicWithNilPointer(t *testing.T) {
72 assert.PanicsWithValue(t, "the given enum flag value cannot be nil", func() {
73 NewEnumValue([]string{"option1"}, "default", nil)
74 })
75}
77func TestEnumValue_Set(t *testing.T) {
78 tests := []struct {
79 name string
80 allowed []string
81 setValue string
82 expectError bool
83 }{
84 {
85 name: "valid value",
86 allowed: []string{"option1", "option2", "option3"},
87 setValue: "option2",
88 expectError: false,
89 },
90 {
91 name: "invalid value",
92 allowed: []string{"option1", "option2", "option3"},
93 setValue: "invalid",
94 expectError: true,
95 },
96 {
97 name: "empty string value when allowed",
98 allowed: []string{"", "option1", "option2"},
99 setValue: "",
100 expectError: false,
101 },
102 {
103 name: "empty string value when not allowed",
104 allowed: []string{"option1", "option2"},
105 setValue: "",
106 expectError: true,
107 },
108 {
109 name: "case sensitive - different case",
110 allowed: []string{"Option1", "option2"},
111 setValue: "option1",
112 expectError: true,
113 },
114 {
115 name: "case sensitive - exact match",
116 allowed: []string{"Option1", "option2"},
117 setValue: "Option1",
118 expectError: false,
119 },
120 }
122 for _, tt := range tests {
123 t.Run(tt.name, func(t *testing.T) {
124 var value string
125 enumValue := NewEnumValue(tt.allowed, "default", &value)
127 err := enumValue.Set(tt.setValue)
129 if tt.expectError {
130 require.Error(t, err, "expected error but got none")
131 assert.NotEmpty(t, err.Error(), "expected non-empty error message")
132 } else {
133 require.NoError(t, err, "unexpected error")
134 assert.Equal(t, tt.setValue, value, "expected value to be set correctly")
135 assert.Equal(t, tt.setValue, enumValue.String(), "expected String() to return set value")
136 }
137 })
138 }
139}
141func TestEnumValue_Set_ErrorMessage(t *testing.T) {
142 var value string
143 allowed := []string{"option1", "option2", "option3"}
144 enumValue := NewEnumValue(allowed, "default", &value)
146 err := enumValue.Set("invalid")
147 require.Error(t, err, "expected error but got none")
149 errorMsg := err.Error()
150 expectedPrefix := "must be one of"
151 assert.GreaterOrEqual(t, len(errorMsg), len(expectedPrefix), "error message should be long enough")
152 assert.Equal(t, expectedPrefix, errorMsg[:len(expectedPrefix)], "error message should start with expected prefix")
154 // Check that all allowed values are mentioned in the error
155 for _, allowed := range allowed {
156 assert.Contains(t, errorMsg, allowed, "expected error message to contain %q", allowed)
157 }
158}
160func TestEnumValue_ValueReference(t *testing.T) {
161 var value string
162 allowed := []string{"option1", "option2", "option3"}
163 enumValue := NewEnumValue(allowed, "option1", &value)
165 // Test that the value reference is properly updated
166 assert.Equal(t, "option1", value, "expected initial value to be %q", "option1")
168 err := enumValue.Set("option2")
169 require.NoError(t, err, "unexpected error")
171 assert.Equal(t, "option2", value, "expected value reference to be updated to %q", "option2")
173 // Test that String() returns the same value as the reference
174 assert.Equal(t, value, enumValue.String(), "expected String() to return same as value reference")
175}
177func TestEnumValue_CustomStringType(t *testing.T) {
178 var value testStateValue
179 allowed := []testStateValue{testStateDisabled, testStateEnabled, testStateDryRun}
180 enumValue := NewEnumValue(allowed, testStateDisabled, &value)
182 assert.Equal(t, testStateDisabled, value, "expected initial value to be %q", testStateDisabled)
183 assert.Equal(t, "disabled", enumValue.String(), "expected String() to return string representation")
185 err := enumValue.Set("enabled")
186 require.NoError(t, err, "unexpected error")
187 assert.Equal(t, testStateEnabled, value, "expected value to be set to typed constant")
189 err = enumValue.Set("invalid")
190 require.Error(t, err, "expected error for invalid value")
191 assert.Contains(t, err.Error(), "must be one of")
192}
Diagnostics6 git calls · 33 ms
CallStatusSizeTime033 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=1 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:02001.2 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=25 git.response.bytes=1238 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=c10f4b8b760c3fec7ed8ca85e428d5bc523f8e17 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1238 git.pkt.payload.max=1211 git.sideband.band1.bytes=1211 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1211 git.pack.trailer=7373aa350f2a611db7d4116eb1450c92f18a27caOpen pack map
fetch2001.4 KiB · hit26 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1434 git.response.cache=HIT git.response.duration_ms=26 git.fetch.wants=0c0288a5f415161fa565885b7fb39ef0579d1213 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1434 git.pkt.payload.max=1407 git.sideband.band1.bytes=1407 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e1301ec84ea9fe
- Request
- 5a01cf40…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format