-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathpost-install.ts
130 lines (102 loc) · 5.04 KB
/
post-install.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { Yok } from "../../lib/common/yok";
import { assert } from "chai";
import { PostInstallCliCommand } from "../../lib/commands/post-install";
import { SettingsService } from "../../lib/common/test/unit-tests/stubs";
const createTestInjector = (): IInjector => {
const testInjector = new Yok();
testInjector.register("fs", {
setCurrentUserAsOwner: async (path: string, owner: string): Promise<void> => undefined
});
testInjector.register("subscriptionService", {
subscribeForNewsletter: async (): Promise<void> => undefined
});
testInjector.register("staticConfig", {});
testInjector.register("commandsService", {
tryExecuteCommand: async (commandName: string, commandArguments: string[]): Promise<void> => undefined
});
testInjector.register("helpService", {
generateHtmlPages: async (): Promise<void> => undefined
});
testInjector.register("options", {});
testInjector.register("doctorService", {
printWarnings: async (): Promise<boolean> => undefined
});
testInjector.register("analyticsService", {
checkConsent: async (): Promise<void> => undefined,
track: async (featureName: string, featureValue: string): Promise<void> => undefined
});
testInjector.register("logger", {
out: (formatStr?: any, ...args: any[]): void => undefined,
printMarkdown: (...args: any[]): void => undefined
});
testInjector.register("settingsService", SettingsService);
testInjector.registerCommand("post-install-cli", PostInstallCliCommand);
testInjector.register("hostInfo", {});
return testInjector;
};
describe("post-install command", () => {
const originalNpmArgv = process.env.npm_config_argv;
const originalSudoUser = process.env.SUDO_USER;
afterEach(() => {
process.env.npm_config_argv = originalNpmArgv;
process.env.SUDO_USER = originalSudoUser;
});
it("calls subscriptionService.subscribeForNewsletter method", async () => {
const testInjector = createTestInjector();
const subscriptionService = testInjector.resolve<ISubscriptionService>("subscriptionService");
let isSubscribeForNewsletterCalled = false;
subscriptionService.subscribeForNewsletter = async (): Promise<void> => {
isSubscribeForNewsletterCalled = true;
};
const postInstallCommand = testInjector.resolveCommand("post-install-cli");
await postInstallCommand.execute([]);
assert.isTrue(isSubscribeForNewsletterCalled, "post-install-cli command must call subscriptionService.subscribeForNewsletter");
});
const verifyResult = async (opts: { shouldCallMethod: boolean }): Promise<void> => {
const testInjector = createTestInjector();
const subscriptionService = testInjector.resolve<ISubscriptionService>("subscriptionService");
let isSubscribeForNewsletterCalled = false;
subscriptionService.subscribeForNewsletter = async (): Promise<void> => {
isSubscribeForNewsletterCalled = true;
};
const helpService = testInjector.resolve<IHelpService>("helpService");
let isGenerateHtmlPagesCalled = false;
helpService.generateHtmlPages = async (): Promise<void> => {
isGenerateHtmlPagesCalled = true;
};
const analyticsService = testInjector.resolve<IAnalyticsService>("analyticsService");
let isCheckConsentCalled = false;
analyticsService.checkConsent = async (): Promise<void> => {
isCheckConsentCalled = true;
};
const commandsService = testInjector.resolve<ICommandsService>("commandsService");
let isTryExecuteCommandCalled = false;
commandsService.tryExecuteCommand = async (): Promise<void> => {
isTryExecuteCommandCalled = true;
};
const postInstallCommand = testInjector.resolveCommand("post-install-cli");
await postInstallCommand.execute([]);
process.env.npm_config_argv = originalNpmArgv;
process.env.SUDO_USER = originalSudoUser;
const hasNotInMsg = opts.shouldCallMethod ? "" : "NOT";
assert.equal(isSubscribeForNewsletterCalled, opts.shouldCallMethod, `post-install-cli command must ${hasNotInMsg} call subscriptionService.subscribeForNewsletter`);
assert.equal(isGenerateHtmlPagesCalled, opts.shouldCallMethod, `post-install-cli command must ${hasNotInMsg} call helpService.generateHtmlPages`);
assert.equal(isCheckConsentCalled, opts.shouldCallMethod, `post-install-cli command must ${hasNotInMsg} call analyticsService.checkConsent`);
assert.equal(isTryExecuteCommandCalled, opts.shouldCallMethod, `post-install-cli command must ${hasNotInMsg} call commandsService.tryExecuteCommand`);
};
it("does not call specific methods when CLI is installed with sudo without `--unsafe-perm`", () => {
process.env.npm_config_argv = JSON.stringify({});
process.env.SUDO_USER = "user1";
return verifyResult({ shouldCallMethod: false });
});
it("calls specific methods when CLI is installed with sudo with `--unsafe-perm`", async () => {
process.env.npm_config_argv = JSON.stringify({ original: ["--unsafe-perm"] });
process.env.SUDO_USER = "user1";
return verifyResult({ shouldCallMethod: true });
});
it("calls specific methods when CLI is installed without sudo", async () => {
process.env.npm_config_argv = JSON.stringify({});
delete process.env.SUDO_USER;
return verifyResult({ shouldCallMethod: true });
});
});