microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/test/common/commandExecutor.test.ts
129lines · 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 "../../common/commandExecutor"; |
| 5 | import {Log} from "../../common/log/log"; |
| 6 | import {ChildProcess} from "child_process"; |
| 7 | |
| 8 | import * as assert from "assert"; |
| 9 | import * as semver from "semver"; |
| 10 | import * as sinon from "sinon"; |
| 11 | import * as Q from "q"; |
| 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(); |
| 27 | let loggedOutput: string = ""; |
| 28 | |
| 29 | sinon.stub(Log, "logMessage", function(message: string, formatMessage: boolean = true) { |
| 30 | loggedOutput += message; |
| 31 | console.log(message); |
| 32 | }); |
| 33 | |
| 34 | return ce.execute("node -v") |
| 35 | .then(() => { |
| 36 | let nodeVersion = semver.clean(loggedOutput); |
| 37 | assert(nodeVersion); |
| 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 | }); |
| 68 | |
| 69 | test("should spawn a command", function(done: MochaDone) { |
| 70 | let ce = new CommandExecutor(); |
| 71 | let loggedOutput: string = ""; |
| 72 | |
| 73 | sinon.stub(Log, "logMessage", function(message: string, formatMessage: boolean = true) { |
| 74 | loggedOutput += message; |
| 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 | }); |
| 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 | }); |
| 128 | }); |
| 129 | }); |