Skip to content

Commit 57b1631

Browse files
committed
test: add tests for plugin dir removal
1 parent 2b9bd70 commit 57b1631

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

lib/commands/plugin/create-plugin.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export class CreatePluginCommand implements ICommand {
55
public allowedParameters: ICommandParameter[] = [];
66
public userMessage = "What is your GitHub username?\n(will be used to update the Github URLs in the plugin's package.json)";
77
public nameMessage = "What will be the name of your plugin?\n(use lowercase characters and dashes only)";
8+
public pathAlreadyExistsMessageTemplate = "Path already exists and is not empty %s";
89
constructor(private $options: IOptions,
910
private $errors: IErrors,
1011
private $terminalSpinnerService: ITerminalSpinnerService,
@@ -79,7 +80,7 @@ export class CreatePluginCommand implements ICommand {
7980
this.$fs.createDirectory(projectDir);
8081

8182
if (this.$fs.exists(projectDir) && !this.$fs.isEmptyDir(projectDir)) {
82-
this.$errors.fail("Path already exists and is not empty %s", projectDir);
83+
this.$errors.fail(this.pathAlreadyExistsMessageTemplate, projectDir);
8384
}
8485
}
8586

test/plugin-create.ts

+64-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ import * as stubs from "./stubs";
33
import { CreatePluginCommand } from "../lib/commands/plugin/create-plugin";
44
import { assert } from "chai";
55
import helpers = require("../lib/common/helpers");
6+
import * as sinon from "sinon";
7+
import temp = require("temp");
8+
import * as path from "path";
9+
import * as util from "util";
10+
temp.track();
611

712
interface IPacoteOutput {
813
packageName: string;
914
destinationDirectory: string;
1015
}
1116

1217
const originalIsInteractive = helpers.isInteractive;
13-
const dummyArgs = ["dummyProjectName"];
18+
const dummyProjectName = "dummyProjectName";
19+
const dummyArgs = [dummyProjectName];
1420
const dummyUser = "devUsername";
1521
const dummyName = "devPlugin";
1622
const dummyPacote: IPacoteOutput = { packageName: "", destinationDirectory: "" };
@@ -142,5 +148,62 @@ describe("Plugin create command tests", () => {
142148
options.pluginName = dummyName;
143149
await createPluginCommand.execute(dummyArgs);
144150
});
151+
152+
describe("when fails", () => {
153+
let sandbox: sinon.SinonSandbox;
154+
let fsSpy: sinon.SinonSpy;
155+
let projectPath: string;
156+
157+
beforeEach(() => {
158+
sandbox = sinon.sandbox.create();
159+
const workingPath = temp.mkdirSync("test_plugin");
160+
options.path = workingPath;
161+
projectPath = path.join(workingPath, dummyProjectName);
162+
const fsService = testInjector.resolve("fs");
163+
fsSpy = sandbox.spy(fsService, "deleteDirectory");
164+
});
165+
166+
afterEach(() => {
167+
sandbox.restore();
168+
});
169+
170+
it("downloadPackage, should remove projectDir", async () => {
171+
const errorMessage = "Test fail";
172+
const pacoteService = testInjector.resolve("pacoteService");
173+
sandbox.stub(pacoteService, "extractPackage").callsFake(() => {
174+
return Promise.reject(new Error(errorMessage));
175+
});
176+
177+
const executePromise = createPluginCommand.execute(dummyArgs);
178+
179+
await assert.isRejected(executePromise, errorMessage);
180+
assert(fsSpy.calledWith(projectPath));
181+
});
182+
183+
it("setupSeed, should remove projectDir", async () => {
184+
const errorMessage = "Test fail";
185+
const npmService = testInjector.resolve("npm");
186+
sandbox.stub(npmService, "install").callsFake(() => {
187+
return Promise.reject(new Error(errorMessage));
188+
});
189+
190+
const executePromise = createPluginCommand.execute(dummyArgs);
191+
192+
await assert.isRejected(executePromise, errorMessage);
193+
assert(fsSpy.calledWith(projectPath));
194+
});
195+
196+
it("ensurePachageDir should not remove projectDir", async () => {
197+
const fsService = testInjector.resolve("fs");
198+
sandbox.stub(fsService, "isEmptyDir").callsFake(() => {
199+
return false;
200+
});
201+
202+
const executePromise = createPluginCommand.execute(dummyArgs);
203+
204+
await assert.isRejected(executePromise, util.format(createPluginCommand.pathAlreadyExistsMessageTemplate, projectPath));
205+
assert(fsSpy.notCalled);
206+
});
207+
});
145208
});
146209
});

0 commit comments

Comments
 (0)