-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathfetch-native.test.mjs
More file actions
124 lines (101 loc) · 4.07 KB
/
Copy pathfetch-native.test.mjs
File metadata and controls
124 lines (101 loc) · 4.07 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import test from 'node:test';
const classifier = 'linux-x64';
const version = '1.0.79';
const integrity = 'sha512-test-integrity';
const runtimeContent = 'runtime content';
const cliContent = 'cli content';
const scriptPath = fileURLToPath(new URL('./fetch-native.mjs', import.meta.url));
test('missing CLI does not use incremental fast path', (t) => {
const fixture = createFixture(t);
fs.rmSync(fixture.cliPath);
const result = runScript(fixture);
assertRestagingAttempted(fixture, result);
});
test('stale CLI does not use incremental fast path', (t) => {
const fixture = createFixture(t);
fs.writeFileSync(fixture.cliPath, 'stale CLI content');
const result = runScript(fixture);
assertRestagingAttempted(fixture, result);
});
test('missing platform metadata does not use incremental fast path', (t) => {
const fixture = createFixture(t);
fs.rmSync(fixture.platformPropertiesPath);
const result = runScript(fixture);
assertRestagingAttempted(fixture, result);
});
test('complete matching artifacts use incremental fast path', (t) => {
const fixture = createFixture(t);
const result = runScript(fixture);
assert.equal(result.status, 0, result.stderr);
assert.match(result.stdout, /already staged/);
assert.equal(fs.existsSync(fixture.npmMarkerPath), false);
});
function createFixture(t) {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'fetch-native-test-'));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const repoRoot = path.join(root, 'repo');
const stagingDir = path.join(root, 'staging');
const resourceDir = path.join(stagingDir, classifier, 'native', classifier);
const fakeBinDir = path.join(root, 'bin');
const npmMarkerPath = path.join(root, 'npm-invoked');
fs.mkdirSync(path.join(repoRoot, 'nodejs'), { recursive: true });
fs.mkdirSync(resourceDir, { recursive: true });
fs.mkdirSync(fakeBinDir);
fs.writeFileSync(
path.join(repoRoot, 'nodejs', 'package-lock.json'),
JSON.stringify({
packages: {
[`node_modules/@github/copilot-${classifier}`]: { version, integrity },
},
}),
);
const runtimePath = path.join(resourceDir, 'runtime.node');
const cliPath = path.join(resourceDir, 'copilot');
const platformPropertiesPath = path.join(resourceDir, 'platform.properties');
fs.writeFileSync(runtimePath, runtimeContent);
fs.writeFileSync(cliPath, cliContent);
fs.writeFileSync(platformPropertiesPath, `classifier=${classifier}\nversion=${version}\n`);
fs.writeFileSync(
path.join(stagingDir, classifier, '.version'),
`${version}\n${integrity}\n${digest(runtimeContent)}\n${digest(cliContent)}\n`,
);
const fakeNpmPath = path.join(fakeBinDir, 'npm');
fs.writeFileSync(fakeNpmPath, '#!/bin/sh\nprintf invoked > \"$FETCH_NATIVE_NPM_MARKER\"\nexit 42\n');
fs.chmodSync(fakeNpmPath, 0o755);
return {
repoRoot,
stagingDir,
fakeBinDir,
npmMarkerPath,
runtimePath,
cliPath,
platformPropertiesPath,
};
}
function runScript(fixture) {
return spawnSync(process.execPath, [scriptPath, fixture.repoRoot, fixture.stagingDir, classifier], {
encoding: 'utf8',
env: {
...process.env,
PATH: `${fixture.fakeBinDir}${path.delimiter}${process.env.PATH}`,
FETCH_NATIVE_NPM_MARKER: fixture.npmMarkerPath,
},
});
}
function assertRestagingAttempted(fixture, result) {
assert.notEqual(result.status, 0, 'The fake npm command should make restaging fail');
assert.equal(fs.readFileSync(fixture.npmMarkerPath, 'utf8'), 'invoked');
}
function digest(content) {
return `sha512-${createHash('sha512').update(content).digest('base64')}`;
}