-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-templates-service.ts
102 lines (88 loc) · 4.66 KB
/
project-templates-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { Yok } from "../lib/common/yok";
import * as stubs from "./stubs";
import { ProjectTemplatesService } from "../lib/services/project-templates-service";
import { assert } from "chai";
import * as path from "path";
import temp = require("temp");
import * as constants from "../lib/constants";
let isDeleteDirectoryCalledForNodeModulesDir = false;
let nativeScriptValidatedTemplatePath = "nsValidatedTemplatePath";
function createTestInjector(configuration?: { shouldNpmInstallThrow: boolean, npmInstallationDirContents: string[], npmInstallationDirNodeModulesContents: string[] }): IInjector {
let injector = new Yok();
injector.register("errors", stubs.ErrorsStub);
injector.register("logger", stubs.LoggerStub);
injector.register("fs", {
readDirectory: (dirPath: string): string[] => {
if (dirPath.toLowerCase().indexOf("node_modules") !== -1) {
return configuration.npmInstallationDirNodeModulesContents;
}
return configuration.npmInstallationDirContents;
},
deleteDirectory: (directory: string) => {
if (directory.indexOf("node_modules") !== -1) {
isDeleteDirectoryCalledForNodeModulesDir = true;
}
}
});
injector.register("npm", {
install: (packageName: string, pathToSave: string, config?: any) => {
if (configuration.shouldNpmInstallThrow) {
throw new Error("NPM install throws error.");
}
return "sample result";
}
});
injector.register("npmInstallationManager", {
install: (packageName: string, options?: INpmInstallOptions) => {
if (configuration.shouldNpmInstallThrow) {
throw new Error("NPM install throws error.");
}
return Promise.resolve(nativeScriptValidatedTemplatePath);
}
});
injector.register("projectTemplatesService", ProjectTemplatesService);
return injector;
}
describe("project-templates-service", () => {
let testInjector: IInjector;
let projectTemplatesService: IProjectTemplatesService;
beforeEach(() => {
isDeleteDirectoryCalledForNodeModulesDir = false;
});
describe("prepareTemplate", () => {
describe("throws error", () => {
it("when npm install fails", async () => {
testInjector = createTestInjector({ shouldNpmInstallThrow: true, npmInstallationDirContents: [], npmInstallationDirNodeModulesContents: null });
projectTemplatesService = testInjector.resolve("projectTemplatesService");
let tempFolder = temp.mkdirSync("preparetemplate");
await assert.isRejected(projectTemplatesService.prepareTemplate("invalidName", tempFolder));
});
});
describe("returns correct path to template", () => {
it("when reserved template name is used", async () => {
testInjector = createTestInjector({ shouldNpmInstallThrow: false, npmInstallationDirContents: [], npmInstallationDirNodeModulesContents: [] });
projectTemplatesService = testInjector.resolve("projectTemplatesService");
let tempFolder = temp.mkdirSync("preparetemplate");
let actualPathToTemplate = await projectTemplatesService.prepareTemplate("typescript", tempFolder);
assert.strictEqual(path.basename(actualPathToTemplate), nativeScriptValidatedTemplatePath);
assert.strictEqual(isDeleteDirectoryCalledForNodeModulesDir, true, "When correct path is returned, template's node_modules directory should be deleted.");
});
it("when reserved template name is used (case-insensitive test)", async () => {
testInjector = createTestInjector({ shouldNpmInstallThrow: false, npmInstallationDirContents: [], npmInstallationDirNodeModulesContents: [] });
projectTemplatesService = testInjector.resolve("projectTemplatesService");
let tempFolder = temp.mkdirSync("preparetemplate");
let actualPathToTemplate = await projectTemplatesService.prepareTemplate("tYpEsCriPT", tempFolder);
assert.strictEqual(path.basename(actualPathToTemplate), nativeScriptValidatedTemplatePath);
assert.strictEqual(isDeleteDirectoryCalledForNodeModulesDir, true, "When correct path is returned, template's node_modules directory should be deleted.");
});
it("uses defaultTemplate when undefined is passed as parameter", async () => {
testInjector = createTestInjector({ shouldNpmInstallThrow: false, npmInstallationDirContents: [], npmInstallationDirNodeModulesContents: [] });
projectTemplatesService = testInjector.resolve("projectTemplatesService");
let tempFolder = temp.mkdirSync("preparetemplate");
let actualPathToTemplate = await projectTemplatesService.prepareTemplate(constants.RESERVED_TEMPLATE_NAMES["default"], tempFolder);
assert.strictEqual(path.basename(actualPathToTemplate), nativeScriptValidatedTemplatePath);
assert.strictEqual(isDeleteDirectoryCalledForNodeModulesDir, true, "When correct path is returned, template's node_modules directory should be deleted.");
});
});
});
});