@@ -49,6 +49,14 @@ const createTestInjector = (): IInjector => {
49
49
} ;
50
50
51
51
describe ( "post-install command" , ( ) => {
52
+ const originalNpmArgv = process . env . npm_config_argv ;
53
+ const originalSudoUser = process . env . SUDO_USER ;
54
+
55
+ afterEach ( ( ) => {
56
+ process . env . npm_config_argv = originalNpmArgv ;
57
+ process . env . SUDO_USER = originalSudoUser ;
58
+ } ) ;
59
+
52
60
it ( "calls subscriptionService.subscribeForNewsletter method" , async ( ) => {
53
61
const testInjector = createTestInjector ( ) ;
54
62
const subscriptionService = testInjector . resolve < ISubscriptionService > ( "subscriptionService" ) ;
@@ -61,4 +69,62 @@ describe("post-install command", () => {
61
69
await postInstallCommand . execute ( [ ] ) ;
62
70
assert . isTrue ( isSubscribeForNewsletterCalled , "post-install-cli command must call subscriptionService.subscribeForNewsletter" ) ;
63
71
} ) ;
72
+
73
+ const verifyResult = async ( opts : { shouldCallMethod : boolean } ) : Promise < void > => {
74
+ const testInjector = createTestInjector ( ) ;
75
+ const subscriptionService = testInjector . resolve < ISubscriptionService > ( "subscriptionService" ) ;
76
+ let isSubscribeForNewsletterCalled = false ;
77
+ subscriptionService . subscribeForNewsletter = async ( ) : Promise < void > => {
78
+ isSubscribeForNewsletterCalled = true ;
79
+ } ;
80
+
81
+ const helpService = testInjector . resolve < IHelpService > ( "helpService" ) ;
82
+ let isGenerateHtmlPagesCalled = false ;
83
+ helpService . generateHtmlPages = async ( ) : Promise < void > => {
84
+ isGenerateHtmlPagesCalled = true ;
85
+ } ;
86
+
87
+ const analyticsService = testInjector . resolve < IAnalyticsService > ( "analyticsService" ) ;
88
+ let isCheckConsentCalled = false ;
89
+ analyticsService . checkConsent = async ( ) : Promise < void > => {
90
+ isCheckConsentCalled = true ;
91
+ } ;
92
+
93
+ const commandsService = testInjector . resolve < ICommandsService > ( "commandsService" ) ;
94
+ let isTryExecuteCommandCalled = false ;
95
+ commandsService . tryExecuteCommand = async ( ) : Promise < void > => {
96
+ isTryExecuteCommandCalled = true ;
97
+ } ;
98
+
99
+ const postInstallCommand = testInjector . resolveCommand ( "post-install-cli" ) ;
100
+ await postInstallCommand . execute ( [ ] ) ;
101
+
102
+ process . env . npm_config_argv = originalNpmArgv ;
103
+ process . env . SUDO_USER = originalSudoUser ;
104
+
105
+ const hasNotInMsg = opts . shouldCallMethod ? "" : "NOT" ;
106
+
107
+ assert . equal ( isSubscribeForNewsletterCalled , opts . shouldCallMethod , `post-install-cli command must ${ hasNotInMsg } call subscriptionService.subscribeForNewsletter` ) ;
108
+ assert . equal ( isGenerateHtmlPagesCalled , opts . shouldCallMethod , `post-install-cli command must ${ hasNotInMsg } call helpService.generateHtmlPages` ) ;
109
+ assert . equal ( isCheckConsentCalled , opts . shouldCallMethod , `post-install-cli command must ${ hasNotInMsg } call analyticsService.checkConsent` ) ;
110
+ assert . equal ( isTryExecuteCommandCalled , opts . shouldCallMethod , `post-install-cli command must ${ hasNotInMsg } call commandsService.tryExecuteCommand` ) ;
111
+ } ;
112
+
113
+ it ( "does not call specific methods when CLI is installed with sudo without `--unsafe-perm`" , ( ) => {
114
+ process . env . npm_config_argv = JSON . stringify ( { } ) ;
115
+ process . env . SUDO_USER = "user1" ;
116
+ return verifyResult ( { shouldCallMethod : false } ) ;
117
+ } ) ;
118
+
119
+ it ( "calls specific methods when CLI is installed with sudo with `--unsafe-perm`" , async ( ) => {
120
+ process . env . npm_config_argv = JSON . stringify ( { original : [ "--unsafe-perm" ] } ) ;
121
+ process . env . SUDO_USER = "user1" ;
122
+ return verifyResult ( { shouldCallMethod : true } ) ;
123
+ } ) ;
124
+
125
+ it ( "calls specific methods when CLI is installed without sudo" , async ( ) => {
126
+ process . env . npm_config_argv = JSON . stringify ( { } ) ;
127
+ delete process . env . SUDO_USER ;
128
+ return verifyResult ( { shouldCallMethod : true } ) ;
129
+ } ) ;
64
130
} ) ;
0 commit comments