microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/test/common/commandExecutor.test.ts
103lines · 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 | |
| 7 | import * as assert from "assert"; |
| 8 | import * as semver from "semver"; |
| 9 | import * as sinon from "sinon"; |
| 10 | import * as Q from "q"; |
| 11 | |
| 12 | suite("commandExecutor", function() { |
| 13 | suite("commonContext", function() { |
| 14 | teardown(function() { |
| 15 | let mockedMethods = [Log.logMessage, Log.logCommandStatus]; |
| 16 | |
| 17 | mockedMethods.forEach((method) => { |
| 18 | if (method.hasOwnProperty("restore")) { |
| 19 | (<any>method).restore(); |
| 20 | } |
| 21 | }); |
| 22 | }); |
| 23 | |
| 24 | test("should execute a command", function() { |
| 25 | let ce = new CommandExecutor(); |
| 26 | let loggedOutput: string = ""; |
| 27 | |
| 28 | sinon.stub(Log, "logMessage", function(message: string, formatMessage: boolean = true) { |
| 29 | loggedOutput += message; |
| 30 | console.log(message); |
| 31 | }); |
| 32 | |
| 33 | return ce.execute("node -v") |
| 34 | .then(() => { |
| 35 | let nodeVersion = semver.clean(loggedOutput); |
| 36 | assert(nodeVersion); |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | test("should reject on bad command", function() { |
| 41 | let ce = new CommandExecutor(); |
| 42 | |
| 43 | return ce.execute("bar") |
| 44 | .then(() => { |
| 45 | assert.fail("bar should not be a valid command"); |
| 46 | }) |
| 47 | .catch((reason) => { |
| 48 | console.log(reason.message); |
| 49 | assert.equal(reason.errorCode, 101); |
| 50 | assert.equal(reason.errorLevel, 0); |
| 51 | }); |
| 52 | }); |
| 53 | |
| 54 | test("should reject on good command that fails", function() { |
| 55 | let ce = new CommandExecutor(); |
| 56 | |
| 57 | return ce.execute("node install bad-package") |
| 58 | .then(() => { |
| 59 | assert.fail("node should not be able to install bad-package"); |
| 60 | }) |
| 61 | .catch((reason) => { |
| 62 | console.log(reason.message); |
| 63 | assert.equal(reason.errorCode, 101); |
| 64 | assert.equal(reason.errorLevel, 0); |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | test("should spawn a command", function(done: MochaDone) { |
| 69 | let ce = new CommandExecutor(); |
| 70 | let loggedOutput: string = ""; |
| 71 | |
| 72 | sinon.stub(Log, "logMessage", function(message: string, formatMessage: boolean = true) { |
| 73 | loggedOutput += message; |
| 74 | console.log(message); |
| 75 | }); |
| 76 | |
| 77 | Q({}) |
| 78 | .then(function () { |
| 79 | return ce.spawn("node", ["-v"]); |
| 80 | }).done(() => done(), done); |
| 81 | }); |
| 82 | |
| 83 | test("spawn should reject a bad command", function(done: MochaDone) { |
| 84 | let ce = new CommandExecutor(); |
| 85 | let loggedOutput: string = ""; |
| 86 | |
| 87 | sinon.stub(Log, "logMessage", function(message: string, formatMessage: boolean = true) { |
| 88 | loggedOutput += message; |
| 89 | console.log(message); |
| 90 | }); |
| 91 | |
| 92 | Q({}) |
| 93 | .then(function() { |
| 94 | return ce.spawn("bar", ["-v"]); |
| 95 | }) |
| 96 | .catch((reason) => { |
| 97 | console.log(reason.message); |
| 98 | assert.equal(reason.errorCode, 101); |
| 99 | assert.equal(reason.errorLevel, 0); |
| 100 | }).done(() => done(), done); |
| 101 | }); |
| 102 | }); |
| 103 | }); |