This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_test.go
334 lines (312 loc) · 8.96 KB
/
cli_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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* cli is a package intended to encourage some standardization in the command line user interface for programs
* developed for Caltech Library.
*
* @author R. S. Doiel, <rsdoiel@caltech.edu>
*
* Copyright (c) 2021, Caltech
* All rights not granted herein are expressly reserved by Caltech.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package cli
import (
"fmt"
"os"
"path"
"regexp"
"strings"
"testing"
)
var (
appName = "testcli"
)
func isSameString(expected, value string) string {
if strings.Compare(expected, value) == 0 {
return ""
}
return fmt.Sprintf("expected %q, got %q", expected, value)
}
func isSameInt(expected, value int) string {
if value == 0 {
return ""
}
return fmt.Sprintf("expected %d, got %d", expected, value)
}
func check(t *testing.T, msg string, failNow bool) {
if len(msg) > 0 {
t.Errorf(msg)
}
}
func TestOpen(t *testing.T) {
// Check of use of fallbackFile (os.Stdout) and cli.Open()
fp, err := Open("", os.Stdout)
if err != nil {
t.Errorf("Should have fallen back to os.Stdout, got error %s", err)
t.FailNow()
}
if fp != os.Stdout {
t.Errorf("fp should be pointing at os.Stdout")
t.FailNow()
}
fp, err = Open("-", os.Stdout)
if err != nil {
t.Errorf("Should have fallen back to os.Stdout, got error %s", err)
t.FailNow()
}
if fp != os.Stdout {
t.Errorf("fp should be pointing at os.Stdout")
t.FailNow()
}
// Check if we can open an existing file, i.e. README.md
fp, err = Open("README.md", os.Stdout)
if err != nil {
t.Errorf("Should have gotten pointer to README.md, got error %s", err)
t.FailNow()
}
if fp == os.Stdout {
t.Errorf("fp should be pointing at README.md NOT os.Stdout")
t.FailNow()
}
if fp == nil {
t.Errorf("fp should be pointing at README.md NOT nil")
t.FailNow()
}
fp.Close()
// Check to see if we fallack to os.Stdout for Create()
fp, err = Create("", os.Stdout)
if err != nil {
t.Errorf("Should have fallen back to os.Stdout, got error %s", err)
t.FailNow()
}
if fp != os.Stdout {
t.Errorf("fp should be pointing at os.Stdout")
t.FailNow()
}
fp, err = Create("-", os.Stdout)
if err != nil {
t.Errorf("Should have fallen back to os.Stdout, got error %s", err)
t.FailNow()
}
if fp != os.Stdout {
t.Errorf("fp should be pointing at os.Stdout")
t.FailNow()
}
// Check to see if we can open test.txt with Create()
fp, err = Create("test.txt", os.Stdout)
if err != nil {
t.Errorf("Should have gotten pointer to test.txt, got error %s", err)
t.FailNow()
}
if fp == os.Stdout {
t.Errorf("fp should be pointing at test.txt NOT os.Stdout")
t.FailNow()
}
if fp == nil {
t.Errorf("fp should be pointing at test.txt NOT nil")
t.FailNow()
}
fp.Close()
os.Remove("test.txt")
}
func TestShiftArg(t *testing.T) {
args := []string{
"one",
"two",
"three",
}
expected := args[0]
expectedL := len(args) - 1
r, args := ShiftArg(args)
if r != expected {
t.Errorf("expected %q, got %q", expected, r)
}
if len(args) != expectedL {
t.Errorf("expected args length to be %d, got %d", expectedL, len(args))
}
expected = args[0]
expectedL = len(args) - 1
r, args = ShiftArg(args)
if r != expected {
t.Errorf("expected %q, got %q", expected, r)
}
if len(args) != expectedL {
t.Errorf("expected args length to be %d, got %d", expectedL, len(args))
}
expected = args[0]
expectedL = len(args) - 1
r, args = ShiftArg(args)
if r != expected {
t.Errorf("expected %q, got %q", expected, r)
}
if args != nil {
t.Errorf("expected args to be nil, got %+v", args)
}
}
func TestApp(t *testing.T) {
appName := path.Base(os.Args[0])
app := NewCli(Version)
if app == nil {
t.Errorf("Expected an 'App' struct, got nil")
t.FailNow()
}
version := app.Version()
expectedS := fmt.Sprintf("%s %s", appName, Version)
if expectedS != version {
t.Errorf("expected %s, got %s", expectedS, version)
}
userName := os.Getenv("USER")
usage := "set USER from the environment"
err := app.EnvStringVar(&userName, "USER", userName, usage)
if err != nil {
t.Errorf("%s", err)
}
gotS := app.Env("USER")
expectedS = usage
if expectedS != gotS {
t.Errorf("expected %q, got %q", expectedS, gotS)
}
err = app.ParseEnv()
if err != nil {
t.Errorf("expected ParseEnv() to return nil, got %s", err)
t.FailNow()
}
// Now set a new default of "jane.doe"
expectedUserS := "jane.doe"
err = app.EnvStringVar(&userName, "USER", expectedUserS, usage)
if err != nil {
t.Errorf("EnvStringVar() returned an error, %s", err)
t.FailNow()
}
e, err := app.EnvAttribute("USER")
if err != nil {
t.Errorf("%s", err)
t.FailNow()
}
gotS = e.StringValue
if expectedUserS != gotS {
t.Errorf("expected %q, got %q", expectedUserS, gotS)
}
if userName != gotS {
t.Errorf("expected %q, got %q", userName, gotS)
}
// We expected to get the current user when we ParseEnv
expectedUserS = os.Getenv("USER")
err = app.ParseEnv()
if err != nil {
t.Errorf("ParseEnv() returned an error, %s", err)
t.FailNow()
}
// After ParseEnv(), userName should have been updated
gotS = app.Getenv("USER")
if expectedUserS != gotS {
t.Errorf("expected %q, got %q", expectedUserS, gotS)
}
e, err = app.EnvAttribute("USER")
if err != nil {
t.Errorf("%s", err)
t.FailNow()
}
if expectedUserS != e.StringValue {
t.Errorf("expected %q, got %q", expectedUserS, e.StringValue)
}
expectedUserS = "bessie.smith"
expectedS = "set the username overridding the enviroment"
app.StringVar(&userName, "u,user", expectedUserS, expectedS)
gotS = app.Option("u")
if expectedS != gotS {
t.Errorf("expected %q, got %q", expectedS, gotS)
}
gotS = app.Option("user")
if expectedS != gotS {
t.Errorf("expected %q, got %q", expectedS, gotS)
}
if expectedUserS != userName {
t.Errorf("expected %s, got %s", expectedUserS, userName)
}
}
func TestMd2Man(t *testing.T) {
s := "This is _in_line_ _italics_ in _Markdown_v1.0.0_"
expected := false
for i := 0; i < len(s); i++ {
start := i
end := i + 3
if end >= len(s) {
break
}
result := hasMidUnderscore(s, i)
if i == 11 || i == 40 {
expected = true
} else {
expected = false
}
if expected != result {
t.Errorf("%d: %q <-- expected %t != %t", i, s[start:end], expected, result)
}
}
src := []byte(`
This is a *test* of using _in_line_ formatting!
`)
srcR := string(md2man(src))
expectedS := `
This is a \fBtest\fP of using \fIin_line\fP formatting!
`
if srcR != expectedS {
t.Errorf("\n%q\n%q\n", expectedS, srcR)
}
s = `use --help EXAMPLE_NAME where EXAMPLE_NAME`
expected = false
for i := 0; i < len(s); i++ {
result := hasMidUnderscore(s, i)
if i == 18 || i == 37 {
expected = true
} else {
expected = false
}
if expected != result {
t.Errorf("%d: %q, expected %t, got %t", i, s, expected, result)
}
}
src = []byte(`
To view a specific example use --help EXAMPLE_NAME where EXAMPLE_NAME
is one of the following: getting-started-with-dataset, cloning-and-samples,
`)
expectedS = `
To view a specific example use --help EXAMPLE_NAME where EXAMPLE_NAME
is one of the following: getting-started-with-dataset, cloning-and-samples,
`
srcR = string(md2man(src))
if expectedS != srcR {
t.Errorf("Expected %q, got %q", expectedS, srcR)
}
}
func TestREMidUnderscoreMethod(t *testing.T) {
re := regexp.MustCompile(`[[:alpha:]0-9]_[[:alpha:]0-9]`)
s := "This is _in_line_ _italics_ in _Markdown_v1.0.0_"
for i := 0; i < len(s); i++ {
start := i
end := i + 3
if end >= len(s) {
break
}
result := re.MatchString(s[start:end])
expected := false
if i == 10 || i == 39 {
expected = true
} else {
expected = false
}
if result != expected {
t.Errorf("%d: %q <-- %t != %t\n", i, s[start:end], result, expected)
}
}
}