Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Increase code coverage (gopasspw#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikschulz authored Jan 3, 2018
1 parent 09f4bba commit 15b5261
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 69 deletions.
8 changes: 8 additions & 0 deletions store/sub/fsck_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package sub

import (
"bytes"
"context"
"io/ioutil"
"os"
"testing"

"github.com/justwatchcom/gopass/utils/out"
"github.com/stretchr/testify/assert"
)

Expand All @@ -20,6 +22,12 @@ func TestFsck(t *testing.T) {
_ = os.RemoveAll(tempdir)
}()

obuf := &bytes.Buffer{}
out.Stdout = obuf
defer func() {
out.Stdout = os.Stdout
}()

s, err := createSubStore(tempdir)
assert.NoError(t, err)

Expand Down
3 changes: 3 additions & 0 deletions store/sub/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func TestGit(t *testing.T) {
s, err := createSubStore(tempdir)
assert.NoError(t, err)

assert.Error(t, s.Git(ctx, "status"))
assert.Error(t, s.GitPush(ctx, "origin", "master"))

t.Skip("flaky")
assert.NoError(t, s.GitInit(ctx, "", "", ""))
assert.NoError(t, s.Git(ctx, "status"))
Expand Down
8 changes: 8 additions & 0 deletions store/sub/gpg_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package sub

import (
"bytes"
"context"
"io/ioutil"
"os"
"testing"

"github.com/justwatchcom/gopass/utils/out"
"github.com/stretchr/testify/assert"
)

Expand All @@ -20,6 +22,12 @@ func TestGPG(t *testing.T) {
_ = os.RemoveAll(tempdir)
}()

obuf := &bytes.Buffer{}
out.Stdout = obuf
defer func() {
out.Stdout = os.Stdout
}()

s, err := createSubStore(tempdir)
assert.NoError(t, err)

Expand Down
21 changes: 13 additions & 8 deletions store/sub/list_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package sub

import (
"bytes"
"context"
"io/ioutil"
"os"
"strings"
"testing"

gitmock "github.com/justwatchcom/gopass/backend/git/mock"
gpgmock "github.com/justwatchcom/gopass/backend/gpg/mock"
"github.com/justwatchcom/gopass/store/secret"
"github.com/justwatchcom/gopass/utils/out"
"github.com/stretchr/testify/assert"
)

func TestList(t *testing.T) {
ctx := context.Background()

obuf := &bytes.Buffer{}
out.Stdout = obuf
defer func() {
out.Stdout = os.Stdout
}()

for _, tc := range []struct {
name string
prep func(s *Store) error
Expand All @@ -24,6 +31,7 @@ func TestList(t *testing.T) {
{
name: "Empty store",
prep: func(s *Store) error { return nil },
out: []string{},
},
{
name: "Single entry",
Expand Down Expand Up @@ -80,13 +88,10 @@ func TestList(t *testing.T) {

// run test case
out, err := s.List("")
if err != nil {
t.Fatalf("Failed to call List(): %s", err)
}
t.Logf("Output: %s", out)
if strings.Join(out, "\n") != strings.Join(tc.out, "\n") {
t.Errorf("Mismatched output: %+v vs. %+v", out, tc.out)
}
assert.NoError(t, err)
assert.Equal(t, tc.out, out)

obuf.Reset()

// common tear down
_ = os.RemoveAll(tempdir)
Expand Down
31 changes: 30 additions & 1 deletion store/sub/move_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sub

import (
"bytes"
"context"
"io/ioutil"
"os"
Expand All @@ -9,12 +10,19 @@ import (
gitmock "github.com/justwatchcom/gopass/backend/git/mock"
gpgmock "github.com/justwatchcom/gopass/backend/gpg/mock"
"github.com/justwatchcom/gopass/store/secret"
"github.com/justwatchcom/gopass/utils/out"
"github.com/stretchr/testify/assert"
)

func TestCopy(t *testing.T) {
ctx := context.Background()

obuf := &bytes.Buffer{}
out.Stdout = obuf
defer func() {
out.Stdout = os.Stdout
}()

for _, tc := range []struct {
name string
tf func(s *Store) func(t *testing.T)
Expand Down Expand Up @@ -62,7 +70,6 @@ func TestCopy(t *testing.T) {
if err != nil {
t.Fatalf("Failed to create tempdir: %s", err)
}
t.Logf("Using tempdir: %s", tempdir)

s := &Store{
alias: "",
Expand All @@ -77,6 +84,7 @@ func TestCopy(t *testing.T) {
// run test case
t.Run(tc.name, tc.tf(s))

obuf.Reset()
// common tear down
_ = os.RemoveAll(tempdir)
}
Expand All @@ -85,6 +93,12 @@ func TestCopy(t *testing.T) {
func TestMove(t *testing.T) {
ctx := context.Background()

obuf := &bytes.Buffer{}
out.Stdout = obuf
defer func() {
out.Stdout = os.Stdout
}()

for _, tc := range []struct {
name string
tf func(s *Store) func(t *testing.T)
Expand Down Expand Up @@ -143,6 +157,7 @@ func TestMove(t *testing.T) {
// run test case
t.Run(tc.name, tc.tf(s))

obuf.Reset()
// common tear down
_ = os.RemoveAll(tempdir)
}
Expand All @@ -151,6 +166,12 @@ func TestMove(t *testing.T) {
func TestDelete(t *testing.T) {
ctx := context.Background()

obuf := &bytes.Buffer{}
out.Stdout = obuf
defer func() {
out.Stdout = os.Stdout
}()

for _, tc := range []struct {
name string
tf func(s *Store) func(t *testing.T)
Expand Down Expand Up @@ -202,6 +223,7 @@ func TestDelete(t *testing.T) {
// run test case
t.Run(tc.name, tc.tf(s))

obuf.Reset()
// common tear down
_ = os.RemoveAll(tempdir)
}
Expand All @@ -210,6 +232,12 @@ func TestDelete(t *testing.T) {
func TestPrune(t *testing.T) {
ctx := context.Background()

obuf := &bytes.Buffer{}
out.Stdout = obuf
defer func() {
out.Stdout = os.Stdout
}()

for _, tc := range []struct {
name string
tf func(s *Store) func(t *testing.T)
Expand Down Expand Up @@ -292,6 +320,7 @@ func TestPrune(t *testing.T) {
// run test case
t.Run(tc.name, tc.tf(s))

obuf.Reset()
// common tear down
_ = os.RemoveAll(tempdir)
}
Expand Down
41 changes: 40 additions & 1 deletion store/sub/recipients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

gitmock "github.com/justwatchcom/gopass/backend/git/mock"
gpgmock "github.com/justwatchcom/gopass/backend/gpg/mock"
"github.com/justwatchcom/gopass/utils/out"
"github.com/stretchr/testify/assert"
)

Expand All @@ -27,6 +28,12 @@ func TestGetRecipientsDefault(t *testing.T) {
_ = os.RemoveAll(tempdir)
}()

obuf := &bytes.Buffer{}
out.Stdout = obuf
defer func() {
out.Stdout = os.Stdout
}()

genRecs, _, err := createStore(tempdir, nil, nil)
assert.NoError(t, err)

Expand All @@ -50,7 +57,13 @@ func TestGetRecipientsSubID(t *testing.T) {
t.Fatalf("Failed to create tempdir: %s", err)
}
defer func() {
//_ = os.RemoveAll(tempdir)
_ = os.RemoveAll(tempdir)
}()

obuf := &bytes.Buffer{}
out.Stdout = obuf
defer func() {
out.Stdout = os.Stdout
}()

genRecs, _, err := createStore(tempdir, nil, nil)
Expand Down Expand Up @@ -88,6 +101,12 @@ func TestSaveRecipients(t *testing.T) {
_, _, err = createStore(tempdir, nil, nil)
assert.NoError(t, err)

obuf := &bytes.Buffer{}
out.Stdout = obuf
defer func() {
out.Stdout = os.Stdout
}()

recp := []string{"john.doe"}
s := &Store{
alias: "",
Expand Down Expand Up @@ -127,6 +146,7 @@ func TestSaveRecipients(t *testing.T) {

func TestAddRecipient(t *testing.T) {
ctx := context.Background()
ctx = out.WithHidden(ctx, true)

tempdir, err := ioutil.TempDir("", "gopass-")
if err != nil {
Expand All @@ -139,6 +159,12 @@ func TestAddRecipient(t *testing.T) {
genRecs, _, err := createStore(tempdir, nil, nil)
assert.NoError(t, err)

obuf := &bytes.Buffer{}
out.Stdout = obuf
defer func() {
out.Stdout = os.Stdout
}()

s := &Store{
alias: "",
path: tempdir,
Expand All @@ -161,6 +187,7 @@ func TestAddRecipient(t *testing.T) {

func TestRemoveRecipient(t *testing.T) {
ctx := context.Background()
ctx = out.WithHidden(ctx, true)

tempdir, err := ioutil.TempDir("", "gopass-")
if err != nil {
Expand All @@ -172,6 +199,12 @@ func TestRemoveRecipient(t *testing.T) {
_, _, err = createStore(tempdir, nil, nil)
assert.NoError(t, err)

obuf := &bytes.Buffer{}
out.Stdout = obuf
defer func() {
out.Stdout = os.Stdout
}()

s := &Store{
alias: "",
path: tempdir,
Expand Down Expand Up @@ -201,6 +234,12 @@ func TestListRecipients(t *testing.T) {
genRecs, _, err := createStore(tempdir, nil, nil)
assert.NoError(t, err)

obuf := &bytes.Buffer{}
out.Stdout = obuf
defer func() {
out.Stdout = os.Stdout
}()

s := New(
"",
tempdir,
Expand Down
6 changes: 5 additions & 1 deletion store/sub/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (s *Store) reencrypt(ctx context.Context) error {
Total: int64(len(entries)),
Width: 120,
}
if !ctxutil.IsTerminal(ctx) {
if !ctxutil.IsTerminal(ctx) || out.IsHidden(ctx) {
bar = nil
}
for _, e := range entries {
Expand Down Expand Up @@ -189,6 +189,10 @@ func (s *Store) reencrypt(ctx context.Context) error {
return nil
}

return s.reencryptGitPush(ctx)
}

func (s *Store) reencryptGitPush(ctx context.Context) error {
if err := s.git.Push(ctx, "", ""); err != nil {
if errors.Cause(err) == store.ErrGitNotInit {
msg := "Warning: git is not initialized for this store. Ignoring auto-push option\n" +
Expand Down
33 changes: 30 additions & 3 deletions utils/completion/fish/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fish
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
)

Expand All @@ -28,8 +29,34 @@ func TestFormatFlag(t *testing.T) {
func TestGetCompletion(t *testing.T) {
app := cli.NewApp()
sv, err := GetCompletion(app)
if err != nil {
t.Fatalf("Error: %s", err)
assert.NoError(t, err)
assert.Contains(t, sv, "#!/usr/bin/env fish")
}

func TestFormatflagFunc(t *testing.T) {
for _, flag := range []cli.Flag{
cli.BoolFlag{Name: "foo", Usage: "bar"},
cli.Float64Flag{Name: "foo", Usage: "bar"},
cli.GenericFlag{Name: "foo", Usage: "bar"},
cli.Int64Flag{Name: "foo", Usage: "bar"},
cli.Int64SliceFlag{Name: "foo", Usage: "bar"},
cli.IntFlag{Name: "foo", Usage: "bar"},
cli.IntSliceFlag{Name: "foo", Usage: "bar"},
cli.StringFlag{Name: "foo", Usage: "bar"},
cli.StringSliceFlag{Name: "foo", Usage: "bar"},
cli.Uint64Flag{Name: "foo", Usage: "bar"},
cli.UintFlag{Name: "foo", Usage: "bar"},
} {
sv, err := formatFlagFunc("short")(flag)
assert.NoError(t, err)
assert.Equal(t, "", sv)

sv, err = formatFlagFunc("long")(flag)
assert.NoError(t, err)
assert.Equal(t, "foo", sv)

sv, err = formatFlagFunc("usage")(flag)
assert.NoError(t, err)
assert.Equal(t, "bar", sv)
}
t.Logf("Output: %s", sv)
}
Loading

0 comments on commit 15b5261

Please sign in to comment.