forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
130 lines (105 loc) · 2.98 KB
/
config_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
package action
import (
"bytes"
"context"
"os"
"strings"
"testing"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/tests/gptest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConfig(t *testing.T) {
u := gptest.NewUnitTester(t)
ctx := context.Background()
ctx = ctxutil.WithInteractive(ctx, false)
act, err := newMock(ctx, u.StoreDir(""))
require.NoError(t, err)
require.NotNil(t, act)
ctx = act.cfg.WithConfig(ctx)
buf := &bytes.Buffer{}
out.Stdout = buf
stdout = buf
defer func() {
out.Stdout = os.Stdout
stdout = os.Stdout
}()
t.Run("display config", func(t *testing.T) {
defer buf.Reset()
c := gptest.CliCtx(ctx, t)
assert.NoError(t, act.Config(c))
want := `core.autoclip = true
core.autoimport = true
core.autosync = true
core.cliptimeout = 45
core.exportkeys = true
core.nopager = true
core.notifications = true
`
want += "mounts.path = " + u.StoreDir("") + "\n"
assert.Equal(t, want, buf.String())
})
t.Run("set valid config value", func(t *testing.T) {
defer buf.Reset()
assert.NoError(t, act.setConfigValue(ctx, "", "core.nopager", "true"))
// should print accepted config value
assert.Equal(t, "true", strings.TrimSpace(buf.String()), "action.setConfigValue")
})
t.Run("set invalid config value", func(t *testing.T) {
defer buf.Reset()
assert.Error(t, act.setConfigValue(ctx, "", "foobar", "true"))
})
t.Run("print single config value", func(t *testing.T) {
defer buf.Reset()
act.printConfigValues(ctx, "", "core.nopager")
want := "true"
assert.Equal(t, want, strings.TrimSpace(buf.String()), "action.printConfigValues")
})
t.Run("print all config values", func(t *testing.T) {
defer buf.Reset()
act.printConfigValues(ctx, "")
want := `core.autoclip = true
core.autoimport = true
core.autosync = true
core.cliptimeout = 45
core.exportkeys = true
core.nopager = true
core.notifications = true
`
want += "mounts.path = " + u.StoreDir("")
assert.Equal(t, want, strings.TrimSpace(buf.String()), "action.printConfigValues")
})
t.Run("show autoimport value", func(t *testing.T) {
defer buf.Reset()
c := gptest.CliCtx(ctx, t, "core.autoimport")
assert.NoError(t, act.Config(c))
assert.Equal(t, "true", strings.TrimSpace(buf.String()))
})
t.Run("disable autoimport", func(t *testing.T) {
defer buf.Reset()
c := gptest.CliCtx(ctx, t, "core.autoimport", "false")
assert.NoError(t, act.Config(c))
assert.Equal(t, "false", strings.TrimSpace(buf.String()))
})
t.Run("complete config items", func(t *testing.T) {
defer buf.Reset()
act.ConfigComplete(gptest.CliCtx(ctx, t))
want := `core.autoclip
core.autoimport
core.autosync
core.cliptimeout
core.exportkeys
core.nopager
core.notifications
mounts.path
`
assert.Equal(t, want, buf.String())
})
t.Run("set autoimport to invalid value", func(t *testing.T) {
defer buf.Reset()
c := gptest.CliCtx(ctx, t, "autoimport", "false", "42")
assert.Error(t, act.Config(c))
})
}