|
| 1 | +import child = require('child_process'); |
| 2 | +import fs = require('fs'); |
| 3 | +import { api } from 'jsii-kernel'; |
| 4 | +import spec = require('jsii-spec'); |
| 5 | +import path = require('path'); |
| 6 | +import { KernelHost, InputOutput, Input, Output } from '../lib'; |
| 7 | + |
| 8 | +test('can load libraries from within a callback', () => { |
| 9 | + const inout = new TestInputOutput( |
| 10 | + [ |
| 11 | + { api: 'load', ...loadRequest('@scope/jsii-calc-base') }, |
| 12 | + { api: 'load', ...loadRequest('@scope/jsii-calc-lib') }, |
| 13 | + { api: 'create', fqn: 'Object', interfaces: ['@scope/jsii-calc-lib.IFriendly'], overrides: [{ method: 'hello' }] }, |
| 14 | + { api: 'invoke', objref: { [api.TOKEN_REF]: 'Object@10000' }, method: 'hello' }, |
| 15 | + { api: 'load', ...loadRequest('jsii-calc') }, |
| 16 | + { complete: { cbid: 'jsii::callback::20000', result: 'SUCCESS!' } }, |
| 17 | + ] |
| 18 | + ); |
| 19 | + const host = new KernelHost(inout, { noStack: true, debug: false }); |
| 20 | + return new Promise<void>(ok => { |
| 21 | + host.on('exit', () => ok(inout.expectCompleted())); |
| 22 | + host.run(); |
| 23 | + }); |
| 24 | +}); |
| 25 | + |
| 26 | +class TestInputOutput extends InputOutput { |
| 27 | + private readonly inputCommands: Input[]; |
| 28 | + |
| 29 | + public constructor(inputCommands: Input[], private readonly allowErrors = false) { |
| 30 | + super(); |
| 31 | + this.inputCommands = inputCommands.reverse(); |
| 32 | + } |
| 33 | + |
| 34 | + public read(): Input | undefined { |
| 35 | + return this.inputCommands.pop(); |
| 36 | + } |
| 37 | + |
| 38 | + public write(obj: Output): void { |
| 39 | + if (!this.allowErrors) { |
| 40 | + expect(obj).not.toHaveProperty('error'); |
| 41 | + } |
| 42 | + if ('ok' in obj && 'assembly' in obj.ok) { |
| 43 | + // Removing the type count as this is subject to change! |
| 44 | + (obj.ok as any).types = '*redacted*'; |
| 45 | + } |
| 46 | + expect(obj).toMatchSnapshot(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Validates that all inputs have been consumed, and all expected outputs have been checked. |
| 51 | + */ |
| 52 | + public expectCompleted(): void { |
| 53 | + expect(this.inputCommands).toEqual([]); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function loadRequest(library: string): api.LoadRequest { |
| 58 | + const assembly = loadAssembly(); |
| 59 | + const tarball = path.join(__dirname, '_tarballs', library, `${assembly.fingerprint.replace('/', '_')}.tgz`); |
| 60 | + if (!fs.existsSync(tarball)) { |
| 61 | + packageLibrary(tarball); |
| 62 | + } |
| 63 | + return { |
| 64 | + name: assembly.name, |
| 65 | + version: assembly.version, |
| 66 | + tarball, |
| 67 | + }; |
| 68 | + |
| 69 | + function loadAssembly(): spec.Assembly { |
| 70 | + const assemblyFile = path.resolve(require.resolve(`${library}/package.json`), '..', '.jsii'); |
| 71 | + return JSON.parse(fs.readFileSync(assemblyFile, { encoding: 'utf-8' })); |
| 72 | + } |
| 73 | + |
| 74 | + function packageLibrary(target: string): void { |
| 75 | + const targetDir = path.dirname(target); |
| 76 | + fs.mkdirSync(targetDir, { recursive: true }); |
| 77 | + const result = child.spawnSync('npm', ['pack', path.dirname(require.resolve(`${library}/package.json`))], { cwd: targetDir, stdio: ['inherit', 'pipe', 'pipe'] }); |
| 78 | + if (result.error) { |
| 79 | + throw result.error; |
| 80 | + } |
| 81 | + if (result.status !== 0) { |
| 82 | + console.error(result.stderr.toString('utf-8')); |
| 83 | + throw new Error(`Unable to 'npm pack' ${library}: process ${result.signal != null ? `killed by ${result.signal}` : `exited with code ${result.status}`}`); |
| 84 | + } |
| 85 | + fs.renameSync(path.join(targetDir, result.stdout.toString('utf-8').trim()), target); |
| 86 | + } |
| 87 | +} |
0 commit comments