Files
gopass-mirror/internal/action/completion_test.go
Dominik Schulz 263b78119b [bugfix] Fix writes to global config from tests (#2727)
* [bugfix] Fix writes to global config from tests

Fixes #2725

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* Shorten readonly config creation.

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* Address review comments

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

---------

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
2023-12-01 14:04:17 +01:00

104 lines
2.5 KiB
Go

package action
import (
"bytes"
"os"
"testing"
"github.com/gopasspw/gopass/internal/config"
"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"
"github.com/urfave/cli/v2"
)
func TestBashEscape(t *testing.T) {
t.Run("bash escape", func(t *testing.T) {
expected := `a\\<\\>\\|\\\\and\\ sometimes\\?\\*\\(\\)\\&\\;\\#`
if escaped := bashEscape(`a<>|\and sometimes?*()&;#`); escaped != expected {
t.Errorf("Expected %q, but got %q", expected, escaped)
}
})
t.Run("bash escape single quote", func(t *testing.T) {
expected := `good\\ ol\'\\ days`
if escaped := bashEscape(`good ol' days`); escaped != expected {
t.Errorf("Expected %q, but got %q", expected, escaped)
}
})
t.Run("bash escape double quote", func(t *testing.T) {
expected := `my\\ \\\"bad\\\"\\ password`
if escaped := bashEscape(`my "bad" password`); escaped != expected {
t.Errorf("Expected %q, but got %q", expected, escaped)
}
})
}
func TestComplete(t *testing.T) {
u := gptest.NewUnitTester(t)
buf := &bytes.Buffer{}
out.Stdout = buf
stdout = buf
defer func() {
out.Stdout = os.Stdout
stdout = os.Stdout
}()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithInteractive(ctx, false)
act, err := newMock(ctx, u.StoreDir(""))
require.NoError(t, err)
require.NotNil(t, act)
ctx = act.cfg.WithConfig(ctx)
app := cli.NewApp()
app.Commands = []*cli.Command{
{
Name: "test",
Aliases: []string{"foo", "bar"},
},
}
t.Run("complete foo", func(t *testing.T) {
defer buf.Reset()
act.Complete(gptest.CliCtx(ctx, t))
assert.Equal(t, "foo\n", buf.String())
})
t.Run("bash completion", func(t *testing.T) {
defer buf.Reset()
require.NoError(t, act.CompletionBash(nil))
assert.Contains(t, buf.String(), "action.test")
})
t.Run("fish completion", func(t *testing.T) {
defer buf.Reset()
require.NoError(t, act.CompletionFish(app))
assert.Contains(t, buf.String(), "action.test")
require.Error(t, act.CompletionFish(nil))
})
t.Run("zsh completion", func(t *testing.T) {
defer buf.Reset()
require.NoError(t, act.CompletionZSH(app))
assert.Contains(t, buf.String(), "action.test")
require.Error(t, act.CompletionZSH(nil))
})
t.Run("openbsdksh completion", func(t *testing.T) {
defer buf.Reset()
require.NoError(t, act.CompletionOpenBSDKsh(app))
assert.Contains(t, buf.String(), "complete_gopass")
require.Error(t, act.CompletionOpenBSDKsh(nil))
})
}