| 1 |
package integration |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"os/exec" |
| 8 |
"regexp" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/ThomasRooney/gexpect" |
| 13 |
"github.com/smallstep/assert" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
func CleanOutput(str string) string { |
| 18 |
if i := strings.Index(str, "?25h"); i > 0 { |
| 19 |
return str[i+4:] |
| 20 |
} |
| 21 |
return str |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
func Command(command string) *exec.Cmd { |
| 26 |
return exec.Command("sh", "-c", command) |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
func ExitError(err error) (*exec.ExitError, bool) { |
| 31 |
v, ok := err.(*exec.ExitError) |
| 32 |
return v, ok |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
func Output(command string) ([]byte, error) { |
| 37 |
return Command(command).Output() |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
func CombinedOutput(command string) ([]byte, error) { |
| 43 |
return Command(command).CombinedOutput() |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
func WithStdin(command string, r io.Reader) ([]byte, error) { |
| 48 |
cmd := Command(command) |
| 49 |
cmd.Stdin = r |
| 50 |
return cmd.Output() |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
type CLICommand struct { |
| 55 |
command string |
| 56 |
arguments string |
| 57 |
flags map[string]string |
| 58 |
stdin io.Reader |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
type CLIOutput struct { |
| 64 |
stdout string |
| 65 |
stderr string |
| 66 |
combined string |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func NewCLICommand() CLICommand { |
| 71 |
return CLICommand{"", "", make(map[string]string), nil} |
| 72 |
} |
| 73 |
|
| 74 |
func (c CLICommand) setFlag(flag, value string) CLICommand { |
| 75 |
flags := make(map[string]string) |
| 76 |
for k, v := range c.flags { |
| 77 |
flags[k] = v |
| 78 |
} |
| 79 |
flags[flag] = value |
| 80 |
return CLICommand{c.command, c.arguments, flags, c.stdin} |
| 81 |
} |
| 82 |
|
| 83 |
func (c CLICommand) setCommand(command string) CLICommand { |
| 84 |
return CLICommand{command, c.arguments, c.flags, c.stdin} |
| 85 |
} |
| 86 |
|
| 87 |
func (c CLICommand) setArguments(arguments string) CLICommand { |
| 88 |
return CLICommand{c.command, arguments, c.flags, c.stdin} |
| 89 |
} |
| 90 |
|
| 91 |
func (c CLICommand) setStdin(stdin string) CLICommand { |
| 92 |
return CLICommand{c.command, c.arguments, c.flags, strings.NewReader(stdin)} |
| 93 |
} |
| 94 |
|
| 95 |
func (c CLICommand) cmd() string { |
| 96 |
flags := "" |
| 97 |
for key, value := range c.flags { |
| 98 |
if strings.Contains(value, " ") { |
| 99 |
value = "\"" + value + "\"" |
| 100 |
} |
| 101 |
flags += fmt.Sprintf("--%s %s ", key, value) |
| 102 |
} |
| 103 |
return fmt.Sprintf("%s %s %s", c.command, c.arguments, flags) |
| 104 |
} |
| 105 |
|
| 106 |
func (c CLICommand) run() (CLIOutput, error) { |
| 107 |
var stdout, stderr, combined bytes.Buffer |
| 108 |
cmd := Command(c.cmd()) |
| 109 |
cmd.Stdout = io.MultiWriter(&stdout, &combined) |
| 110 |
cmd.Stderr = io.MultiWriter(&stderr, &combined) |
| 111 |
cmd.Stdin = c.stdin |
| 112 |
err := cmd.Run() |
| 113 |
return CLIOutput{stdout.String(), stderr.String(), combined.String()}, err |
| 114 |
} |
| 115 |
|
| 116 |
func (c CLICommand) spawn() (*gexpect.ExpectSubprocess, error) { |
| 117 |
return gexpect.Spawn(c.cmd()) |
| 118 |
} |
| 119 |
|
| 120 |
func (c CLICommand) test(t *testing.T, name string, expected string, msg ...interface{}) { |
| 121 |
t.Run(name, func(t *testing.T) { |
| 122 |
out, err := c.run() |
| 123 |
assert.FatalError(t, err, fmt.Sprintf("`%s`: returned error '%s'\n\nOutput:\n%s", c.cmd(), err, out.combined)) |
| 124 |
assert.Equals(t, out.combined, expected, msg...) |
| 125 |
}) |
| 126 |
} |
| 127 |
|
| 128 |
func (c CLICommand) fail(t *testing.T, name string, expected interface{}, msg ...interface{}) { |
| 129 |
t.Run(name, func(t *testing.T) { |
| 130 |
out, err := c.run() |
| 131 |
if assert.NotNil(t, err) { |
| 132 |
assert.Equals(t, err.Error(), "exit status 1") |
| 133 |
} |
| 134 |
switch v := expected.(type) { |
| 135 |
case string: |
| 136 |
assert.Equals(t, expected, out.stderr) |
| 137 |
case *regexp.Regexp: |
| 138 |
re := expected.(*regexp.Regexp) |
| 139 |
if !re.MatchString(out.stderr) { |
| 140 |
t.Errorf("Error message did not match regex:\n Regex: %s\n\n Output:\n%s", re.String(), out.stderr) |
| 141 |
} |
| 142 |
default: |
| 143 |
t.Errorf("unexpected type %T", v) |
| 144 |
} |
| 145 |
assert.Equals(t, "", out.stdout) |
| 146 |
}) |
| 147 |
} |