github.com/beanworks/dcm@v0.0.0-20230726194615-49d2d0417e04/src/cmd_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "errors" 6 "fmt" 7 "os" 8 "os/exec" 9 "reflect" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 // ========== Mocked command executer as test helpers for Cmd ========== 16 17 // Test helper functions and command mock 18 func helperCommand(t *testing.T, s ...string) *exec.Cmd { 19 cs := []string{"-test.run=TestHelperProcess", "--"} 20 cs = append(cs, s...) 21 cmd := exec.Command(os.Args[0], cs...) 22 cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} 23 return cmd 24 } 25 26 // TestHelperProcess isn't a real test. It's used as a helper process 27 // for TestParameterRun. 28 func TestHelperProcess(*testing.T) { 29 if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { 30 return 31 } 32 defer os.Exit(0) 33 34 args := os.Args 35 for len(args) > 0 { 36 if args[0] == "--" { 37 args = args[1:] 38 break 39 } 40 args = args[1:] 41 } 42 if len(args) == 0 { 43 fmt.Fprintf(os.Stderr, "No command\n") 44 os.Exit(2) 45 } 46 47 cmd, args := args[0], args[1:] 48 switch cmd { 49 case "echo": 50 iargs := []interface{}{} 51 for _, s := range args { 52 iargs = append(iargs, s) 53 } 54 fmt.Println(iargs...) 55 default: 56 fmt.Fprintf(os.Stderr, "Unknown command %q\n", cmd) 57 os.Exit(2) 58 } 59 } 60 61 // ========== Here starts the real tests for Cmd ========== 62 63 func TestCmdExec(t *testing.T) { 64 name := os.Args[0] 65 args := []string{"-test.run=TestHelperProcess", "--", "echo"} 66 67 c := &Cmd{} 68 c.Exec(name, args...) 69 70 assert.Equal(t, name, c.name) 71 assert.Equal(t, args, c.args) 72 assert.Equal(t, "*exec.Cmd", reflect.TypeOf(c.cmd).String()) 73 } 74 75 func TestCmdSetStdin(t *testing.T) { 76 c := &Cmd{} 77 c.SetStdin(os.Stdin) 78 79 assert.IsType(t, os.Stdin, c.stdin) 80 } 81 82 func TestCmdSetStderr(t *testing.T) { 83 c := &Cmd{} 84 c.SetStderr(os.Stderr) 85 86 assert.IsType(t, os.Stderr, c.stderr) 87 } 88 89 func TestCmdSetdir(t *testing.T) { 90 c := &Cmd{} 91 c.cmd = helperCommand(t, "echo") 92 93 assert.Equal(t, "", c.cmd.Dir) 94 c.Setdir("/test/dir") 95 assert.Equal(t, "/test/dir", c.cmd.Dir) 96 } 97 98 func TestCmdSetenvAndGetenv(t *testing.T) { 99 c := &Cmd{} 100 101 assert.Equal(t, []string{}, c.Getenv()) 102 103 c.cmd = helperCommand(t, "echo") 104 105 assert.Equal(t, []string{"GO_WANT_HELPER_PROCESS=1"}, c.cmd.Env) 106 assert.Equal(t, []string{"GO_WANT_HELPER_PROCESS=1"}, c.Getenv()) 107 108 c.Setenv([]string{"GO_WANT_HELPER_PROCESS=1", "foo=bar", "baz=qux"}) 109 110 assert.Equal(t, []string{"GO_WANT_HELPER_PROCESS=1", "foo=bar", "baz=qux"}, c.cmd.Env) 111 assert.Equal(t, []string{"GO_WANT_HELPER_PROCESS=1", "foo=bar", "baz=qux"}, c.Getenv()) 112 } 113 114 func TestCmdRun(t *testing.T) { 115 var out bytes.Buffer 116 117 NewCmd(). 118 SetStdout(&out). 119 Setcmd(helperCommand(t, "echo", "foo", "bar")). 120 Run() 121 122 assert.Equal(t, "foo bar\n", out.String()) 123 } 124 125 func TestCmdOut(t *testing.T) { 126 out, _ := NewCmd(). 127 Setcmd(helperCommand(t, "echo", "baz", "qux")). 128 Out() 129 130 assert.Equal(t, "baz qux\n", string(out)) 131 } 132 133 func TestCmdFormatOutput(t *testing.T) { 134 c := NewCmd() 135 fixture := []byte("foobar\n") 136 137 assert.Equal(t, "foobar", c.FormatOutput(fixture)) 138 } 139 140 func TestCmdFormatError(t *testing.T) { 141 c := NewCmd() 142 err := errors.New("foobar") 143 out := []byte("bazqux") 144 145 assert.Equal(t, errors.New("foobar: bazqux"), c.FormatError(err, out)) 146 }