Skip to content

Commit

Permalink
feat: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Oct 9, 2020
1 parent 48ad9f5 commit ed032d9
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 28 deletions.
10 changes: 5 additions & 5 deletions packages/@jsii/kernel/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
43 changes: 33 additions & 10 deletions packages/@jsii/kernel/lib/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions packages/@jsii/kernel/test/kernel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } = {};
Expand Down
1 change: 1 addition & 0 deletions packages/@jsii/python-runtime/tests/test_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions packages/jsii-calc/bin/calc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('./calc.js');
5 changes: 5 additions & 0 deletions packages/jsii-calc/bin/calc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

/* eslint-disable no-console */

console.info('Hello World!');
3 changes: 3 additions & 0 deletions packages/jsii-calc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
],
"url": "https://aws.amazon.com"
},
"bin": {
"calc": "bin/calc"
},
"bundled": {
"@fixtures/jsii-calc-bundled": "^0.19.0"
},
Expand Down Expand Up @@ -14229,5 +14232,5 @@
}
},
"version": "0.0.0",
"fingerprint": "azqNkkl+/4FLzTVBLkOyHcokS4xLoYtHsri0z9kIehQ="
"fingerprint": "QHc8YZS13IljwCQpg6AZlBxIZvkAbfnCFh6Vi+e0Cgg="
}
10 changes: 3 additions & 7 deletions packages/jsii-pacmak/lib/targets/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1485,7 +1485,7 @@ class PythonModule implements PythonType {
const scripts: string[] = [];
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),
Expand Down Expand Up @@ -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:]',
],
')',
Expand Down Expand Up @@ -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.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/jsii/lib/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ export class Assembler implements Emitter {
docs,
readme,
jsiiVersion,
fingerprint: '<TBD>',
bin: this.projectInfo.bin,
fingerprint: '<TBD>',
};

const validator = new Validator(this.projectInfo, assembly);
Expand Down

0 comments on commit ed032d9

Please sign in to comment.