Skip to content

Commit

Permalink
fix: testscript on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Dec 5, 2023
1 parent bac9533 commit 51afb07
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ require (
github.com/prometheus/client_golang v1.17.0
github.com/robfig/cron/v3 v3.0.1
github.com/rogpeppe/go-internal v1.11.0
github.com/rubyist/tracerx v0.0.0-20170927163412-787959303086
github.com/spf13/cobra v1.8.0
go.uber.org/automaxprocs v1.5.3
golang.org/x/crypto v0.16.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rubyist/tracerx v0.0.0-20170927163412-787959303086 h1:mncRSDOqYCng7jOD+Y6+IivdRI6Kzv2BLWYkWkdQfu0=
github.com/rubyist/tracerx v0.0.0-20170927163412-787959303086/go.mod h1:YpdgDXpumPB/+EGmGTYHeiW/0QVFRzBYTNFaxWfPDk4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
Expand Down
26 changes: 21 additions & 5 deletions testscript/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
Expand All @@ -14,6 +15,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"syscall"
Expand Down Expand Up @@ -45,6 +47,9 @@ func TestMain(m *testing.M) {
defer os.RemoveAll(tmp)

binPath = filepath.Join(tmp, "soft")
if runtime.GOOS == "windows" {
binPath += ".exe"
}

// Build the soft binary with -cover flag.
cmd := exec.Command("go", "build", "-cover", "-o", binPath, filepath.Join("..", "cmd", "soft"))
Expand All @@ -59,8 +64,8 @@ func TestMain(m *testing.M) {

func TestScript(t *testing.T) {
flag.Parse()

var lock sync.Mutex

mkkey := func(name string) (string, *keygen.SSHKeyPair) {
path := filepath.Join(t.TempDir(), name)
pair, err := keygen.New(path, keygen.WithKeyType(keygen.Ed25519), keygen.WithWrite())
Expand Down Expand Up @@ -168,23 +173,34 @@ func TestScript(t *testing.T) {
cmd.Dir = e.WorkDir
cmd.Env = e.Vars
cmd.Stderr = os.Stderr
lock.Unlock()

// Start the server
go func() {
if err := cmd.Run(); err != nil {
e.T().Fatal(err)
var exitErr *exec.ExitError
if !errors.As(err, &exitErr) {
e.T().Fatal(err)
}
e.T().Log(exitErr.Stderr)
}
}()
lock.Unlock()

e.Defer(func() {
lock.Lock()
defer lock.Unlock()
if cmd.Process == nil {
e.T().Fatal("process not started")
}
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
e.T().Fatal(err)
if runtime.GOOS == "windows" {
// Windows doesn't support SIGTERM
if err := cmd.Process.Kill(); err != nil {
e.T().Fatal(err)
}
} else {
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
e.T().Fatal(err)
}
}
})

Expand Down

0 comments on commit 51afb07

Please sign in to comment.