Skip to content

Commit

Permalink
Increase test coverage (gopasspw#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikschulz authored Dec 26, 2017
1 parent cda0c8b commit 38df5b2
Show file tree
Hide file tree
Showing 12 changed files with 340 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ codequality:
$(GO) get -u github.com/fzipp/gocyclo; \
fi
@$(foreach gofile, $(GOFILES_NOVENDOR),\
gocyclo -over 15 $(gofile);)
gocyclo -over 15 $(gofile) || exit 1;)
@$(call ok)

@echo -n " LINT "
Expand Down
6 changes: 6 additions & 0 deletions action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/blang/semver"
"github.com/fatih/color"
"github.com/google/go-cmp/cmp"
gpgmock "github.com/justwatchcom/gopass/backend/gpg/mock"
"github.com/justwatchcom/gopass/config"
Expand Down Expand Up @@ -53,6 +54,10 @@ func newMock(ctx context.Context, dir string) (*Action, error) {
func capture(t *testing.T, fn func() error) string {
t.Helper()
old := os.Stdout

oldcol := color.NoColor
color.NoColor = true

r, w, _ := os.Pipe()
os.Stdout = w

Expand All @@ -67,6 +72,7 @@ func capture(t *testing.T, fn func() error) string {
// back to normal
_ = w.Close()
os.Stdout = old
color.NoColor = oldcol
if err != nil {
t.Errorf("Error: %s", err)
}
Expand Down
114 changes: 108 additions & 6 deletions action/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,93 @@ func TestBinary(t *testing.T) {
if err := act.BinarySum(ctx, c); err == nil {
t.Errorf("Should fail")
}
}

func TestBinaryCat(t *testing.T) {
td, err := ioutil.TempDir("", "gopass-")
if err != nil {
t.Fatalf("Error: %s", err)
}
defer func() {
_ = os.RemoveAll(td)
}()

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = out.WithHidden(ctx, true)
act, err := newMock(ctx, td)
if err != nil {
t.Fatalf("Error: %s", err)
}

app := cli.NewApp()

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

infile := filepath.Join(td, "input.txt")
if err := ioutil.WriteFile(infile, []byte("0xDEADBEEF"), 0644); err != nil {
t.Fatalf("Failed to write input file: %s", err)
}
if err := act.binaryCopy(ctx, infile, "bar", true); err != nil {
t.Fatalf("Failed to move file to store: %s", err)
}

// binary cat bar
fs = flag.NewFlagSet("default", flag.ContinueOnError)
fs := flag.NewFlagSet("default", flag.ContinueOnError)
if err := fs.Parse([]string{"bar"}); err != nil {
t.Fatalf("Error: %s", err)
}
c = cli.NewContext(app, fs, nil)
c := cli.NewContext(app, fs, nil)
if err := act.BinaryCat(ctx, c); err != nil {
t.Errorf("Should not fail")
}
}

func TestBinaryCopy(t *testing.T) {
td, err := ioutil.TempDir("", "gopass-")
if err != nil {
t.Fatalf("Error: %s", err)
}
defer func() {
_ = os.RemoveAll(td)
}()

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = out.WithHidden(ctx, true)
act, err := newMock(ctx, td)
if err != nil {
t.Fatalf("Error: %s", err)
}

app := cli.NewApp()

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

infile := filepath.Join(td, "input.txt")
if err := ioutil.WriteFile(infile, []byte("0xDEADBEEF"), 0644); err != nil {
t.Fatalf("Failed to write input file: %s", err)
}
if err := act.binaryCopy(ctx, infile, "bar", true); err != nil {
t.Fatalf("Failed to move file to store: %s", err)
}

outfile := filepath.Join(td, "output.txt")

// binary copy bar tempdir/bar
fs = flag.NewFlagSet("default", flag.ContinueOnError)
fs := flag.NewFlagSet("default", flag.ContinueOnError)
if err := fs.Parse([]string{"bar", outfile}); err != nil {
t.Fatalf("Error: %s", err)
}
c = cli.NewContext(app, fs, nil)
c := cli.NewContext(app, fs, nil)
if err := act.BinaryCopy(ctx, c); err != nil {
t.Errorf("Should not fail")
}
Expand All @@ -94,13 +162,47 @@ func TestBinary(t *testing.T) {
if err := act.BinaryMove(ctx, c); err != nil {
t.Errorf("Should not fail")
}
}

func TestBinarySum(t *testing.T) {
td, err := ioutil.TempDir("", "gopass-")
if err != nil {
t.Fatalf("Error: %s", err)
}
defer func() {
_ = os.RemoveAll(td)
}()

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = out.WithHidden(ctx, true)
act, err := newMock(ctx, td)
if err != nil {
t.Fatalf("Error: %s", err)
}

app := cli.NewApp()

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

infile := filepath.Join(td, "input.txt")
if err := ioutil.WriteFile(infile, []byte("0xDEADBEEF"), 0644); err != nil {
t.Fatalf("Failed to write input file: %s", err)
}
if err := act.binaryCopy(ctx, infile, "bar", true); err != nil {
t.Fatalf("Failed to move file to store: %s", err)
}

// binary sum bar
fs = flag.NewFlagSet("default", flag.ContinueOnError)
fs := flag.NewFlagSet("default", flag.ContinueOnError)
if err := fs.Parse([]string{"bar"}); err != nil {
t.Fatalf("Error: %s", err)
}
c = cli.NewContext(app, fs, nil)
c := cli.NewContext(app, fs, nil)
if err := act.BinarySum(ctx, c); err != nil {
t.Errorf("Should not fail")
}
Expand Down
38 changes: 36 additions & 2 deletions action/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ func TestConfig(t *testing.T) {
}
buf.Reset()

act.cfg.Mounts["foo"] = &config.StoreConfig{}

// action.printConfigValues
act.cfg.Mounts["foo"] = &config.StoreConfig{}
if err := act.printConfigValues(ctx, "", "nopager"); err != nil {
t.Errorf("Error: %s", err)
}
Expand All @@ -84,5 +83,40 @@ foo/nopager: false`
if sv != want {
t.Errorf("Wrong config result: '%s' != '%s'", sv, want)
}

delete(act.cfg.Mounts, "foo")
buf.Reset()

// config autoimport
fs := flag.NewFlagSet("default", flag.ContinueOnError)
if err := fs.Parse([]string{"autoimport"}); err != nil {
t.Fatalf("Error: %s", err)
}
c = cli.NewContext(app, fs, nil)
if err := act.Config(ctx, c); err != nil {
t.Errorf("Error: %s", err)
}
want = `autoimport: true`
sv = strings.TrimSpace(buf.String())
if sv != want {
t.Errorf("Wrong config result: '%s' != '%s'", sv, want)
}
buf.Reset()

// config autoimport false
fs = flag.NewFlagSet("default", flag.ContinueOnError)
if err := fs.Parse([]string{"autoimport", "false"}); err != nil {
t.Fatalf("Error: %s", err)
}
c = cli.NewContext(app, fs, nil)
if err := act.Config(ctx, c); err != nil {
t.Errorf("Error: %s", err)
}
want = `autoimport: false`
sv = strings.TrimSpace(buf.String())
if sv != want {
t.Errorf("Wrong config result: '%s' != '%s'", sv, want)
}
buf.Reset()

}
35 changes: 29 additions & 6 deletions action/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,43 @@ func TestCopy(t *testing.T) {
t.Fatalf("Error: %s", err)
}

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

app := cli.NewApp()
// copy foo bar
fs := flag.NewFlagSet("default", flag.ContinueOnError)
if err := fs.Parse([]string{"foo", "bar"}); err != nil {
t.Fatalf("Error: %s", err)
}
c := cli.NewContext(app, fs, nil)

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

if err := act.Copy(ctx, c); err != nil {
t.Errorf("Error: %s", err)
}
buf.Reset()

// copy not-found still-not-there
fs = flag.NewFlagSet("default", flag.ContinueOnError)
if err := fs.Parse([]string{"not-found", "still-not-there"}); err != nil {
t.Fatalf("Error: %s", err)
}
c = cli.NewContext(app, fs, nil)

if err := act.Copy(ctx, c); err == nil {
t.Errorf("Should fail")
}
buf.Reset()

// copy
fs = flag.NewFlagSet("default", flag.ContinueOnError)
c = cli.NewContext(app, fs, nil)

if err := act.Copy(ctx, c); err == nil {
t.Errorf("Should fail")
}
buf.Reset()
}
47 changes: 46 additions & 1 deletion action/create_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package action

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

"github.com/justwatchcom/gopass/utils/ctxutil"
"github.com/justwatchcom/gopass/utils/out"
"github.com/urfave/cli"
)

func TestExtractHostname(t *testing.T) {
for in, out := range map[string]string{
"": "",
"http://www.example.org/": "www.example.org",
"++#+++#jhlkadsrezu 33 553q ++++##$§&": "jhlkadsrezu_33_553q",
"www.example.org/?foo=bar#abc": "www.example.org",
Expand All @@ -13,3 +25,36 @@ func TestExtractHostname(t *testing.T) {
}
}
}

func TestCreate(t *testing.T) {
td, err := ioutil.TempDir("", "gopass-")
if err != nil {
t.Fatalf("Error: %s", err)
}
defer func() {
_ = os.RemoveAll(td)
}()

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
act, err := newMock(ctx, td)
if err != nil {
t.Fatalf("Error: %s", err)
}

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

app := cli.NewApp()
// create
fs := flag.NewFlagSet("default", flag.ContinueOnError)
c := cli.NewContext(app, fs, nil)

if err := act.Create(ctx, c); err == nil {
t.Errorf("Should fail")
}
buf.Reset()
}
Loading

0 comments on commit 38df5b2

Please sign in to comment.