Repository navigation
Expand file tree
/
Copy pathgithub_test.go
More file actions
424 lines (382 loc) · 12.9 KB
/
Copy pathgithub_test.go
File metadata and controls
424 lines (382 loc) · 12.9 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package githubplugin
import (
"bytes"
"context"
"fmt"
"strings"
"testing"
"github.com/ubgo/dotenv"
"github.com/ubgo/dotenv/cli/envkit"
"github.com/ubgo/dotenv/cli/outfmt"
"github.com/ubgo/dotenv/cli/providerkit"
)
// call records one Runner invocation for argv/stdin assertions.
type call struct {
name string
args string // space-joined for readable assertions
stdin string
}
// fakeRunner scripts gh responses by argv prefix and records every call —
// the no-network, no-gh test seam the spec mandates.
type fakeRunner struct {
calls []call
responses map[string]string // argv-prefix → stdout
failOn string // argv containing this substring errors
}
func (r *fakeRunner) Run(_ context.Context, stdin string, name string, args ...string) ([]byte, error) {
joined := strings.Join(args, " ")
r.calls = append(r.calls, call{name: name, args: joined, stdin: stdin})
if r.failOn != "" && strings.Contains(joined, r.failOn) {
return nil, fmt.Errorf("gh: boom")
}
for prefix, out := range r.responses {
if strings.HasPrefix(joined, prefix) {
return []byte(out), nil
}
}
return []byte(""), nil
}
// ghResponses is the standard happy-path script.
func ghResponses() map[string]string {
return map[string]string{
"repo view": "solverhood/sync_go\n",
"api user": "khanakia\n",
"secret list": `[{"name":"OLD_STALE"},{"name":"DEPLOY_PATH"}]`,
}
}
// runGithub drives the plugin's command tree directly with scripted deps.
func runGithub(t *testing.T, runner *fakeRunner, src providerkit.Source, args ...string) (string, error) {
t.Helper()
var out bytes.Buffer
deps := providerkit.Deps{
Printer: &outfmt.Printer{Out: &out, Err: &out},
File: ".env.test",
Source: func(providerkit.SelectOpts) (providerkit.Source, error) { return src, nil },
Runner: runner,
Interactive: func() bool { return false },
Ask: func(string) bool { return false },
}
c := New().Command(deps)
c.SetArgs(args)
err := c.Execute()
return out.String(), err
}
func TestPush_ArgvAndStdin(t *testing.T) {
t.Parallel()
runner := &fakeRunner{responses: ghResponses()}
src := &envkit.Selection{Pairs: []dotenv.Pair{{Key: "DEPLOY_PATH", Value: "/srv/app"}}}
out, err := runGithub(t, runner, src, "push", "--yes")
if err != nil {
t.Fatalf("push: %v\n%s", err, out)
}
var set *call
for i := range runner.calls {
if strings.HasPrefix(runner.calls[i].args, "secret set") {
set = &runner.calls[i]
}
}
if set == nil {
t.Fatalf("no secret set call: %+v", runner.calls)
}
// The value must travel by STDIN and must never appear in argv.
if set.args != "secret set DEPLOY_PATH" {
t.Errorf("argv = %q", set.args)
}
if set.stdin != "/srv/app" {
t.Errorf("stdin = %q, want the value", set.stdin)
}
if strings.Contains(set.args, "/srv/app") {
t.Error("value leaked into argv")
}
// Banner resolved from gh, auth source labeled.
for _, want := range []string{"target : solverhood/sync_go (from cwd git remote)", "account: khanakia", "DEPLOY_PATH: pushed"} {
if !strings.Contains(out, want) {
t.Errorf("output missing %q:\n%s", want, out)
}
}
}
func TestPush_ScopeFlagsPassThrough(t *testing.T) {
t.Parallel()
runner := &fakeRunner{responses: ghResponses()}
src := &envkit.Selection{Pairs: []dotenv.Pair{{Key: "K", Value: "v"}}}
if _, err := runGithub(t, runner, src, "push", "--yes", "--repo", "o/r", "--environment", "production"); err != nil {
t.Fatal(err)
}
found := false
for _, c := range runner.calls {
if c.args == "secret set K --repo o/r --env production" {
found = true
}
}
if !found {
t.Errorf("scope flags not passed through: %+v", runner.calls)
}
// --org excludes --repo.
if _, err := runGithub(t, runner, src, "push", "--yes", "--org", "acme", "--repo", "o/r"); err == nil {
t.Error("org+repo must be a usage error")
}
}
func TestPush_InvalidSecretNamesAbortBeforeAnyWrite(t *testing.T) {
t.Parallel()
runner := &fakeRunner{responses: ghResponses()}
// Legal env keys under the Compose charset, illegal as GH secret names.
src := &envkit.Selection{Pairs: []dotenv.Pair{
{Key: "GOOD_NAME", Value: "x"},
{Key: "spring.datasource.url", Value: "x"},
{Key: "MY-TOKEN", Value: "x"},
}}
out, err := runGithub(t, runner, src, "push", "--yes")
if err == nil {
t.Fatal("want failure")
}
for _, c := range runner.calls {
if strings.HasPrefix(c.args, "secret set") {
t.Errorf("write happened despite invalid names: %q", c.args)
}
}
if !strings.Contains(out, "spring.datasource.url") || !strings.Contains(out, "MY-TOKEN") {
t.Errorf("offending keys not listed:\n%s", out)
}
}
func TestPush_NonInteractiveRequiresYes(t *testing.T) {
t.Parallel()
runner := &fakeRunner{responses: ghResponses()}
src := &envkit.Selection{Pairs: []dotenv.Pair{{Key: "K", Value: "v"}}}
if _, err := runGithub(t, runner, src, "push"); err == nil {
t.Error("non-interactive push without --yes must refuse")
}
for _, c := range runner.calls {
if strings.HasPrefix(c.args, "secret set") {
t.Error("refused push still wrote")
}
}
}
func TestPrune_DeletesOnlyStale(t *testing.T) {
t.Parallel()
runner := &fakeRunner{responses: ghResponses()} // remote: OLD_STALE, DEPLOY_PATH
src := &envkit.Selection{Pairs: []dotenv.Pair{{Key: "DEPLOY_PATH", Value: "x"}}}
out, err := runGithub(t, runner, src, "prune", "--yes")
if err != nil {
t.Fatalf("prune: %v\n%s", err, out)
}
deletes := []string{}
for _, c := range runner.calls {
if strings.HasPrefix(c.args, "secret delete") {
deletes = append(deletes, c.args)
}
}
if len(deletes) != 1 || deletes[0] != "secret delete OLD_STALE" {
t.Errorf("deletes = %v, want only the stale name", deletes)
}
}
func TestList_ParsesGhJSON(t *testing.T) {
t.Parallel()
runner := &fakeRunner{responses: ghResponses()}
out, err := runGithub(t, runner, &envkit.Selection{}, "list")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(out, "DEPLOY_PATH") || !strings.Contains(out, "OLD_STALE") {
t.Errorf("list output = %q", out)
}
}
func TestGate_GhFailureIsActionableAndAborts(t *testing.T) {
t.Parallel()
runner := &fakeRunner{failOn: "repo view"} // gh missing/unauthenticated
src := &envkit.Selection{Pairs: []dotenv.Pair{{Key: "K", Value: "v"}}}
out, err := runGithub(t, runner, src, "push", "--yes")
if err == nil {
t.Fatal("want failure")
}
if !strings.Contains(out, "cli.github.com") {
t.Errorf("failure not actionable:\n%s", out)
}
for _, c := range runner.calls {
if strings.HasPrefix(c.args, "secret set") {
t.Error("wrote despite failed resolution")
}
}
}
func TestPush_TokenOverrideLabeledInBanner(t *testing.T) {
// t.Setenv forbids t.Parallel — the label depends on process env.
t.Setenv("GH_TOKEN", "ghp_test_override")
runner := &fakeRunner{responses: ghResponses()}
src := &envkit.Selection{Pairs: []dotenv.Pair{{Key: "K", Value: "v"}}}
out, err := runGithub(t, runner, src, "push", "--yes")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(out, "khanakia (via GH_TOKEN)") {
t.Errorf("auth source not labeled:\n%s", out)
}
}
func TestPush_JSONCarriesMetaAndResults(t *testing.T) {
t.Parallel()
runner := &fakeRunner{responses: ghResponses()}
src := &envkit.Selection{Pairs: []dotenv.Pair{{Key: "PAT", Value: "v"}}, Skipped: []providerkit.Skip{{Name: "WHO", Reason: envkit.SkipPlaceholder}}}
var out bytes.Buffer
deps := providerkit.Deps{
Printer: &outfmt.Printer{Out: &out, JSON: true},
Source: func(providerkit.SelectOpts) (providerkit.Source, error) { return src, nil },
Runner: runner,
Interactive: func() bool { return false },
Ask: func(string) bool { return false },
}
c := New().Command(deps)
c.SetArgs([]string{"push", "--yes"})
if err := c.Execute(); err != nil {
t.Fatal(err)
}
for _, want := range []string{
`"target":"solverhood/sync_go (from cwd git remote)"`,
`"account":"khanakia (stored gh auth)"`,
`{"name":"WHO","action":"skipped-placeholder"}`,
`{"name":"PAT","action":"pushed"}`,
} {
if !strings.Contains(out.String(), want) {
t.Errorf("json missing %s:\n%s", want, out.String())
}
}
if strings.Contains(out.String(), `"v"`) {
t.Error("a value leaked into json output")
}
}
func TestPush_OrgScope(t *testing.T) {
t.Parallel()
runner := &fakeRunner{responses: ghResponses()}
src := &envkit.Selection{Pairs: []dotenv.Pair{{Key: "K", Value: "v"}}}
out, err := runGithub(t, runner, src, "push", "--yes", "--org", "acme")
if err != nil {
t.Fatalf("%v\n%s", err, out)
}
found := false
for _, c := range runner.calls {
if c.args == "secret set K --org acme" {
found = true
}
}
if !found {
t.Errorf("org scope not passed: %+v", runner.calls)
}
if !strings.Contains(out, "target : org:acme") {
t.Errorf("banner should name the org target:\n%s", out)
}
}
func TestStore_DirectEdges(t *testing.T) {
t.Parallel()
t.Run("Set rejects invalid names even without the gate", func(t *testing.T) {
t.Parallel()
s := &ghStore{deps: providerkit.Deps{Runner: &fakeRunner{}}}
if err := s.Set(context.Background(), "bad-name", "v"); err == nil {
t.Error("defense-in-depth name check missing")
}
})
t.Run("Names surfaces unparseable gh output", func(t *testing.T) {
t.Parallel()
s := &ghStore{deps: providerkit.Deps{Runner: &fakeRunner{responses: map[string]string{"secret list": "not json"}}}}
if _, err := s.Names(context.Background()); err == nil || !strings.Contains(err.Error(), "parse gh secret list") {
t.Errorf("err = %v", err)
}
})
t.Run("resolveTarget covers every scope shape", func(t *testing.T) {
t.Parallel()
runner := &fakeRunner{responses: ghResponses()}
tests := []struct {
name, repo, env, org, want string
}{
{"org", "", "", "acme", "org:acme"},
{"repo only", "o/r", "", "", "o/r"},
{"repo with environment", "o/r", "prod", "", "o/r (environment prod)"},
{"cwd with environment", "", "prod", "", "solverhood/sync_go (from cwd git remote) (environment prod)"},
}
for _, tt := range tests {
s := &ghStore{deps: providerkit.Deps{Runner: runner}, repo: tt.repo, environment: tt.env, org: tt.org}
got, err := s.resolveTarget(context.Background())
if err != nil || got != tt.want {
t.Errorf("%s: target = %q err %v, want %q", tt.name, got, err, tt.want)
}
}
})
t.Run("meta propagates resolution failure so the kit omits it", func(t *testing.T) {
t.Parallel()
s := &ghStore{deps: providerkit.Deps{Runner: &fakeRunner{failOn: "repo view"}}}
if _, err := s.meta(context.Background()); err == nil {
t.Error("meta must not fabricate context on failure")
}
})
}
func TestEnvAliasFlag(t *testing.T) {
t.Parallel()
runner := &fakeRunner{responses: ghResponses()}
src := &envkit.Selection{Pairs: []dotenv.Pair{{Key: "K", Value: "v"}}}
// --env must behave exactly like --environment (gh's own spelling).
if _, err := runGithub(t, runner, src, "push", "--yes", "--env", "production"); err != nil {
t.Fatal(err)
}
found := false
for _, c := range runner.calls {
if c.args == "secret set K --env production" {
found = true
}
}
if !found {
t.Errorf("--env alias not honored: %+v", runner.calls)
}
}
func TestEnvCreate(t *testing.T) {
t.Parallel()
t.Run("resolves repo, gates, PUTs the environments API", func(t *testing.T) {
t.Parallel()
runner := &fakeRunner{responses: ghResponses()}
out, err := runGithub(t, runner, &envkit.Selection{}, "env-create", "prod", "--yes")
if err != nil {
t.Fatalf("%v\n%s", err, out)
}
found := false
for _, c := range runner.calls {
if c.args == "api --method PUT repos/solverhood/sync_go/environments/prod" {
found = true
}
}
if !found {
t.Errorf("PUT call missing: %+v", runner.calls)
}
if !strings.Contains(out, "prod: environment ready") {
t.Errorf("output:\n%s", out)
}
})
t.Run("explicit --repo skips resolution", func(t *testing.T) {
t.Parallel()
runner := &fakeRunner{responses: ghResponses()}
if _, err := runGithub(t, runner, &envkit.Selection{}, "env-create", "stag", "--repo", "o/r", "--yes"); err != nil {
t.Fatal(err)
}
for _, c := range runner.calls {
if strings.HasPrefix(c.args, "repo view") {
t.Error("resolved cwd repo despite explicit --repo")
}
}
})
t.Run("non-interactive without --yes refuses before any API call", func(t *testing.T) {
t.Parallel()
runner := &fakeRunner{responses: ghResponses()}
if _, err := runGithub(t, runner, &envkit.Selection{}, "env-create", "prod"); err == nil {
t.Error("want refusal")
}
for _, c := range runner.calls {
if strings.Contains(c.args, "--method PUT") {
t.Error("wrote despite refusal")
}
}
})
}
func TestPush_SelectionFlagsEndToEnd(t *testing.T) {
t.Parallel()
runner := &fakeRunner{responses: ghResponses()}
src := &envkit.Selection{Pairs: []dotenv.Pair{{Key: "KEEP", Value: "v"}}}
// The kit registers the new flags on plugin verbs; they parse and flow
// into SelectOpts (semantics unit-tested at the Source; this pins wiring).
if _, err := runGithub(t, runner, src, "push", "--yes", "--exclude-prefix", "X_", "--include", "A", "--exclude", "B"); err != nil {
t.Fatalf("new selection flags did not parse: %v", err)
}
}