Skip to content

Commit 8ca2f5e

Browse files
committed
test spec file
1 parent 1226d8f commit 8ca2f5e

File tree

3 files changed

+140
-32
lines changed

3 files changed

+140
-32
lines changed

apptrust/commands/version/create_app_version_cmd.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (cv *createAppVersionCommand) buildRequestPayload(ctx *components.Context)
8989
}
9090

9191
return &model.CreateAppVersionRequest{
92-
ApplicationKey: ctx.GetStringFlagValue(commands.ApplicationKeyFlag),
92+
ApplicationKey: ctx.Arguments[0],
9393
Version: ctx.Arguments[1],
9494
Sources: appendSourceIfExists(sources),
9595
Tag: ctx.GetStringFlagValue(commands.TagFlag),
@@ -241,6 +241,9 @@ func (cv *createAppVersionCommand) parseSourceVersions(sourceVersionsStr string)
241241

242242
// Updated validation logic to support multiple source types
243243
func validateCreateAppVersionContext(ctx *components.Context) error {
244+
if err := validateNoSpecAndFlagsTogether(ctx); err != nil {
245+
return err
246+
}
244247
if len(ctx.Arguments) != 2 {
245248
return pluginsCommon.WrongNumberOfArgumentsHandler(ctx)
246249
}

apptrust/commands/version/create_app_version_cmd_test.go

Lines changed: 108 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"go.uber.org/mock/gomock"
1010

1111
"github.com/jfrog/jfrog-cli-application/apptrust/model"
12+
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
1213
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
1314
"github.com/stretchr/testify/assert"
1415
)
@@ -22,14 +23,12 @@ func TestCreateAppVersionCommand_Run(t *testing.T) {
2223
ApplicationKey: "app-key",
2324
Version: "1.0.0",
2425
Sources: &model.CreateVersionSources{
25-
Packages: []model.CreateVersionPackage{
26-
{
27-
Type: "type",
28-
Name: "name",
29-
Version: "1.0.0",
30-
Repository: "repo",
31-
},
32-
},
26+
Packages: []model.CreateVersionPackage{{
27+
Type: "type",
28+
Name: "name",
29+
Version: "1.0.0",
30+
Repository: "repo",
31+
}},
3332
},
3433
}
3534

@@ -47,7 +46,7 @@ func TestCreateAppVersionCommand_Run(t *testing.T) {
4746
assert.NoError(t, err)
4847
}
4948

50-
func TestCreateAppVersionCommand_Run_WithSigningKey(t *testing.T) {
49+
func TestCreateAppVersionCommand_Run_ContextError(t *testing.T) {
5150
ctrl := gomock.NewController(t)
5251
defer ctrl.Finish()
5352

@@ -69,7 +68,7 @@ func TestCreateAppVersionCommand_Run_WithSigningKey(t *testing.T) {
6968

7069
mockVersionService := mockversions.NewMockVersionService(ctrl)
7170
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), requestPayload).
72-
Return(nil).Times(1)
71+
Return(errors.New("context error")).Times(1)
7372

7473
cmd := &createAppVersionCommand{
7574
versionService: mockVersionService,
@@ -78,10 +77,91 @@ func TestCreateAppVersionCommand_Run_WithSigningKey(t *testing.T) {
7877
}
7978

8079
err := cmd.Run()
80+
assert.Error(t, err)
81+
assert.Equal(t, "context error", err.Error())
82+
}
83+
84+
func TestCreateAppVersionCommand_FromSpecFile(t *testing.T) {
85+
ctrl := gomock.NewController(t)
86+
defer ctrl.Finish()
87+
88+
testSpecPath := "./testfiles/test-spec.json"
89+
ctx := &components.Context{
90+
Arguments: []string{"app-key", "1.0.0"},
91+
}
92+
ctx.AddStringFlag("spec", testSpecPath)
93+
94+
serverDetails := &config.ServerDetails{Url: "https://example.com"}
95+
96+
var actualPayload *model.CreateAppVersionRequest
97+
mockVersionService := mockversions.NewMockVersionService(ctrl)
98+
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), gomock.Any()).
99+
DoAndReturn(func(_ interface{}, req *model.CreateAppVersionRequest) error {
100+
actualPayload = req
101+
return nil
102+
}).Times(1)
103+
104+
cmd := &createAppVersionCommand{
105+
versionService: mockVersionService,
106+
serverDetails: serverDetails,
107+
}
108+
109+
err := cmd.prepareAndRunCommand(ctx)
81110
assert.NoError(t, err)
111+
112+
expected := &model.CreateAppVersionRequest{
113+
ApplicationKey: "app-key",
114+
Version: "1.0.0",
115+
Sources: &model.CreateVersionSources{
116+
Packages: []model.CreateVersionPackage{{
117+
Type: "npm",
118+
Name: "pkg1",
119+
Version: "1.0.0",
120+
Repository: "repo1",
121+
}},
122+
Builds: []model.CreateVersionBuild{{
123+
Name: "build1",
124+
Number: "5",
125+
}},
126+
ReleaseBundles: []model.CreateVersionReleaseBundle{{
127+
Name: "rb1",
128+
Version: "1.0.0",
129+
}},
130+
Versions: []model.CreateVersionReference{{
131+
ApplicationKey: "app1",
132+
Version: "1.0.0",
133+
}},
134+
},
135+
}
136+
assert.Equal(t, expected, actualPayload)
82137
}
83138

