github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/internal/testenv/exec.go (about)

     1  // Copyright 2015 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  package testenv
     6  
     7  import (
     8  	"github.com/shogo82148/std/context"
     9  	"github.com/shogo82148/std/os/exec"
    10  	"github.com/shogo82148/std/testing"
    11  )
    12  
    13  // MustHaveExec checks that the current system can start new processes
    14  // using os.StartProcess or (more commonly) exec.Command.
    15  // If not, MustHaveExec calls t.Skip with an explanation.
    16  //
    17  // On some platforms MustHaveExec checks for exec support by re-executing the
    18  // current executable, which must be a binary built by 'go test'.
    19  // We intentionally do not provide a HasExec function because of the risk of
    20  // inappropriate recursion in TestMain functions.
    21  //
    22  // To check for exec support outside of a test, just try to exec the command.
    23  // If exec is not supported, testenv.SyscallIsNotSupported will return true
    24  // for the resulting error.
    25  func MustHaveExec(t testing.TB)
    26  
    27  // MustHaveExecPath checks that the current system can start the named executable
    28  // using os.StartProcess or (more commonly) exec.Command.
    29  // If not, MustHaveExecPath calls t.Skip with an explanation.
    30  func MustHaveExecPath(t testing.TB, path string)
    31  
    32  // CleanCmdEnv will fill cmd.Env with the environment, excluding certain
    33  // variables that could modify the behavior of the Go tools such as
    34  // GODEBUG and GOTRACEBACK.
    35  //
    36  // If the caller wants to set cmd.Dir, set it before calling this function,
    37  // so PWD will be set correctly in the environment.
    38  func CleanCmdEnv(cmd *exec.Cmd) *exec.Cmd
    39  
    40  // CommandContext is like exec.CommandContext, but:
    41  //   - skips t if the platform does not support os/exec,
    42  //   - sends SIGQUIT (if supported by the platform) instead of SIGKILL
    43  //     in its Cancel function
    44  //   - if the test has a deadline, adds a Context timeout and WaitDelay
    45  //     for an arbitrary grace period before the test's deadline expires,
    46  //   - fails the test if the command does not complete before the test's deadline, and
    47  //   - sets a Cleanup function that verifies that the test did not leak a subprocess.
    48  func CommandContext(t testing.TB, ctx context.Context, name string, args ...string) *exec.Cmd
    49  
    50  // Command is like exec.Command, but applies the same changes as
    51  // testenv.CommandContext (with a default Context).
    52  func Command(t testing.TB, name string, args ...string) *exec.Cmd