From dcfe86387b51cb9bb01d1cdfc087cbe8975f6290 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 2 Oct 2020 07:49:45 +0000 Subject: [PATCH] feat: add unit test --- packages/@jsii/kernel/lib/api.ts | 10 ++--- packages/@jsii/kernel/lib/kernel.ts | 43 ++++++++++++++----- packages/@jsii/kernel/test/kernel.test.ts | 9 ++++ .../python-runtime/tests/test_compliance.py | 1 + packages/jsii-calc/bin/calc | 2 + packages/jsii-calc/bin/calc.ts | 5 +++ packages/jsii-calc/package.json | 3 ++ packages/jsii-calc/test/assembly.jsii | 5 ++- packages/jsii-pacmak/lib/targets/python.ts | 14 +++--- .../__snapshots__/target-dotnet.test.ts.snap | 5 ++- .../__snapshots__/target-python.test.ts.snap | 28 ++++++++++-- packages/jsii/lib/assembler.ts | 2 +- 12 files changed, 97 insertions(+), 30 deletions(-) create mode 100755 packages/jsii-calc/bin/calc create mode 100644 packages/jsii-calc/bin/calc.ts diff --git a/packages/@jsii/kernel/lib/api.ts b/packages/@jsii/kernel/lib/api.ts index 57b28d3f57..113077df57 100644 --- a/packages/@jsii/kernel/lib/api.ts +++ b/packages/@jsii/kernel/lib/api.ts @@ -101,16 +101,16 @@ export interface LoadResponse { } export interface InvokeScriptRequest { - readonly pkgname: string; + readonly assembly: string; readonly script: string; - readonly args?: any[]; + readonly args?: string[]; } export interface InvokeScriptResponse { readonly status: number | null; - readonly stdout: string; - readonly stderr: string; - readonly output: string[]; + readonly stdout: Buffer; + readonly stderr: Buffer; + readonly signal: string | null; } export interface CreateRequest { diff --git a/packages/@jsii/kernel/lib/kernel.ts b/packages/@jsii/kernel/lib/kernel.ts index f7d1546660..82fd987068 100644 --- a/packages/@jsii/kernel/lib/kernel.ts +++ b/packages/@jsii/kernel/lib/kernel.ts @@ -139,17 +139,40 @@ export class Kernel { public invokeBinScript( req: api.InvokeScriptRequest, ): api.InvokeScriptResponse { - const result = cp.spawnSync( - path.join(this._getPackageDir(req.pkgname), req.script), - req.args, - ); + const packageDir = this._getPackageDir(req.assembly); + if (fs.pathExistsSync(packageDir)) { + // module exists, verify version + const epkg = fs.readJsonSync(path.join(packageDir, 'package.json')); - return { - output: result.output, - stdout: result.stdout, - stderr: result.stderr, - status: result.status, - }; + if (!epkg.bin) { + throw new Error('There is no bin scripts defined for this package.'); + } + + const scriptPath = epkg.bin[req.script]; + + if (!epkg.bin) { + throw new Error(`Script with name ${req.script} was not defined.`); + } + + const result = cp.spawnSync( + process.execPath, + [ + ...process.execArgv, + '--', + path.join(packageDir, scriptPath), + ...(req.args ?? []), + ], + { encoding: 'utf-8' }, + ); + + return { + stdout: result.stdout, + stderr: result.stderr, + status: result.status, + signal: result.signal, + }; + } + throw new Error(`Package with name ${req.assembly} was not loaded.`); } public create(req: api.CreateRequest): api.CreateResponse { diff --git a/packages/@jsii/kernel/test/kernel.test.ts b/packages/@jsii/kernel/test/kernel.test.ts index 842e814810..55b91f9047 100644 --- a/packages/@jsii/kernel/test/kernel.test.ts +++ b/packages/@jsii/kernel/test/kernel.test.ts @@ -2133,6 +2133,15 @@ defineTest('Override transitive property', (sandbox) => { expect(propValue).toBe('N3W'); }); +defineTest('invokeBinScript() return output', (sandbox) => { + const result = sandbox.invokeBinScript({ + assembly: 'jsii-calc', + script: 'calc', + }); + + expect(result.stdout).toEqual('Hello World!\n'); +}); + // ================================================================================================= const testNames: { [name: string]: boolean } = {}; diff --git a/packages/@jsii/python-runtime/tests/test_compliance.py b/packages/@jsii/python-runtime/tests/test_compliance.py index 9262679811..99a9ccc60d 100644 --- a/packages/@jsii/python-runtime/tests/test_compliance.py +++ b/packages/@jsii/python-runtime/tests/test_compliance.py @@ -85,6 +85,7 @@ from scope.jsii_calc_lib import IFriendly, EnumFromScopedModule, Number from scope.jsii_calc_lib.custom_submodule_name import IReflectable, ReflectableEntry +from subprocess import Popen, PIPE, STDOUT # Note: The names of these test functions have been chosen to map as closely to the # Java Compliance tests as possible. diff --git a/packages/jsii-calc/bin/calc b/packages/jsii-calc/bin/calc new file mode 100755 index 0000000000..fdfa40a1b7 --- /dev/null +++ b/packages/jsii-calc/bin/calc @@ -0,0 +1,2 @@ +#!/usr/bin/env node +require('./calc.js'); \ No newline at end of file diff --git a/packages/jsii-calc/bin/calc.ts b/packages/jsii-calc/bin/calc.ts new file mode 100644 index 0000000000..296a77b93d --- /dev/null +++ b/packages/jsii-calc/bin/calc.ts @@ -0,0 +1,5 @@ +#!/usr/bin/env node + +/* eslint-disable no-console */ + +console.info('Hello World!'); diff --git a/packages/jsii-calc/package.json b/packages/jsii-calc/package.json index 6863d3c036..fb622b707c 100644 --- a/packages/jsii-calc/package.json +++ b/packages/jsii-calc/package.json @@ -14,6 +14,9 @@ "bugs": { "url": "https://github.com/aws/jsii/issues" }, + "bin": { + "calc": "bin/calc" + }, "repository": { "type": "git", "url": "https://github.com/aws/jsii.git", diff --git a/packages/jsii-calc/test/assembly.jsii b/packages/jsii-calc/test/assembly.jsii index 28f05c35b4..6fc25c4244 100644 --- a/packages/jsii-calc/test/assembly.jsii +++ b/packages/jsii-calc/test/assembly.jsii @@ -7,6 +7,9 @@ ], "url": "https://aws.amazon.com" }, + "bin": { + "calc": "bin/calc" + }, "bundled": { "@fixtures/jsii-calc-bundled": "^0.19.0" }, @@ -14229,5 +14232,5 @@ } }, "version": "0.0.0", - "fingerprint": "azqNkkl+/4FLzTVBLkOyHcokS4xLoYtHsri0z9kIehQ=" + "fingerprint": "QHc8YZS13IljwCQpg6AZlBxIZvkAbfnCFh6Vi+e0Cgg=" } diff --git a/packages/jsii-pacmak/lib/targets/python.ts b/packages/jsii-pacmak/lib/targets/python.ts index 5467b7fefb..978a16725e 100644 --- a/packages/jsii-pacmak/lib/targets/python.ts +++ b/packages/jsii-pacmak/lib/targets/python.ts @@ -1482,10 +1482,10 @@ class PythonModule implements PythonType { * Emit the bin scripts if bin section defined. */ public emitBinScripts(code: CodeMaker): string[] { - const scripts: string[] = []; + const scripts = new Array(); if (this.loadAssembly) { if (this.assembly.bin != null) { - for (const [name, script_path] of Object.entries(this.assembly.bin)) { + for (const name of Object.keys(this.assembly.bin)) { const script_file = path.join( 'src', pythonModuleNameToFilename(this.pythonName), @@ -1515,7 +1515,7 @@ class PythonModule implements PythonType { '__jsii_assembly__.invokeBinScript(', [ JSON.stringify(this.assembly.name), - JSON.stringify(script_path), + JSON.stringify(name), 'sys.argv[1:]', ], ')', @@ -1681,7 +1681,7 @@ class Package { a.pythonName.localeCompare(b.pythonName), ); - const scripts: string[] = []; + const scripts = new Array(); // Iterate over all of our modules, and write them out to disk. for (const mod of modules) { @@ -1695,11 +1695,7 @@ class Package { mod.emit(code, context); code.closeFile(filename); - for (const script of mod.emitBinScripts(code)) { - if (scripts.indexOf(script) < 0) { - scripts.push(script); - } - } + Array.prototype.push.apply(scripts, mod.emitBinScripts(code)); } // Handle our package data. diff --git a/packages/jsii-pacmak/test/generated-code/__snapshots__/target-dotnet.test.ts.snap b/packages/jsii-pacmak/test/generated-code/__snapshots__/target-dotnet.test.ts.snap index 3381d9c333..c4520ea15e 100644 --- a/packages/jsii-pacmak/test/generated-code/__snapshots__/target-dotnet.test.ts.snap +++ b/packages/jsii-pacmak/test/generated-code/__snapshots__/target-dotnet.test.ts.snap @@ -3351,6 +3351,9 @@ exports[`Generated code for "jsii-calc": /dotnet/Amazon.JSII.Tests.Calcu ], "url": "https://aws.amazon.com" }, + "bin": { + "calc": "bin/calc" + }, "bundled": { "@fixtures/jsii-calc-bundled": "^0.19.0" }, @@ -17573,7 +17576,7 @@ exports[`Generated code for "jsii-calc": /dotnet/Amazon.JSII.Tests.Calcu } }, "version": "0.0.0", - "fingerprint": "azqNkkl+/4FLzTVBLkOyHcokS4xLoYtHsri0z9kIehQ=" + "fingerprint": "QHc8YZS13IljwCQpg6AZlBxIZvkAbfnCFh6Vi+e0Cgg=" } `; diff --git a/packages/jsii-pacmak/test/generated-code/__snapshots__/target-python.test.ts.snap b/packages/jsii-pacmak/test/generated-code/__snapshots__/target-python.test.ts.snap index 747d079916..b1be333630 100644 --- a/packages/jsii-pacmak/test/generated-code/__snapshots__/target-python.test.ts.snap +++ b/packages/jsii-pacmak/test/generated-code/__snapshots__/target-python.test.ts.snap @@ -85,7 +85,8 @@ kwargs = json.loads( "Programming Language :: Python :: 3.8", "Typing :: Typed", "License :: OSI Approved" - ] + ], + "scripts": [] } """ ) @@ -331,7 +332,8 @@ kwargs = json.loads( "Programming Language :: Python :: 3.8", "Typing :: Typed", "License :: OSI Approved" - ] + ], + "scripts": [] } """ ) @@ -573,7 +575,8 @@ kwargs = json.loads( "Typing :: Typed", "Development Status :: 7 - Inactive", "License :: OSI Approved" - ] + ], + "scripts": [] } """ ) @@ -1284,6 +1287,8 @@ exports[`Generated code for "jsii-calc": / 1`] = ` ┣━ 📄 __init__.py ┣━ 📁 _jsii ┃ ┣━ 📄 __init__.py + ┃ ┣━ 📁 bin + ┃ ┃ ┗━ 📄 calc ┃ ┗━ 📄 jsii-calc@0.0.0.jsii.tgz ┣━ 📁 composition ┃ ┗━ 📄 __init__.py @@ -1419,6 +1424,9 @@ kwargs = json.loads( "Development Status :: 5 - Production/Stable", "License :: OSI Approved", "Test :: Classifier :: Is Dummy" + ], + "scripts": [ + "src/jsii_calc/_jsii/bin/calc" ] } """ @@ -8738,6 +8746,20 @@ publication.publish() `; +exports[`Generated code for "jsii-calc": /python/src/jsii_calc/_jsii/bin/calc 1`] = ` +#!/usr/bin/env python + +import jsii +import sys + +__jsii_assembly__ = jsii.JSIIAssembly.load( + "jsii-calc", "0.0.0", "jsii_calc", "jsii-calc@0.0.0.jsii.tgz" +) + +__jsii_assembly__.invokeBinScript("jsii-calc", "calc", sys.argv[1:]) + +`; + exports[`Generated code for "jsii-calc": /python/src/jsii_calc/_jsii/jsii-calc@0.0.0.jsii.tgz 1`] = `python/src/jsii_calc/_jsii/jsii-calc@0.0.0.jsii.tgz is a tarball`; exports[`Generated code for "jsii-calc": /python/src/jsii_calc/composition/__init__.py 1`] = ` diff --git a/packages/jsii/lib/assembler.ts b/packages/jsii/lib/assembler.ts index ef1d5b76d3..523f11494d 100644 --- a/packages/jsii/lib/assembler.ts +++ b/packages/jsii/lib/assembler.ts @@ -200,8 +200,8 @@ export class Assembler implements Emitter { docs, readme, jsiiVersion, - fingerprint: '', bin: this.projectInfo.bin, + fingerprint: '', }; const validator = new Validator(this.projectInfo, assembly);