microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
test/extension/commandExecutor.test.ts
134lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import {CommandExecutor} from "../../src/common/commandExecutor"; |
| 5 | import { ConsoleLogger } from "../../src/extension/log/ConsoleLogger"; |
| 6 | |
| 7 | import { Node } from "../../src/common/node/node"; |
| 8 | import { ChildProcess } from "../../src/common/node/childProcess"; |
| 9 | |
| 10 | |
| 11 | import { EventEmitter } from "events"; |
| 12 | import * as assert from "assert"; |
| 13 | import * as semver from "semver"; |
| 14 | import * as sinon from "sinon"; |
| 15 | import * as Q from "q"; |
| 16 | |
| 17 | suite("commandExecutor", function() { |
| 18 | suite("extensionContext", function () { |
| 19 | |
| 20 | let childProcessStubInstance = new ChildProcess(); |
| 21 | let childProcessStub: Sinon.SinonStub & ChildProcess; |
| 22 | let Log = new ConsoleLogger(); |
| 23 | |
| 24 | teardown(function() { |
| 25 | let mockedMethods = [Log.log, ...Object.keys(childProcessStubInstance)]; |
| 26 | |
| 27 | mockedMethods.forEach((method) => { |
| 28 | if (method.hasOwnProperty("restore")) { |
| 29 | (<any>method).restore(); |
| 30 | } |
| 31 | }); |
| 32 | |
| 33 | childProcessStub.restore(); |
| 34 | }); |
| 35 | |
| 36 | setup(() => { |
| 37 | childProcessStub = sinon.stub(Node, "ChildProcess") |
| 38 | .returns(childProcessStubInstance) as ChildProcess & Sinon.SinonStub; |
| 39 | }); |
| 40 | |
| 41 | test("should execute a command", function() { |
| 42 | let ce = new CommandExecutor(process.cwd(), Log); |
| 43 | let loggedOutput: string = ""; |
| 44 | |
| 45 | sinon.stub(Log, "log", function(message: string, formatMessage: boolean = true) { |
| 46 | loggedOutput += semver.clean(message) || ""; |
| 47 | console.log(message); |
| 48 | }); |
| 49 | |
| 50 | return ce.execute("node -v") |
| 51 | .then(() => { |
| 52 | assert(loggedOutput); |
| 53 | }); |
| 54 | }); |
| 55 | |
| 56 | test("should reject on bad command", function() { |
| 57 | let ce = new CommandExecutor(); |
| 58 | |
| 59 | return ce.execute("bar") |
| 60 | .then(() => { |
| 61 | assert.fail(null, null, "bar should not be a valid command"); |
| 62 | }) |
| 63 | .catch((reason) => { |
| 64 | console.log(reason.message); |
| 65 | assert.equal(reason.errorCode, 101); |
| 66 | assert.equal(reason.errorLevel, 0); |
| 67 | }); |
| 68 | }); |
| 69 | |
| 70 | test("should reject on good command that fails", function() { |
| 71 | let ce = new CommandExecutor(); |
| 72 | |
| 73 | return ce.execute("node install bad-package") |
| 74 | .then(() => { |
| 75 | assert.fail(null, null, "node should not be able to install bad-package"); |
| 76 | }) |
| 77 | .catch((reason) => { |
| 78 | console.log(reason.message); |
| 79 | assert.equal(reason.errorCode, 101); |
| 80 | assert.equal(reason.errorLevel, 0); |
| 81 | }); |
| 82 | }); |
| 83 | |
| 84 | test("should spawn a command", function(done: MochaDone) { |
| 85 | let ce = new CommandExecutor(); |
| 86 | let loggedOutput: string = ""; |
| 87 | |
| 88 | sinon.stub(Log, "log", function(message: string, formatMessage: boolean = true) { |
| 89 | loggedOutput += message; |
| 90 | console.log(message); |
| 91 | }); |
| 92 | |
| 93 | Q({}) |
| 94 | .then(function () { |
| 95 | return ce.spawn("node", ["-v"]); |
| 96 | }).done(() => done(), done); |
| 97 | }); |
| 98 | |
| 99 | test("spawn should reject a bad command", function(done: MochaDone) { |
| 100 | let ce = new CommandExecutor(); |
| 101 | let loggedOutput: string = ""; |
| 102 | |
| 103 | sinon.stub(Log, "log", function(message: string, formatMessage: boolean = true) { |
| 104 | loggedOutput += message; |
| 105 | console.log(message); |
| 106 | }); |
| 107 | |
| 108 | Q({}) |
| 109 | .then(function() { |
| 110 | return ce.spawn("bar", ["-v"]); |
| 111 | }) |
| 112 | .catch((reason) => { |
| 113 | console.log(reason.message); |
| 114 | assert.equal(reason.errorCode, 101); |
| 115 | assert.equal(reason.errorLevel, 0); |
| 116 | }).done(() => done(), done); |
| 117 | }); |
| 118 | |
| 119 | test("should not fail on react-native command without arguments", function (done: MochaDone) { |
| 120 | (sinon.stub(childProcessStubInstance, "spawn") as Sinon.SinonStub) |
| 121 | .returns({ |
| 122 | stdout: new EventEmitter(), |
| 123 | stderr: new EventEmitter(), |
| 124 | outcome: Promise.resolve(void 0), |
| 125 | }); |
| 126 | |
| 127 | new CommandExecutor() |
| 128 | .spawnReactCommand("run-ios").outcome |
| 129 | .then(done, err => { |
| 130 | assert.fail(null, null, "react-natibe command was not expected to fail"); |
| 131 | }); |
| 132 | }); |
| 133 | }); |
| 134 | }); |
| 135 | |