forked from jfrog/jfrog-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_test.go
131 lines (106 loc) · 5.12 KB
/
cli_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package lifecycle
import (
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli/utils/cliutils"
"github.com/jfrog/jfrog-cli/utils/tests"
clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/stretchr/testify/assert"
"path/filepath"
"testing"
)
func TestValidateCreateReleaseBundleContext(t *testing.T) {
testRuns := []struct {
name string
args []string
flags []string
expectError bool
}{
{"withoutArgs", []string{}, []string{}, true},
{"oneArg", []string{"one"}, []string{}, true},
{"twoArgs", []string{"one", "two"}, []string{}, true},
{"extraArgs", []string{"one", "two", "three", "four"}, []string{}, true},
{"bothSources", []string{"one", "two", "three"}, []string{cliutils.Builds + "=/path/to/file", cliutils.ReleaseBundles + "=/path/to/file"}, true},
{"noSources", []string{"one", "two", "three"}, []string{}, true},
{"builds without signing key", []string{"name", "version"}, []string{cliutils.Builds + "=/path/to/file"}, false},
{"builds correct", []string{"name", "version"}, []string{
cliutils.Builds + "=/path/to/file", cliutils.SigningKey + "=key"}, false},
{"releaseBundles without signing key", []string{"name", "version"}, []string{cliutils.ReleaseBundles + "=/path/to/file"}, false},
{"releaseBundles correct", []string{"name", "version"}, []string{
cliutils.ReleaseBundles + "=/path/to/file", cliutils.SigningKey + "=key"}, false},
{"spec without signing key", []string{"name", "version", "env"}, []string{"spec=/path/to/file"}, true},
{"spec correct", []string{"name", "version"}, []string{
"spec=/path/to/file", cliutils.SigningKey + "=key"}, false},
}
for _, test := range testRuns {
t.Run(test.name, func(t *testing.T) {
context, buffer := tests.CreateContext(t, test.flags, test.args)
err := validateCreateReleaseBundleContext(context)
if test.expectError {
assert.Error(t, err, buffer)
} else {
assert.NoError(t, err, buffer)
}
})
}
}
// Validates that the project option does not override the project field in the spec file.
func TestCreateReleaseBundleSpecWithProject(t *testing.T) {
projectKey := "myproj"
specFile := filepath.Join("testdata", "specfile.json")
context, _ := tests.CreateContext(t, []string{"spec=" + specFile, "project=" + projectKey}, []string{})
creationSpec, err := getReleaseBundleCreationSpec(context)
assert.NoError(t, err)
assert.Equal(t, creationSpec.Get(0).Pattern, "path/to/file")
creationSpec.Get(0).Project = ""
assert.Equal(t, projectKey, cliutils.GetProject(context))
}
func TestGetReleaseBundleCreationSpec(t *testing.T) {
t.Run("Spec Flag Set", func(t *testing.T) {
specFile := filepath.Join("testdata", "specfile.json")
ctx, _ := tests.CreateContext(t, []string{"spec=" + specFile}, []string{})
spec, err := getReleaseBundleCreationSpec(ctx)
assert.NoError(t, err)
assert.NotNil(t, spec)
})
t.Run("Build Name and Number Set via Flags", func(t *testing.T) {
ctx, _ := tests.CreateContext(t, []string{"build-name=Common-builds", "build-number=1.0.0"}, []string{})
spec, err := getReleaseBundleCreationSpec(ctx)
assert.NoError(t, err)
assert.NotNil(t, spec)
assert.Equal(t, "Common-builds/1.0.0", spec.Files[0].Build)
})
t.Run("Build Name and Number Set via Env Variables", func(t *testing.T) {
setEnvBuildNameCallBack := clientTestUtils.SetEnvWithCallbackAndAssert(t, coreutils.BuildName, "Common-builds")
defer setEnvBuildNameCallBack()
setEnvBuildNumberCallBack := clientTestUtils.SetEnvWithCallbackAndAssert(t, coreutils.BuildNumber, "2.0.0")
defer setEnvBuildNumberCallBack()
ctx, _ := tests.CreateContext(t, []string{}, []string{})
spec, err := getReleaseBundleCreationSpec(ctx)
assert.NoError(t, err)
assert.NotNil(t, spec)
assert.Equal(t, "Common-builds/2.0.0", spec.Files[0].Build)
})
t.Run("Missing Build Name and Number", func(t *testing.T) {
ctx, _ := tests.CreateContext(t, []string{}, []string{})
spec, err := getReleaseBundleCreationSpec(ctx)
assert.Error(t, err)
assert.Nil(t, spec)
assert.EqualError(t, err, "either the --spec flag must be provided, or both --build-name and --build-number flags (or their corresponding environment variables JFROG_CLI_BUILD_NAME and JFROG_CLI_BUILD_NUMBER) must be set")
})
t.Run("Only One Build Variable Set", func(t *testing.T) {
ctx, _ := tests.CreateContext(t, []string{"build-name=Common-builds"}, []string{})
spec, err := getReleaseBundleCreationSpec(ctx)
assert.Error(t, err)
assert.Nil(t, spec)
assert.EqualError(t, err, "either the --spec flag must be provided, or both --build-name and --build-number flags (or their corresponding environment variables JFROG_CLI_BUILD_NAME and JFROG_CLI_BUILD_NUMBER) must be set")
})
t.Run("One Env Variable One Flag", func(t *testing.T) {
ctx, _ := tests.CreateContext(t, []string{"build-name=Common-builds"}, []string{})
setEnvBuildNumberCallBack := clientTestUtils.SetEnvWithCallbackAndAssert(t, coreutils.BuildNumber, "2.0.0")
defer setEnvBuildNumberCallBack()
spec, err := getReleaseBundleCreationSpec(ctx)
assert.NoError(t, err)
assert.NotNil(t, spec)
assert.Equal(t, "Common-builds/2.0.0", spec.Files[0].Build)
})
}