Exit 1 when a deletion prompt is not confirmed (closes #423)
check / check (push) Successful in 9m27s
check / check (push) Successful in 9m27s
`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
This commit is contained in:
1 parent
aae7fa17ae
commit
71a232d0f7
9 files changed
+94
-19
No files matched your search
@@ -0,0 +1,48 @@
|
||||
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())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user