/
index.spec.ts
181 lines (168 loc) · 5.17 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
import { getPkgReleases } from '..';
import { Fixtures } from '../../../../test/fixtures';
import * as httpMock from '../../../../test/http-mock';
import { EXTERNAL_HOST_ERROR } from '../../../constants/error-messages';
import { datasource, defaultRegistryUrl } from './common';
describe('modules/datasource/hexpm-bob/index', () => {
describe('getReleases', () => {
it('throws for error', async () => {
httpMock
.scope(defaultRegistryUrl)
.get('/builds/elixir/builds.txt')
.replyWithError('error');
await expect(
getPkgReleases({
datasource,
packageName: 'elixir',
})
).rejects.toThrow(EXTERNAL_HOST_ERROR);
});
it('returns null for 404', async () => {
httpMock
.scope(defaultRegistryUrl)
.get('/builds/elixir/builds.txt')
.reply(404);
expect(
await getPkgReleases({
datasource,
packageName: 'elixir',
})
).toBeNull();
});
it('returns null for empty result', async () => {
httpMock
.scope(defaultRegistryUrl)
.get('/builds/elixir/builds.txt')
.reply(200, '');
expect(
await getPkgReleases({
datasource,
packageName: 'elixir',
})
).toBeNull();
});
it('returns empty list for empty 200 OK', async () => {
httpMock
.scope(defaultRegistryUrl)
.get('/builds/elixir/builds.txt')
.reply(200, '');
expect(
await getPkgReleases({
datasource,
packageName: 'elixir',
})
).toBeNull();
});
it('throws for 5xx', async () => {
httpMock
.scope(defaultRegistryUrl)
.get('/builds/elixir/builds.txt')
.reply(502);
await expect(
getPkgReleases({
datasource,
packageName: 'elixir',
})
).rejects.toThrow(EXTERNAL_HOST_ERROR);
});
it('processes real data', async () => {
httpMock
.scope(defaultRegistryUrl)
.get('/builds/elixir/builds.txt')
.reply(200, Fixtures.get('elixir/builds.txt'));
const res = await getPkgReleases({
datasource,
packageName: 'elixir',
});
expect(res).toEqual({
homepage: 'https://elixir-lang.org/',
registryUrl: 'https://builds.hex.pm',
releases: [
{
gitRef: '185eeec5ecbc2a0c8d9b8b97cb2d23108615ffdb',
isStable: false,
releaseTimestamp: '2022-08-15T10:28:05.000Z',
version: '1.14.0-rc.1',
},
{
gitRef: '185eeec5ecbc2a0c8d9b8b97cb2d23108615ffdb',
isStable: false,
releaseTimestamp: '2022-08-15T10:25:05.000Z',
version: '1.14.0-rc.1-otp-25',
},
{
gitRef: 'c5151e6890b5ac8df13276459696f0f47a8e634b',
isStable: true,
releaseTimestamp: '2022-09-01T18:24:21.000Z',
version: '1.14.0',
},
{
gitRef: 'a285e5876b6f69e7c340d04851aae63c8a32bf1d',
isStable: true,
releaseTimestamp: '2022-10-10T22:26:05.000Z',
version: '1.14.1-otp-25',
},
{
gitRef: 'a285e5876b6f69e7c340d04851aae63c8a32bf1d',
isStable: true,
releaseTimestamp: '2022-10-10T22:27:46.000Z',
version: '1.14.1',
},
],
sourceUrl: 'https://github.com/elixir-lang/elixir',
});
});
it('processes real data (erlang / ubuntu 20.04)', async () => {
httpMock
.scope(defaultRegistryUrl)
.get('/builds/otp/ubuntu-20.04/builds.txt')
.reply(200, Fixtures.get('otp/ubuntu-20.04/builds.txt'));
const res = await getPkgReleases({
datasource,
packageName: 'otp/ubuntu-20.04',
versioning:
'regex:^(?<major>\\d+?)\\.(?<minor>\\d+?)(\\.(?<patch>\\d+))?$',
});
expect(res).toEqual({
homepage: 'https://www.erlang.org/',
registryUrl: 'https://builds.hex.pm',
releases: [
{
gitRef: '6efb5e31df6bc512ed6c466584ef15b846dcecab',
isStable: true,
releaseTimestamp: '2022-09-21T09:54:48.000Z',
version: '25.1',
},
{
gitRef: '38ad8e28421c745c06ef9bd55f6e6204cd1f15ef',
isStable: true,
releaseTimestamp: '2022-10-24T13:23:18.000Z',
version: '25.1.2',
},
],
sourceUrl: 'https://github.com/erlang/otp',
});
});
it('can override registry url', async () => {
const registryUrl = 'https://repo.example.com/';
httpMock
.scope(registryUrl)
.get('/builds/otp/ubuntu-20.04/builds.txt')
.reply(200, Fixtures.get('otp/ubuntu-20.04/builds.txt'));
const res = await getPkgReleases({
datasource,
packageName: 'otp/ubuntu-20.04',
registryUrls: [registryUrl],
});
expect(res?.releases).toHaveLength(1);
});
it('returns empty list for invalid package name', async () => {
expect(
await getPkgReleases({
datasource,
packageName: 'invalid',
})
).toBeNull();
});
});
});