Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Commit

Permalink
main: do not print *exec.ExitError for -run (#61)
Browse files Browse the repository at this point in the history
Instead, rely on the program to output something sensible on
stdout/stderr. Exit with the exit code from the program we ran.

For this we use a local version of *os.ProcessState.ExitStatus()
https://go-review.googlesource.com/c/go/+/125443/ which is due to land
in Go 1.12
  • Loading branch information
myitcv committed Feb 16, 2019
1 parent 4cdfd9a commit 7e4da01
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 14 deletions.
16 changes: 16 additions & 0 deletions exitcode_plan9.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"os"
"syscall"
)

// ExitCode returns the exit code of the exited process, or -1
// if the process hasn't exited or was terminated by a signal.
func ExitCode(p *os.ProcessState) int {
// return -1 if the process hasn't started.
if p == nil {
return -1
}
return p.Sys().(syscall.WaitStatus).ExitStatus()
}
18 changes: 18 additions & 0 deletions exitcode_posix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// +build darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris windows

package main

import (
"os"
"syscall"
)

// ExitCode returns the exit code of the exited process, or -1
// if the process hasn't exited or was terminated by a signal.
func ExitCode(p *os.ProcessState) int {
// return -1 if the process hasn't started.
if p == nil {
return -1
}
return p.Sys().(syscall.WaitStatus).ExitStatus()
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func main() {

func main1() int {
if err := mainerr(); err != nil {
if ee, ok := err.(*exec.ExitError); ok {
return ExitCode(ee.ProcessState)
}
fmt.Fprintln(os.Stderr, err)
return 1
}
Expand Down
61 changes: 50 additions & 11 deletions script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"runtime"
Expand Down Expand Up @@ -39,6 +40,41 @@ func (m gobinMain) Run() int {
return m.m.Run()
}

func TestExitCode(t *testing.T) {
var err error
self, err := os.Executable()
if err != nil {
t.Fatalf("failed to determine os.Executable: %v", err)
}

temp, err := ioutil.TempDir("", "gobin-exitcode-test")
if err != nil {
t.Fatalf("failed to create temp directory for home: %v", err)
}
defer os.RemoveAll(temp)

cmd := exec.Command(self, "-run", "example.com/fail")
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, homeEnv(temp)...)
cmd.Env = append(cmd.Env,
"GOPROXY="+proxyURL,
"TESTSCRIPT_COMMAND=gobin",
)

err = cmd.Run()
if err == nil {
t.Fatalf("unexpected success")
}
ee, ok := err.(*exec.ExitError)
if !ok {
t.Fatalf("expected *exec.ExitError; got %T: %v", err, err)
}
want := 42
if got := ExitCode(ee.ProcessState); want != got {
t.Fatalf("expected exit code %v; got %v", want, got)
}
}

func TestScripts(t *testing.T) {
var (
pathToMod string // local path to this module
Expand Down Expand Up @@ -93,17 +129,8 @@ func TestScripts(t *testing.T) {
"USERCACHEDIR="+ucd,
)

if runtime.GOOS == "windows" {
e.Vars = append(e.Vars,
"USERPROFILE="+wd+"\\home",
"LOCALAPPDATA="+wd+"\\appdata",
"HOME="+wd+"\\home", // match USERPROFILE
)
} else {
e.Vars = append(e.Vars,
"HOME="+wd+"/home",
)
}
e.Vars = append(e.Vars, homeEnv(wd)...)

return nil
},
}
Expand All @@ -112,3 +139,15 @@ func TestScripts(t *testing.T) {
}
testscript.Run(t, p)
}

func homeEnv(base string) []string {
if runtime.GOOS == "windows" {
return []string{
"USERPROFILE=" + base + "\\home",
"LOCALAPPDATA=" + base + "\\appdata",
"HOME=" + base + "\\home", // match USERPROFILE
}
} else {
return []string{"HOME=" + base + "/home"}
}
}
2 changes: 1 addition & 1 deletion testdata/failing_run.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
! gobin -run example.com/fail
stdout '^This will fail$'
stderr '^exit status 42$'
stderr '^It''s bad$'


5 changes: 3 additions & 2 deletions testdata/mod/example.com_fail_v1.0.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import "fmt"
import "os"

func main() {
fmt.Println("This will fail")
os.Exit(42)
fmt.Println("This will fail")
fmt.Fprintln(os.Stderr, "It's bad")
os.Exit(42)
}

0 comments on commit 7e4da01

Please sign in to comment.