github.com/NeowayLabs/nash@v0.2.2-0.20200127205349-a227041ffd50/tests/internal/tester/tester.go (about) 1 // Package tester makes it easy to run multiple 2 // script test cases. 3 package tester 4 5 import ( 6 "testing" 7 8 "github.com/madlambda/nash/tests/internal/assert" 9 "github.com/madlambda/nash/tests/internal/sh" 10 ) 11 12 type TestCase struct { 13 Name string 14 ScriptCode string 15 ExpectStdout string 16 ExpectStderrToContain string 17 Fails bool 18 } 19 20 func Run(t *testing.T, nashcmd string, cases ...TestCase) { 21 for _, tcase := range cases { 22 t.Run(tcase.Name, func(t *testing.T) { 23 stdout, stderr, err := sh.Exec(t, nashcmd, tcase.ScriptCode) 24 if !tcase.Fails { 25 if err != nil { 26 t.Fatalf( 27 "error[%s] stdout[%s] stderr[%s]", 28 err, 29 stdout, 30 stderr, 31 ) 32 } 33 34 if stderr != "" { 35 t.Fatalf( 36 "unexpected stderr[%s], on success no stderr is expected", 37 stderr, 38 ) 39 } 40 } 41 42 if tcase.ExpectStdout != "" { 43 assert.EqualStrings(t, tcase.ExpectStdout, stdout) 44 } 45 46 if tcase.ExpectStderrToContain != "" { 47 assert.ContainsString(t, stderr, tcase.ExpectStderrToContain) 48 } 49 }) 50 } 51 }