Skip to content

Commit

Permalink
Increase test coverage (gopasspw#2461)
Browse files Browse the repository at this point in the history
RELEASE_NOTES=n/a

Signed-off-by: Dominik Schulz <[email protected]>

Signed-off-by: Dominik Schulz <[email protected]>
  • Loading branch information
dominikschulz authored Dec 10, 2022
1 parent 6fedd78 commit f584544
Show file tree
Hide file tree
Showing 138 changed files with 1,125 additions and 644 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ linters:
- noctx
- nolintlint
- nosnakecase
- paralleltest
- revive
- rowserrcheck
- scopelint
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ fulltest: $(GOPASS_OUTPUT)
$(GO) test -run '(Test|Example)' $(BUILDFLAGS) $(TESTFLAGS) -coverprofile=coverage.out -covermode=atomic $(pkg) || exit 1;\
tail -n +2 coverage.out >> coverage-all.out;)
@$(GO) tool cover -html=coverage-all.out -o coverage-all.html
@which go-cover-treemap > /dev/null; if [ $$? -ne 0 ]; then \
$(GO) install github.com/nikolaydubina/go-cover-treemap@latest; \
fi
@go-cover-treemap -coverprofile coverage-all.out > coverage-all.svg

test: $(GOPASS_OUTPUT)
@echo ">> TEST, \"fast-mode\": race detector off"
Expand Down
9 changes: 2 additions & 7 deletions internal/action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ func newMock(ctx context.Context, path string) (*Action, error) {
}

