@@ -23,6 +23,7 @@ const runUpdateNotifier = async (t, {
23
23
prefixDir,
24
24
version = CURRENT_VERSION ,
25
25
argv = [ ] ,
26
+ wroteFile = false ,
26
27
...config
27
28
} = { } ) => {
28
29
const mockFs = {
@@ -37,6 +38,7 @@ const runUpdateNotifier = async (t, {
37
38
return { mtime : new Date ( STAT_MTIME ) }
38
39
} ,
39
40
writeFile : async ( path , content ) => {
41
+ wroteFile = true
40
42
if ( content !== '' ) {
41
43
t . fail ( 'no write file content allowed' )
42
44
}
@@ -86,106 +88,132 @@ const runUpdateNotifier = async (t, {
86
88
const result = await updateNotifier ( mock . npm )
87
89
88
90
return {
91
+ wroteFile,
89
92
result,
90
93
MANIFEST_REQUEST ,
91
94
}
92
95
}
93
96
94
- t . test ( 'does not notify by default' , async t => {
95
- const { result, MANIFEST_REQUEST } = await runUpdateNotifier ( t )
97
+ t . test ( 'duration has elapsed, no updates' , async t => {
98
+ const { wroteFile, result, MANIFEST_REQUEST } = await runUpdateNotifier ( t )
99
+ t . equal ( wroteFile , true )
96
100
t . not ( result )
97
101
t . equal ( MANIFEST_REQUEST . length , 1 )
98
102
} )
99
103
100
104
t . test ( 'situations in which we do not notify' , t => {
101
105
t . test ( 'nothing to do if notifier disabled' , async t => {
102
- const { result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , {
106
+ const { wroteFile , result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , {
103
107
'update-notifier' : false ,
104
108
} )
109
+ t . equal ( wroteFile , false )
105
110
t . equal ( result , null )
106
111
t . strictSame ( MANIFEST_REQUEST , [ ] , 'no requests for manifests' )
107
112
} )
108
113
109
114
t . test ( 'do not suggest update if already updating' , async t => {
110
- const { result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , {
115
+ const { wroteFile , result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , {
111
116
command : 'install' ,
112
117
prefixDir : { 'package.json' : `{"name":"${ t . testName } "}` } ,
113
118
argv : [ 'npm' ] ,
114
119
global : true ,
115
120
} )
121
+ t . equal ( wroteFile , false )
116
122
t . equal ( result , null )
117
123
t . strictSame ( MANIFEST_REQUEST , [ ] , 'no requests for manifests' )
118
124
} )
119
125
120
126
t . test ( 'do not suggest update if already updating with spec' , async t => {
121
- const { result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , {
127
+ const { wroteFile , result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , {
122
128
command : 'install' ,
123
129
prefixDir : { 'package.json' : `{"name":"${ t . testName } "}` } ,
124
130
argv : [ 'npm@latest' ] ,
125
131
global : true ,
126
132
} )
133
+ t . equal ( wroteFile , false )
127
134
t . equal ( result , null )
128
135
t . strictSame ( MANIFEST_REQUEST , [ ] , 'no requests for manifests' )
129
136
} )
130
137
131
138
t . test ( 'do not update if same as latest' , async t => {
132
- const { result, MANIFEST_REQUEST } = await runUpdateNotifier ( t )
139
+ const { wroteFile, result, MANIFEST_REQUEST } = await runUpdateNotifier ( t )
140
+ t . equal ( wroteFile , true )
133
141
t . equal ( result , null )
134
142
t . strictSame ( MANIFEST_REQUEST , [ 'npm@latest' ] , 'requested latest version' )
135
143
} )
136
144
t . test ( 'check if stat errors (here for coverage)' , async t => {
137
145
const STAT_ERROR = new Error ( 'blorg' )
138
- const { result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , { STAT_ERROR } )
146
+ const { wroteFile, result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , { STAT_ERROR } )
147
+ t . equal ( wroteFile , true )
139
148
t . equal ( result , null )
140
149
t . strictSame ( MANIFEST_REQUEST , [ 'npm@latest' ] , 'requested latest version' )
141
150
} )
142
151
t . test ( 'ok if write errors (here for coverage)' , async t => {
143
152
const WRITE_ERROR = new Error ( 'grolb' )
144
- const { result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , { WRITE_ERROR } )
153
+ const { wroteFile, result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , { WRITE_ERROR } )
154
+ t . equal ( wroteFile , true )
145
155
t . equal ( result , null )
146
156
t . strictSame ( MANIFEST_REQUEST , [ 'npm@latest' ] , 'requested latest version' )
147
157
} )
148
158
t . test ( 'ignore pacote failures (here for coverage)' , async t => {
149
159
const PACOTE_ERROR = new Error ( 'pah-KO-tchay' )
150
- const { result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , { PACOTE_ERROR } )
160
+ const { wroteFile , result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , { PACOTE_ERROR } )
151
161
t . equal ( result , null )
162
+ t . equal ( wroteFile , true )
152
163
t . strictSame ( MANIFEST_REQUEST , [ 'npm@latest' ] , 'requested latest version' )
153
164
} )
154
165
t . test ( 'do not update if newer than latest, but same as next' , async t => {
155
- const { result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , { version : NEXT_VERSION } )
166
+ const {
167
+ wroteFile,
168
+ result,
169
+ MANIFEST_REQUEST ,
170
+ } = await runUpdateNotifier ( t , { version : NEXT_VERSION } )
156
171
t . equal ( result , null )
172
+ t . equal ( wroteFile , true )
157
173
const reqs = [ 'npm@latest' , `npm@^${ NEXT_VERSION } ` ]
158
174
t . strictSame ( MANIFEST_REQUEST , reqs , 'requested latest and next versions' )
159
175
} )
160
176
t . test ( 'do not update if on the latest beta' , async t => {
161
- const { result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , { version : CURRENT_BETA } )
177
+ const {
178
+ wroteFile,
179
+ result,
180
+ MANIFEST_REQUEST ,
181
+ } = await runUpdateNotifier ( t , { version : CURRENT_BETA } )
162
182
t . equal ( result , null )
183
+ t . equal ( wroteFile , true )
163
184
const reqs = [ `npm@^${ CURRENT_BETA } ` ]
164
185
t . strictSame ( MANIFEST_REQUEST , reqs , 'requested latest and next versions' )
165
186
} )
166
187
167
188
t . test ( 'do not update in CI' , async t => {
168
- const { result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , { mocks : {
189
+ const { wroteFile , result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , { mocks : {
169
190
'ci-info' : { isCI : true , name : 'something' } ,
170
191
} } )
192
+ t . equal ( wroteFile , false )
171
193
t . equal ( result , null )
172
194
t . strictSame ( MANIFEST_REQUEST , [ ] , 'no requests for manifests' )
173
195
} )
174
196
175
197
t . test ( 'only check weekly for GA releases' , async t => {
176
198
// One week (plus five minutes to account for test environment fuzziness)
177
199
const STAT_MTIME = Date . now ( ) - 1000 * 60 * 60 * 24 * 7 + 1000 * 60 * 5
178
- const { result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , { STAT_MTIME } )
200
+ const { wroteFile, result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , { STAT_MTIME } )
201
+ t . equal ( wroteFile , false , 'duration was not reset' )
179
202
t . equal ( result , null )
180
203
t . strictSame ( MANIFEST_REQUEST , [ ] , 'no requests for manifests' )
181
204
} )
182
205
183
206
t . test ( 'only check daily for betas' , async t => {
184
207
// One day (plus five minutes to account for test environment fuzziness)
185
208
const STAT_MTIME = Date . now ( ) - 1000 * 60 * 60 * 24 + 1000 * 60 * 5
186
- const res = await runUpdateNotifier ( t , { STAT_MTIME , version : HAVE_BETA } )
187
- t . equal ( res . result , null )
188
- t . strictSame ( res . MANIFEST_REQUEST , [ ] , 'no requests for manifests' )
209
+ const {
210
+ wroteFile,
211
+ result,
212
+ MANIFEST_REQUEST ,
213
+ } = await runUpdateNotifier ( t , { STAT_MTIME , version : HAVE_BETA } )
214
+ t . equal ( wroteFile , false , 'duration was not reset' )
215
+ t . equal ( result , null )
216
+ t . strictSame ( MANIFEST_REQUEST , [ ] , 'no requests for manifests' )
189
217
} )
190
218
191
219
t . end ( )
@@ -204,8 +232,13 @@ t.test('notification situations', async t => {
204
232
for ( const [ version , reqs ] of Object . entries ( cases ) ) {
205
233
for ( const color of [ false , 'always' ] ) {
206
234
await t . test ( `${ version } - color=${ color } ` , async t => {
207
- const { result, MANIFEST_REQUEST } = await runUpdateNotifier ( t , { version, color } )
235
+ const {
236
+ wroteFile,
237
+ result,
238
+ MANIFEST_REQUEST ,
239
+ } = await runUpdateNotifier ( t , { version, color } )
208
240
t . matchSnapshot ( result )
241
+ t . equal ( wroteFile , true )
209
242
t . strictSame ( MANIFEST_REQUEST , reqs . map ( r => `npm@${ r . replace ( '{V}' , version ) } ` ) )
210
243
} )
211
244
}
0 commit comments