Skip to content

Commit

Permalink
Improve test coverage (gopasspw#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikschulz authored Dec 27, 2017
1 parent 38df5b2 commit c7938fc
Show file tree
Hide file tree
Showing 8 changed files with 362 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FIRST_GOPATH := $(firstword $(subst :, ,$(GOPATH)))
PKGS := $(shell go list ./... | grep -v /tests)
GOFILES_NOVENDOR := $(shell find . -type f -name '*.go' -not -path "./vendor/*")
GOFILES_NOTEST := $(shell find . -type f -name '*.go' -not -path "./vendor/*" -not -name "*_test.go")
GOPASS_VERSION ?= $(shell cat VERSION)
GOPASS_OUTPUT ?= gopass
GOPASS_REVISION := $(shell cat COMMIT 2>/dev/null || git rev-parse --short=8 HEAD)
Expand Down Expand Up @@ -131,7 +132,7 @@ codequality:
@which gocyclo > /dev/null; if [ $$? -ne 0 ]; then \
$(GO) get -u github.com/fzipp/gocyclo; \
fi
@$(foreach gofile, $(GOFILES_NOVENDOR),\
@$(foreach gofile, $(GOFILES_NOTEST),\
gocyclo -over 15 $(gofile) || exit 1;)
@$(call ok)

Expand Down
55 changes: 45 additions & 10 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"os"
"path/filepath"
"testing"
)
Expand All @@ -11,15 +12,49 @@ func TestHomedir(t *testing.T) {
}
}

