| 1 | //go:build windows |
| 2 | |
| 3 | package proc |
| 4 | |
| 5 | import ( |
| 6 | "os/exec" |
| 7 | "strings" |
| 8 | "syscall" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestHideWindowSetsCreateNoWindow(t *testing.T) { |
| 13 | cmd := exec.Command("cmd", "/c", "echo", "hi") |
| 14 | HideWindow(cmd) |
| 15 | if cmd.SysProcAttr == nil { |
| 16 | t.Fatal("SysProcAttr is nil; HideWindow did not set it") |
| 17 | } |
| 18 | if cmd.SysProcAttr.CreationFlags&createNoWindow == 0 { |
| 19 | t.Fatalf("CREATE_NO_WINDOW not set; CreationFlags=%#x", cmd.SysProcAttr.CreationFlags) |
| 20 | } |
| 21 | const detachedProcess = 0x00000008 |
| 22 | if cmd.SysProcAttr.CreationFlags&detachedProcess != 0 { |
| 23 | t.Fatalf("DETACHED_PROCESS should not be set by HideWindow; CreationFlags=%#x", cmd.SysProcAttr.CreationFlags) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestHideWindowPreservesExistingFlags(t *testing.T) { |
| 28 | const createNewProcessGroup = 0x00000200 |
| 29 | cmd := exec.Command("cmd", "/c", "echo", "hi") |
| 30 | cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: createNewProcessGroup} |
| 31 | HideWindow(cmd) |
| 32 | if cmd.SysProcAttr.CreationFlags&createNewProcessGroup == 0 { |
| 33 | t.Fatal("HideWindow clobbered a pre-existing creation flag") |
| 34 | } |
| 35 | if cmd.SysProcAttr.CreationFlags&createNoWindow == 0 { |
| 36 | t.Fatal("HideWindow did not add CREATE_NO_WINDOW") |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestHideWindowPreservesStdoutCapture(t *testing.T) { |
| 41 | cmd := exec.Command("cmd", "/c", "echo", "reasonix-ok") |
| 42 | HideWindow(cmd) |
| 43 | out, err := cmd.Output() |
| 44 | if err != nil { |
| 45 | t.Fatalf("command failed: %v", err) |
| 46 | } |
| 47 | if !strings.Contains(string(out), "reasonix-ok") { |
| 48 | t.Fatalf("output = %q, want it to contain reasonix-ok", out) |
| 49 | } |
| 50 | } |
| 51 |