-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathlockfile.test.ts
More file actions
261 lines (216 loc) · 11.5 KB
/
lockfile.test.ts
File metadata and controls
261 lines (216 loc) · 11.5 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import * as path from 'path';
import * as semver from 'semver';
import { shellExec } from '../testUtils';
import { cpLocal, readLocalFile, rmLocal } from '../../spec-utils/pfs';
const pkg = require('../../../package.json');
describe('Lockfile', function () {
this.timeout('240s');
const tmp = path.relative(process.cwd(), path.join(__dirname, 'tmp'));
const cli = `npx --prefix ${tmp} devcontainer`;
before('Install', async () => {
await shellExec(`rm -rf ${tmp}/node_modules`);
await shellExec(`mkdir -p ${tmp}`);
await shellExec(`npm --prefix ${tmp} install devcontainers-cli-${pkg.version}.tgz`);
});
it('write lockfile', async () => {
const workspaceFolder = path.join(__dirname, 'configs/lockfile');
const lockfilePath = path.join(workspaceFolder, '.devcontainer-lock.json');
await rmLocal(lockfilePath, { force: true });
const res = await shellExec(`${cli} build --workspace-folder ${workspaceFolder} --experimental-lockfile`);
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'success');
const actual = await readLocalFile(lockfilePath);
const expected = await readLocalFile(path.join(workspaceFolder, 'expected.devcontainer-lock.json'));
assert.equal(actual.toString(), expected.toString());
});
it('lockfile with dependencies', async () => {
const workspaceFolder = path.join(__dirname, 'configs/lockfile-dependson');
const lockfilePath = path.join(workspaceFolder, '.devcontainer-lock.json');
await rmLocal(lockfilePath, { force: true });
const res = await shellExec(`${cli} build --workspace-folder ${workspaceFolder} --experimental-lockfile`);
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'success');
const actual = await readLocalFile(lockfilePath);
const expected = await readLocalFile(path.join(workspaceFolder, 'expected.devcontainer-lock.json'));
assert.equal(actual.toString(), expected.toString());
});
it('frozen lockfile', async () => {
const workspaceFolder = path.join(__dirname, 'configs/lockfile-frozen');
const lockfilePath = path.join(workspaceFolder, '.devcontainer-lock.json');
const expected = await readLocalFile(lockfilePath);
const res = await shellExec(`${cli} build --workspace-folder ${workspaceFolder} --experimental-lockfile --experimental-frozen-lockfile`);
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'success');
const actual = await readLocalFile(lockfilePath);
assert.equal(actual.toString(), expected.toString());
});
it('outdated lockfile', async () => {
const workspaceFolder = path.join(__dirname, 'configs/lockfile-outdated');
const lockfilePath = path.join(workspaceFolder, '.devcontainer-lock.json');
await cpLocal(path.join(workspaceFolder, 'original.devcontainer-lock.json'), lockfilePath);
{
try {
throw await shellExec(`${cli} build --workspace-folder ${workspaceFolder} --experimental-lockfile --experimental-frozen-lockfile`);
} catch (res) {
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'error');
}
}
{
const res = await shellExec(`${cli} build --workspace-folder ${workspaceFolder} --experimental-lockfile`);
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'success');
const actual = await readLocalFile(lockfilePath);
const expected = await readLocalFile(path.join(workspaceFolder, 'expected.devcontainer-lock.json'));
assert.equal(actual.toString(), expected.toString());
}
});
it('outdated command with json output', async () => {
const workspaceFolder = path.join(__dirname, 'configs/lockfile-outdated-command');
const res = await shellExec(`${cli} outdated --workspace-folder ${workspaceFolder} --output-format json`);
const response = JSON.parse(res.stdout);
const git = response.features['ghcr.io/devcontainers/features/git:1.0'];
assert.ok(git);
assert.strictEqual(git.current, '1.0.4');
assert.ok(semver.gt(git.wanted, git.current), `semver.gt(${git.wanted}, ${git.current}) is false`);
assert.ok(semver.gt(git.latest, git.wanted), `semver.gt(${git.latest}, ${git.wanted}) is false`);
const lfs = response.features['ghcr.io/devcontainers/features/git-lfs@sha256:24d5802c837b2519b666a8403a9514c7296d769c9607048e9f1e040e7d7e331c'];
assert.ok(lfs);
assert.strictEqual(lfs.current, '1.0.6');
assert.strictEqual(lfs.current, lfs.wanted);
assert.ok(semver.gt(lfs.latest, lfs.wanted), `semver.gt(${lfs.latest}, ${lfs.wanted}) is false`);
const github = response.features['ghcr.io/devcontainers/features/github-cli'];
assert.ok(github);
assert.strictEqual(github.current, github.latest);
assert.strictEqual(github.wanted, github.latest);
const azure = response.features['ghcr.io/devcontainers/features/azure-cli:0'];
assert.ok(azure);
assert.strictEqual(azure.current, undefined);
assert.strictEqual(azure.wanted, undefined);
assert.ok(azure.latest);
const foo = response.features['ghcr.io/codspace/versioning/foo:0.3.1'];
assert.ok(foo);
assert.strictEqual(foo.current, '0.3.1');
assert.strictEqual(foo.wanted, '0.3.1');
assert.strictEqual(foo.wantedMajor, '0');
assert.strictEqual(foo.latest, '2.11.1');
assert.strictEqual(foo.latestMajor, '2');
const doesnotexist = response.features['ghcr.io/codspace/doesnotexist:0.1.2'];
assert.ok(doesnotexist);
assert.strictEqual(doesnotexist.current, undefined);
assert.strictEqual(doesnotexist.wanted, undefined);
assert.strictEqual(doesnotexist.wantedMajor, undefined);
assert.strictEqual(doesnotexist.latest, undefined);
assert.strictEqual(doesnotexist.latestMajor, undefined);
});
it('outdated command with text output', async () => {
const workspaceFolder = path.join(__dirname, 'configs/lockfile-outdated-command');
const res = await shellExec(`${cli} outdated --workspace-folder ${workspaceFolder} --output-format text`);
const response = res.stdout;
// Count number of lines of output
assert.strictEqual(response.split('\n').length, 8); // 5 valid Features + header + empty line
// Check that the header is present
assert.ok(response.includes('Current'), 'Current column is missing');
assert.ok(response.includes('Wanted'), 'Wanted column is missing');
assert.ok(response.includes('Latest'), 'Latest column is missing');
// Check that the features are present
// The version values are checked for correctness in the json variant of this test
assert.ok(response.includes('ghcr.io/devcontainers/features/git'), 'git Feature is missing');
assert.ok(response.includes('ghcr.io/devcontainers/features/git-lfs'), 'git-lfs Feature is missing');
assert.ok(response.includes('ghcr.io/devcontainers/features/github-cli'), 'github-cli Feature is missing');
assert.ok(response.includes('ghcr.io/devcontainers/features/azure-cli'), 'azure-cli Feature is missing');
assert.ok(response.includes('ghcr.io/codspace/versioning/foo'), 'foo Feature is missing');
assert.ok(response.includes('ghcr.io/codspace/doesnotexist'), 'doesnotexist Feature is missing');
// Check that filtered Features are not present
assert.ok(!response.includes('mylocalfeature'));
assert.ok(!response.includes('terraform'));
assert.ok(!response.includes('myfeatures'));
});
it('upgrade command', async () => {
const workspaceFolder = path.join(__dirname, 'configs/lockfile-upgrade-command');
const lockfilePath = path.join(workspaceFolder, '.devcontainer-lock.json');
await cpLocal(path.join(workspaceFolder, 'outdated.devcontainer-lock.json'), lockfilePath);
await shellExec(`${cli} upgrade --workspace-folder ${workspaceFolder}`);
const actual = await readLocalFile(lockfilePath);
const expected = await readLocalFile(path.join(workspaceFolder, 'upgraded.devcontainer-lock.json'));
assert.equal(actual.toString(), expected.toString());
});
it('upgrade command in --dry-run mode', async () => {
const workspaceFolder = path.join(__dirname, 'configs/lockfile-dependson');
const res = await shellExec(`${cli} upgrade --dry-run --workspace-folder ${workspaceFolder}`);
const lockfile = JSON.parse(res.stdout);
assert.ok(lockfile);
assert.ok(lockfile.features);
assert.ok(lockfile.features['ghcr.io/codspace/dependson/A:2']);
});
it('upgrade command with --feature', async () => {
const workspaceFolder = path.join(__dirname, 'configs/lockfile-upgrade-feature');
await cpLocal(path.join(workspaceFolder, 'input.devcontainer.json'), path.join(workspaceFolder, '.devcontainer.json'));
const res = await shellExec(`${cli} upgrade --dry-run --workspace-folder ${workspaceFolder} --feature ghcr.io/codspace/versioning/foo --target-version 2`);
// Check devcontainer.json was updated
const actual = await readLocalFile(path.join(workspaceFolder, '.devcontainer.json'));
const expected = await readLocalFile(path.join(workspaceFolder, 'expected.devcontainer.json'));
assert.equal(actual.toString(), expected.toString());
// Check lockfile was updated
const lockfile = JSON.parse(res.stdout);
assert.ok(lockfile);
assert.ok(lockfile.features);
assert.ok(lockfile.features['ghcr.io/codspace/versioning/foo:2'].version === '2.11.1');
});
it('OCI feature integrity', async () => {
const workspaceFolder = path.join(__dirname, 'configs/lockfile-oci-integrity');
try {
throw await shellExec(`${cli} build --workspace-folder ${workspaceFolder} --experimental-lockfile`);
} catch (res) {
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'error');
}
});
it('tarball URI feature integrity', async () => {
const workspaceFolder = path.join(__dirname, 'configs/lockfile-tarball-integrity');
try {
throw await shellExec(`${cli} build --workspace-folder ${workspaceFolder} --experimental-lockfile`);
} catch (res) {
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'error');
}
});
it('empty lockfile should init', async () => {
const workspaceFolder = path.join(__dirname, 'configs/lockfile-generate-from-empty-file');
const lockfilePath = path.join(workspaceFolder, '.devcontainer', 'devcontainer-lock.json');
const cleanup = async () => {
await rmLocal(lockfilePath, { force: true });
await shellExec(`touch ${lockfilePath}`);
};
await cleanup();
const res = await shellExec(`${cli} build --workspace-folder ${workspaceFolder}`);
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'success');
const actual = JSON.parse((await readLocalFile(lockfilePath)).toString());
assert.ok(actual.features['ghcr.io/devcontainers/features/dotnet:2']);
await cleanup();
});
it('empty lockfile should not init when frozen', async () => {
const workspaceFolder = path.join(__dirname, 'configs/lockfile-generate-from-empty-file-frozen');
const lockfilePath = path.join(workspaceFolder, '.devcontainer', 'devcontainer-lock.json');
const cleanup = async () => {
await rmLocal(lockfilePath, { force: true });
await shellExec(`touch ${lockfilePath}`);
};
await cleanup();
try {
await shellExec(`${cli} build --workspace-folder ${workspaceFolder} --experimental-frozen-lockfile`);
await cleanup();
} catch (res) {
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'error');
assert.equal(response.message, 'Lockfile does not match.');
await cleanup();
}
});
});