-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_test.go
More file actions
122 lines (108 loc) · 4.35 KB
/
cli_test.go
File metadata and controls
122 lines (108 loc) · 4.35 KB
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
package main
import (
"bytes"
"os/exec"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func runCliForTesting(t *testing.T, args ...string) (code int, stdout string, stderr string) {
cmd := exec.Command("go", append([]string{"run", "."}, args...)...)
var stdoutBuffer, stderrBuffer bytes.Buffer
cmd.Stdout = &stdoutBuffer
cmd.Stderr = &stderrBuffer
err := cmd.Run()
if err != nil {
if _, isExitError := err.(*exec.ExitError); isExitError {
// must be caused by exit code, ignore the error
} else {
t.Fatalf("Failed to run with arguments '%s': %s\n", strings.Join(args, " "), err)
}
}
stdout, stderr = stdoutBuffer.String(), stderrBuffer.String()
code = cmd.ProcessState.ExitCode()
stderr = strings.TrimSuffix(stderr, "exit status 1\n")
return
}
func shouldTestPrintHelp(t *testing.T, stdout string, stderr string) {
assert.Regexp(t, "^Usage:.*", stdout, "Should print usage to stdout")
assert.Regexp(t, "Options:", stdout, "Should print options to stdout")
assert.Regexp(t, "References:", stdout, "Should print references to stdout")
}
func TestCliNoArgument(t *testing.T) {
code, stdout, stderr := runCliForTesting(t)
assert.Equal(t, 1, code, "Exit code should be 1")
shouldTestPrintHelp(t, stdout, stderr)
assert.Equal(t, "Error: flag file or check file must be specified\n", stderr)
}
func TestCliErrorPanicNonExistingDir(t *testing.T) {
code, stdout, stderr := runCliForTesting(t, "-f", "package.json", "--error", "panic", "./non-existing-dir")
assert.Equal(t, 1, code, "Exit code should be 1")
assert.Equal(t, 0, len(stdout), "Should have nothing sent to stdout")
assert.Equal(t, "Error: stat ./non-existing-dir: no such file or directory\n", stderr)
}
func TestCliErrorIgnoreNonExistingDir(t *testing.T) {
code, stdout, stderr := runCliForTesting(t, "-f", "package.json", "--error", "ignore", "./non-existing-dir")
assert.Equal(t, 0, code, "Exit code should be 0")
assert.Equal(t, 0, len(stdout), "Should have nothing sent to stdout")
assert.Equal(t, 0, len(stderr), "Should have nothing sent to stderr")
}
func TestCliErrorPrintNonExistingDir(t *testing.T) {
code, stdout, stderr := runCliForTesting(t, "-f", "package.json", "--error", "print", "./non-existing-dir")
assert.Equal(t, 0, code, "Exit code should be 0")
assert.Equal(t, 0, len(stdout), "Should have nothing sent to stdout")
assert.Equal(t, "Error: stat ./non-existing-dir: no such file or directory\n", stderr)
}
func TestCliHelp(t *testing.T) {
code, stdout, stderr := runCliForTesting(t, "-h")
assert.Equal(t, 0, code, "Exit code should be 0")
shouldTestPrintHelp(t, stdout, stderr)
assert.Equal(t, "", stderr)
}
func TestCliPackageJsonInRepo1(t *testing.T) {
code, stdout, stderr := runCliForTesting(t, "-f", "package.json", "testdata/repo1")
assert.Equal(t, 0, code, "Exit code should be 0")
assert.Equal(t, "", stderr)
assert.Equal(t,
`testdata/repo1
testdata/repo1/inbound
testdata/repo1/outbound/New Zealand
testdata/repo1/outbound/china
testdata/repo1/outbound/china/mainland
`,
stdout, "Should output exactly these")
}
func TestCliPrint0PackageJsonInRepo1(t *testing.T) {
code, stdout, stderr := runCliForTesting(t, "-0", "-f", "package.json", "testdata/repo1")
assert.Equal(t, 0, code, "Exit code should be 0")
assert.Equal(t, "", stderr)
assert.Equal(t,
strings.ReplaceAll(`testdata/repo1
testdata/repo1/inbound
testdata/repo1/outbound/New Zealand
testdata/repo1/outbound/china
testdata/repo1/outbound/china/mainland
`, "\n", string([]byte{0})),
stdout, "Should output exactly these")
}
func TestCliDepth0PackageJsonInRepo1(t *testing.T) {
code, stdout, stderr := runCliForTesting(t, "-f", "package.json", "-d", "0", "testdata/repo1")
assert.Equal(t, 0, code, "Exit code should be 0")
assert.Equal(t, "", stderr)
assert.Equal(t, "testdata/repo1\n", stdout, "Should output the root directory")
}
func TestCliNoDefaultExcludesPackageJsonInRepo1(t *testing.T) {
code, stdout, stderr := runCliForTesting(t, "-f", "package.json", "--no-default-excludes", "testdata/repo1")
assert.Equal(t, 0, code, "Exit code should be 0")
assert.Equal(t, "", stderr)
assert.Equal(t,
`testdata/repo1
testdata/repo1/inbound
testdata/repo1/outbound/New Zealand
testdata/repo1/outbound/china
testdata/repo1/outbound/china/mainland
testdata/repo1/outbound/china/mainland/node_modules/package1
testdata/repo1/outbound/china/mainland/node_modules/package2
`,
stdout, "Should output exactly these")
}