-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
127 lines (117 loc) · 2.97 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import "bytes"
import "io/ioutil"
import "net/http"
import "os"
import "os/exec"
import "strings"
import "testing"
func TestSanity(t *testing.T) {
command := exec.Command("./licensezero")
var stdout bytes.Buffer
command.Stdout = &stdout
err := command.Run()
if err != nil {
t.Error(err)
}
output := string(stdout.Bytes())
if !strings.Contains(output, "Subcommands:") {
t.Error("does not list subcommands")
}
if !strings.Contains(output, "License Zero") {
t.Error("does not mention License Zero")
}
}
func TestIdentify(t *testing.T) {
InTestDir(t, func() {
command := exec.Command("./licensezero", "identify", "--name", "John Doe", "--jurisdiction", "US-CA", "--email", "[email protected]")
var stdout bytes.Buffer
command.Stdout = &stdout
err := command.Run()
if err != nil {
t.Error(err)
}
output := string(stdout.Bytes())
if !strings.Contains(output, "Saved") {
t.Error("Does not print \"Saved\"")
}
})
}
func TestIdentifySilent(t *testing.T) {
InTestDir(t, func() {
command := exec.Command("./licensezero", "identify", "--name", "John Doe", "--jurisdiction", "US-CA", "--email", "[email protected]", "--silent")
var stdout bytes.Buffer
command.Stdout = &stdout
err := command.Run()
if err != nil {
t.Error(err)
}
output := string(stdout.Bytes())
if output != "" {
t.Error("No output")
}
})
}
func TestWhoAmIWithoutIdentity(t *testing.T) {
InTestDir(t, func() {
command := exec.Command("./licensezero", "whoami")
var stdout bytes.Buffer
command.Stdout = &stdout
err := command.Run()
if err == nil {
t.Error("Should fail")
}
})
}
func TestWhoAmIWithIdentity(t *testing.T) {
InTestDir(t, func() {
name := "John Doe"
email := "[email protected]"
jurisdiction := "US-CA"
exec.Command("./licensezero", "identify", "--name", name, "--jurisdiction", jurisdiction, "--email", email, "--silent").Run()
whoami := exec.Command("./licensezero", "whoami")
var stdout bytes.Buffer
whoami.Stdout = &stdout
err := whoami.Run()
if err != nil {
t.Error(err)
}
output := string(stdout.Bytes())
if !strings.Contains(output, name) {
t.Error("does not list name")
}
if !strings.Contains(output, email) {
t.Error("does not list e-mail")
}
if !strings.Contains(output, jurisdiction) {
t.Error("does not list jurisdiction")
}
})
}
func Identify() {
name := "John Doe"
email := "[email protected]"
jurisdiction := "US-CA"
exec.Command("./licensezero", "identify", "--name", name, "--jurisdiction", jurisdiction, "--email", email, "--silent").Run()
}
func InTestDir(t *testing.T, script func()) {
directory, err := ioutil.TempDir("/tmp", "licensezero-test")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(directory)
os.Setenv("LICENSEZERO_CONFIG", directory)
script()
}
const port = "8888"
func WithDataServer(script func()) {
server := http.Server{
Addr: ":" + port,
Handler: http.FileServer(http.Dir(".")),
}
go func() {
server.ListenAndServe()
}()
script()
server.Shutdown(nil)
}