-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathey-berks_test.go
74 lines (62 loc) · 1.36 KB
/
ey-berks_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
package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"strings"
"testing"
)
func TestParseArgs(t *testing.T) {
cmd := []string{"help", "pathpath", "--config=Berkfile"}
command, args, options := parseArgs(cmd)
if command != "help" {
log.Fatal("erro parse command")
}
if options["config"] != "Berkfile" {
log.Fatal("erro parse --config")
}
if args[0] != "pathpath" {
log.Fatal("erro parse args")
}
}
func commandOutput(args []string) (output string) {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
Command(args)
outC := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
w.Close()
os.Stdout = old
output = <-outC
return
}
func getLine(text string, line int) string {
lines := strings.Split(text, "\n")
if len(lines) < line+1 {
log.Fatalf("There is no line at line %d in %s", line, text)
}
return lines[line]
}
func TestVersion(t *testing.T) {
command := []string{"version"}
actual := commandOutput(command)
expect := fmt.Sprintf("ey-berks version is: %s", Version)
if actual != expect {
t.Errorf("Expected %s, Got %s", expect, actual)
}
}
func TestHelp(t *testing.T) {
command := []string{"help"}
actual := getLine(commandOutput(command), 0)
expect := "Engine Yard Cloud cookbook tool like Berkshelf"
if actual != expect {
t.Errorf("Expected %s, Got %s", expect, actual)
}
}