-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
buildpack.spec.ts
187 lines (168 loc) · 5.47 KB
/
buildpack.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
import { mocked } from '../../../test/util';
import { GlobalConfig } from '../../config/global';
import * as _datasource from '../../modules/datasource';
import {
generateInstallCommands,
isDynamicInstall,
resolveConstraint,
} from './buildpack';
import type { ToolConstraint } from './types';
jest.mock('../../modules/datasource');
const datasource = mocked(_datasource);
describe('util/exec/buildpack', () => {
describe('isDynamicInstall()', () => {
beforeEach(() => {
GlobalConfig.reset();
delete process.env.BUILDPACK;
});
it('returns false if binarySource is not install', () => {
expect(isDynamicInstall()).toBeFalse();
});
it('returns false if not buildpack', () => {
GlobalConfig.set({ binarySource: 'install' });
expect(isDynamicInstall()).toBeFalse();
});
it('returns false if any unsupported tools', () => {
GlobalConfig.set({ binarySource: 'install' });
process.env.BUILDPACK = 'true';
const toolConstraints: ToolConstraint[] = [
{ toolName: 'node' },
{ toolName: 'invalid' },
];
expect(isDynamicInstall(toolConstraints)).toBeFalse();
});
it('returns true if supported tools', () => {
GlobalConfig.set({ binarySource: 'install' });
process.env.BUILDPACK = 'true';
const toolConstraints: ToolConstraint[] = [{ toolName: 'npm' }];
expect(isDynamicInstall(toolConstraints)).toBeTrue();
});
});
describe('resolveConstraint()', () => {
it('returns from config', async () => {
expect(
await resolveConstraint({ toolName: 'composer', constraint: '1.1.0' })
).toBe('1.1.0');
});
it('returns highest stable', async () => {
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [
{ version: '1.0.0' },
{ version: '1.1.0' },
{ version: '1.3.0' },
{ version: '2.0.14' },
{ version: '2.1.0' },
{ version: '2.2.0-pre.0' },
],
});
expect(await resolveConstraint({ toolName: 'composer' })).toBe('2.1.0');
});
it('returns highest unstable', async () => {
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [{ version: '2.0.14-b.1' }, { version: '2.1.0-a.1' }],
});
expect(await resolveConstraint({ toolName: 'composer' })).toBe(
'2.1.0-a.1'
);
});
it('respects latest', async () => {
datasource.getPkgReleases.mockResolvedValueOnce({
tags: {
latest: '2.0.14',
},
releases: [
{ version: '1.0.0' },
{ version: '1.1.0' },
{ version: '1.3.0' },
{ version: '2.0.14' },
{ version: '2.1.0' },
{ version: '2.2.0-pre.0' },
],
});
expect(await resolveConstraint({ toolName: 'composer' })).toBe('2.0.14');
});
it('throws for unknown tools', async () => {
await expect(resolveConstraint({ toolName: 'whoops' })).rejects.toThrow(
'Invalid tool to install: whoops'
);
});
it('throws no releases', async () => {
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [],
});
await expect(resolveConstraint({ toolName: 'composer' })).rejects.toThrow(
'No tool releases found.'
);
});
it('falls back to latest version if no compatible release', async () => {
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [{ version: '1.2.3' }],
});
expect(
await resolveConstraint({ toolName: 'composer', constraint: '^3.1.0' })
).toBe('1.2.3');
});
it('falls back to latest version if invalid constraint', async () => {
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [{ version: '1.2.3' }],
});
expect(
await resolveConstraint({ toolName: 'composer', constraint: 'whoops' })
).toBe('1.2.3');
});
it.each`
version | expected
${'^3.9.0'} | ${'3.10.4'}
${'^3.9'} | ${'3.10.4'}
${'3.9.*'} | ${'3.9.1'}
${'>3.8,<3.10'} | ${'3.9.1'}
${'==3.9.*'} | ${'3.9.1'}
`(
'supports python ranges "$version" => "$expected"',
async ({ version: constraint, expected }) => {
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [
{ version: '3.9.0' },
{ version: '3.9.1' },
{ version: '3.10.0' },
{ version: '3.10.4' },
],
});
expect(
await resolveConstraint({ toolName: 'python', constraint })
).toBe(expected);
}
);
});
describe('generateInstallCommands()', () => {
beforeEach(() => {
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [
{ version: '1.0.0' },
{ version: '1.1.0' },
{ version: '1.3.0' },
{ version: '2.0.14' },
{ version: '2.1.0' },
],
});
});
it('returns install commands', async () => {
const toolConstraints: ToolConstraint[] = [
{
toolName: 'composer',
},
];
expect(await generateInstallCommands(toolConstraints)).toEqual([
'install-tool composer 2.1.0',
]);
});
it('hashes npm', async () => {
const toolConstraints: ToolConstraint[] = [{ toolName: 'npm' }];
const res = await generateInstallCommands(toolConstraints);
expect(res).toEqual([
'install-tool npm 2.1.0',
'hash -d npm 2>/dev/null || true',
]);
});
});
});