Files
vaultik/internal/cli/database_test.go
T
sneak 1a972ee0a1
check / check (push) Successful in 10m0s
Exit 1 when a deletion prompt is not confirmed (closes #423)
`prune`, `snapshot purge`, `snapshot remove` and `database delete`
printed "Cancelled" or "Aborted." and exited 0 when the prompt was
answered no or stdin ended. They now return vaultik.ErrNotConfirmed,
so the command reports "deletion not confirmed" and exits 1.

`database delete` reads its answer from the command's stdin instead of
os.Stdin, so a test can supply it.

Model: opus-5-5
2026-11-11 02:32:35 +00:00

49 lines
1.1 KiB
Go

package cli //nolint:testpackage // sets the unexported rootFlags directly
import (
"bytes"
"errors"
"io"
"os"
"strings"
"testing"
"sneak.berlin/go/vaultik/internal/vaultik"
)
// TestDatabaseDeleteDeclinedIsError checks that answering anything but
// "yes" keeps the database and returns ErrNotConfirmed, so the command
// exits non-zero.
//
//nolint:paralleltest // mutates the process-global rootFlags
func TestDatabaseDeleteDeclinedIsError(t *testing.T) {
dir := t.TempDir()
dbPath, cfgPath := seedDatabaseDeleteConfig(t, dir)
setRootFlags(t, RootFlags{ConfigPath: cfgPath})
var out bytes.Buffer
cmd := newDatabaseDeleteCommand()
cmd.SetOut(&out)
cmd.SetErr(io.Discard)
cmd.SetIn(strings.NewReader("no\n"))
cmd.SetArgs(nil)
err := cmd.Execute()
if !errors.Is(err, vaultik.ErrNotConfirmed) {
t.Errorf("declined database delete returned %v, want %v",
err, vaultik.ErrNotConfirmed)
}
_, err = os.Stat(dbPath)
if err != nil {
t.Errorf("declined database delete removed the file: %v", err)
}
if !strings.Contains(out.String(), "Aborted.") {
t.Errorf("declined database delete printed %q, want Aborted.",
out.String())
}
}