forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands_test.go
138 lines (125 loc) · 3.57 KB
/
commands_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
132
133
134
135
136
137
138
package main
import (
"bytes"
"context"
"flag"
"os"
"testing"
"github.com/atotto/clipboard"
"github.com/blang/semver"
"github.com/fatih/color"
"github.com/gopasspw/gopass/pkg/action"
"github.com/gopasspw/gopass/pkg/backend"
"github.com/gopasspw/gopass/pkg/config"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/out"
"github.com/gopasspw/gopass/tests/gptest"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
)
// commandsWithError is a list of commands that return an error when
// invoked without arguments
var commandsWithError = map[string]struct{}{
".audit": {},
".audit.hibp": {},
".binary.cat": {},
".binary.copy": {},
".binary.move": {},
".binary.sum": {},
".clone": {},
".copy": {},
".create": {},
".delete": {},
".edit": {},
".find": {},
".generate": {},
".git.push": {},
".git.remote.add": {},
".git.remote.remove": {},
".grep": {},
".history": {},
".init": {},
".insert": {},
".mounts.add": {},
".mounts.remove": {},
".move": {},
".otp": {},
".recipients.add": {},
".recipients.remove": {},
".setup": {},
".show": {},
".templates.edit": {},
".templates.remove": {},
".templates.show": {},
".unclip": {},
".xc.decrypt": {},
".xc.encrypt": {},
".xc.export": {},
".xc.export-private-key": {},
".xc.generate": {},
".xc.import": {},
".xc.import-private-key": {},
".xc.remove": {},
}
func TestGetCommands(t *testing.T) {
u := gptest.NewUnitTester(t)
defer u.Remove()
buf := &bytes.Buffer{}
out.Stdout = buf
color.NoColor = true
defer func() {
out.Stdout = os.Stdout
}()
cfg := config.New()
cfg.Root.Path = backend.FromPath(u.StoreDir(""))
clipboard.Unsupported = true
ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithInteractive(ctx, false)
ctx = ctxutil.WithTerminal(ctx, false)
ctx = out.WithHidden(ctx, true)
ctx = backend.WithRCSBackendString(ctx, "noop")
ctx = backend.WithCryptoBackendString(ctx, "plain")
act, err := action.New(ctx, cfg, semver.Version{})
assert.NoError(t, err)
app := cli.NewApp()
fs := flag.NewFlagSet("default", flag.ContinueOnError)
c := cli.NewContext(app, fs, nil)
commands := getCommands(ctx, act, app)
assert.Equal(t, 33, len(commands))
prefix := ""
testCommands(t, c, commands, prefix)
}
func testCommands(t *testing.T, c *cli.Context, commands []cli.Command, prefix string) {
for _, cmd := range commands {
if cmd.Name == "agent" || cmd.Name == "update" {
// the agent command is blocking
continue
}
if cmd.Name == "configure" && prefix == ".jsonapi" {
// running jsonapi configure will overwrite the chrome manifest
continue
}
if len(cmd.Subcommands) > 0 {
testCommands(t, c, cmd.Subcommands, prefix+"."+cmd.Name)
}
if cmd.Before != nil {
if err := cmd.Before(c); err != nil {
continue
}
}
if cmd.BashComplete != nil {
cmd.BashComplete(c)
}
if cmd.Action != nil {
fullName := prefix + "." + cmd.Name
if av, ok := cmd.Action.(func(c *cli.Context) error); ok {
if _, found := commandsWithError[fullName]; found {
assert.Error(t, av(c), fullName)
continue
}
assert.NoError(t, av(c), fullName)
}
}
}
}