Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions internal/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func stopAll(ctx context.Context, d Deps, reports []report.PortReport, flags Fla
Yes: flags.Yes,
Force: flags.Force,
DryRun: flags.DryRun,
Interactive: d.IsTTY && d.StdinIsTTY,
Interactive: d.StdinIsTTY && d.StderrIsTTY,
In: d.Stdin,
Out: d.Stderr,
}
Expand All @@ -104,7 +104,12 @@ func stopAll(ctx context.Context, d Deps, reports []report.PortReport, flags Fla
for _, r := range reports {
for _, f := range r.Findings {
if f.Explanation.Terminator == nil {
line(render.KillFailed, r.Port, "cannot stop it: the owning process is not visible; try again with sudo")
const hint = "cannot stop it: the owning process is not visible; try again with sudo"
if flags.DryRun {
_, _ = fmt.Fprintln(d.Stderr, render.KillLine(render.KillFailed, r.Port, hint, ropts))
continue
}
line(render.KillFailed, r.Port, hint)
failed = true
continue
}
Expand Down
51 changes: 46 additions & 5 deletions internal/cli/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,7 @@ func TestRun_KillWithoutTTYAndWithoutYesExit3(t *testing.T) {
func TestRun_KillNeedsStdinTTYToo(t *testing.T) {
ft := &fakeTerminator{describe: "x"}
d, _ := killDeps(ft, &freeingSockets{})
d.IsTTY = true
d.StdinIsTTY = false
d.StdinIsTTY, d.StderrIsTTY = false, true
d.Stdin = strings.NewReader("y\n")

if got := Run(context.Background(), d, []string{"3000"}, Flags{Kill: true}); got != 3 {
Expand All @@ -421,7 +420,7 @@ func TestRun_KillPromptAccepted(t *testing.T) {
c := &freeingSockets{}
ft := &fakeTerminator{describe: "x", onTerm: func() { c.freed = true }}
d, _ := killDeps(ft, c)
d.IsTTY, d.StdinIsTTY = true, true
d.StdinIsTTY, d.StderrIsTTY = true, true
d.Stdin = strings.NewReader("y\n")

if got := Run(context.Background(), d, []string{"3000"}, Flags{Kill: true}); got != 0 {
Expand All @@ -435,7 +434,7 @@ func TestRun_KillPromptAccepted(t *testing.T) {
func TestRun_KillDeclinedIsNotAFailure(t *testing.T) {
ft := &fakeTerminator{describe: "x"}
d, _ := killDeps(ft, &freeingSockets{})
d.IsTTY, d.StdinIsTTY = true, true
d.StdinIsTTY, d.StderrIsTTY = true, true
d.Stdin = strings.NewReader("n\n")

if got := Run(context.Background(), d, []string{"3000"}, Flags{Kill: true}); got != 0 {
Expand Down Expand Up @@ -640,7 +639,7 @@ func TestRun_KillReportsStatusLinesAndAResultBox(t *testing.T) {

func TestRun_KillDeclinedShowsLeftUntouched(t *testing.T) {
d, stderr := killDeps(&fakeTerminator{describe: "x"}, &freeingSockets{})
d.IsTTY, d.StdinIsTTY = true, true
d.StdinIsTTY, d.StderrIsTTY = true, true
d.Stdin = strings.NewReader("n\n")

Run(context.Background(), d, []string{"3000"}, Flags{Kill: true})
Expand Down Expand Up @@ -685,3 +684,45 @@ func TestRootCmd_ExpandedFlagIsParsed(t *testing.T) {
t.Errorf("--expanded not applied:\n%s", stdout.String())
}
}

func TestRun_KillNeedsStderrTTYForPrompt(t *testing.T) {
ft := &fakeTerminator{describe: "x"}
d, _ := killDeps(ft, &freeingSockets{})
d.IsTTY, d.StdinIsTTY, d.StderrIsTTY = true, true, false
d.Stdin = strings.NewReader("y\n")

if got := Run(context.Background(), d, []string{"3000"}, Flags{Kill: true}); got != 3 {
t.Errorf("exit = %d, want 3", got)
}
if ft.calls != 0 {
t.Errorf("Terminate calls = %d, want 0", ft.calls)
}
}

func TestRun_KillPromptDoesNotNeedStdoutTTY(t *testing.T) {
c := &freeingSockets{}
ft := &fakeTerminator{describe: "x", onTerm: func() { c.freed = true }}
d, _ := killDeps(ft, c)
d.IsTTY, d.StdinIsTTY, d.StderrIsTTY = false, true, true
d.Stdin = strings.NewReader("y\n")

if got := Run(context.Background(), d, []string{"3000"}, Flags{Kill: true}); got != 0 {
t.Errorf("exit = %d, want 0", got)
}
if ft.calls != 1 {
t.Errorf("Terminate calls = %d, want 1", ft.calls)
}
}

func TestRun_KillDryRunInvisibleOwnerIsAHintNotAFailure(t *testing.T) {
d, stderr := killDeps(nil, &freeingSockets{})

got := Run(context.Background(), d, []string{"3000"}, Flags{Kill: true, DryRun: true})

if got != 0 {
t.Errorf("exit = %d, want 0 (busy and explained, no kill failure)", got)
}
if !strings.Contains(stderr.String(), "not visible") {
t.Errorf("stderr = %q, want a hint about the invisible owner", stderr.String())
}
}
17 changes: 15 additions & 2 deletions internal/kill/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ func Run(ctx context.Context, t Terminator, port uint16, o Options, check PortCh
t = configure(t, o)

if o.DryRun {
_, err := fmt.Fprintf(o.Out, "Dry run, would execute: %s\n", t.Describe())
return err
return dryRun(ctx, t, o)
}
if p, ok := t.(preflighter); ok {
if err := p.preflight(ctx); err != nil {
Expand All @@ -122,6 +121,20 @@ func Run(ctx context.Context, t Terminator, port uint16, o Options, check PortCh
return verifyFree(ctx, port, o, check)
}

// dryRun reports what a real run would do without signalling. A protected
// target is named as such instead of "would execute", and is not an error:
// nothing is executed either way.
func dryRun(ctx context.Context, t Terminator, o Options) error {
if p, ok := t.(preflighter); ok {
if err := p.preflight(ctx); errors.Is(err, ErrProtected) {
_, werr := fmt.Fprintf(o.Out, "Dry run, would not execute: %v\n", err)
return werr
}
}
_, err := fmt.Fprintf(o.Out, "Dry run, would execute: %s\n", t.Describe())
return err
}

// verifyFree polls check until the port is free or the verify timeout passes.
func verifyFree(ctx context.Context, port uint16, o Options, check PortChecker) error {
if check == nil {
Expand Down
23 changes: 23 additions & 0 deletions internal/kill/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,26 @@ func TestProcessTerminator_HostileNameCannotSpoofPromptOrErrors(t *testing.T) {
err = Run(context.Background(), mk(4242, &fakeSignaler{}), 3000, withYes(o), freePort)
noControl("still running message", err.Error())
}

func TestRun_DryRunNamesProtectionReason(t *testing.T) {
for _, pid := range []int{1, 99} {
p := proc.Process{PID: pid, Name: "init", Started: started}
sig := &fakeSignaler{}
term := NewProcessTerminator(p, fakeReader{p: p}, 99, sig)
o, out := options("", false)
o.DryRun = true

if err := Run(context.Background(), term, 3000, o, busyPort); err != nil {
t.Fatalf("pid %d: err = %v, want nil", pid, err)
}
if strings.Contains(out.String(), "would execute") {
t.Errorf("pid %d: output %q claims it would execute", pid, out.String())
}
if !strings.Contains(out.String(), "refusing to stop a protected process") {
t.Errorf("pid %d: output %q does not name the protection", pid, out.String())
}
if len(sig.sent) != 0 {
t.Errorf("pid %d: signals sent: %v", pid, sig.sent)
}
}
}
Loading