-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_test.go
199 lines (159 loc) · 4.1 KB
/
app_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package cli_test
import (
"bytes"
"fmt"
"os"
"sync"
"syscall"
"github.com/phogolabs/cli"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("App", func() {
var app *cli.App
BeforeEach(func() {
noop := func(ctx *cli.Context) error {
return nil
}
commands := []*cli.Command{
&cli.Command{
Name: "sync",
Usage: "Generate a SQL script of CRUD operations for given database schema",
Description: "Generate a SQL script of CRUD operations for given database schema",
},
}
app = &cli.App{
Name: "prana",
Usage: "Golang Database Manager",
UsageText: "prana [global options]",
HelpName: "prana",
Description: "Golang Database Manager",
ArgsUsage: "[args]",
Version: "1.0-beta-04",
Copyright: "Open Source",
Authors: []*cli.Author{
&cli.Author{
Name: "John Freeman",
Email: "john@exmaple.com",
},
},
Before: noop,
After: noop,
Action: nil,
Commands: commands,
}
})
It("executes the app's command successfully", func() {
app.Action = func(ctx *cli.Context) error {
cmd := ctx.Command
Expect(cmd).NotTo(BeNil())
Expect(cmd.Name).To(Equal(app.Name))
Expect(cmd.Usage).To(Equal(app.Usage))
Expect(cmd.UsageText).To(Equal(app.UsageText))
Expect(cmd.HideHelp).To(Equal(app.HideHelp))
Expect(cmd.HelpName).To(Equal(app.HelpName))
Expect(cmd.Description).To(Equal(app.Description))
Expect(cmd.ArgsUsage).To(Equal(app.ArgsUsage))
Expect(ctx.Args).To(BeEmpty())
return nil
}
app.Run([]string{"app"})
})
Context("when the operation system sends a signal", func() {
It("handles the signal", func() {
var (
count = 0
rw sync.RWMutex
)
app.Signals = []os.Signal{syscall.SIGUSR1}
app.Action = func(ctx *cli.Context) error {
return nil
}
app.OnSignal = func(ctx *cli.Context, signal os.Signal) error {
rw.Lock()
defer rw.Unlock()
count++
cmd := ctx.Command
Expect(cmd).NotTo(BeNil())
Expect(cmd.Name).To(Equal(app.Name))
Expect(signal).To(Equal(syscall.SIGUSR1))
return nil
}
app.Run([]string{"app"})
process, err := os.FindProcess(os.Getpid())
Expect(err).NotTo(HaveOccurred())
Expect(process.Signal(syscall.SIGUSR1)).To(Succeed())
Eventually(func() int {
rw.RLock()
defer rw.RUnlock()
return count
}).Should(Equal(1))
})
})
Context("when the app name is not provided", func() {
It("sets the app name", func() {
app.Name = ""
app.Action = func(ctx *cli.Context) error {
cmd := ctx.Command
Expect(cmd.Name).To(Equal(app.Name))
Expect(cmd.Name).To(Equal("app"))
return nil
}
app.Run([]string{"app"})
})
})
Context("when the app version is requested", func() {
var buffer *bytes.Buffer
BeforeEach(func() {
buffer = &bytes.Buffer{}
app.Writer = buffer
})
It("shows the version", func() {
app.Run([]string{"app", "-v"})
Expect(buffer.String()).To(Equal("prana version 1.0-beta-04\n"))
})
})
Context("when the app fails", func() {
It("exits with the provided code", func() {
app.Action = func(ctx *cli.Context) error {
return cli.NewExitError("oh no!", 78)
}
app.Exit = func(code int) {
Expect(code).To(Equal(78))
}
app.Run([]string{"app"})
})
Context("when the error is not exit error", func() {
It("exits with the exit code 1001", func() {
app.Action = func(ctx *cli.Context) error {
return fmt.Errorf("oh no")
}
app.OnExitError = func(err error) error {
Expect(err).To(MatchError("oh no"))
return err
}
app.Exit = func(code int) {
Expect(code).To(Equal(1001))
}
app.Run([]string{"app"})
})
})
})
})
var _ = Describe("Author", func() {
It("returns the author as string", func() {
author := &cli.Author{
Name: "John",
}
Expect(author.String()).To(Equal("John"))
})
Context("when the author has email", func() {
It("returns the author as string", func() {
author := &cli.Author{
Name: "John",
Email: "john@example.com",
}
Expect(author.String()).To(Equal("John <john@example.com>"))
})
})
})