Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remaining missing unit tests #12

Merged
merged 4 commits into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.0",
"version": "0.2.0",
"configurations": [
{
"type": "extensionHost",
Expand Down
11 changes: 1 addition & 10 deletions test/lib/CommandHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,13 @@ import { commands, ExtensionContext, Memento, window } from "vscode";
import { ICommand } from "../../src/lib/CommandBase";
import { CommandHandler } from "../../src/lib/CommandHandler";
import { NodeKind, ServerlessNode } from "../../src/lib/ServerlessNode";
import { TestContext } from "./TestContext";

// tslint:disable:max-classes-per-file
// tslint:disable:no-unused-expression

chai.use(sinon_chai);
const expect = chai.expect;

class TestContext implements ExtensionContext {
public subscriptions: Array<{ dispose(): any; }> = [];
public workspaceState: Memento;
public globalState: Memento;
public extensionPath: string = "myExtensionPath";
public asAbsolutePath: sinon.SinonStub = sinon.stub();
public storagePath: string = "myStoragePath";
}

class TestCommand implements ICommand {
constructor(public context: ExtensionContext) {
this.invoke = sinon.stub();
Expand Down
246 changes: 246 additions & 0 deletions test/lib/Serverless.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
import * as chai from "chai";
import * as chai_as_promised from "chai-as-promised";
import * as child_process from "child_process";
import * as _ from "lodash";
import * as sinon from "sinon";
import * as sinon_chai from "sinon-chai";
import { commands, ExtensionContext, Memento, OutputChannel, window } from "vscode";
import { ICommand } from "../../src/lib/CommandBase";
import { CommandHandler } from "../../src/lib/CommandHandler";
import { IServerlessInvokeOptions, Serverless } from "../../src/lib/Serverless";
import { NodeKind, ServerlessNode } from "../../src/lib/ServerlessNode";

// tslint:disable:max-classes-per-file
// tslint:disable:no-unused-expression

chai.use(chai_as_promised);
chai.use(sinon_chai);
const expect = chai.expect;

class TestOutputChannel implements OutputChannel {
public static create(sandbox: sinon.SinonSandbox) {
return new TestOutputChannel(sandbox);
}

public name: string;
public append: sinon.SinonStub;
public appendLine: sinon.SinonStub;
public clear: sinon.SinonStub;
public show: sinon.SinonStub;
public hide: sinon.SinonStub;
public dispose: sinon.SinonStub;

private constructor(sandbox: sinon.SinonSandbox) {
this.name = "";
this.append = sandbox.stub();
this.appendLine = sandbox.stub();
this.clear = sandbox.stub();
this.show = sandbox.stub();
this.hide = sandbox.stub();
this.dispose = sandbox.stub();
}
}

describe("Serverless", () => {
let sandbox: sinon.SinonSandbox;
let testOutputChannel: TestOutputChannel;
let testChildProcess: any;
let windowCreateOutputChannelStub: sinon.SinonStub;
let spawnStub: sinon.SinonStub;

before(() => {
sandbox = sinon.createSandbox();
});

beforeEach(() => {
windowCreateOutputChannelStub = sandbox.stub(window, "createOutputChannel");
testOutputChannel = TestOutputChannel.create(sandbox);
windowCreateOutputChannelStub.returns(testOutputChannel);
spawnStub = sandbox.stub(child_process, "spawn");
testChildProcess = {
on: sandbox.stub(),
stderr: {
on: sandbox.stub(),
},
stdout: {
on: sandbox.stub(),
},
};
spawnStub.returns(testChildProcess);
});

afterEach(() => {
sandbox.resetHistory();
sandbox.restore();
});

describe("invoke", () => {
it("should spawn serverless", () => {
testChildProcess.on.withArgs("exit").yields(0);
return expect(Serverless.invoke("my command")).to.be.fulfilled
.then(() => {
expect(spawnStub).to.have.been.calledOnce;
expect(spawnStub.firstCall.args[0]).to.equal("node");
expect(spawnStub.firstCall.args[1]).to.deep.equal([
"node_modules/serverless/bin/serverless",
"my",
"command",
"--stage=dev",
]);
expect(spawnStub.firstCall.args[2]).to.be.an("object")
.that.has.property("cwd");
});
});

it("should use custom options", () => {
const options = {
cwd: "myCwd",
myOpt: "myOption",
stage: "myStage",
};
testChildProcess.on.withArgs("exit").yields(0);
return expect(Serverless.invoke("my command", options)).to.be.fulfilled
.then(() => {
expect(spawnStub).to.have.been.calledOnce;
expect(spawnStub.firstCall.args[0]).to.equal("node");
expect(spawnStub.firstCall.args[1]).to.deep.equal([
"node_modules/serverless/bin/serverless",
"my",
"command",
"--myOpt=myOption",
"--stage=myStage",
]);
expect(spawnStub.firstCall.args[2]).to.be.an("object")
.that.has.property("cwd", "myCwd");
});
});

it("should reject if spawn fails", () => {
const options = {
cwd: "myCwd",
myOpt: "myOption",
stage: "myStage",
};
testChildProcess.on.withArgs("error").yields(new Error("SPAWN FAILED"));
return expect(Serverless.invoke("my command", options))
.to.be.rejectedWith("SPAWN FAILED");
});

it("should log stdout to channel", () => {
const testOutput = "My command output";
const options = {
cwd: "myCwd",
myOpt: "myOption",
stage: "myStage",
};
testChildProcess.stdout.on.withArgs("data").yields(testOutput);
testChildProcess.on.withArgs("exit").yields(0);
return expect(Serverless.invoke("my command", options))
.to.be.fulfilled
.then(() => {
expect(testOutputChannel.append).to.have.been.calledWithExactly(testOutput);
});
});

it("should log stderr to channel", () => {
const testOutput = "My error output";
const options = {
cwd: "myCwd",
myOpt: "myOption",
stage: "myStage",
};
testChildProcess.stderr.on.withArgs("data").yields(testOutput);
testChildProcess.on.withArgs("exit").yields(0);
return expect(Serverless.invoke("my command", options))
.to.be.fulfilled
.then(() => {
expect(testOutputChannel.append).to.have.been.calledWithExactly(testOutput);
});
});
});

describe("invokeWithResult", () => {
it("should spawn serverless", () => {
testChildProcess.on.withArgs("exit").yields(0);
return expect(Serverless.invokeWithResult("my command")).to.be.fulfilled
.then(() => {
expect(spawnStub).to.have.been.calledOnce;
expect(spawnStub.firstCall.args[0]).to.equal("node");
expect(spawnStub.firstCall.args[1]).to.deep.equal([
"node_modules/serverless/bin/serverless",
"my",
"command",
"--stage=dev",
]);
expect(spawnStub.firstCall.args[2]).to.be.an("object")
.that.has.property("cwd");
});
});

it("should use custom options", () => {
const options = {
cwd: "myCwd",
myOpt: "myOption",
stage: "myStage",
};
testChildProcess.on.withArgs("exit").yields(0);
return expect(Serverless.invokeWithResult("my command", options)).to.be.fulfilled
.then(() => {
expect(spawnStub).to.have.been.calledOnce;
expect(spawnStub.firstCall.args[0]).to.equal("node");
expect(spawnStub.firstCall.args[1]).to.deep.equal([
"node_modules/serverless/bin/serverless",
"my",
"command",
"--myOpt=myOption",
"--stage=myStage",
]);
expect(spawnStub.firstCall.args[2]).to.be.an("object")
.that.has.property("cwd", "myCwd");
});
});

it("should reject if spawn fails", () => {
const options = {
cwd: "myCwd",
myOpt: "myOption",
stage: "myStage",
};
testChildProcess.on.withArgs("error").yields(new Error("SPAWN FAILED"));
return expect(Serverless.invokeWithResult("my command", options))
.to.be.rejectedWith("SPAWN FAILED");
});

it("should capture stdout and not log to channel", () => {
const testOutput = "My command output";
const options = {
cwd: "myCwd",
myOpt: "myOption",
stage: "myStage",
};
testChildProcess.stdout.on.withArgs("data").yields(testOutput);
testChildProcess.on.withArgs("exit").yields(0);
return expect(Serverless.invokeWithResult("my command", options))
.to.be.fulfilled
.then(() => {
expect(testOutputChannel.append).to.not.have.been.called;
});
});

it("should log stderr to channel", () => {
const testOutput = "My error output";
const options = {
cwd: "myCwd",
myOpt: "myOption",
stage: "myStage",
};
testChildProcess.stderr.on.withArgs("data").yields(testOutput);
testChildProcess.on.withArgs("exit").yields(0);
return expect(Serverless.invoke("my command", options))
.to.be.fulfilled
.then(() => {
expect(testOutputChannel.append).to.have.been.calledWithExactly(testOutput);
});
});
});
});
49 changes: 49 additions & 0 deletions test/lib/ServerlessNode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as chai from "chai";
import * as _ from "lodash";
import * as sinon from "sinon";
import { NodeKind, ServerlessNode } from "../../src/lib/ServerlessNode";

// tslint:disable:no-unused-expression

const expect = chai.expect;

describe("ServerlessNode", () => {
it("should provide hasChildren property", () => {
const root = new ServerlessNode("root", NodeKind.ROOT);
const noChildren = new ServerlessNode("root", NodeKind.ROOT);

root.children.push(new ServerlessNode("Child", NodeKind.CONTAINER));

expect(root.hasChildren).to.be.true;
expect(noChildren.hasChildren).to.be.false;
});

it("should set document root resursively", () => {
const docRoot = "myRoot";
const root = new ServerlessNode("root", NodeKind.ROOT);

// Add some child nodes
for (let n = 0; n < 5; n++) {
const childNode = new ServerlessNode(`child ${n}`, NodeKind.CONTAINER);
for (let m = 0; m < 2; m++) {
childNode.children.push(new ServerlessNode(`func ${m}`, NodeKind.FUNCTION));
}
root.children.push(childNode);
}

expect(root.setDocumentRoot(docRoot)).to.not.throw;

// Check all child nodes
const allChildren = _.flatMap(root.children, child => {
const result = [ child.documentRoot ];
if (child.hasChildren) {
const childDocRoots = _.flatMap(child.children, subChild => subChild.documentRoot);
Array.prototype.push.apply(result, childDocRoots);
}
return result;
});
expect(allChildren).to.been.of.length(15);
expect(_.every(allChildren, (childDocRoot: string) => _.isEqual(childDocRoot, docRoot)))
.to.been.true;
});
});
11 changes: 11 additions & 0 deletions test/lib/TestContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as sinon from "sinon";
import { commands, ExtensionContext, Memento } from "vscode";

export class TestContext implements ExtensionContext {
public subscriptions: Array<{ dispose(): any; }> = [];
public workspaceState: Memento;
public globalState: Memento;
public extensionPath: string = "myExtensionPath";
public asAbsolutePath: sinon.SinonStub = sinon.stub();
public storagePath: string = "myStoragePath";
}
11 changes: 1 addition & 10 deletions test/lib/commands/DeployFunction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,18 @@ import * as chai from "chai";
import * as chaiAsPromised from "chai-as-promised";
import * as _ from "lodash";
import * as sinon from "sinon";
import { ExtensionContext, Memento } from "vscode";
import { CommandBase, ICommand } from "../../../src/lib/CommandBase";
import { DeployFunction } from "../../../src/lib/commands/DeployFunction";
import { Serverless } from "../../../src/lib/Serverless";
import { NodeKind, ServerlessNode } from "../../../src/lib/ServerlessNode";
import { TestContext } from "../TestContext";

// tslint:disable:no-unused-expression

// tslint:disable-next-line:no-var-requires
chai.use(chaiAsPromised);
const expect = chai.expect;

class TestContext implements ExtensionContext {
public subscriptions: Array<{ dispose(): any; }> = [];
public workspaceState: Memento;
public globalState: Memento;
public extensionPath: string = "myExtensionPath";
public asAbsolutePath: sinon.SinonStub = sinon.stub();
public storagePath: string = "myStoragePath";
}

/**
* Unit tests for the DeployFunction command
*/
Expand Down
12 changes: 2 additions & 10 deletions test/lib/commands/InvokeLocal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,19 @@ import * as chaiAsPromised from "chai-as-promised";
import * as _ from "lodash";
import * as path from "path";
import * as sinon from "sinon";
import { ExtensionContext, Memento, Uri, window } from "vscode";
import { Uri, window } from "vscode";
import { CommandBase, ICommand } from "../../../src/lib/CommandBase";
import { InvokeLocal } from "../../../src/lib/commands/InvokeLocal";
import { Serverless } from "../../../src/lib/Serverless";
import { NodeKind, ServerlessNode } from "../../../src/lib/ServerlessNode";
import { TestContext } from "../TestContext";

// tslint:disable:no-unused-expression

// tslint:disable-next-line:no-var-requires
chai.use(chaiAsPromised);
const expect = chai.expect;

class TestContext implements ExtensionContext {
public subscriptions: Array<{ dispose(): any; }> = [];
public workspaceState: Memento;
public globalState: Memento;
public extensionPath: string = "myExtensionPath";
public asAbsolutePath: sinon.SinonStub = sinon.stub();
public storagePath: string = "myStoragePath";
}

/**
* Unit tests for the InvokeLocal command
*/
Expand Down
Loading