-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
main_test.go
66 lines (58 loc) · 1.63 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"bytes"
"os"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMainFunc(t *testing.T) {
main()
}
func TestShell(t *testing.T) {
t.Run("echo+pipe", func(t *testing.T) {
cmd := exec.Command("bash", "-c", "echo pipetest | ./nyan")
var o bytes.Buffer
cmd.Stdout = &o
err := cmd.Run()
assert.Nil(t, err)
assert.NotNil(t, o.String())
assert.Equal(t, "pipetest\n", o.String())
})
t.Run("< StdInput", func(t *testing.T) {
cmd := exec.Command("bash", "-c", "./nyan < testdata/dummyfile")
var o bytes.Buffer
cmd.Stdout = &o
err := cmd.Run()
assert.Nil(t, err)
assert.NotNil(t, o.String())
assert.Equal(t, "This is dummy.", o.String())
})
t.Run("direct input over echo+pipe", func(t *testing.T) {
cmd := exec.Command("bash", "-c", "echo pipetest | ./nyan testdata/dummyfile")
var o bytes.Buffer
cmd.Stdout = &o
err := cmd.Run()
assert.Nil(t, err)
assert.NotNil(t, o.String())
assert.NotContains(t, o.String(), "pipetest")
assert.Equal(t, "This is dummy.", o.String())
})
t.Run("echo+pipe & < StdInput", func(t *testing.T) {
cmd := exec.Command("bash", "-c", "echo pipetest | ./nyan < testdata/dummyfile")
var o bytes.Buffer
cmd.Stdout = &o
err := cmd.Run()
assert.Nil(t, err)
assert.NotNil(t, o.String())
assert.Equal(t, "This is dummy.", o.String())
})
t.Run("`> file` out is not highlighted", func(t *testing.T) {
outfile := "testdata/output"
cmd := exec.Command("bash", "-c", "echo 'package main' | ./nyan > "+outfile)
err := cmd.Run()
data, err := os.ReadFile(outfile)
assert.Nil(t, err)
assert.Equal(t, "package main\n", string(data))
})
}