scripttest

Imports

Imports #

"bytes"
"cmd/internal/script"
"internal/diff"
"internal/testenv"
"os"
"path/filepath"
"strings"
"testing"
"text/template"
"bytes"
"cmd/internal/script"
"context"
"fmt"
"internal/testenv"
"internal/txtar"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"bufio"
"cmd/internal/pathcache"
"cmd/internal/script"
"errors"
"io"
"strings"
"testing"
"io"
"os"
"path/filepath"
"runtime"
"testing"
"cmd/internal/script"
"fmt"
"internal/buildcfg"
"internal/platform"
"internal/testenv"
"runtime"
"strings"
"testing"

Constants & Variables

readmeTmpl const #

const readmeTmpl = `
This file is generated by 'go generate'. DO NOT EDIT.

This directory holds test scripts *.txt run during 'go test cmd/'.
To run a specific script foo.txt

	go test cmd/ -run=Script/^foo$

In general script files should have short names: a few words,
 not whole sentences.
The first word should be the general category of behavior being tested,
often the name of a go subcommand (build, link, compile, ...) or concept (vendor, pattern).

Each script is a text archive (go doc internal/txtar).
The script begins with an actual command script to run
followed by the content of zero or more supporting files to
create in the script's temporary file system before it starts executing.

As an example, run_hello.txt says:

	# hello world
	go run hello.go
	stderr 'hello world'
	! stdout .

	-- hello.go --
	package main
	func main() { println("hello world") }

Each script runs in a fresh temporary work directory tree, available to scripts as $WORK.
Scripts also have access to other environment variables, including:

	GOARCH=
	GOOS=
	TMPDIR=$WORK/tmp
	devnull=
	goversion=

On Plan 9, the variables $path and $home are set instead of $PATH and $HOME.
On Windows, the variables $USERPROFILE and $TMP are set instead of
$HOME and $TMPDIR.

The lines at the top of the script are a sequence of commands to be executed by
a small script engine configured in .../cmd/internal/script/scripttest/run.go (not the system shell).

{{.Language}}

When TestScript runs a script and the script fails, by default TestScript shows
the execution of the most recent phase of the script (since the last # comment)
and only shows the # comments for earlier phases.

Note also that in reported output, the actual name of the per-script temporary directory
has been consistently replaced with the literal string $WORK.

The available commands are:
{{.Commands}}

The available conditions are:
{{.Conditions}}
`

Structs

ToolReplacement struct #

ToolReplacement records the name of a tool to replace within a given GOROOT for script testing purposes.

type ToolReplacement struct {
ToolName string
ReplacementPath string
EnvVar string
}

skipError struct #

type skipError struct {
msg string
}

Functions

AddToolChainScriptConditions function #

AddToolChainScriptConditions accepts a [script.Cond] map and adds into it a set of commonly used conditions for doing toolchains testing, including whether the platform supports cgo, a buildmode condition, support for GOEXPERIMENT testing, etc. Callers must also pass in current GOHOSTOOS/GOHOSTARCH settings, since some of the conditions introduced can be influenced by them.

func AddToolChainScriptConditions(t *testing.T, conds map[string]script.Cond, goHostOS string, goHostArch string)

CachedExec function #

CachedExec returns a Condition that reports whether the PATH of the test binary itself (not the script's current environment) contains the named executable.

func CachedExec() script.Cond

DefaultCmds function #

DefaultCmds returns a set of broadly useful script commands. This set includes all of the commands in script.DefaultCmds, as well as a "skip" command that halts the script and causes the testing.TB passed to Run to be skipped.

func DefaultCmds() map[string]script.Cmd

DefaultConds function #

DefaultConds returns a set of broadly useful script conditions. This set includes all of the conditions in script.DefaultConds, as well as: - Conditions of the form "exec:foo" are active when the executable "foo" is found in the test process's PATH, and inactive when the executable is not found. - "short" is active when testing.Short() is true. - "verbose" is active when testing.Verbose() is true.

func DefaultConds() map[string]script.Cond

Error method #

func (s skipError) Error() string

ReplaceGoToolInTestGoRoot function #

ReplaceGoToolInTestGoRoot replaces the go tool binary toolname with an alternate executable newtoolpath within a test GOROOT directory previously created by SetupTestGoRoot.

func ReplaceGoToolInTestGoRoot(t *testing.T, testgoroot string, toolname string, newtoolpath string)

Run function #

Run runs the script from the given filename starting at the given initial state. When the script completes, Run closes the state.

func Run(t testing.TB, e *script.Engine, s *script.State, filename string, testScript io.Reader)

RunTests function #

RunTests kicks off one or more script-based tests using the specified engine, running all test files that match pattern. This function adapted from Russ's rsc.io/script/scripttest#Run function, which was in turn forked off cmd/go's runner.

func RunTests(t *testing.T, ctx context.Context, engine *script.Engine, env []string, pattern string)

RunToolScriptTest function #

RunToolScriptTest kicks off a set of script tests runs for a tool of some sort (compiler, linker, etc). The expectation is that we'll be called from the top level cmd/X dir for tool X, and that instead of executing the install tool X we'll use the test binary instead.

func RunToolScriptTest(t *testing.T, repls []ToolReplacement, scriptsdir string, fixReadme bool)

SetupTestGoRoot function #

SetupTestGoRoot sets up a temporary GOROOT for use with script test execution. It copies the existing goroot bin and pkg dirs using symlinks (if possible) or raw copying. Return value is the path to the newly created testgoroot dir.

func SetupTestGoRoot(t *testing.T, tmpdir string, goroot string) string

Skip function #

Skip returns a sentinel error that causes Run to mark the test as skipped.

func Skip() script.Cmd

cgoLinkExt function #

func cgoLinkExt(s *script.State) (bool, error)

checkScriptReadme function #

func checkScriptReadme(t *testing.T, engine *script.Engine, env []string, scriptspath string, gotool string, fixReadme bool)

hasBuildmode function #

func hasBuildmode(s *script.State, mode string) (bool, error)

hasGodebug function #

func hasGodebug(s *script.State, value string) (bool, error)

hasGoexperiment function #

func hasGoexperiment(s *script.State, value string) (bool, error)

initScriptDirs function #

func initScriptDirs(t testing.TB, s *script.State)

linkOrCopy function #

linkOrCopy creates a link to src at dst, or if the symlink fails (platform doesn't support) then copies src to dst.

func linkOrCopy(t *testing.T, src string, dst string)

mustLinkExt function #

func mustLinkExt(s *script.State) (bool, error)

pieLinkExt function #

func pieLinkExt(s *script.State) (bool, error)

scriptCC function #

scriptCC runs the platform C compiler.

func scriptCC(cmdExec script.Cmd, ccexe string) script.Cmd

sysCondition function #

func sysCondition(flag string, f func(goos string, goarch string) bool, needsCgo bool, goHostOS string, goHostArch string) script.Cond

tempEnvName function #

func tempEnvName() string

Generated with Arrow