func TestAction(t *testing.T) {
t.Parallel()

u := gptest.NewUnitTester(t)
defer u.Remove()

ctx := context.Background()
ctx = ctxutil.WithInteractive(ctx, false)
Expand All @@ -72,18 +69,16 @@ func TestAction(t *testing.T) {
}

func TestNew(t *testing.T) {
t.Parallel()

td := t.TempDir()
cfg := config.NewNoWrites()
sv := semver.Version{}

t.Run("init a new store", func(t *testing.T) { //nolint:paralleltest
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) { //nolint:paralleltest
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))
Expand Down
36 changes: 36 additions & 0 deletions internal/action/aliases_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package action

import (
"bytes"
"context"
"os"
"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 TestAliases(t *testing.T) {
u := gptest.NewUnitTester(t)

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithHidden(ctx, true)
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
}()

assert.NoError(t, act.AliasesPrint(gptest.CliCtx(ctx, t)))
}
9 changes: 4 additions & 5 deletions internal/action/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import (
"github.com/stretchr/testify/require"
)

func TestAudit(t *testing.T) { //nolint:paralleltest
func TestAudit(t *testing.T) {
u := gptest.NewUnitTester(t)
defer u.Remove()

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
Expand All @@ -34,7 +33,7 @@ func TestAudit(t *testing.T) { //nolint:paralleltest
stdout = os.Stdout
}()

t.Run("expect audit complaints on very weak passwords", func(t *testing.T) { //nolint:paralleltest
t.Run("expect audit complaints on very weak passwords", func(t *testing.T) {
sec := secrets.NewAKV()
sec.SetPassword("123")
assert.NoError(t, act.Store.Set(ctx, "bar", sec))
Expand All @@ -44,13 +43,13 @@ func TestAudit(t *testing.T) { //nolint:paralleltest
buf.Reset()
})

t.Run("test with filter and very passwords", func(t *testing.T) { //nolint:paralleltest
t.Run("test with filter and very passwords", func(t *testing.T) {
c := gptest.CliCtx(ctx, t, "foo")
assert.Error(t, act.Audit(c))
buf.Reset()
})

t.Run("test empty store", func(t *testing.T) { //nolint:paralleltest
t.Run("test empty store", func(t *testing.T) {
for _, v := range []string{"foo", "bar", "baz"} {
assert.NoError(t, act.Store.Delete(ctx, v))
}
Expand Down
41 changes: 18 additions & 23 deletions internal/action/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import (
"github.com/stretchr/testify/require"
)

func TestBinary(t *testing.T) { //nolint:paralleltest
func TestBinary(t *testing.T) {
u := gptest.NewUnitTester(t)
defer u.Remove()

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
Expand All @@ -40,9 +39,8 @@ func TestBinary(t *testing.T) { //nolint:paralleltest
assert.Error(t, act.Sum(gptest.CliCtx(ctx, t)))
}

func TestBinaryCat(t *testing.T) { //nolint:paralleltest
func TestBinaryCat(t *testing.T) {
u := gptest.NewUnitTester(t)
defer u.Remove()

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
Expand All @@ -64,16 +62,16 @@ func TestBinaryCat(t *testing.T) { //nolint:paralleltest
infile := filepath.Join(u.Dir, "input.txt")
writeBinfile(t, infile)

t.Run("populate store", func(t *testing.T) { //nolint:paralleltest
t.Run("populate store", func(t *testing.T) {
assert.NoError(t, act.binaryCopy(ctx, gptest.CliCtx(ctx, t), infile, "bar", true))
})

t.Run("binary cat bar", func(t *testing.T) { //nolint:paralleltest
t.Run("binary cat bar", func(t *testing.T) {
assert.NoError(t, act.Cat(gptest.CliCtx(ctx, t, "bar")))
})

stdinfile := filepath.Join(u.Dir, "stdin")
t.Run("binary cat baz from stdin", func(t *testing.T) { //nolint:paralleltest
t.Run("binary cat baz from stdin", func(t *testing.T) {
writeBinfile(t, stdinfile)

fd, err := os.Open(stdinfile)
Expand All @@ -87,7 +85,7 @@ func TestBinaryCat(t *testing.T) { //nolint:paralleltest
assert.NoError(t, act.Cat(gptest.CliCtx(ctx, t, "baz")))
})

t.Run("compare output", func(t *testing.T) { //nolint:paralleltest
t.Run("compare output", func(t *testing.T) {
buf, err := os.ReadFile(stdinfile)
require.NoError(t, err)
sec, err := act.binaryGet(ctx, "baz")
Expand All @@ -96,9 +94,8 @@ func TestBinaryCat(t *testing.T) { //nolint:paralleltest
})
}

func TestBinaryCopy(t *testing.T) { //nolint:paralleltest
func TestBinaryCopy(t *testing.T) {
u := gptest.NewUnitTester(t)
defer u.Remove()

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
Expand All @@ -115,7 +112,7 @@ func TestBinaryCopy(t *testing.T) { //nolint:paralleltest
require.NotNil(t, act)
ctx = act.cfg.WithConfig(ctx)

t.Run("copy textfile", func(t *testing.T) { //nolint:paralleltest
t.Run("copy textfile", func(t *testing.T) {
defer buf.Reset()

infile := filepath.Join(u.Dir, "input.txt")
Expand All @@ -125,43 +122,42 @@ func TestBinaryCopy(t *testing.T) { //nolint:paralleltest

infile := filepath.Join(u.Dir, "input.raw")
outfile := filepath.Join(u.Dir, "output.raw")
t.Run("copy binary file", func(t *testing.T) { //nolint:paralleltest
t.Run("copy binary file", func(t *testing.T) {
defer buf.Reset()

writeBinfile(t, infile)
assert.NoError(t, act.binaryCopy(ctx, gptest.CliCtx(ctx, t), infile, "bar", true))
})

t.Run("binary copy bar tempdir/bar", func(t *testing.T) { //nolint:paralleltest
t.Run("binary copy bar tempdir/bar", func(t *testing.T) {
defer buf.Reset()
assert.NoError(t, act.BinaryCopy(gptest.CliCtx(ctx, t, "bar", outfile)))
})

t.Run("binary copy tempdir/bar tempdir/bar", func(t *testing.T) { //nolint:paralleltest
t.Run("binary copy tempdir/bar tempdir/bar", func(t *testing.T) {
defer buf.Reset()

assert.Error(t, act.BinaryCopy(gptest.CliCtx(ctx, t, outfile, outfile)))
})

t.Run("binary copy bar bar", func(t *testing.T) { //nolint:paralleltest
t.Run("binary copy bar bar", func(t *testing.T) {
defer buf.Reset()
assert.Error(t, act.BinaryCopy(gptest.CliCtx(ctx, t, "bar", "bar")))
})

t.Run("binary move tempdir/bar bar2", func(t *testing.T) { //nolint:paralleltest
t.Run("binary move tempdir/bar bar2", func(t *testing.T) {
defer buf.Reset()
assert.NoError(t, act.BinaryMove(gptest.CliCtx(ctx, t, outfile, "bar2")))
})

t.Run("binary move bar2 tempdir/bar", func(t *testing.T) { //nolint:paralleltest
t.Run("binary move bar2 tempdir/bar", func(t *testing.T) {
defer buf.Reset()
assert.NoError(t, act.BinaryMove(gptest.CliCtx(ctx, t, "bar2", outfile)))
})
}

func TestBinarySum(t *testing.T) { //nolint:paralleltest
func TestBinarySum(t *testing.T) {
u := gptest.NewUnitTester(t)
defer u.Remove()

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
Expand All @@ -180,20 +176,19 @@ func TestBinarySum(t *testing.T) { //nolint:paralleltest

infile := filepath.Join(u.Dir, "input.raw")

t.Run("populate store", func(t *testing.T) { //nolint:paralleltest
t.Run("populate store", func(t *testing.T) {
writeBinfile(t, infile)
assert.NoError(t, act.binaryCopy(ctx, gptest.CliCtx(ctx, t), infile, "bar", true))
})

t.Run("binary sum bar", func(t *testing.T) { //nolint:paralleltest
t.Run("binary sum bar", func(t *testing.T) {
assert.NoError(t, act.Sum(gptest.CliCtx(ctx, t, "bar")))
buf.Reset()
})
}

func TestBinaryGet(t *testing.T) { //nolint:paralleltest
func TestBinaryGet(t *testing.T) {
u := gptest.NewUnitTester(t)
defer u.Remove()

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
Expand Down
4 changes: 0 additions & 4 deletions internal/action/clihelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
)

func TestParseArgs(t *testing.T) {
t.Parallel()

for _, tc := range []struct {
name string
argIn []string
Expand Down Expand Up @@ -69,8 +67,6 @@ func TestParseArgs(t *testing.T) {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

if tc.argOut == nil {
tc.argOut = argList{}
}
Expand Down
56 changes: 47 additions & 9 deletions internal/action/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/blang/semver/v4"
Expand Down Expand Up @@ -39,9 +40,8 @@ func aGitRepo(ctx context.Context, t *testing.T, u *gptest.Unit, name string) st
return gd
}

func TestClone(t *testing.T) { //nolint:paralleltest
func TestClone(t *testing.T) {
u := gptest.NewUnitTester(t)
defer u.Remove()

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
Expand All @@ -63,27 +63,26 @@ func TestClone(t *testing.T) { //nolint:paralleltest
stdout = os.Stdout
}()

t.Run("no args", func(t *testing.T) { //nolint:paralleltest
t.Run("no args", func(t *testing.T) {
defer buf.Reset()
c := gptest.CliCtx(ctx, t)
assert.Error(t, act.Clone(c))
})

t.Run("clone to initialized store", func(t *testing.T) { //nolint:paralleltest
t.Run("clone to initialized store", func(t *testing.T) {
defer buf.Reset()
assert.Error(t, act.clone(ctx, "/tmp/non-existing-repo.git", "", filepath.Join(u.Dir, "store")))
})

t.Run("clone to mount", func(t *testing.T) { //nolint:paralleltest
t.Run("clone to mount", func(t *testing.T) {
defer buf.Reset()
gd := aGitRepo(ctx, t, u, "other-repo")
assert.NoError(t, act.clone(ctx, gd, "gd", filepath.Join(u.Dir, "mount")))
})
}

func TestCloneBackendIsStoredForMount(t *testing.T) { //nolint:paralleltest
func TestCloneBackendIsStoredForMount(t *testing.T) {
u := gptest.NewUnitTester(t)
defer u.Remove()

buf := &bytes.Buffer{}
out.Stdout = buf
Expand Down Expand Up @@ -118,9 +117,8 @@ func TestCloneBackendIsStoredForMount(t *testing.T) { //nolint:paralleltest
require.Contains(t, act.cfg.Mounts(), "the-project")
}

func TestCloneGetGitConfig(t *testing.T) { //nolint:paralleltest
func TestCloneGetGitConfig(t *testing.T) {
u := gptest.NewUnitTester(t)
defer u.Remove()

r1 := gptest.UnsetVars(termio.NameVars...)
defer r1()
Expand All @@ -141,3 +139,43 @@ func TestCloneGetGitConfig(t *testing.T) { //nolint:paralleltest
assert.Equal(t, "0xDEADBEEF", name)
assert.Equal(t, "0xDEADBEEF", email)
}

func TestCloneCheckDecryptionKeys(t *testing.T) {
u := gptest.NewUnitTester(t)

buf := &bytes.Buffer{}
out.Stdout = buf
out.Stderr = buf
stdout = buf
defer func() {
out.Stdout = os.Stdout
out.Stderr = os.Stderr
stdout = os.Stdout
}()

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithInteractive(ctx, false)

cfg := config.NewNoWrites()
require.NoError(t, cfg.SetPath(u.StoreDir("")))

act, err := newAction(cfg, semver.Version{}, false)
require.NoError(t, err)
require.NotNil(t, act)
ctx = act.cfg.WithConfig(ctx)

c := gptest.CliCtx(ctx, t)
require.NoError(t, act.IsInitialized(c))

repo := aGitRepo(ctx, t, u, "my-project")

if runtime.GOOS != "linux" {
t.Skip("TODO: not working on non-linux builders, yet")
}

c = gptest.CliCtxWithFlags(ctx, t, map[string]string{"check-keys": "true"}, repo, "the-project")
assert.NoError(t, act.Clone(c))

require.Contains(t, act.cfg.Mounts(), "the-project")
}
Loading

0 comments on commit f584544

Please sign in to comment.