| 1 |
package godo |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io/ioutil" |
| 6 |
"runtime" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"gopkg.in/stretchr/testify.v1/assert" |
| 11 |
|
| 12 |
"github.com/mgutz/str" |
| 13 |
) |
| 14 |
|
| 15 |
var cat = "cat" |
| 16 |
|
| 17 |
func init() { |
| 18 |
if runtime.GOOS == "windows" { |
| 19 |
cat = "type" |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
func TestRunMultiline(t *testing.T) { |
| 24 |
output, _ := RunOutput(` |
| 25 |
{{.cat}} test/foo.txt |
| 26 |
{{.cat}} test/bar.txt |
| 27 |
`, M{"cat": cat}) |
| 28 |
assert.Equal(t, "foo\nbar\n", output) |
| 29 |
} |
| 30 |
|
| 31 |
func TestRunError(t *testing.T) { |
| 32 |
output, err := RunOutput(` |
| 33 |
{{.cat}} test/doesnotexist.txt |
| 34 |
{{.cat}} test/bar.txt |
| 35 |
`, M{"cat": cat}) |
| 36 |
assert.Error(t, err) |
| 37 |
assert.Contains(t, err.Error(), "line=1") |
| 38 |
assert.Contains(t, output, "doesnotexist") |
| 39 |
} |
| 40 |
|
| 41 |
func TestInside(t *testing.T) { |
| 42 |
Inside("test", func() { |
| 43 |
var out string |
| 44 |
if isWindows { |
| 45 |
out, _ = RunOutput("foo.cmd") |
| 46 |
} else { |
| 47 |
out, _ = RunOutput("bash foo.sh") |
| 48 |
} |
| 49 |
|
| 50 |
if str.Clean(out) != "FOOBAR" { |
| 51 |
t.Error("Inside failed. Got", fmt.Sprintf("%q", out)) |
| 52 |
} |
| 53 |
}) |
| 54 |
|
| 55 |
version, _ := ioutil.ReadFile("./VERSION.go") |
| 56 |
if !strings.Contains(string(version), "var Version") { |
| 57 |
t.Error("Inside failed to reset work directory") |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func TestBash(t *testing.T) { |
| 62 |
if isWindows { |
| 63 |
return |
| 64 |
} |
| 65 |
out, _ := BashOutput(`echo -n foobar`) |
| 66 |
if out != "foobar" { |
| 67 |
t.Error("Simple bash failed. Got", out) |
| 68 |
} |
| 69 |
|
| 70 |
out, _ = BashOutput(` |
| 71 |
echo -n foobar |
| 72 |
echo -n bahbaz |
| 73 |
`) |
| 74 |
if out != "foobarbahbaz" { |
| 75 |
t.Error("Multiline bash failed. Got", out) |
| 76 |
} |
| 77 |
|
| 78 |
out, _ = BashOutput(` |
| 79 |
echo -n \ |
| 80 |
foobar |
| 81 |
`) |
| 82 |
if out != "foobar" { |
| 83 |
t.Error("Bash line continuation failed. Got", out) |
| 84 |
} |
| 85 |
|
| 86 |
out, _ = BashOutput(` |
| 87 |
echo -n "foobar" |
| 88 |
`) |
| 89 |
if out != "foobar" { |
| 90 |
t.Error("Bash quotes failed. Got", out) |
| 91 |
} |
| 92 |
|
| 93 |
out, _ = BashOutput(` |
| 94 |
echo -n "fo\"obar" |
| 95 |
`) |
| 96 |
if out != "fo\"obar" { |
| 97 |
t.Error("Bash quoted string failed. Got", out) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func TestTemplatedCommands(t *testing.T) { |
| 102 |
echo := "echo" |
| 103 |
if isWindows { |
| 104 |
echo = "cmd /c echo" |
| 105 |
|
| 106 |
} |
| 107 |
// in V2 BashOutput accepts an options map |
| 108 |
out, err := RunOutput(echo+" {{.name}}", M{"name": "oy"}) |
| 109 |
assert.NoError(t, err) |
| 110 |
assert.Equal(t, "oy", str.Clean(out)) |
| 111 |
|
| 112 |
if isWindows { |
| 113 |
return |
| 114 |
} |
| 115 |
|
| 116 |
// in V2 BashOutput accepts an options map |
| 117 |
out, err = BashOutput("echo {{.name}}", M{"name": "oy"}) |
| 118 |
assert.NoError(t, err) |
| 119 |
assert.Equal(t, "oy", str.Clean(out)) |
| 120 |
} |