Skip to content

Commit

Permalink
Improve test coverage.
Browse files Browse the repository at this point in the history
RELEASE_NOTES=n/a

Signed-off-by: Dominik Schulz <[email protected]>
  • Loading branch information
dominikschulz committed Dec 7, 2022
1 parent 4c4f821 commit 288e31f
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 10 deletions.
2 changes: 1 addition & 1 deletion fish.completion
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ complete -c $PROG -f -n '__fish_gopass_uses_command age identities -l noparsing
complete -c $PROG -f -n '__fish_gopass_uses_command age identities -l chars -d "Print specific characters from the secret"'
complete -c $PROG -f -n '__fish_gopass_uses_command age identities -l help -d "show help"'
complete -c $PROG -f -n '__fish_gopass_uses_command age identities -l version -d "print the version"'
complete -c $PROG -f -n '__fish_gopass_needs_command' -a alias -d 'Command: Manage domain aliases'
complete -c $PROG -f -n '__fish_gopass_needs_command' -a alias -d 'Command: Print domain aliases'
complete -c $PROG -f -n '__fish_gopass_needs_command' -a audit -d 'Command: Decrypt all secrets and scan for weak or leaked passwords'
complete -c $PROG -f -n '__fish_gopass_needs_command' -a cat -d 'Command: Decode and print content of a binary secret to stdout, or encode and insert from stdin'
complete -c $PROG -f -n '__fish_gopass_needs_command' -a clone -d 'Command: Clone a password store from a git repository'
Expand Down
10 changes: 8 additions & 2 deletions internal/cache/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ type OnDisk struct {
// NewOnDisk creates a new on disk cache.
func NewOnDisk(name string, ttl time.Duration) (*OnDisk, error) {
d := filepath.Join(appdir.UserCache(), "gopass", name)
debug.Log("New on disk cache %s created at %s", name, d)

return NewOnDiskWithDir(name, d, ttl)
}

// NewOnDiskWithDir creates a new on disk cache.
func NewOnDiskWithDir(name, dir string, ttl time.Duration) (*OnDisk, error) {
debug.Log("New on disk cache %s created at %s", name, dir)

o := &OnDisk{
ttl: ttl,
name: name,
dir: d,
dir: dir,
}

return o, o.ensureDir()
Expand Down
29 changes: 26 additions & 3 deletions internal/cache/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,38 @@ import (
"github.com/stretchr/testify/assert"
)

func TestOnDisk(t *testing.T) { //nolint:paralleltest
func TestOnDisk(t *testing.T) {
t.Parallel()

td := t.TempDir()
t.Setenv("GOPASS_HOMEDIR", td)

odc, err := NewOnDisk("test", time.Hour)
odc, err := NewOnDiskWithDir("test", td, time.Hour)
assert.NoError(t, err)

assert.NoError(t, odc.Set("foo", []string{"bar"}))
res, err := odc.Get("foo")
assert.NoError(t, err)
assert.Equal(t, []string{"bar"}, res)

assert.Error(t, odc.Remove("bar"))
assert.NoError(t, odc.Remove("foo"))
assert.NoError(t, odc.Purge())
}

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

td := t.TempDir()

odc, err := NewOnDiskWithDir("test", td, time.Second)
assert.NoError(t, err)
assert.NoError(t, odc.Set("foo", []string{"bar"}))
res, err := odc.Get("foo")
assert.NoError(t, err)
assert.Equal(t, []string{"bar"}, res)

time.Sleep(time.Second + 100*time.Millisecond)
res, err = odc.Get("foo")
assert.Error(t, err)
assert.NotEqual(t, []string{"bar"}, res)
}
35 changes: 35 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package config

import (
"context"
"testing"

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

func TestConfig(t *testing.T) { //nolint:paralleltest
td := t.TempDir()
t.Setenv("GOPASS_HOMEDIR", td)

// this will write to the tempdir
cfg := New()

assert.False(t, cfg.IsSet("core.string"))
assert.NoError(t, cfg.Set("", "core.string", "foo"))
assert.NoError(t, cfg.Set("", "core.bool", "true"))
assert.NoError(t, cfg.Set("", "core.int", "42"))

assert.Equal(t, "foo", cfg.Get("core.string"))
assert.Equal(t, true, cfg.GetBool("core.bool"))
assert.Equal(t, 42, cfg.GetInt("core.int"))

assert.NoError(t, cfg.SetEnv("env.string", "foo"))
assert.Equal(t, "foo", cfg.Get("env.string"))

assert.Equal(t, []string{"core.autosync", "core.bool", "core.cliptimeout", "core.exportkeys", "core.int", "core.notifications", "core.string", "env.string", "mounts.path"}, cfg.Keys(""))

ctx := cfg.WithConfig(context.Background())
assert.Equal(t, true, Bool(ctx, "core.bool"))
assert.Equal(t, "foo", String(ctx, "core.string"))
assert.Equal(t, 42, Int(ctx, "core.int"))
}
16 changes: 16 additions & 0 deletions internal/set/filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package set

import (
"testing"

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

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

in := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
out := Filter(in, 6, 7, 8, 9)

assert.Equal(t, []int{1, 2, 3, 4, 5}, out)
}
19 changes: 19 additions & 0 deletions internal/set/map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package set

import (
"testing"

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

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

assert.Equal(t, map[int]bool{1: true, 2: true, 3: true}, Map([]int{1, 2, 3}))
}

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

assert.Equal(t, []int{2, 3, 4}, Apply([]int{1, 2, 3}, func(i int) int { return i + 1 }))
}
2 changes: 2 additions & 0 deletions internal/set/sorted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ func TestSortedFiltered(t *testing.T) {
assert.Equal(t, want, SortedFiltered(in, func(i int) bool {
return i%2 == 0
}))

assert.Equal(t, []int{}, SortedFiltered([]int{}, func(i int) bool { return true }))
}
3 changes: 2 additions & 1 deletion pkg/gitconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type Config struct {
readonly bool // do not allow modifying values (even in memory)
noWrites bool // do not persist changes to disk (e.g. for tests)
raw strings.Builder
vars map[string]string
// TODO(#2457): support multi-vars
vars map[string]string
}

// Unset deletes a key.
Expand Down
6 changes: 3 additions & 3 deletions zsh.completion
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ WARNING: This will update the secret content to the latest format. This might be

;;
generate)
_arguments : "--clip[Copy the generated password to the clipboard]" "--print[Print the generated password to the terminal]" "--force[Force to overwrite existing password]" "--edit[Open secret for editing after generating a password]" "--symbols[Use symbols in the password]" "--generator[Choose a password generator, use one of: cryptic, memorable, xkcd or external. Default: cryptic]" "--strict[Require strict character class rules]" "--sep[Word separator for generated passwords. If no separator is specified, the words are combined without spaces/separator and the first character of words is capitalised.]" "--lang[Language to generate password from, currently only en (english, default) is supported]"
_arguments : "--clip[Copy the generated password to the clipboard]" "--print[Print the generated password to the terminal]" "--force[Force to overwrite existing password]" "--edit[Open secret for editing after generating a password]" "--symbols[Use symbols in the password]" "--generator[Choose a password generator, use one of: cryptic, memorable, xkcd or external. Default: cryptic]" "--strict[Require strict character class rules]" "--sep[Word separator for generated passwords. If no separator is specified, the words are combined without spaces/separator and the first character of words is capitalised.]" "--lang[Language to generate password from, currently only en (english, default) or de are supported]"
_gopass_complete_folders
_gopass_complete_passwords
;;
Expand Down Expand Up @@ -180,7 +180,7 @@ WARNING: This will update the secret content to the latest format. This might be

;;
pwgen)
_arguments : "--no-numerals[Do not include numerals in the generated passwords.]" "--no-capitalize[Do not include capital letter in the generated passwords.]" "--ambiguous[Do not include characters that could be easily confused with each other, like '1' and 'l' or '0' and 'O']" "--symbols[Include at least one symbol in the password.]" "--one-per-line[Print one password per line]" "--xkcd[Use multiple random english words combined to a password. By default, space is used as separator and all words are lowercase]" "--sep[Word separator for generated xkcd style password. If no separator is specified, the words are combined without spaces/separator and the first character of words is capitalised. This flag implies -xkcd]" "--lang[Language to generate password from, currently only en (english, default) is supported]"
_arguments : "--no-numerals[Do not include numerals in the generated passwords.]" "--no-capitalize[Do not include capital letter in the generated passwords.]" "--ambiguous[Do not include characters that could be easily confused with each other, like '1' and 'l' or '0' and 'O']" "--symbols[Include at least one symbol in the password.]" "--one-per-line[Print one password per line]" "--xkcd[Use multiple random english words combined to a password. By default, space is used as separator and all words are lowercase]" "--sep[Word separator for generated xkcd style password. If no separator is specified, the words are combined without spaces/separator and the first character of words is capitalised. This flag implies -xkcd]" "--lang[Language to generate password from, currently only en (english, default) or de are supported]"


;;
Expand Down Expand Up @@ -266,7 +266,7 @@ WARNING: This will update the secret content to the latest format. This might be
local -a subcommands
subcommands=(
"age:age commands"
"alias:Manage domain aliases"
"alias:Print domain aliases"
"audit:Decrypt all secrets and scan for weak or leaked passwords"
"cat:Decode and print content of a binary secret to stdout, or encode and insert from stdin"
"clone:Clone a password store from a git repository"
Expand Down

0 comments on commit 288e31f

Please sign in to comment.