From e1e35c5743b913fb068091b2b2aa202f5c5d8c7b Mon Sep 17 00:00:00 2001 From: Nick Chen Date: Sun, 25 Jun 2017 20:56:28 -0700 Subject: [PATCH] Add tests for commandExecutor --- .vscode/launch.json | 15 ++++- .../salesforcedx-utils-vscode/package.json | 4 +- .../src/cli/commandExecutor.ts | 6 +- .../test/cli/commandExecutorTest.ts | 61 +++++++++++++++++++ .../salesforcedx-vscode-apex/package.json | 3 +- .../salesforcedx-vscode-core/package.json | 3 +- 6 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 packages/salesforcedx-utils-vscode/test/cli/commandExecutorTest.ts diff --git a/.vscode/launch.json b/.vscode/launch.json index f8c7fda..5a3b4a5 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -2,6 +2,17 @@ { "version": "0.2.0", "configurations": [ + { + "type": "node", + "request": "attach", + "protocol": "legacy", + "name": "Attach to Process (Legacy Protocol)", + "port": 5858, + "sourceMaps": true, + "outFiles": [ + "${workspaceRoot}/packages/*/out/**/*.js" + ] + }, { "name": "Launch Extensions", "type": "extensionHost", @@ -29,7 +40,7 @@ "stopOnEntry": false, "sourceMaps": true, "outFiles": [ - "${workspaceRoot}/packages/*/out/test/**/*.js" + "${workspaceRoot}/packages/*/out/**/*.js" ], "preLaunchTask": "Watch" }, @@ -45,7 +56,7 @@ "stopOnEntry": false, "sourceMaps": true, "outFiles": [ - "${workspaceRoot}/packages/*/out/test/**/*.js" + "${workspaceRoot}/packages/*/out/**/*.js" ], "preLaunchTask": "Watch" } diff --git a/packages/salesforcedx-utils-vscode/package.json b/packages/salesforcedx-utils-vscode/package.json index 150fcaf..67fd1ab 100644 --- a/packages/salesforcedx-utils-vscode/package.json +++ b/packages/salesforcedx-utils-vscode/package.json @@ -17,6 +17,7 @@ "@types/node": "^6.0.40", "chai": "^4.0.2", "mocha": "3.2.0", + "nyc": "^11.0.2", "typescript": "2.3.4" }, "scripts": { @@ -24,7 +25,8 @@ "lint": "tslint --project .", "watch": "tsc -watch -p .", "clean": "rm -rf node_modules && rm -rf out", - "test": "node ./node_modules/.bin/_mocha --recursive out/test" + "test": "node ./node_modules/.bin/_mocha --recursive out/test", + "coverage": "./node_modules/.bin/nyc npm test" }, "main": "./out/src/" } \ No newline at end of file diff --git a/packages/salesforcedx-utils-vscode/src/cli/commandExecutor.ts b/packages/salesforcedx-utils-vscode/src/cli/commandExecutor.ts index 092d441..efed902 100644 --- a/packages/salesforcedx-utils-vscode/src/cli/commandExecutor.ts +++ b/packages/salesforcedx-utils-vscode/src/cli/commandExecutor.ts @@ -56,7 +56,11 @@ export class CommandExecution { const timer = Observable.interval(1000); timerSubscriber = timer.subscribe(next => { if (cancellationToken.isCancellationRequested) { - childProcess.kill(); + try { + childProcess.kill(); + } catch (e) { + // This is best effort, by the time we get here, the process might have been killed. + } } }); } diff --git a/packages/salesforcedx-utils-vscode/test/cli/commandExecutorTest.ts b/packages/salesforcedx-utils-vscode/test/cli/commandExecutorTest.ts new file mode 100644 index 0000000..3305d84 --- /dev/null +++ b/packages/salesforcedx-utils-vscode/test/cli/commandExecutorTest.ts @@ -0,0 +1,61 @@ +import { expect } from 'chai'; + +import { CliCommandExecutor, SfdxCommandBuilder } from '../../src/cli'; + +describe('CommandExecutor tests', () => { + describe('Handle listeners on stdout and stderr', () => { + it('Should pipe stdout', async () => { + const execution = new CliCommandExecutor( + new SfdxCommandBuilder().withArg('force').withArg('--help').build(), + {} + ).execute(); + + let stdout = ''; + execution.stdoutSubject.subscribe(data => (stdout += data.toString())); + let stderr = ''; + execution.stderrSubject.subscribe(data => (stderr += data.toString())); + const exitCode = await new Promise((resolve, reject) => { + execution.processExitSubject.subscribe( + data => { + resolve(data.toString()); + }, + err => { + reject(err); + } + ); + }); + + expect(exitCode).to.equal('0'); + expect(stdout).to.contain( + 'Usage: sfdx COMMAND [command-specific-options]' + ); + expect(stderr).to.contain(''); + }); + + it('Should pipe stderr', async () => { + const execution = new CliCommandExecutor( + new SfdxCommandBuilder().withArg('force').withArg('--unknown').build(), + {} + ).execute(); + + let stdout = ''; + execution.stdoutSubject.subscribe(data => (stdout += data.toString())); + let stderr = ''; + execution.stderrSubject.subscribe(data => (stderr += data.toString())); + const exitCode = await new Promise((resolve, reject) => { + execution.processExitSubject.subscribe( + data => { + resolve(data.toString()); + }, + err => { + reject(err); + } + ); + }); + + expect(exitCode).to.not.equal('0'); + expect(stdout).to.contain(''); + expect(stderr).to.contain('Error: Unexpected flag --unknown'); + }); + }); +}); diff --git a/packages/salesforcedx-vscode-apex/package.json b/packages/salesforcedx-vscode-apex/package.json index c9dd649..c6c6434 100644 --- a/packages/salesforcedx-vscode-apex/package.json +++ b/packages/salesforcedx-vscode-apex/package.json @@ -19,6 +19,7 @@ "@types/path-exists": "^1.0.29", "chai": "^4.0.2", "mocha": "3.2.0", + "nyc": "^11.0.2", "typescript": "2.3.4", "vscode": "1.1.0" }, @@ -64,4 +65,4 @@ "portfinder": "1.0.12", "vscode-languageclient": "3.3.0" } -} \ No newline at end of file +} diff --git a/packages/salesforcedx-vscode-core/package.json b/packages/salesforcedx-vscode-core/package.json index 64aaea4..ca5db75 100644 --- a/packages/salesforcedx-vscode-core/package.json +++ b/packages/salesforcedx-vscode-core/package.json @@ -22,6 +22,7 @@ "@types/path-exists": "^1.0.29", "chai": "^4.0.2", "mocha": "3.2.0", + "nyc": "^11.0.2", "typescript": "2.3.4", "vscode": "1.1.0" }, @@ -78,4 +79,4 @@ } ] } -} \ No newline at end of file +}