/
index.spec.ts
194 lines (186 loc) · 6.2 KB
/
index.spec.ts
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
import { ReleaseResult, getPkgReleases } from '..';
import * as httpMock from '../../../test/httpMock';
import { mocked, partial } from '../../../test/util';
import * as _github from '../github-tags';
import { id as datasource, getDigest } from '.';
jest.mock('../github-tags');
const github = mocked(_github);
const res1 = `<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="golang.org/x/text git https://go.googlesource.com/text">
<meta name="go-source" content="golang.org/x/text https://github.com/golang/text/ https://github.com/golang/text/tree/master{/dir} https://github.com/golang/text/blob/master{/dir}/{file}#L{line}">
<meta http-equiv="refresh" content="0; url=https://godoc.org/golang.org/x/text">
</head>
<body>
Nothing to see here; <a href="https://godoc.org/golang.org/x/text">move along</a>.
</body>
</html>`;
describe('datasource/go', () => {
beforeEach(() => {
httpMock.setup();
});
afterEach(() => {
httpMock.reset();
});
describe('getDigest', () => {
it('returns null for wrong name', async () => {
httpMock
.scope('https://golang.org/')
.get('/y/text?go-get=1')
.reply(200, res1);
github.getDigest.mockResolvedValueOnce('abcdefabcdefabcdefabcdef');
const res = await getDigest({ lookupName: 'golang.org/y/text' }, null);
expect(res).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns digest', async () => {
httpMock
.scope('https://golang.org/')
.get('/x/text?go-get=1')
.reply(200, res1);
github.getDigest.mockResolvedValueOnce('abcdefabcdefabcdefabcdef');
const res = await getDigest({ lookupName: 'golang.org/x/text' }, null);
expect(res).toBe('abcdefabcdefabcdefabcdef');
expect(httpMock.getTrace()).toMatchSnapshot();
});
});
describe('getReleases', () => {
it('returns null for empty result', async () => {
httpMock
.scope('https://golang.org/')
.get('/foo/something?go-get=1')
.reply(200, res1);
expect(
await getPkgReleases({
datasource,
depName: 'golang.org/foo/something',
})
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for 404', async () => {
httpMock
.scope('https://golang.org/')
.get('/foo/something?go-get=1')
.reply(404);
expect(
await getPkgReleases({
datasource,
depName: 'golang.org/foo/something',
})
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for unknown error', async () => {
httpMock
.scope('https://golang.org/')
.get('/foo/something?go-get=1')
.replyWithError('error');
expect(
await getPkgReleases({
datasource,
depName: 'golang.org/foo/something',
})
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('processes real data', async () => {
httpMock
.scope('https://golang.org/')
.get('/x/text?go-get=1')
.reply(200, res1);
github.getReleases.mockResolvedValueOnce({
releases: [{ version: 'v1.0.0' }, { version: 'v2.0.0' }],
});
const res = await getPkgReleases({
datasource,
depName: 'golang.org/x/text',
});
expect(res).toMatchSnapshot();
expect(res).not.toBeNull();
expect(res).toBeDefined();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('skips wrong package', async () => {
httpMock
.scope('https://golang.org/')
.get('/x/sys?go-get=1')
.reply(200, res1);
const res = await getPkgReleases({
datasource,
depName: 'golang.org/x/sys',
});
expect(res).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('skips unsupported platform', async () => {
httpMock
.scope('https://golang.org/')
.get('/x/text?go-get=1')
.reply(
200,
res1.replace(
'https://github.com/golang/text/',
'https://google.com/golang/text/'
)
);
const res = await getPkgReleases({
datasource,
depName: 'golang.org/x/text',
});
expect(res).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('works for known servers', async () => {
github.getReleases.mockClear();
const packages = [
{ datasource, depName: 'github.com/x/text' },
{ datasource, depName: 'gopkg.in/x/text' },
{ datasource, depName: 'gopkg.in/x' },
];
const githubRes = {
releases: [],
} as any;
for (const pkg of packages) {
github.getReleases.mockResolvedValueOnce(
partial<ReleaseResult>(githubRes)
);
expect(await getPkgReleases(pkg)).toBeNull();
}
expect(github.getReleases.mock.calls).toMatchSnapshot();
});
it('works for nested modules on github', async () => {
github.getReleases.mockClear();
const packages = [
{ datasource, depName: 'github.com/x/text/a' },
{ datasource, depName: 'github.com/x/text/b' },
];
for (const pkg of packages) {
github.getReleases.mockResolvedValueOnce({
releases: [{ version: 'a/v1.0.0' }, { version: 'b/v2.0.0' }],
});
const prefix = pkg.depName.split('/')[3];
const result = await getPkgReleases(pkg);
expect(result.releases).toHaveLength(1);
expect(result.releases[0].version.startsWith(prefix)).toBeFalse();
}
expect(github.getReleases.mock.calls).toMatchSnapshot();
});
it('falls back to old behaviour', async () => {
github.getReleases.mockClear();
const packages = [
{ datasource, depName: 'github.com/x/text/a' },
{ datasource, depName: 'github.com/x/text/b' },
];
const releases = {
releases: [{ version: 'v1.0.0' }, { version: 'v2.0.0' }],
};
for (const pkg of packages) {
github.getReleases.mockResolvedValueOnce(releases);
expect(await getPkgReleases(pkg)).toStrictEqual(releases);
}
});
});
});