func TestPwStoreDir(t *testing.T) {
for in, out := range map[string]string{
"": filepath.Join(Homedir(), ".password-store"),
"work": filepath.Join(Homedir(), ".password-store-work"),
filepath.Join("foo", "bar"): filepath.Join(Homedir(), ".password-store-foo-bar"),
} {
got := PwStoreDir(in)
if got != out {
t.Errorf("Mismatch for %s: %s != %s", in, got, out)
}
func TestNewConfig(t *testing.T) {
if err := os.Setenv("GOPASS_CONFIG", filepath.Join(os.TempDir(), ".gopass.yml")); err != nil {
t.Fatalf("Failed to set GOPASS_CONFIG: %s", err)
}

cfg := New()
if cfg.Root.AskForMore {
t.Errorf("AskForMore should be false")
}
}

func TestSetConfigValue(t *testing.T) {
if err := os.Setenv("GOPASS_CONFIG", filepath.Join(os.TempDir(), ".gopass.yml")); err != nil {
t.Fatalf("Failed to set GOPASS_CONFIG: %s", err)
}

cfg := New()
if err := cfg.SetConfigValue("", "autosync", "false"); err != nil {
t.Errorf("Error: %s", err)
}
if err := cfg.SetConfigValue("", "askformore", "true"); err != nil {
t.Errorf("Error: %s", err)
}
if err := cfg.SetConfigValue("", "askformore", "yo"); err == nil {
t.Errorf("Should fail")
}
if err := cfg.SetConfigValue("", "cliptimeout", "900"); err != nil {
t.Errorf("Error: %s", err)
}
if err := cfg.SetConfigValue("", "path", "/tmp"); err != nil {
t.Errorf("Error: %s", err)
}
cfg.Mounts["foo"] = &StoreConfig{}
if err := cfg.SetConfigValue("foo", "autosync", "true"); err != nil {
t.Errorf("Error: %s", err)
}
if err := cfg.SetConfigValue("foo", "askformore", "true"); err != nil {
t.Errorf("Error: %s", err)
}
if err := cfg.SetConfigValue("foo", "askformore", "yo"); err == nil {
t.Errorf("Should fail")
}
if err := cfg.SetConfigValue("foo", "cliptimeout", "900"); err != nil {
t.Errorf("Error: %s", err)
}
}
141 changes: 140 additions & 1 deletion config/io_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package config

import "testing"
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

"github.com/fatih/color"
)

func TestConfigs(t *testing.T) {
for _, cfg := range []string{
Expand Down Expand Up @@ -103,3 +114,131 @@ version: "1.0.0"`,
}
}
}

const testConfig = `root:
askformore: true
autoimport: true
autosync: true
cliptimeout: 5
noconfirm: true
nopager: true
path: /home/johndoe/.password-store
safecontent: true
mounts:
foo/sub:
askformore: false
autoimport: false
autosync: false
cliptimeout: 45
noconfirm: false
nopager: false
path: /home/johndoe/.password-store-foo-sub
safecontent: false
work:
askformore: false
autoimport: false
autosync: false
cliptimeout: 45
noconfirm: false
nopager: false
path: /home/johndoe/.password-store-work
safecontent: false
version: 1.4.0`

func TestLoad(t *testing.T) {
gcfg := filepath.Join(os.TempDir(), ".gopass.yml")
if err := os.Setenv("GOPASS_CONFIG", gcfg); err != nil {
t.Fatalf("Failed to set GOPASS_CONFIG: %s", err)
}

if err := ioutil.WriteFile(gcfg, []byte(testConfig), 0600); err != nil {
t.Fatalf("Failed to write config %s: %s", gcfg, err)
}

cfg := Load()
if !cfg.Root.SafeContent {
t.Errorf("SafeContent should be true")
}
}

func TestLoadError(t *testing.T) {
gcfg := filepath.Join(os.TempDir(), ".gopass-err.yml")
if err := os.Setenv("GOPASS_CONFIG", gcfg); err != nil {
t.Fatalf("Failed to set GOPASS_CONFIG: %s", err)
}

_ = os.Remove(gcfg)
if err := ioutil.WriteFile(gcfg, []byte(testConfig), 0000); err != nil {
t.Fatalf("Failed to write config %s: %s", gcfg, err)
}

capture(t, func() error {
_, err := load(gcfg)
if err == nil {
return fmt.Errorf("Should fail")
}
return nil
})

_ = os.Remove(gcfg)
cfg, err := load(gcfg)
if err == nil {
t.Errorf("Should fail")
}
gcfg = filepath.Join(os.TempDir(), "foo", ".gopass.yml")
if err := os.Setenv("GOPASS_CONFIG", gcfg); err != nil {
t.Fatalf("Failed to set GOPASS_CONFIG: %s", err)
}
if err := cfg.Save(); err != nil {
t.Errorf("Error: %s", err)
}
}

func TestDecodeError(t *testing.T) {
gcfg := filepath.Join(os.TempDir(), ".gopass-err2.yml")
if err := os.Setenv("GOPASS_CONFIG", gcfg); err != nil {
t.Fatalf("Failed to set GOPASS_CONFIG: %s", err)
}

_ = os.Remove(gcfg)
if err := ioutil.WriteFile(gcfg, []byte(testConfig+"\nfoobar: zab\n"), 0600); err != nil {
t.Fatalf("Failed to write config %s: %s", gcfg, err)
}

capture(t, func() error {
_, err := load(gcfg)
if err == nil {
return fmt.Errorf("Should fail")
}
return nil
})
}

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

done := make(chan string)
go func() {
buf := &bytes.Buffer{}
_, _ = io.Copy(buf, r)
done <- buf.String()
}()

err := fn()
// back to normal
_ = w.Close()
os.Stdout = old
color.NoColor = oldcol
if err != nil {
t.Errorf("Error: %s", err)
}
out := <-done
return strings.TrimSpace(out)
}
95 changes: 95 additions & 0 deletions config/location_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package config

import (
"os"
"path/filepath"
"testing"
)

func TestPwStoreDirNoEnv(t *testing.T) {
for in, out := range map[string]string{
"": filepath.Join(Homedir(), ".password-store"),
"work": filepath.Join(Homedir(), ".password-store-work"),
filepath.Join("foo", "bar"): filepath.Join(Homedir(), ".password-store-foo-bar"),
} {
got := PwStoreDir(in)
if got != out {
t.Errorf("Mismatch for %s: %s != %s", in, got, out)
}
}
}

func TestPwStoreDir(t *testing.T) {
gph := filepath.Join(os.TempDir(), "home")
_ = os.Setenv("GOPASS_HOMEDIR", gph)

if d := PwStoreDir(""); d != filepath.Join(gph, ".password-store") {
t.Errorf("Wrong dir: %s", d)
}
if d := PwStoreDir("foo"); d != filepath.Join(gph, ".password-store-foo") {
t.Errorf("Wrong dir: %s", d)
}

psd := filepath.Join(gph, ".password-store-test")
_ = os.Setenv("PASSWORD_STORE_DIR", psd)

if d := PwStoreDir(""); d != psd {
t.Errorf("Wrong dir: %s", d)
}
if d := PwStoreDir("foo"); d != filepath.Join(gph, ".password-store-foo") {
t.Errorf("Wrong dir: %s", d)
}
}

func TestConfigLocation(t *testing.T) {
evs := map[string]struct {
ev string
loc string
}{
"GOPASS_CONFIG": {ev: filepath.Join(os.TempDir(), "gopass.yml"), loc: filepath.Join(os.TempDir(), "gopass.yml")},
"XDG_CONFIG_HOME": {ev: filepath.Join(os.TempDir(), "xdg"), loc: filepath.Join(os.TempDir(), "xdg", "gopass", "config.yml")},
"GOPASS_HOMEDIR": {ev: filepath.Join(os.TempDir(), "home"), loc: filepath.Join(os.TempDir(), "home", ".config", "gopass", "config.yml")},
}
for k := range evs {
_ = os.Unsetenv(k)
}
for k, v := range evs {
_ = os.Setenv(k, v.ev)
loc := configLocation()
t.Logf("%s = %s -> %s", k, v.ev, loc)
if loc != v.loc {
t.Errorf("'%s' != '%s'", loc, v.loc)
}
_ = os.Unsetenv(k)
}
}

func TestConfigLocations(t *testing.T) {
gpcfg := filepath.Join(os.TempDir(), "config", ".gopass.yml")
_ = os.Setenv("GOPASS_CONFIG", gpcfg)
xdghome := filepath.Join(os.TempDir(), "xdg")
_ = os.Setenv("XDG_CONFIG_HOME", xdghome)
gphome := filepath.Join(os.TempDir(), "home")
_ = os.Setenv("GOPASS_HOMEDIR", gphome)

locs := configLocations()
t.Logf("Locations: %+v", locs)
if len(locs) != 4 {
t.Errorf("Expects 4 locations not %d", len(locs))
}
if locs[0] != gpcfg {
t.Errorf("'%s' != '%s'", locs[0], gpcfg)
}
xdgcfg := filepath.Join(xdghome, "gopass", "config.yml")
if locs[1] != xdgcfg {
t.Errorf("'%s' != '%s'", locs[1], xdgcfg)
}
curcfg := filepath.Join(gphome, ".config", "gopass", "config.yml")
if locs[2] != curcfg {
t.Errorf("'%s' != '%s'", locs[2], curcfg)
}
oldcfg := filepath.Join(gphome, ".gopass.yml")
if locs[3] != oldcfg {
t.Errorf("'%s' != '%s'", locs[3], oldcfg)
}
}
2 changes: 1 addition & 1 deletion config/store_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (c *StoreConfig) SetConfigValue(key, value string) error {
} else if value == "false" {
f.SetBool(false)
} else {
return errors.Errorf("No a bool: %s", value)
return errors.Errorf("not a bool: %s", value)
}
case reflect.Int:
iv, err := strconv.Atoi(value)
Expand Down
12 changes: 12 additions & 0 deletions config/store_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package config

import "testing"

func TestStoreConfigMap(t *testing.T) {
sc := &StoreConfig{}
scm := sc.ConfigMap()
t.Logf("map: %+v", scm)
if scm["nopager"] != "false" {
t.Errorf("nopager should be false")
}
}
Loading

0 comments on commit c7938fc

Please sign in to comment.