microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
test/extension/commandExecutor.test.ts
134lines · modeblame
bdad2966Joshua Skelton10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
3c172a05Artem Egorov8 years ago | 4 | import {CommandExecutor} from "../../src/common/commandExecutor"; |
0a68f8dbArtem Egorov8 years ago | 5 | import { ConsoleLogger } from "../../src/extension/log/ConsoleLogger"; |
bdad2966Joshua Skelton10 years ago | 6 | |
3c172a05Artem Egorov8 years ago | 7 | import { Node } from "../../src/common/node/node"; |
| 8 | import { ChildProcess } from "../../src/common/node/childProcess"; | |
dbff275eVladimir Kotikov8 years ago | 9 | |
| 10 | | |
| 11 | import { EventEmitter } from "events"; | |
bdad2966Joshua Skelton10 years ago | 12 | import * as assert from "assert"; |
| 13 | import * as semver from "semver"; | |
| 14 | import * as sinon from "sinon"; | |
af70238cJoshua Skelton10 years ago | 15 | import * as Q from "q"; |
bdad2966Joshua Skelton10 years ago | 16 | |
| 17 | suite("commandExecutor", function() { | |
0a68f8dbArtem Egorov8 years ago | 18 | suite("extensionContext", function () { |
dbff275eVladimir Kotikov8 years ago | 19 | |
| 20 | let childProcessStubInstance = new ChildProcess(); | |
| 21 | let childProcessStub: Sinon.SinonStub & ChildProcess; | |
0a68f8dbArtem Egorov8 years ago | 22 | let Log = new ConsoleLogger(); |
dbff275eVladimir Kotikov8 years ago | 23 | |
bdad2966Joshua Skelton10 years ago | 24 | teardown(function() { |
0a68f8dbArtem Egorov8 years ago | 25 | let mockedMethods = [Log.log, ...Object.keys(childProcessStubInstance)]; |
bdad2966Joshua Skelton10 years ago | 26 | |
| 27 | mockedMethods.forEach((method) => { | |
| 28 | if (method.hasOwnProperty("restore")) { | |
| 29 | (<any>method).restore(); | |
| 30 | } | |
| 31 | }); | |
dbff275eVladimir Kotikov8 years ago | 32 | |
| 33 | childProcessStub.restore(); | |
| 34 | }); | |
| 35 | | |
| 36 | setup(() => { | |
| 37 | childProcessStub = sinon.stub(Node, "ChildProcess") | |
| 38 | .returns(childProcessStubInstance) as ChildProcess & Sinon.SinonStub; | |
bdad2966Joshua Skelton10 years ago | 39 | }); |
| 40 | | |
| 41 | test("should execute a command", function() { | |
0a68f8dbArtem Egorov8 years ago | 42 | let ce = new CommandExecutor(process.cwd(), Log); |
0c4ba74cJoshua Skelton10 years ago | 43 | let loggedOutput: string = ""; |
bdad2966Joshua Skelton10 years ago | 44 | |
0a68f8dbArtem Egorov8 years ago | 45 | sinon.stub(Log, "log", function(message: string, formatMessage: boolean = true) { |
| 46 | loggedOutput += semver.clean(message) || ""; | |
bdad2966Joshua Skelton10 years ago | 47 | console.log(message); |
| 48 | }); | |
| 49 | | |
| 50 | return ce.execute("node -v") | |
| 51 | .then(() => { | |
0a68f8dbArtem Egorov8 years ago | 52 | assert(loggedOutput); |
bdad2966Joshua Skelton10 years ago | 53 | }); |
| 54 | }); | |
| 55 | | |
| 56 | test("should reject on bad command", function() { | |
| 57 | let ce = new CommandExecutor(); | |
| 58 | | |
| 59 | return ce.execute("bar") | |
| 60 | .then(() => { | |
5c8365a6Artem Egorov8 years ago | 61 | assert.fail(null, null, "bar should not be a valid command"); |
bdad2966Joshua Skelton10 years ago | 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(() => { | |
5c8365a6Artem Egorov8 years ago | 75 | assert.fail(null, null, "node should not be able to install bad-package"); |
bdad2966Joshua Skelton10 years ago | 76 | }) |
| 77 | .catch((reason) => { | |
| 78 | console.log(reason.message); | |
| 79 | assert.equal(reason.errorCode, 101); | |
| 80 | assert.equal(reason.errorLevel, 0); | |
| 81 | }); | |
| 82 | }); | |
af70238cJoshua Skelton10 years ago | 83 | |
| 84 | test("should spawn a command", function(done: MochaDone) { | |
| 85 | let ce = new CommandExecutor(); | |
0c4ba74cJoshua Skelton10 years ago | 86 | let loggedOutput: string = ""; |
af70238cJoshua Skelton10 years ago | 87 | |
0a68f8dbArtem Egorov8 years ago | 88 | sinon.stub(Log, "log", function(message: string, formatMessage: boolean = true) { |
0c4ba74cJoshua Skelton10 years ago | 89 | loggedOutput += message; |
af70238cJoshua Skelton10 years ago | 90 | console.log(message); |
| 91 | }); | |
| 92 | | |
58b0a463Joshua Skelton10 years ago | 93 | Q({}) |
| 94 | .then(function () { | |
b956e11eMeena Kunnathur Balakrishnan10 years ago | 95 | return ce.spawn("node", ["-v"]); |
58b0a463Joshua Skelton10 years ago | 96 | }).done(() => done(), done); |
| 97 | }); | |
| 98 | | |
b956e11eMeena Kunnathur Balakrishnan10 years ago | 99 | test("spawn should reject a bad command", function(done: MochaDone) { |
58b0a463Joshua Skelton10 years ago | 100 | let ce = new CommandExecutor(); |
| 101 | let loggedOutput: string = ""; | |
| 102 | | |
0a68f8dbArtem Egorov8 years ago | 103 | sinon.stub(Log, "log", function(message: string, formatMessage: boolean = true) { |
58b0a463Joshua Skelton10 years ago | 104 | loggedOutput += message; |
| 105 | console.log(message); | |
| 106 | }); | |
| 107 | | |
| 108 | Q({}) | |
| 109 | .then(function() { | |
b956e11eMeena Kunnathur Balakrishnan10 years ago | 110 | return ce.spawn("bar", ["-v"]); |
58b0a463Joshua Skelton10 years ago | 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 | }); | |
dbff275eVladimir Kotikov8 years ago | 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 => { | |
5c8365a6Artem Egorov8 years ago | 130 | assert.fail(null, null, "react-natibe command was not expected to fail"); |
dbff275eVladimir Kotikov8 years ago | 131 | }); |
| 132 | }); | |
bdad2966Joshua Skelton10 years ago | 133 | }); |
dbff275eVladimir Kotikov8 years ago | 134 | }); |