github.com/NeowayLabs/nash@v0.2.2-0.20200127205349-a227041ffd50/internal/sh/builtin/exit_test.go (about) 1 package builtin_test 2 3 import ( 4 "os" 5 "os/exec" 6 "testing" 7 ) 8 9 func TestExit(t *testing.T) { 10 type exitDesc struct { 11 script string 12 status string 13 result int 14 fail bool 15 } 16 17 tests := map[string]exitDesc{ 18 "success": { 19 script: "./testdata/exit.sh", 20 status: "0", 21 result: 0, 22 }, 23 "failure": { 24 script: "./testdata/exit.sh", 25 status: "1", 26 result: 1, 27 }, 28 } 29 30 // WHY: We need to run Exec because the script will call the exit syscall, 31 // killing the process (the test process on this case). 32 // When calling Exec we need to guarantee that we are using the nash 33 // built directly from the project, not the one installed on the host. 34 projectnash := "../../../cmd/nash/nash" 35 36 for name, desc := range tests { 37 t.Run(name, func(t *testing.T) { 38 cmd := exec.Command(projectnash, desc.script, desc.status) 39 cmd.Stdout = os.Stdout 40 cmd.Stderr = os.Stderr // to know why scripts were failing 41 cmd.Run() 42 43 if cmd.ProcessState == nil { 44 t.Fatalf("expected cmd[%v] to have a process state, can't validate status code", cmd) 45 } 46 got := cmd.ProcessState.ExitCode() 47 if desc.result != got { 48 t.Fatalf("expected[%d] got[%d]", desc.result, got) 49 } 50 }) 51 } 52 }