exec_windows_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2021 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build windows
  5. package exec_test
  6. import (
  7. "io"
  8. "os"
  9. "os/exec"
  10. "strconv"
  11. "syscall"
  12. "testing"
  13. )
  14. func TestPipePassing(t *testing.T) {
  15. r, w, err := os.Pipe()
  16. if err != nil {
  17. t.Error(err)
  18. }
  19. const marker = "arrakis, dune, desert planet"
  20. childProc := helperCommand(t, "pipehandle", strconv.FormatUint(uint64(w.Fd()), 16), marker)
  21. childProc.SysProcAttr = &syscall.SysProcAttr{AdditionalInheritedHandles: []syscall.Handle{syscall.Handle(w.Fd())}}
  22. err = childProc.Start()
  23. if err != nil {
  24. t.Error(err)
  25. }
  26. w.Close()
  27. response, err := io.ReadAll(r)
  28. if err != nil {
  29. t.Error(err)
  30. }
  31. r.Close()
  32. if string(response) != marker {
  33. t.Errorf("got %q; want %q", string(response), marker)
  34. }
  35. err = childProc.Wait()
  36. if err != nil {
  37. t.Error(err)
  38. }
  39. }
  40. func TestNoInheritHandles(t *testing.T) {
  41. cmd := exec.Command("cmd", "/c exit 88")
  42. cmd.SysProcAttr = &syscall.SysProcAttr{NoInheritHandles: true}
  43. err := cmd.Run()
  44. exitError, ok := err.(*exec.ExitError)
  45. if !ok {
  46. t.Fatalf("got error %v; want ExitError", err)
  47. }
  48. if exitError.ExitCode() != 88 {
  49. t.Fatalf("got exit code %d; want 88", exitError.ExitCode())
  50. }
  51. }