microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.1.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/test/common/commandExecutor.test.ts

129lines · modeblame

bdad2966Joshua Skelton10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import {CommandExecutor} from "../../common/commandExecutor";
5import {Log} from "../../common/log/log";
af70238cJoshua Skelton10 years ago6import {ChildProcess} from "child_process";
bdad2966Joshua Skelton10 years ago7
8import * as assert from "assert";
9import * as semver from "semver";
10import * as sinon from "sinon";
af70238cJoshua Skelton10 years ago11import * as Q from "q";
bdad2966Joshua Skelton10 years ago12
13suite("commandExecutor", function() {
14suite("commonContext", function() {
15teardown(function() {
16let mockedMethods = [Log.logMessage, Log.logCommandStatus];
17
18mockedMethods.forEach((method) => {
19if (method.hasOwnProperty("restore")) {
20(<any>method).restore();
21}
22});
23});
24
25test("should execute a command", function() {
26let ce = new CommandExecutor();
0c4ba74cJoshua Skelton10 years ago27let loggedOutput: string = "";
bdad2966Joshua Skelton10 years ago28
886053d9Joshua Skelton10 years ago29sinon.stub(Log, "logMessage", function(message: string, formatMessage: boolean = true) {
0c4ba74cJoshua Skelton10 years ago30loggedOutput += message;
bdad2966Joshua Skelton10 years ago31console.log(message);
32});
33
34return ce.execute("node -v")
35.then(() => {
0c4ba74cJoshua Skelton10 years ago36let nodeVersion = semver.clean(loggedOutput);
886053d9Joshua Skelton10 years ago37assert(nodeVersion);
bdad2966Joshua Skelton10 years ago38});
39});
40
41test("should reject on bad command", function() {
42let ce = new CommandExecutor();
43
44return ce.execute("bar")
45.then(() => {
46assert.fail("bar should not be a valid command");
47})
48.catch((reason) => {
49console.log(reason.message);
50assert.equal(reason.errorCode, 101);
51assert.equal(reason.errorLevel, 0);
52});
53});
54
55test("should reject on good command that fails", function() {
56let ce = new CommandExecutor();
57
58return ce.execute("node install bad-package")
59.then(() => {
60assert.fail("node should not be able to install bad-package");
61})
62.catch((reason) => {
63console.log(reason.message);
64assert.equal(reason.errorCode, 101);
65assert.equal(reason.errorLevel, 0);
66});
67});
af70238cJoshua Skelton10 years ago68
69test("should spawn a command", function(done: MochaDone) {
70let ce = new CommandExecutor();
0c4ba74cJoshua Skelton10 years ago71let loggedOutput: string = "";
af70238cJoshua Skelton10 years ago72
73sinon.stub(Log, "logMessage", function(message: string, formatMessage: boolean = true) {
0c4ba74cJoshua Skelton10 years ago74loggedOutput += message;
af70238cJoshua Skelton10 years ago75console.log(message);
76});
77
78Q({})
79.then(function() {
80let process: ChildProcess = ce.spawn("node", ["-v"]);
81let deferred = Q.defer<string>();
82
83process.stdout.on("data", function(data: any) {
84deferred.resolve(data.toString());
85});
86
87return deferred.promise;
88})
89.then(function(output: string) {
90assert(semver.clean(output));
91}).done(() => done(), done);
92});
58b0a463Joshua Skelton10 years ago93
94test("should spawnAndWaitForCompletion a command", function(done: MochaDone) {
95let ce = new CommandExecutor();
96let loggedOutput: string = "";
97
98sinon.stub(Log, "logMessage", function(message: string, formatMessage: boolean = true) {
99loggedOutput += message;
100console.log(message);
101});
102
103Q({})
104.then(function () {
105return ce.spawnAndWaitForCompletion("node", ["-v"]);
106}).done(() => done(), done);
107});
108
109test("spawnAndWaitForCompletion should reject a bad command", function(done: MochaDone) {
110let ce = new CommandExecutor();
111let loggedOutput: string = "";
112
113sinon.stub(Log, "logMessage", function(message: string, formatMessage: boolean = true) {
114loggedOutput += message;
115console.log(message);
116});
117
118Q({})
119.then(function() {
120return ce.spawnAndWaitForCompletion("bar", ["-v"]);
121})
122.catch((reason) => {
123console.log(reason.message);
124assert.equal(reason.errorCode, 101);
125assert.equal(reason.errorLevel, 0);
126}).done(() => done(), done);
127});
bdad2966Joshua Skelton10 years ago128});
129});