Skip to content

Commit 918e18c

Browse files
committed
fix unit tests
1 parent 0de6975 commit 918e18c

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

apptrust/commands/utils/utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ func ParseDelimitedSlice(input string) [][]string {
125125
// Returns an error if any entry does not have exactly two parts.
126126
func ParseNameVersionPairs(input string) ([][2]string, error) {
127127
var result [][2]string
128+
if input == "" {
129+
return result, nil
130+
}
128131
for _, parts := range ParseDelimitedSlice(input) {
129132
if len(parts) != 2 {
130133
return nil, fmt.Errorf("invalid format: %v", parts)

apptrust/commands/utils/utils_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func TestParseDelimitedSlice(t *testing.T) {
155155
input string
156156
expected [][]string
157157
}{
158-
{"empty string", "", [][]string{}},
158+
{"empty string", "", nil},
159159
{"single entry", "foo:bar", [][]string{{"foo", "bar"}}},
160160
{"multiple entries", "foo:bar;baz:qux", [][]string{{"foo", "bar"}, {"baz", "qux"}}},
161161
{"entries with extra parts", "a:1:2;b:3", [][]string{{"a", "1", "2"}, {"b", "3"}}},
@@ -170,3 +170,30 @@ func TestParseDelimitedSlice(t *testing.T) {
170170
})
171171
}
172172
}
173+
174+
func TestParseNameVersionPairs(t *testing.T) {
175+
tests := []struct {
176+
name string
177+
input string
178+
expected [][2]string
179+
expectErr bool
180+
}{
181+
{"empty string", "", nil, false},
182+
{"single pair", "foo:1.0.0", [][2]string{{"foo", "1.0.0"}}, false},
183+
{"multiple pairs", "foo:1.0.0;bar:2.0.0", [][2]string{{"foo", "1.0.0"}, {"bar", "2.0.0"}}, false},
184+
{"spaces", " foo:1.0.0 ; bar:2.0.0 ", [][2]string{{" foo", "1.0.0 "}, {" bar", "2.0.0 "}}, false},
185+
{"invalid format", "foo", nil, true},
186+
{"too many parts", "foo:1.0.0:extra", nil, true},
187+
}
188+
for _, tt := range tests {
189+
t.Run(tt.name, func(t *testing.T) {
190+
result, err := ParseNameVersionPairs(tt.input)
191+
if tt.expectErr {
192+
assert.Error(t, err, "expected error for input %q", tt.input)
193+
return
194+
}
195+
assert.NoError(t, err, "unexpected error for input %q: %v", tt.input, err)
196+
assert.Equal(t, tt.expected, result, "ParseNameVersionPairs(%q) = %v, want %v", tt.input, result, tt.expected)
197+
})
198+
}
199+
}

apptrust/commands/version/create_app_version_cmd_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
mockversions "github.com/jfrog/jfrog-cli-application/apptrust/service/versions/mocks"
88
"go.uber.org/mock/gomock"
99

10+
"github.com/jfrog/jfrog-cli-application/apptrust/commands"
1011
"github.com/jfrog/jfrog-cli-application/apptrust/model"
1112
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
1213
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
@@ -114,11 +115,11 @@ func TestCreateAppVersionCommand_FlagsSuite(t *testing.T) {
114115
name: "all flags",
115116
ctxSetup: func(ctx *components.Context) {
116117
ctx.Arguments = []string{"app-key", "1.0.0"}
117-
ctx.AddStringFlag("tag", "release-tag")
118-
ctx.AddStringFlag("packages", "pkg1:1.2.3,pkg2:2.3.4")
119-
ctx.AddStringFlag("build", "build1:1.0.0:2024-01-01;build2:2.0.0:2024-02-01")
120-
ctx.AddStringFlag("release-bundle", "rb1:1.0.0;rb2:2.0.0")
121-
ctx.AddStringFlag("source-version", "source-app:3.2.1")
118+
ctx.AddStringFlag(commands.TagFlag, "release-tag")
119+
ctx.AddStringFlag(commands.PackagesFlag, "pkg1:1.2.3,pkg2:2.3.4")
120+
ctx.AddStringFlag(commands.BuildsFlag, "build1:1.0.0:2024-01-01;build2:2.0.0:2024-02-01")
121+
ctx.AddStringFlag(commands.ReleaseBundlesFlag, "rb1:1.0.0;rb2:2.0.0")
122+
ctx.AddStringFlag(commands.SourceVersionFlag, "source-app:3.2.1")
122123
},
123124
expectsPayload: &model.CreateAppVersionRequest{
124125
ApplicationKey: "app-key",
@@ -129,8 +130,14 @@ func TestCreateAppVersionCommand_FlagsSuite(t *testing.T) {
129130
{Name: "pkg1", Version: "1.2.3"},
130131
{Name: "pkg2", Version: "2.3.4"},
131132
},
132-
Builds: nil,
133-
ReleaseBundles: nil,
133+
Builds: []model.CreateVersionBuild{
134+
{Name: "build1", Number: "1.0.0", Started: "2024-01-01"},
135+
{Name: "build2", Number: "2.0.0", Started: "2024-02-01"},
136+
},
137+
ReleaseBundles: []model.CreateVersionReleaseBundle{
138+
{Name: "rb1", Version: "1.0.0"},
139+
{Name: "rb2", Version: "2.0.0"},
140+
},
134141
Versions: []model.CreateVersionReference{
135142
{ApplicationKey: "source-app", Version: "3.2.1"},
136143
},

0 commit comments

Comments
 (0)