Files
w3cjandClaude Opus 5.5 d58b24affe refactor(ui): prompts in one place: each question declares its answer
Every question is a msg.Question that declares what answers it where
devvm can't ask: AnsweredBy("--force") names a flag or command, which the
refusal names (no_input), and RefusedWith(msg.X(...)) gives a question no
flag answers its own refusal. msg.AnswerFor and its table go.

The Reporter's Ask, Confirm and Answer, and the UI's Secret, take a
Question and return a *msg.Refusal where they can't ask: with --no-input,
without a terminal, or at the end of the input, one refusal for all three
causes. Commands return it through the failure path (showFailure ->
UI.Refused: the question, then the refusal); nothing dies on an unanswered
question any more. Questions with a usual answer fall back on it instead:
network policy on, restart no, history import later, the Include line
warned, an unenforced VM stopped, the first run's size kept.

Gone: confirmed() in cli (4 callers), force_needed and no_input_terminal,
vm.Env.CanAsk and ui.CanAsk, vm's interactive, setup's terminal, the
Reporter's StdinIsTerminal/StdoutIsTerminal and the Fake's StdoutTerminal,
and every caller's terminal check (18 in vm, netpolicy, build and
sshconfig; 3 CanAsk; 1 interactive; 2 in cli). The picker's check
(CanPick) stays in the picker, which keeps its choices with --no-input
too; setup and the first run's agents and the new-VM summary still read
CanPick, because what they save or say depends on whether the picker
showed.

A Question's About context (the network policy's, a secret's howto, the
restart's changes, the size's host) is said only when it's asked.
devvm update asks before its tools change, so a refusal leaves the VM as
it is.

The CLI tests answer through a terminal (ttyin) or, explicitly, with a
test build's DEVVM_PIPED_ANSWERS=1, a line of input each.

Tests: ui's TestConfirm, TestAsk, TestAnswer, TestPromptsOnlyAtATerminal,
TestSecret, TestNoInput, TestNoTerminal, TestNoAnswer, TestPipedAnswers,
TestRefusedInterrupted, TestPickNoInput; msg's TestQuestionRefused,
TestQuestionAbout; cli's agreement test TestQuestionsDeclareAnswers (every
AnsweredBy names devvm's flags and commands, every RefusedWith a message,
no Question made otherwise) and TestCheckAnswer; vm's
TestSizeChoiceRefused; testscript prompts.txtar (each cause, a refusal of
its own, piped answers).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 11:49:57 -07:00

40 lines
1.6 KiB
Go

package msg
import "testing"
// A question's refusal names what answers it, after the question unless
// its prompt was shown; one RefusedWith made is its own refusal alone.
func TestQuestionRefused(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
q Question
shown bool
question string
refusal string
}{
{GCConfirm().AnsweredBy("--force"), false, "gc_confirm", "no_input answer=--force"},
{GCConfirm().AnsweredBy("--force"), true, "", "no_input answer=--force"},
{DeleteAnyway().RefusedWith(NoTerminalDelete("devvm rm")), false, "", "no_terminal_delete command=devvm rm"},
{DeleteAnyway().RefusedWith(NoTerminalDelete("devvm rm")), true, "", "no_terminal_delete command=devvm rm"},
} {
r := tc.q.Refused(tc.shown)
if r.Question.Names() != tc.question || r.Msg.Names() != tc.refusal || r.Error() != r.Msg.Error() {
t.Errorf("%s shown=%v: question %q, refusal %q; want %q, %q", tc.q.M().ID(), tc.shown, r.Question.Names(), r.Msg.Names(), tc.question, tc.refusal)
}
}
}
// About adds to what's said first, never to the question it came from.
func TestQuestionAbout(t *testing.T) {
t.Parallel()
q := Continue().AnsweredBy("--force").About(NoVM("/a"))
r := q.About(NoVM("/b"))
q = q.About(NoVM("/c"))
if len(r.Context()) != 2 || r.Context()[1].Names() != "no_vm dir=/b" || q.Context()[1].Names() != "no_vm dir=/c" {
t.Errorf("contexts %v and %v", r.Context(), q.Context())
}
if q.M().ID() != "continue" || q.Refused(true).Msg.Names() != "no_input answer=--force" {
t.Errorf("asks %q, refused with %q", q.M().ID(), q.Refused(true).Msg.Names())
}
}