-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
201 lines (166 loc) · 4 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
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
package main
import (
"bytes"
"context"
"fmt"
"net/http"
"os"
"os/exec"
"path"
"testing"
"time"
"github.com/stretchr/testify/require"
)
var infoCases = []struct {
line string
owner string
repo string
}{
{
"github.com/sahilm/fuzzy v0.1.0",
"sahilm",
"fuzzy",
},
{
"github.com/cenkalti/backoff/v4 v4.1.2",
"cenkalti",
"backoff",
},
{
"Go forward",
"",
"",
},
}
func Test_repoInfo(t *testing.T) {
for _, tc := range infoCases {
t.Run(tc.line, func(t *testing.T) {
owner, repo := repoInfo(tc.line)
require.Equal(t, tc.owner, owner, "owner")
require.Equal(t, tc.repo, repo, "repo")
})
}
}
func Test_repoDesc(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip("In CI")
}
owner, repo := "pkg", "errors"
expected := "Simple error handling primitives" // FIXME: brittle
desc, err := repoDesc(owner, repo)
require.NoError(t, err, "API")
require.Equal(t, expected, desc, "description")
}
func testCtx(t *testing.T) (context.Context, context.CancelFunc) {
deadline, ok := t.Deadline()
if ok {
return context.WithDeadline(context.Background(), deadline)
}
return context.WithTimeout(context.Background(), 3*time.Second)
}
func build(t *testing.T) string {
exe := path.Join(t.TempDir(), "expmod")
ctx, cancel := testCtx(t)
defer cancel()
err := exec.CommandContext(ctx, "go", "build", "-o", exe).Run()
require.NoError(t, err, "build")
return exe
}
var testMod = "testdata/go.mod"
var exeExpected = `github.com/sahilm/fuzzy v0.1.0:
Go library that provides fuzzy string matching optimized for filenames and code symbols in the style of Sublime Text, VSCode, IntelliJ IDEA et al.
github.com/stretchr/testify v1.8.4:
A toolkit with common assertions and mocks that plays nicely with the standard library
`
var proxyMod = "testdata/proxy.mod"
var proxyExpected = `gopkg.in/yaml.v3 v3.0.1:
YAML support for the Go language.
`
var exeCases = []struct {
file string
output string
}{
{testMod, exeExpected},
{proxyMod, proxyExpected},
}
func TestExe(t *testing.T) {
exe := build(t)
for _, tc := range exeCases {
t.Run(tc.file, func(t *testing.T) {
ctx, cancel := testCtx(t)
defer cancel()
var buf bytes.Buffer
cmd := exec.CommandContext(ctx, exe, testMod)
cmd.Stdout = &buf
err := cmd.Run()
require.NoError(t, err, "run")
require.Equal(t, exeExpected, buf.String())
})
}
}
func TestExeStdin(t *testing.T) {
exe := build(t)
ctx, cancel := testCtx(t)
defer cancel()
file, err := os.Open(testMod)
require.NoError(t, err, "open mod")
defer file.Close()
var buf bytes.Buffer
cmd := exec.CommandContext(ctx, exe)
cmd.Stdin = file
cmd.Stdout = &buf
err = cmd.Run()
require.NoError(t, err, "run")
require.Equal(t, exeExpected, buf.String())
}
var flagCases = []struct {
flag string
fragment string
}{
{"-version", "version"},
{"-help", "usage"},
}
func TestExeFlags(t *testing.T) {
exe := build(t)
for _, tc := range flagCases {
t.Run(tc.flag, func(t *testing.T) {
ctx, cancel := testCtx(t)
defer cancel()
cmd := exec.CommandContext(ctx, exe, tc.flag)
out, err := cmd.CombinedOutput()
require.NoError(t, err, "run")
require.Contains(t, string(out), tc.fragment)
})
}
}
type mockTripper struct {
token string
}
func (t *mockTripper) RoundTrip(req *http.Request) (*http.Response, error) {
auth := req.Header.Get("Authorization")
i := len("Bearer ")
if len(auth) > i {
t.token = auth[i:]
}
return nil, fmt.Errorf("oopsie")
}
func TestGHToken(t *testing.T) {
token := "s3cr3t"
t.Setenv(tokenKey, token)
oldTransport := http.DefaultClient.Transport
var mt mockTripper
http.DefaultClient.Transport = &mt
t.Cleanup(func() {
http.DefaultClient.Transport = oldTransport
})
repoDesc("tebeka", "expmod")
require.Equal(t, token, mt.token)
}
func Test_githubRawURL(t *testing.T) {
// TODO: More tests
url := "https://github.com/nxadm/tail/blob/master/go.mod"
expected := "https://raw.githubusercontent.com/nxadm/tail/master/go.mod"
out, err := githubRawURL(url)
require.NoError(t, err)
require.Equal(t, expected, out)
}