84-
func TestCreateAppVersionCommand_Run_Async(t *testing.T) {
139+
func TestCreateAppVersionCommand_SpecAndFlags_Error(t *testing.T) {
140+
ctrl := gomock.NewController(t)
141+
defer ctrl.Finish()
142+
143+
// Set up context with both --spec and another source flag
144+
testSpecPath := "./testfiles/test-spec.json"
145+
ctx := &components.Context{
146+
Arguments: []string{"app-key", "1.0.0"},
147+
}
148+
ctx.AddStringFlag("spec", testSpecPath)
149+
ctx.AddStringFlag("package-name", "name") // Any other source flag
150+
151+
serverDetails := &config.ServerDetails{Url: "https://example.com"}
152+
mockVersionService := mockversions.NewMockVersionService(ctrl)
153+
154+
cmd := &createAppVersionCommand{
155+
versionService: mockVersionService,
156+
serverDetails: serverDetails,
157+
}
158+
159+
err := cmd.prepareAndRunCommand(ctx)
160+
assert.Error(t, err)
161+
assert.Contains(t, err.Error(), "--spec provided")
162+
}
163+
164+
func TestCreateAppVersionCommand_AllFlags(t *testing.T) {
85165
ctrl := gomock.NewController(t)
86166
defer ctrl.Finish()
87167

@@ -90,14 +170,12 @@ func TestCreateAppVersionCommand_Run_Async(t *testing.T) {
90170
ApplicationKey: "app-key",
91171
Version: "1.0.0",
92172
Sources: &model.CreateVersionSources{
93-
Packages: []model.CreateVersionPackage{
94-
{
95-
Type: "type",
96-
Name: "name",
97-
Version: "1.0.0",
98-
Repository: "repo",
99-
},
100-
},
173+
Packages: []model.CreateVersionPackage{{
174+
Type: "type",
175+
Name: "name",
176+
Version: "1.0.0",
177+
Repository: "repo",
178+
}},
101179
},
102180
}
103181

@@ -109,13 +187,14 @@ func TestCreateAppVersionCommand_Run_Async(t *testing.T) {
109187
versionService: mockVersionService,
110188
serverDetails: serverDetails,
111189
requestPayload: requestPayload,
190+
// ...set other flags...
112191
}
113192

114193
err := cmd.Run()
115194
assert.NoError(t, err)
116195
}
117196

118-
func TestCreateAppVersionCommand_Run_ContextError(t *testing.T) {
197+
func TestCreateAppVersionCommand_MinimumFlags(t *testing.T) {
119198
ctrl := gomock.NewController(t)
120199
defer ctrl.Finish()
121200

@@ -124,30 +203,28 @@ func TestCreateAppVersionCommand_Run_ContextError(t *testing.T) {
124203
ApplicationKey: "app-key",
125204
Version: "1.0.0",
126205
Sources: &model.CreateVersionSources{
127-
Packages: []model.CreateVersionPackage{
128-
{
129-
Type: "type",
130-
Name: "name",
131-
Version: "1.0.0",
132-
Repository: "repo",
133-
},
134-
},
206+
Packages: []model.CreateVersionPackage{{
207+
Type: "type",
208+
Name: "name",
209+
Version: "1.0.0",
210+
Repository: "repo",
211+
}},
135212
},
136213
}
137214

138215
mockVersionService := mockversions.NewMockVersionService(ctrl)
139216
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), requestPayload).
140-
Return(errors.New("context error")).Times(1)
217+
Return(nil).Times(1)
141218

142219
cmd := &createAppVersionCommand{
143220
versionService: mockVersionService,
144221
serverDetails: serverDetails,
145222
requestPayload: requestPayload,
223+
// ...set minimum required flags...
146224
}
147225

148226
err := cmd.Run()
149-
assert.Error(t, err)
150-
assert.Equal(t, "context error", err.Error())
227+
assert.NoError(t, err)
151228
}
152229

153230
func TestParseBuilds(t *testing.T) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"packages": [
3+
{
4+
"name": "pkg1",
5+
"version": "1.0.0",
6+
"repository": "repo1",
7+
"type": "npm"
8+
}
9+
],
10+
"builds": [
11+
{
12+
"name": "build1",
13+
"number": "5"
14+
}
15+
],
16+
"release_bundles": [
17+
{
18+
"name": "rb1",
19+
"version": "1.0.0"
20+
}
21+
],
22+
"versions": [
23+
{
24+
"application_key": "app1",
25+
"version": "1.0.0"
26+
}
27+
]
28+
}

0 commit comments

Comments
 (0)