microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.16

Branches

Tags

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

Clone

HTTPS

Download ZIP

test/extension/commandExecutor.test.ts

134lines · 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
3c172a05Artem Egorov8 years ago4import {CommandExecutor} from "../../src/common/commandExecutor";
0a68f8dbArtem Egorov8 years ago5import { ConsoleLogger } from "../../src/extension/log/ConsoleLogger";
bdad2966Joshua Skelton10 years ago6
3c172a05Artem Egorov8 years ago7import { Node } from "../../src/common/node/node";
8import { ChildProcess } from "../../src/common/node/childProcess";
dbff275eVladimir Kotikov8 years ago9
10
11import { EventEmitter } from "events";
bdad2966Joshua Skelton10 years ago12import * as assert from "assert";
13import * as semver from "semver";
14import * as sinon from "sinon";
af70238cJoshua Skelton10 years ago15import * as Q from "q";
bdad2966Joshua Skelton10 years ago16
17suite("commandExecutor", function() {
0a68f8dbArtem Egorov8 years ago18suite("extensionContext", function () {
dbff275eVladimir Kotikov8 years ago19
20let childProcessStubInstance = new ChildProcess();
21let childProcessStub: Sinon.SinonStub & ChildProcess;
0a68f8dbArtem Egorov8 years ago22let Log = new ConsoleLogger();
dbff275eVladimir Kotikov8 years ago23
bdad2966Joshua Skelton10 years ago24teardown(function() {
0a68f8dbArtem Egorov8 years ago25let mockedMethods = [Log.log, ...Object.keys(childProcessStubInstance)];
bdad2966Joshua Skelton10 years ago26
27mockedMethods.forEach((method) => {
28if (method.hasOwnProperty("restore")) {
29(<any>method).restore();
30}
31});
dbff275eVladimir Kotikov8 years ago32
33childProcessStub.restore();
34});
35
36setup(() => {
37childProcessStub = sinon.stub(Node, "ChildProcess")
38.returns(childProcessStubInstance) as ChildProcess & Sinon.SinonStub;
bdad2966Joshua Skelton10 years ago39});
40
41test("should execute a command", function() {
0a68f8dbArtem Egorov8 years ago42let ce = new CommandExecutor(process.cwd(), Log);
0c4ba74cJoshua Skelton10 years ago43let loggedOutput: string = "";
bdad2966Joshua Skelton10 years ago44
0a68f8dbArtem Egorov8 years ago45sinon.stub(Log, "log", function(message: string, formatMessage: boolean = true) {
46loggedOutput += semver.clean(message) || "";
bdad2966Joshua Skelton10 years ago47console.log(message);
48});
49
50return ce.execute("node -v")
51.then(() => {
0a68f8dbArtem Egorov8 years ago52assert(loggedOutput);
bdad2966Joshua Skelton10 years ago53});
54});
55
56test("should reject on bad command", function() {
57let ce = new CommandExecutor();
58
59return ce.execute("bar")
60.then(() => {
5c8365a6Artem Egorov8 years ago61assert.fail(null, null, "bar should not be a valid command");
bdad2966Joshua Skelton10 years ago62})
63.catch((reason) => {
64console.log(reason.message);
65assert.equal(reason.errorCode, 101);
66assert.equal(reason.errorLevel, 0);
67});
68});
69
70test("should reject on good command that fails", function() {
71let ce = new CommandExecutor();
72
73return ce.execute("node install bad-package")
74.then(() => {
5c8365a6Artem Egorov8 years ago75assert.fail(null, null, "node should not be able to install bad-package");
bdad2966Joshua Skelton10 years ago76})
77.catch((reason) => {
78console.log(reason.message);
79assert.equal(reason.errorCode, 101);
80assert.equal(reason.errorLevel, 0);
81});
82});
af70238cJoshua Skelton10 years ago83
84test("should spawn a command", function(done: MochaDone) {
85let ce = new CommandExecutor();
0c4ba74cJoshua Skelton10 years ago86let loggedOutput: string = "";
af70238cJoshua Skelton10 years ago87
0a68f8dbArtem Egorov8 years ago88sinon.stub(Log, "log", function(message: string, formatMessage: boolean = true) {
0c4ba74cJoshua Skelton10 years ago89loggedOutput += message;
af70238cJoshua Skelton10 years ago90console.log(message);
91});
92
58b0a463Joshua Skelton10 years ago93Q({})
94.then(function () {
b956e11eMeena Kunnathur Balakrishnan10 years ago95return ce.spawn("node", ["-v"]);
58b0a463Joshua Skelton10 years ago96}).done(() => done(), done);
97});
98
b956e11eMeena Kunnathur Balakrishnan10 years ago99test("spawn should reject a bad command", function(done: MochaDone) {
58b0a463Joshua Skelton10 years ago100let ce = new CommandExecutor();
101let loggedOutput: string = "";
102
0a68f8dbArtem Egorov8 years ago103sinon.stub(Log, "log", function(message: string, formatMessage: boolean = true) {
58b0a463Joshua Skelton10 years ago104loggedOutput += message;
105console.log(message);
106});
107
108Q({})
109.then(function() {
b956e11eMeena Kunnathur Balakrishnan10 years ago110return ce.spawn("bar", ["-v"]);
58b0a463Joshua Skelton10 years ago111})
112.catch((reason) => {
113console.log(reason.message);
114assert.equal(reason.errorCode, 101);
115assert.equal(reason.errorLevel, 0);
116}).done(() => done(), done);
117});
dbff275eVladimir Kotikov8 years ago118
119test("should not fail on react-native command without arguments", function (done: MochaDone) {
120(sinon.stub(childProcessStubInstance, "spawn") as Sinon.SinonStub)
121.returns({
122stdout: new EventEmitter(),
123stderr: new EventEmitter(),
124outcome: Promise.resolve(void 0),
125});
126
127new CommandExecutor()
128.spawnReactCommand("run-ios").outcome
129.then(done, err => {
5c8365a6Artem Egorov8 years ago130assert.fail(null, null, "react-natibe command was not expected to fail");
dbff275eVladimir Kotikov8 years ago131});
132});
bdad2966Joshua Skelton10 years ago133});
dbff275eVladimir Kotikov8 years ago134});