microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/test/common/commandExecutor.test.ts
129lines · 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 | | |
| 4 | import {CommandExecutor} from "../../common/commandExecutor"; | |
| 5 | import {Log} from "../../common/log/log"; | |
af70238cJoshua Skelton10 years ago | 6 | import {ChildProcess} from "child_process"; |
bdad2966Joshua Skelton10 years ago | 7 | |
| 8 | import * as assert from "assert"; | |
| 9 | import * as semver from "semver"; | |
| 10 | import * as sinon from "sinon"; | |
af70238cJoshua Skelton10 years ago | 11 | import * as Q from "q"; |
bdad2966Joshua Skelton10 years ago | 12 | |
| 13 | suite("commandExecutor", function() { | |
| 14 | suite("commonContext", function() { | |
| 15 | teardown(function() { | |
| 16 | let mockedMethods = [Log.logMessage, Log.logCommandStatus]; | |
| 17 | | |
| 18 | mockedMethods.forEach((method) => { | |
| 19 | if (method.hasOwnProperty("restore")) { | |
| 20 | (<any>method).restore(); | |
| 21 | } | |
| 22 | }); | |
| 23 | }); | |
| 24 | | |
| 25 | test("should execute a command", function() { | |
| 26 | let ce = new CommandExecutor(); | |
0c4ba74cJoshua Skelton10 years ago | 27 | let loggedOutput: string = ""; |
bdad2966Joshua Skelton10 years ago | 28 | |
886053d9Joshua Skelton10 years ago | 29 | sinon.stub(Log, "logMessage", function(message: string, formatMessage: boolean = true) { |
0c4ba74cJoshua Skelton10 years ago | 30 | loggedOutput += message; |
bdad2966Joshua Skelton10 years ago | 31 | console.log(message); |
| 32 | }); | |
| 33 | | |
| 34 | return ce.execute("node -v") | |
| 35 | .then(() => { | |
0c4ba74cJoshua Skelton10 years ago | 36 | let nodeVersion = semver.clean(loggedOutput); |
886053d9Joshua Skelton10 years ago | 37 | assert(nodeVersion); |
bdad2966Joshua Skelton10 years ago | 38 | }); |
| 39 | }); | |
| 40 | | |
| 41 | test("should reject on bad command", function() { | |
| 42 | let ce = new CommandExecutor(); | |
| 43 | | |
| 44 | return ce.execute("bar") | |
| 45 | .then(() => { | |
| 46 | assert.fail("bar should not be a valid command"); | |
| 47 | }) | |
| 48 | .catch((reason) => { | |
| 49 | console.log(reason.message); | |
| 50 | assert.equal(reason.errorCode, 101); | |
| 51 | assert.equal(reason.errorLevel, 0); | |
| 52 | }); | |
| 53 | }); | |
| 54 | | |
| 55 | test("should reject on good command that fails", function() { | |
| 56 | let ce = new CommandExecutor(); | |
| 57 | | |
| 58 | return ce.execute("node install bad-package") | |
| 59 | .then(() => { | |
| 60 | assert.fail("node should not be able to install bad-package"); | |
| 61 | }) | |
| 62 | .catch((reason) => { | |
| 63 | console.log(reason.message); | |
| 64 | assert.equal(reason.errorCode, 101); | |
| 65 | assert.equal(reason.errorLevel, 0); | |
| 66 | }); | |
| 67 | }); | |
af70238cJoshua Skelton10 years ago | 68 | |
| 69 | test("should spawn a command", function(done: MochaDone) { | |
| 70 | let ce = new CommandExecutor(); | |
0c4ba74cJoshua Skelton10 years ago | 71 | let loggedOutput: string = ""; |
af70238cJoshua Skelton10 years ago | 72 | |
| 73 | sinon.stub(Log, "logMessage", function(message: string, formatMessage: boolean = true) { | |
0c4ba74cJoshua Skelton10 years ago | 74 | loggedOutput += message; |
af70238cJoshua Skelton10 years ago | 75 | console.log(message); |
| 76 | }); | |
| 77 | | |
| 78 | Q({}) | |
| 79 | .then(function() { | |
| 80 | let process: ChildProcess = ce.spawn("node", ["-v"]); | |
| 81 | let deferred = Q.defer<string>(); | |
| 82 | | |
| 83 | process.stdout.on("data", function(data: any) { | |
| 84 | deferred.resolve(data.toString()); | |
| 85 | }); | |
| 86 | | |
| 87 | return deferred.promise; | |
| 88 | }) | |
| 89 | .then(function(output: string) { | |
| 90 | assert(semver.clean(output)); | |
| 91 | }).done(() => done(), done); | |
| 92 | }); | |
58b0a463Joshua Skelton10 years ago | 93 | |
| 94 | test("should spawnAndWaitForCompletion a command", function(done: MochaDone) { | |
| 95 | let ce = new CommandExecutor(); | |
| 96 | let loggedOutput: string = ""; | |
| 97 | | |
| 98 | sinon.stub(Log, "logMessage", function(message: string, formatMessage: boolean = true) { | |
| 99 | loggedOutput += message; | |
| 100 | console.log(message); | |
| 101 | }); | |
| 102 | | |
| 103 | Q({}) | |
| 104 | .then(function () { | |
| 105 | return ce.spawnAndWaitForCompletion("node", ["-v"]); | |
| 106 | }).done(() => done(), done); | |
| 107 | }); | |
| 108 | | |
| 109 | test("spawnAndWaitForCompletion should reject a bad command", function(done: MochaDone) { | |
| 110 | let ce = new CommandExecutor(); | |
| 111 | let loggedOutput: string = ""; | |
| 112 | | |
| 113 | sinon.stub(Log, "logMessage", function(message: string, formatMessage: boolean = true) { | |
| 114 | loggedOutput += message; | |
| 115 | console.log(message); | |
| 116 | }); | |
| 117 | | |
| 118 | Q({}) | |
| 119 | .then(function() { | |
| 120 | return ce.spawnAndWaitForCompletion("bar", ["-v"]); | |
| 121 | }) | |
| 122 | .catch((reason) => { | |
| 123 | console.log(reason.message); | |
| 124 | assert.equal(reason.errorCode, 101); | |
| 125 | assert.equal(reason.errorLevel, 0); | |
| 126 | }).done(() => done(), done); | |
| 127 | }); | |
bdad2966Joshua Skelton10 years ago | 128 | }); |
| 129 | }); |