forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_test.go
91 lines (74 loc) · 2.14 KB
/
action_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
package action
import (
"context"
"flag"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/blang/semver/v4"
"github.com/gopasspw/gopass/internal/backend"
"github.com/gopasspw/gopass/internal/backend/crypto/plain"
"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/tests/gptest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
)
func newMock(ctx context.Context, path string) (*Action, error) {
cfg := config.NewNoWrites()
if err := cfg.SetPath(path); err != nil {
return nil, err
}
ctx = cfg.WithConfig(ctx)
if !backend.HasCryptoBackend(ctx) {
ctx = backend.WithCryptoBackend(ctx, backend.Plain)
}
ctx = backend.WithStorageBackend(ctx, backend.GitFS)
act, err := newAction(cfg, semver.Version{}, false)
if err != nil {
return nil, err
}
fs := flag.NewFlagSet("default", flag.ContinueOnError)
c := cli.NewContext(cli.NewApp(), fs, nil)
c.Context = ctx
if err := act.IsInitialized(c); err != nil {
return nil, err
}
return act, nil
}
func TestAction(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) //nolint:ineffassign
actName := "action.test"
if runtime.GOOS == "windows" {
actName = "action.test.exe"
}
assert.Equal(t, actName, act.Name)
assert.Contains(t, act.String(), u.StoreDir(""))
assert.Equal(t, 0, len(act.Store.Mounts()))
}
func TestNew(t *testing.T) {
u := gptest.NewUnitTester(t)
assert.NotNil(t, u)
td := t.TempDir()
cfg := config.NewNoWrites()
sv := semver.Version{}
t.Run("init a new store", func(t *testing.T) {
_, err := New(cfg, sv)
require.NoError(t, err)
})
t.Run("init an existing plain store", func(t *testing.T) {
require.NoError(t, cfg.SetPath(filepath.Join(td, "store")))
assert.NoError(t, os.MkdirAll(cfg.Path(), 0o700))
assert.NoError(t, os.WriteFile(filepath.Join(cfg.Path(), plain.IDFile), []byte("foobar"), 0o600))
_, err := New(cfg, sv)
assert.NoError(t, err)
})
}