From af6001973ff7aa04efa749cb6d1b32c2ff0795e2 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 | 6 ++-- packages/@jsii/kernel/lib/kernel.ts | 9 ++++-- packages/@jsii/kernel/test/kernel.test.ts | 11 ++++++++ .../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 +++- .../__snapshots__/target-dotnet.test.ts.snap | 5 +++- .../__snapshots__/target-python.test.ts.snap | 28 +++++++++++++++++-- 10 files changed, 64 insertions(+), 11 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..a09701ab23 100644 --- a/packages/@jsii/kernel/lib/api.ts +++ b/packages/@jsii/kernel/lib/api.ts @@ -103,13 +103,13 @@ export interface LoadResponse { export interface InvokeScriptRequest { readonly pkgname: string; readonly script: string; - readonly args?: any[]; + readonly args?: string[]; } export interface InvokeScriptResponse { readonly status: number | null; - readonly stdout: string; - readonly stderr: string; + readonly stdout: Buffer; + readonly stderr: Buffer; readonly output: string[]; } diff --git a/packages/@jsii/kernel/lib/kernel.ts b/packages/@jsii/kernel/lib/kernel.ts index f7d1546660..bd7ea32493 100644 --- a/packages/@jsii/kernel/lib/kernel.ts +++ b/packages/@jsii/kernel/lib/kernel.ts @@ -140,12 +140,15 @@ export class Kernel { req: api.InvokeScriptRequest, ): api.InvokeScriptResponse { const result = cp.spawnSync( - path.join(this._getPackageDir(req.pkgname), req.script), - req.args, + 'node', + [path.join(this._getPackageDir(req.pkgname), req.script)].concat( + req.args ?? [], + ), + { encoding: 'utf-8' }, ); return { - output: result.output, + output: [path.join(this._getPackageDir(req.pkgname), req.script)], stdout: result.stdout, stderr: result.stderr, status: result.status, diff --git a/packages/@jsii/kernel/test/kernel.test.ts b/packages/@jsii/kernel/test/kernel.test.ts index 842e814810..f5f68de43f 100644 --- a/packages/@jsii/kernel/test/kernel.test.ts +++ b/packages/@jsii/kernel/test/kernel.test.ts @@ -2133,6 +2133,17 @@ defineTest('Override transitive property', (sandbox) => { expect(propValue).toBe('N3W'); }); +defineTest('invokeBinScript() return output', (sandbox) => { + sandbox.create({ fqn: 'jsii-calc.Calculator' }); + + const result = sandbox.invokeBinScript({ + pkgname: 'jsii-calc', + script: 'bin/calc', + }); + + expect(result).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/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..69f714471f 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", "bin/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`] = `