-
Notifications
You must be signed in to change notification settings - Fork 1
/
gotic_test.go
109 lines (84 loc) · 2.01 KB
/
gotic_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package gotic
import (
"errors"
"io"
"log"
"testing"
)
func TestExecBashPipedCommand(t *testing.T) {
logFatal = log.Print
output, err := ExecBashPipedCommand("echo Arthur", true)
if err != nil {
t.Errorf("got '%v'", output)
}
if output != "Arthur" {
t.Errorf("got '%v'", output)
}
}
func buildCommandMockFail(cc string) ([]byte, error) {
return []byte("Arthur\n"), errors.New("error")
}
func buildCommandMock(cc string) ([]byte, error) {
return []byte("Arthur\n"), nil
}
func TestExecBashPipedCommandLogFatal(t *testing.T) {
BuildCommand = buildCommandMockFail
logFatal = log.Print
output, err := ExecBashPipedCommand("echo Arthur\n", true)
if err == nil {
t.Errorf("got '%v'", output)
}
if output != "Arthur" {
t.Errorf("got '%v'", output)
}
}
func TestExecBashPipedCommandIgnoreExitCode(t *testing.T) {
BuildCommand = buildCommandMock
output, err := ExecBashPipedCommandIgnoreExitCode("echo Arthur\n", true)
if err != nil {
t.Errorf("got '%v'", output)
}
if output != "Arthur" {
t.Errorf("got '%v'", output)
}
}
func TestExecBashPipedCommandIgnoreExitCodeLogPrintf(t *testing.T) {
BuildCommand = buildCommandMockFail
output, err := ExecBashPipedCommandIgnoreExitCode("echo Arthur\n", true)
if err == nil {
t.Errorf("got '%v'", output)
}
if output != "Arthur" {
t.Errorf("got '%v'", output)
}
}
func TestExecShellScript(t *testing.T) {
BuildCommand = buildCommandMockFail
out, err := ExecShellScript("echo Arthur", true)
if err == "" {
t.Errorf("got '%v'", out)
}
if out != "Arthur\n" {
t.Errorf("got '%v'", out)
}
}
func readPromptMock(stdin io.Reader) string {
return "y"
}
func readPromptMockFail(stdin io.Reader) string {
return "n"
}
func TestPrompt(t *testing.T) {
ReadPrompt = readPromptMock
result := Prompt("question", "ls", false)
if result == false {
t.Errorf("got '%v'", result)
}
}
func TestPromptFail(t *testing.T) {
ReadPrompt = readPromptMockFail
result := Prompt("question", "ls", false)
if result == true {
t.Errorf("got '%v'", result)
}
}