Skip to content

Commit

Permalink
Allow script-style ztests to run on Windows (#4581)
Browse files Browse the repository at this point in the history
Script-style ztests require Bash on every OS and run with PATH set to
the equivalent of "$ZTEST_PATH:$PATH".  This is a change from the
previous value of "/bin:/usr/bin:$ZTEST_PATH".
  • Loading branch information
nwt authored May 9, 2023
1 parent fb44c52 commit 0d949c4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 28 deletions.
21 changes: 8 additions & 13 deletions ztest/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,23 @@ import (
"io"
"os"
"os/exec"
"runtime"
)

func RunShell(dir, bindir, script string, stdin io.Reader, useenvs []string) (string, string, error) {
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd.exe", "/c", script)
} else {
// "-e -o pipefile" ensures a test will fail if any command
// fails unexpectedly.
cmd = exec.Command("bash", "-e", "-o", "pipefail", "-c", script)
// "-e -o pipefile" ensures a test will fail if any command
// fails unexpectedly.
cmd := exec.Command("bash", "-e", "-o", "pipefail", "-c", script)
cmd.Dir = dir
cmd.Env = []string{
"AppData=" + dir, // For os.User*Dir on Windows.
"HOME=" + dir, // For os.User*Dir on Unix.
"PATH=" + bindir + string(os.PathListSeparator) + os.Getenv("PATH"),
}

for _, env := range useenvs {
if v, ok := os.LookupEnv(env); ok {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", env, v))
}
}

cmd.Env = append(cmd.Env, "HOME="+dir)
cmd.Env = append(cmd.Env, "PATH=/bin:/usr/bin:"+bindir)
cmd.Dir = dir
cmd.Stdin = stdin
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
Expand Down
6 changes: 0 additions & 6 deletions ztest/ztest.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ import (
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"testing"
"unicode/utf8"
Expand Down Expand Up @@ -329,11 +328,6 @@ func FromYAMLFile(filename string) (*ZTest, error) {

func (z *ZTest) ShouldSkip(path string) string {
switch {
case z.Script != "" && runtime.GOOS == "windows":
// XXX skip in windows until we figure out the best
// way to support script-driven tests across
// environments
return "script test on Windows"
case z.Script != "" && path == "":
return "script test on in-process run"
case z.Skip != "":
Expand Down
10 changes: 1 addition & 9 deletions ztest/ztest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,19 @@ package ztest
import (
"os"
"path/filepath"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestShouldSkip(t *testing.T) {
if runtime.GOOS == "windows" {
assert.Equal(t, "script test on Windows", (&ZTest{Script: "x"}).ShouldSkip(""))
} else {
assert.Equal(t, "script test on in-process run", (&ZTest{Script: "x"}).ShouldSkip(""))
}
assert.Equal(t, "script test on in-process run", (&ZTest{Script: "x"}).ShouldSkip(""))
assert.Equal(t, "reason", (&ZTest{Skip: "reason"}).ShouldSkip(""))
assert.Equal(t, `tag "x" does not match ZTEST_TAG=""`, (&ZTest{Tag: "x"}).ShouldSkip(""))
}

func TestRunScript(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping test on Windows because RunScript uses cmd.exe instead of bash")
}
t.Run("outputs", func(t *testing.T) {
testDir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(testDir, "testdirfile"), []byte("testdirfile\n"), 0644))
Expand Down

0 comments on commit 0d949c4

Please sign in to comment.