|
1 | 1 | /* eslint-disable @typescript-eslint/prefer-promise-reject-errors */ |
| 2 | +import { Assembly, SchemaVersion } from '@jsii/spec'; |
2 | 3 | import * as childProcess from 'child_process'; |
3 | 4 | import * as fs from 'fs-extra'; |
4 | 5 | import * as os from 'os'; |
@@ -1336,6 +1337,36 @@ defineTest('loading a module twice idepotently succeeds', async (sandbox) => { |
1336 | 1337 | }); |
1337 | 1338 | }); |
1338 | 1339 |
|
| 1340 | +defineTest.skipIf(!!recordingOutput)( |
| 1341 | + 'loading an assembly with unsupported features fails', |
| 1342 | + async (sandbox) => { |
| 1343 | + const testAssembly: Assembly = { |
| 1344 | + author: { |
| 1345 | + name: 'Me', |
| 1346 | + roles: ['owner'], |
| 1347 | + }, |
| 1348 | + description: 'Test', |
| 1349 | + fingerprint: 'asdf', |
| 1350 | + homepage: 'www.example.com', |
| 1351 | + jsiiVersion: '1.20.3', |
| 1352 | + license: 'MIT', |
| 1353 | + name: 'test', |
| 1354 | + repository: { type: 'other', url: 'www.example.com' }, |
| 1355 | + schema: SchemaVersion.LATEST, |
| 1356 | + version: '1.2.3', |
| 1357 | + usedFeatures: ['will-never-be-used' as any], |
| 1358 | + }; |
| 1359 | + |
| 1360 | + await expect(async () => |
| 1361 | + sandbox.load({ |
| 1362 | + tarball: await makeJsiiTarball(testAssembly), |
| 1363 | + name: testAssembly.name, |
| 1364 | + version: testAssembly.version, |
| 1365 | + }), |
| 1366 | + ).rejects.toThrow(/will-never-be-used/); |
| 1367 | + }, |
| 1368 | +); |
| 1369 | + |
1339 | 1370 | defineTest( |
1340 | 1371 | 'fails if trying to load two different versions of the same module', |
1341 | 1372 | async (sandbox) => { |
@@ -2275,27 +2306,57 @@ async function preparePackage(module: string, useCache = true) { |
2275 | 2306 | } |
2276 | 2307 |
|
2277 | 2308 | const packageRoot = findPackageRoot(module); |
2278 | | - await new Promise<void>((ok, ko) => { |
2279 | | - const child = childProcess.spawn('npm', ['pack', packageRoot], { |
2280 | | - cwd: staging, |
| 2309 | + |
| 2310 | + await runNpm(['pack', packageRoot], staging); |
| 2311 | + |
| 2312 | + const dir = path.join(staging, (await fs.readdir(staging))[0]); |
| 2313 | + cache.set(module, dir); |
| 2314 | + return dir; |
| 2315 | +} |
| 2316 | + |
| 2317 | +async function makeJsiiTarball(assembly: Assembly) { |
| 2318 | + const staging = await fs.mkdtemp( |
| 2319 | + path.join(os.tmpdir(), 'jsii-kernel-tests-'), |
| 2320 | + ); |
| 2321 | + |
| 2322 | + // clean up only if we are not recording, so playback can refer to these |
| 2323 | + if (!recordingOutput) { |
| 2324 | + process.on('exit', () => fs.removeSync(staging)); // cleanup |
| 2325 | + } |
| 2326 | + |
| 2327 | + await fs.writeJson(path.join(staging, 'package.json'), { |
| 2328 | + name: assembly.name, |
| 2329 | + version: assembly.version, |
| 2330 | + }); |
| 2331 | + |
| 2332 | + await fs.writeJson(path.join(staging, '.jsii'), assembly); |
| 2333 | + |
| 2334 | + await runNpm(['pack'], staging); |
| 2335 | + |
| 2336 | + const tarballs = (await fs.readdir(staging)).filter((x) => x.endsWith('gz')); |
| 2337 | + |
| 2338 | + return path.join(staging, tarballs[0]); |
| 2339 | +} |
| 2340 | + |
| 2341 | +async function runNpm(args: string[], cwd: string): Promise<string> { |
| 2342 | + return new Promise<string>((ok, ko) => { |
| 2343 | + const child = childProcess.spawn('npm', args, { |
| 2344 | + cwd, |
2281 | 2345 | shell: true, |
2282 | 2346 | stdio: ['ignore', 'pipe', 'ignore'], |
2283 | 2347 | }); |
2284 | 2348 | const stdout = new Array<Buffer>(); |
2285 | 2349 | child.stdout.on('data', (chunk) => stdout.push(Buffer.from(chunk))); |
2286 | 2350 | child.once('close', (code, signal) => { |
2287 | 2351 | if (code === 0) { |
2288 | | - return ok(); |
| 2352 | + return ok(Buffer.concat(stdout).toString()); |
2289 | 2353 | } |
2290 | 2354 | if (code != null) { |
2291 | 2355 | return ko(`'npm pack' exited with code ${code}`); |
2292 | 2356 | } |
2293 | 2357 | return ko(`'npm pack' killed by signal ${signal}`); |
2294 | 2358 | }); |
2295 | 2359 | }); |
2296 | | - const dir = path.join(staging, (await fs.readdir(staging))[0]); |
2297 | | - cache.set(module, dir); |
2298 | | - return dir; |
2299 | 2360 | } |
2300 | 2361 |
|
2301 | 2362 | function findPackageRoot(pkg: string) { |
|
0 commit comments