github.com/comwrg/go/src@v0.0.0-20220319063731-c238d0440370/os/exec/exec_windows_test.go (about)

     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  
     5  //go:build windows
     6  // +build windows
     7  
     8  package exec_test
     9  
    10  import (
    11  	"io"
    12  	"os"
    13  	"os/exec"
    14  	"strconv"
    15  	"syscall"
    16  	"testing"
    17  )
    18  
    19  func TestPipePassing(t *testing.T) {
    20  	r, w, err := os.Pipe()
    21  	if err != nil {
    22  		t.Error(err)
    23  	}
    24  	const marker = "arrakis, dune, desert planet"
    25  	childProc := helperCommand(t, "pipehandle", strconv.FormatUint(uint64(w.Fd()), 16), marker)
    26  	childProc.SysProcAttr = &syscall.SysProcAttr{AdditionalInheritedHandles: []syscall.Handle{syscall.Handle(w.Fd())}}
    27  	err = childProc.Start()
    28  	if err != nil {
    29  		t.Error(err)
    30  	}
    31  	w.Close()
    32  	response, err := io.ReadAll(r)
    33  	if err != nil {
    34  		t.Error(err)
    35  	}
    36  	r.Close()
    37  	if string(response) != marker {
    38  		t.Errorf("got %q; want %q", string(response), marker)
    39  	}
    40  	err = childProc.Wait()
    41  	if err != nil {
    42  		t.Error(err)
    43  	}
    44  }
    45  
    46  func TestNoInheritHandles(t *testing.T) {
    47  	cmd := exec.Command("cmd", "/c exit 88")
    48  	cmd.SysProcAttr = &syscall.SysProcAttr{NoInheritHandles: true}
    49  	err := cmd.Run()
    50  	exitError, ok := err.(*exec.ExitError)
    51  	if !ok {
    52  		t.Fatalf("got error %v; want ExitError", err)
    53  	}
    54  	if exitError.ExitCode() != 88 {
    55  		t.Fatalf("got exit code %d; want 88", exitError.ExitCode())
    56  	}
    57  }