forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_test.go
236 lines (198 loc) · 5.27 KB
/
list_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
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
package action
import (
"bytes"
"context"
"os"
"testing"
"github.com/fatih/color"
"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/internal/tree"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/gopass/secrets"
"github.com/gopasspw/gopass/tests/gptest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestList(t *testing.T) {
u := gptest.NewUnitTester(t)
ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
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() {
stdout = os.Stdout
out.Stdout = os.Stdout
}()
color.NoColor = true
assert.NoError(t, act.List(gptest.CliCtx(ctx, t)))
want := `gopass
└── foo
`
assert.Equal(t, want, buf.String())
buf.Reset()
// add foo/bar and list folder foo
sec := secrets.NewAKV()
sec.SetPassword("123")
assert.NoError(t, sec.Set("bar", "zab"))
assert.NoError(t, act.Store.Set(ctx, "foo/bar", sec))
buf.Reset()
assert.NoError(t, act.List(gptest.CliCtx(ctx, t, "foo")))
want = `foo/
└── bar
`
assert.Equal(t, want, buf.String())
buf.Reset()
// list --flat foo
assert.NoError(t, act.List(gptest.CliCtxWithFlags(ctx, t, map[string]string{"flat": "true"}, "foo")))
want = `foo/bar
`
assert.Equal(t, want, buf.String())
buf.Reset()
// list --folders
// add more folders and subfolders
sec = secrets.NewAKV()
sec.SetPassword("123")
assert.NoError(t, act.Store.Set(ctx, "foo/zen/bar", sec))
assert.NoError(t, act.Store.Set(ctx, "foo2/bar2", sec))
buf.Reset()
assert.NoError(t, act.List(gptest.CliCtxWithFlags(ctx, t, map[string]string{"folders": "true"})))
want = `foo/
foo/zen/
foo2/
`
assert.Equal(t, want, buf.String())
buf.Reset()
// add shadowed entry
sec = secrets.NewAKV()
sec.SetPassword("123")
assert.NoError(t, act.Store.Set(ctx, "foo/zen", sec))
buf.Reset()
assert.NoError(t, act.List(gptest.CliCtxWithFlags(ctx, t, map[string]string{"flat": "true"})))
want = `foo
foo/bar
foo/zen
foo/zen/bar
foo2/bar2
`
assert.Equal(t, want, buf.String())
buf.Reset()
assert.NoError(t, act.List(gptest.CliCtx(ctx, t, "foo")))
want = `foo/
├── bar
└── zen/ (shadowed)
└── bar
`
assert.Equal(t, want, buf.String())
buf.Reset()
// list not-present
assert.Error(t, act.List(gptest.CliCtx(ctx, t, "not-present")))
buf.Reset()
}
func TestListLimit(t *testing.T) {
u := gptest.NewUnitTester(t)
ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
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() {
stdout = os.Stdout
out.Stdout = os.Stdout
}()
color.NoColor = true
assert.NoError(t, act.List(gptest.CliCtx(ctx, t)))
want := `gopass
└── foo
`
sec := secrets.NewAKV()
sec.SetPassword("123")
assert.NoError(t, act.Store.Set(ctx, "foo/bar", sec))
assert.NoError(t, act.Store.Set(ctx, "foo/zen/baz/bar", sec))
assert.NoError(t, act.Store.Set(ctx, "foo2/bar2", sec))
assert.Equal(t, want, buf.String())
buf.Reset()
t.Run("folders-limit-0", func(t *testing.T) {
assert.NoError(t, act.List(gptest.CliCtxWithFlags(ctx, t, map[string]string{"folders": "true", "limit": "0"})))
want = `foo/
foo2/
`
assert.Equal(t, want, buf.String())
buf.Reset()
})
t.Run("folders-limit-1", func(t *testing.T) {
assert.NoError(t, act.List(gptest.CliCtxWithFlags(ctx, t, map[string]string{"folders": "true", "limit": "1"})))
want = `foo/
foo/zen/
foo2/
`
assert.Equal(t, want, buf.String())
buf.Reset()
})
t.Run("folders-limit--1", func(t *testing.T) {
assert.NoError(t, act.List(gptest.CliCtxWithFlags(ctx, t, map[string]string{"folders": "true", "limit": "-1"})))
want = `foo/
foo/zen/
foo/zen/baz/
foo2/
`
assert.Equal(t, want, buf.String())
buf.Reset()
})
t.Run("flat-limit--1", func(t *testing.T) {
assert.NoError(t, act.List(gptest.CliCtxWithFlags(ctx, t, map[string]string{"flat": "true", "limit": "-1"})))
want = `foo
foo/bar
foo/zen/baz/bar
foo2/bar2
`
assert.Equal(t, want, buf.String())
buf.Reset()
})
t.Run("folders-limit-0", func(t *testing.T) {
assert.NoError(t, act.List(gptest.CliCtxWithFlags(ctx, t, map[string]string{"flat": "true", "limit": "0"})))
want = `foo
foo2/
`
assert.Equal(t, want, buf.String())
buf.Reset()
})
t.Run("folders-limit-2", func(t *testing.T) {
assert.NoError(t, act.List(gptest.CliCtxWithFlags(ctx, t, map[string]string{"flat": "true", "limit": "2"})))
want = `foo
foo/bar
foo/zen/baz/
foo2/bar2
`
assert.Equal(t, want, buf.String())
buf.Reset()
})
}
func TestRedirectPager(t *testing.T) {
ctx := context.Background()
var buf *bytes.Buffer
var subtree *tree.Root
cfg := config.NewNoWrites()
ctx = cfg.WithConfig(ctx)
// no pager
require.NoError(t, cfg.Set("", "core.nopager", "true"))
so, buf := redirectPager(ctx, subtree)
assert.Nil(t, buf)
assert.NotNil(t, so)
// no term
require.NoError(t, cfg.Set("", "core.nopager", "false"))
so, buf = redirectPager(ctx, subtree)
assert.Nil(t, buf)
assert.NotNil(t, so)
}