check / check (push) Waiting to run
An empty currentvault, or one naming a vault that does not exist, made `secret list` report an empty vault with exit 0 and `secret add` fail on a missing directory. ResolveVaultSymlink now refuses an empty or invalid name, or one with no directory under vaults.d, with an error that wraps ErrVaultNotFound, names the file and says to run `secret vault select <name>`. Every command that uses the current vault gets it; `secret vault list` and `secret info` still run. newSizeTestVault wrote a full path into currentvault, which resolved only because the last path element was taken as the name; it now relies on CreateVault selecting the vault. Model: opus-5-5
61 lines
1.9 KiB
Go
61 lines
1.9 KiB
Go
package cli_test
|
|
|
|
import (
|
|
"bytes"
|
|
"path/filepath"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/spf13/afero"
|
|
"github.com/spf13/cobra"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"sneak.berlin/go/secret/internal/cli"
|
|
"sneak.berlin/go/secret/internal/secret"
|
|
"sneak.berlin/go/secret/internal/vault"
|
|
)
|
|
|
|
// TestListFailsWhenCurrentVaultDoesNotExist is a regression test for
|
|
// https://git.eeqj.de/sneak/secret/issues/156, where a currentvault file
|
|
// that was empty, held an invalid vault name, or named a vault that does
|
|
// not exist, made `secret list` report an empty vault. Listing must fail
|
|
// with an error that names the file and says to run `secret vault select`,
|
|
// `secret vault list` must still list the vaults, and selecting a vault
|
|
// must make listing work again. The name "." would make vaults.d itself the
|
|
// vault, and "../vaults.d/work" would reach the vault work by a path.
|
|
func TestListFailsWhenCurrentVaultDoesNotExist(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, name := range []string{"", ".", "../vaults.d/work", "gone"} {
|
|
t.Run(strconv.Quote(name), func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fs := newTwoVaultFs(t)
|
|
currentVaultPath := filepath.Join(testStateDir, "currentvault")
|
|
require.NoError(t, afero.WriteFile(fs, currentVaultPath,
|
|
[]byte(name), secret.FilePerms))
|
|
|
|
c := cli.NewCLIInstanceWithStateDir(fs, testStateDir)
|
|
|
|
var out bytes.Buffer
|
|
|
|
cmd := &cobra.Command{}
|
|
cmd.SetOut(&out)
|
|
|
|
err := c.ListSecrets(cmd, false, true, "")
|
|
require.ErrorIs(t, err, vault.ErrVaultNotFound)
|
|
require.ErrorContains(t, err, currentVaultPath)
|
|
require.ErrorContains(t, err, "secret vault select")
|
|
assert.Empty(t, out.String())
|
|
|
|
require.NoError(t, c.ListVaults(cmd, false))
|
|
assert.Equal(t, "Available vaults:\n default\n work\n", out.String())
|
|
|
|
require.NoError(t, c.SelectVault(cmd, "default"))
|
|
out.Reset()
|
|
require.NoError(t, c.ListSecrets(cmd, false, true, ""))
|
|
assert.Equal(t, "x\n", out.String())
|
|
})
|
|
}
|
|
}
|