From 8b977bee740d49796029f9768d98cfa6ef151730 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Wed, 24 Jul 2019 23:41:28 +0900 Subject: [PATCH] Support Windows --- .travis.yml | 36 +++++++++++++++++++++++++++++------- exec_test.go | 14 ++++++++++++++ exec_windows.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 exec_windows.go diff --git a/.travis.yml b/.travis.yml index 4d41f3b..075675b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,19 +3,41 @@ matrix: include: - go: 1.12.x os: linux + env: GO111MODULE=on + before_install: + - echo $TRAVIS_GO_VERSION + - sudo pip install codecov + after_script: + - codecov - go: master os: linux + env: GO111MODULE=on + before_install: + - echo $TRAVIS_GO_VERSION + - sudo pip install codecov - go: 1.12.x os: osx + env: GO111MODULE=on + before_install: + - echo $TRAVIS_GO_VERSION - go: master os: osx + env: GO111MODULE=on + before_install: + - echo $TRAVIS_GO_VERSION + - go: 1.12.x + os: windows + env: GO111MODULE=on + before_install: + - echo $TRAVIS_GO_VERSION + - echo $PATH + - echo $GOPATH + - go: master + os: windows + env: GO111MODULE=on + before_install: + - echo $TRAVIS_GO_VERSION allow_failures: - go: master -env: GO111MODULE=on -install: - - echo $TRAVIS_GO_VERSION - - sudo pip install codecov script: - - make ci -after_script: - - codecov + - go test ./... diff --git a/exec_test.go b/exec_test.go index bdcc251..27092c7 100644 --- a/exec_test.go +++ b/exec_test.go @@ -6,6 +6,7 @@ import ( "fmt" "math/rand" "os/exec" + "runtime" "strconv" "strings" "syscall" @@ -157,6 +158,19 @@ type testcase struct { func gentests(withSleepTest bool) []testcase { tests := []testcase{} + if runtime.GOOS == "windows" { + r := random() + tests = append(tests, testcase{"echo", []string{"echo", r}, r, true}) + r = random() + tests = append(tests, testcase{"cmd /c echo", []string{"cmd", "/c", fmt.Sprintf("echo %s", r)}, r, true}) + if withSleepTest { + r = "123456" + tests = append(tests, testcase{"timeout", []string{"timeout", r, "/nobreak"}, r, false}) + r = "654321" + tests = append(tests, testcase{"cmd /c timeout", []string{"cmd", "/c", fmt.Sprintf("timeout %s /nobreak && echo %s", r, r)}, r, false}) + } + return tests + } r := random() tests = append(tests, testcase{"echo", []string{"echo", r}, r, true}) r = random() diff --git a/exec_windows.go b/exec_windows.go new file mode 100644 index 0000000..0d923a4 --- /dev/null +++ b/exec_windows.go @@ -0,0 +1,30 @@ +package exec + +import ( + "os" + "os/exec" + "strconv" + "syscall" +) + +var defaultSignal = os.Interrupt + +// Reference code: +// https://github.com/Songmu/timeout/blob/517fff103abc7d0e88a663609515d8bb55f6535d/timeout_windows.go +func command(name string, arg ...string) *exec.Cmd { + // #nosec + cmd := exec.Command(name, arg...) + if cmd.SysProcAttr == nil { + cmd.SysProcAttr = &syscall.SysProcAttr{} + } + cmd.SysProcAttr.CreationFlags = syscall.CREATE_UNICODE_ENVIRONMENT | 0x00000200 + return cmd +} + +func terminate(cmd *exec.Cmd, sig os.Signal) error { + return cmd.Process.Signal(sig) +} + +func killall(cmd *exec.Cmd) error { + return exec.Command("taskkill", "/F", "/T", "/PID", strconv.Itoa(cmd.Process.Pid)).Run() +}