fix(spec): isolate __complete probe from the controlling terminal (#111)

run the `__complete` probe subprocess in its own session so that a
cobra-incompatible binary that ignores `__complete` can't mess with the
user's controlling terminal.
tested on both macos and nixos.
encountered the problem while trying to use
[visidata](https://www.visidata.org/).

---------

Co-authored-by: shemishtamesh <shemishtamail@gmail.com>
This commit is contained in:
shemishtamesh
2026-08-03 17:05:47 +07:00
committed by GitHub
co-authored by shemishtamesh
parent d669e97423
commit 0b77c9334b
2 changed files with 48 additions and 1 deletions
+35
View File
@@ -1,7 +1,13 @@
package spec
import (
"context"
"errors"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
)
func TestParseCobraOutput_ValidCobra(t *testing.T) {
@@ -131,6 +137,35 @@ func TestLookup_CobraKubectl(t *testing.T) {
}
}
func TestNewProbeCmd_Setsid(t *testing.T) {
cmd := newProbeCmd(context.Background(), "true", nil)
if cmd.SysProcAttr == nil || !cmd.SysProcAttr.Setsid {
t.Fatalf("expected probe command to set Setsid: true, got %+v", cmd.SysProcAttr)
}
}
// TestNewProbeCmd_NoControllingTerminal writes a small script
// and runs it to check
// if newProbeCmd actually denies it access to tty
func TestNewProbeCmd_NoControllingTerminal(t *testing.T) {
dir := t.TempDir()
script := "#!/bin/sh\nexec 3<>/dev/tty"
binPath := filepath.Join(dir, "ttyprobe")
if err := os.WriteFile(binPath, []byte(script), 0o755); err != nil {
t.Fatalf("failed to write probe script: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
runErr := newProbeCmd(ctx, binPath, nil).Run()
var exitErr *exec.ExitError
if !errors.As(runErr, &exitErr) {
t.Fatalf("expected probe script to exit nonzero after failing to open /dev/tty, got %v", runErr)
}
}
func TestLookup_CobraGolangciLint(t *testing.T) {
results := Lookup("golangci-lint ")
if len(results) == 0 {