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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
package get
import (
"bytes"
"net/http"
"testing"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
gitlab "gitlab.com/gitlab-org/api/client-go"
"gitlab.com/gitlab-org/cli/api"
"gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/internal/glrepo"
"gitlab.com/gitlab-org/cli/pkg/httpmock"
"gitlab.com/gitlab-org/cli/pkg/iostreams"
)
func Test_NewCmdGet(t *testing.T) {
tests := []struct {
name string
cli string
wants GetOps
wantsErr bool
}{
{
name: "good key",
cli: "good_key",
wantsErr: false,
wants: GetOps{
Key: "good_key",
},
},
{
name: "bad key",
cli: "bad-key",
wantsErr: true,
},
{
name: "no key",
cli: "",
wantsErr: true,
},
{
name: "good key",
cli: "-g group good_key",
wants: GetOps{
Key: "good_key",
Group: "group",
},
wantsErr: false,
},
{
name: "good key, with scope",
cli: "-s foo -g group good_key",
wants: GetOps{
Key: "good_key",
Group: "group",
Scope: "foo",
},
wantsErr: false,
},
{
name: "good key, with default scope",
cli: "-g group good_key",
wants: GetOps{
Key: "good_key",
Group: "group",
Scope: "*",
},
wantsErr: false,
},
{
name: "bad key",
cli: "-g group bad-key",
wants: GetOps{
Group: "group",
},
wantsErr: true,
},
{
name: "good key but no group",
cli: "good_key --group",
wantsErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
io, _, _, _ := iostreams.Test()
f := &cmdutils.Factory{
IO: io,
}
argv, err := shlex.Split(test.cli)
assert.NoError(t, err)
var gotOpts *GetOps
cmd := NewCmdGet(f, func(opts *GetOps) error {
gotOpts = opts
return nil
})
cmd.SetArgs(argv)
cmd.SetIn(&bytes.Buffer{})
cmd.SetOut(&bytes.Buffer{})
cmd.SetErr(&bytes.Buffer{})
_, err = cmd.ExecuteC()
if test.wantsErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, test.wants.Key, gotOpts.Key)
assert.Equal(t, test.wants.Group, gotOpts.Group)
})
}
}
func Test_getRun_project(t *testing.T) {
reg := &httpmock.Mocker{}
defer reg.Verify(t)
varContent := `
TEST variable\n
content
`
body := struct {
Key string `json:"key"`
VariableType string `json:"variable_type"`
Value string `json:"value"`
Protected bool `json:"protected"`
Masked bool `json:"masked"`
EnvironmentScope string `json:"environment_scope"`
}{
Key: "TEST_VAR",
VariableType: "env_var",
Value: varContent,
Protected: false,
Masked: false,
EnvironmentScope: "*",
}
reg.RegisterResponder(http.MethodGet, "/projects/owner/repo/variables/TEST_VAR",
httpmock.NewJSONResponse(http.StatusOK, body),
)
io, _, stdout, _ := iostreams.Test()
opts := &GetOps{
HTTPClient: func() (*gitlab.Client, error) {
a, _ := api.TestClient(&http.Client{Transport: reg}, "", "gitlab.com", false)
return a.Lab(), nil
},
BaseRepo: func() (glrepo.Interface, error) {
return glrepo.FromFullName("owner/repo")
},
IO: io,
Key: "TEST_VAR",
}
_, _ = opts.HTTPClient()
err := getRun(opts)
assert.NoError(t, err)
assert.Equal(t, varContent, stdout.String())
}
|