1
1
const { join } = require ( 'path' )
2
- const { promisify } = require ( 'util ' )
3
- const fs = require ( 'fs ' )
2
+ const fs = require ( 'fs/promises ' )
3
+ const ini = require ( 'ini ' )
4
4
const tspawk = require ( '../../fixtures/tspawk' )
5
5
const t = require ( 'tap' )
6
6
7
7
const spawk = tspawk ( t )
8
8
9
- const readFile = promisify ( fs . readFile )
10
-
11
9
const Sandbox = require ( '../../fixtures/sandbox.js' )
12
10
13
11
t . test ( 'config no args' , async t => {
@@ -142,60 +140,100 @@ t.test('config delete no args', async t => {
142
140
t . test ( 'config delete single key' , async t => {
143
141
// location defaults to user, so we work with a userconfig
144
142
const home = t . testdir ( {
145
- '.npmrc' : 'foo=bar\nbar=baz ' ,
143
+ '.npmrc' : 'access=public\nall=true ' ,
146
144
} )
147
145
148
146
const sandbox = new Sandbox ( t )
149
- await sandbox . run ( 'config' , [ 'delete' , 'foo ' ] , { home } )
147
+ await sandbox . run ( 'config' , [ 'delete' , 'access ' ] , { home } )
150
148
151
- t . equal ( sandbox . config . get ( 'foo ' ) , undefined , 'foo should no longer be set ' )
149
+ t . equal ( sandbox . config . get ( 'access ' ) , null , 'acces should be defaulted ' )
152
150
153
- const contents = await readFile ( join ( home , '.npmrc' ) , { encoding : 'utf8' } )
154
- t . not ( contents . includes ( 'foo=' ) , 'foo was removed on disk' )
151
+ const contents = await fs . readFile ( join ( home , '.npmrc' ) , { encoding : 'utf8' } )
152
+ const rc = ini . parse ( contents )
153
+ t . not ( rc . access , 'access is not set' )
155
154
} )
156
155
157
156
t . test ( 'config delete multiple keys' , async t => {
158
157
const home = t . testdir ( {
159
- '.npmrc' : 'foo=bar\nbar=baz\nbaz=buz ' ,
158
+ '.npmrc' : 'access=public\nall=true\naudit=false ' ,
160
159
} )
161
160
162
161
const sandbox = new Sandbox ( t )
163
- await sandbox . run ( 'config' , [ 'delete' , 'foo ' , 'bar ' ] , { home } )
162
+ await sandbox . run ( 'config' , [ 'delete' , 'access ' , 'all ' ] , { home } )
164
163
165
- t . equal ( sandbox . config . get ( 'foo ' ) , undefined , 'foo should no longer be set ' )
166
- t . equal ( sandbox . config . get ( 'bar ' ) , undefined , 'bar should no longer be set ' )
164
+ t . equal ( sandbox . config . get ( 'access ' ) , null , 'access should be defaulted ' )
165
+ t . equal ( sandbox . config . get ( 'all ' ) , false , 'all should be defaulted ' )
167
166
168
- const contents = await readFile ( join ( home , '.npmrc' ) , { encoding : 'utf8' } )
169
- t . not ( contents . includes ( 'foo=' ) , 'foo was removed on disk' )
170
- t . not ( contents . includes ( 'bar=' ) , 'bar was removed on disk' )
167
+ const contents = await fs . readFile ( join ( home , '.npmrc' ) , { encoding : 'utf8' } )
168
+ const rc = ini . parse ( contents )
169
+ t . not ( rc . access , 'access is not set' )
170
+ t . not ( rc . all , 'all is not set' )
171
171
} )
172
172
173
173
t . test ( 'config delete key --location=global' , async t => {
174
174
const global = t . testdir ( {
175
- npmrc : 'foo=bar\nbar=baz ' ,
175
+ npmrc : 'access=public\nall=true ' ,
176
176
} )
177
177
178
178
const sandbox = new Sandbox ( t )
179
- await sandbox . run ( 'config' , [ 'delete' , 'foo ' , '--location=global' ] , { global } )
179
+ await sandbox . run ( 'config' , [ 'delete' , 'access ' , '--location=global' ] , { global } )
180
180
181
- t . equal ( sandbox . config . get ( 'foo ' , 'global' ) , undefined , 'foo should no longer be set ' )
181
+ t . equal ( sandbox . config . get ( 'access ' , 'global' ) , undefined , 'access should be defaulted ' )
182
182
183
- const contents = await readFile ( join ( global , 'npmrc' ) , { encoding : 'utf8' } )
184
- t . not ( contents . includes ( 'foo=' ) , 'foo was removed on disk' )
183
+ const contents = await fs . readFile ( join ( global , 'npmrc' ) , { encoding : 'utf8' } )
184
+ const rc = ini . parse ( contents )
185
+ t . not ( rc . access , 'access is not set' )
185
186
} )
186
187
187
188
t . test ( 'config delete key --global' , async t => {
188
189
const global = t . testdir ( {
189
- npmrc : 'foo=bar\nbar=baz ' ,
190
+ npmrc : 'access=public\nall=true ' ,
190
191
} )
191
192
192
193
const sandbox = new Sandbox ( t )
193
- await sandbox . run ( 'config' , [ 'delete' , 'foo' , '--global' ] , { global } )
194
+ await sandbox . run ( 'config' , [ 'delete' , 'access' , '--global' ] , { global } )
195
+
196
+ t . equal ( sandbox . config . get ( 'access' , 'global' ) , undefined , 'access should no longer be set' )
197
+
198
+ const contents = await fs . readFile ( join ( global , 'npmrc' ) , { encoding : 'utf8' } )
199
+ const rc = ini . parse ( contents )
200
+ t . not ( rc . access , 'access is not set' )
201
+ } )
202
+
203
+ t . test ( 'config set invalid option' , async t => {
204
+ const sandbox = new Sandbox ( t )
205
+ await t . rejects (
206
+ sandbox . run ( 'config' , [ 'set' , 'nonexistantconfigoption' , 'something' ] ) ,
207
+ / n o t a v a l i d n p m o p t i o n /
208
+ )
209
+ } )
210
+
211
+ t . test ( 'config set deprecated option' , async t => {
212
+ const sandbox = new Sandbox ( t )
213
+ await t . rejects (
214
+ sandbox . run ( 'config' , [ 'set' , 'shrinkwrap' , 'true' ] ) ,
215
+ / d e p r e c a t e d /
216
+ )
217
+ } )
194
218
195
- t . equal ( sandbox . config . get ( 'foo' , 'global' ) , undefined , 'foo should no longer be set' )
219
+ t . test ( 'config set nerf-darted option' , async t => {
220
+ const sandbox = new Sandbox ( t )
221
+ await sandbox . run ( 'config' , [ 'set' , '//npm.pkg.github.com/:_authToken' , '0xdeadbeef' ] )
222
+ t . equal (
223
+ sandbox . config . get ( '//npm.pkg.github.com/:_authToken' ) ,
224
+ '0xdeadbeef' ,
225
+ 'nerf-darted config is set'
226
+ )
227
+ } )
196
228
197
- const contents = await readFile ( join ( global , 'npmrc' ) , { encoding : 'utf8' } )
198
- t . not ( contents . includes ( 'foo=' ) , 'foo was removed on disk' )
229
+ t . test ( 'config set scoped optoin' , async t => {
230
+ const sandbox = new Sandbox ( t )
231
+ await sandbox . run ( 'config' , [ 'set' , '@npm:registry' , 'https://registry.npmjs.org' ] )
232
+ t . equal (
233
+ sandbox . config . get ( '@npm:registry' ) ,
234
+ 'https://registry.npmjs.org' ,
235
+ 'scoped config is set'
236
+ )
199
237
} )
200
238
201
239
t . test ( 'config set no args' , async t => {
@@ -212,65 +250,67 @@ t.test('config set no args', async t => {
212
250
213
251
t . test ( 'config set key' , async t => {
214
252
const home = t . testdir ( {
215
- '.npmrc' : 'foo=bar ' ,
253
+ '.npmrc' : 'access=public ' ,
216
254
} )
217
255
218
256
const sandbox = new Sandbox ( t , { home } )
219
257
220
- await sandbox . run ( 'config' , [ 'set' , 'foo ' ] )
258
+ await sandbox . run ( 'config' , [ 'set' , 'access ' ] )
221
259
222
- t . equal ( sandbox . config . get ( 'foo ' ) , '' , 'set the value for foo ' )
260
+ t . equal ( sandbox . config . get ( 'access ' ) , null , 'set the value for access ' )
223
261
224
- const contents = await readFile ( join ( home , '.npmrc' ) , { encoding : 'utf8' } )
225
- t . ok ( contents . includes ( 'foo=' ) , 'wrote foo to disk' )
262
+ await t . rejects ( fs . stat ( join ( home , '.npmrc' ) , { encoding : 'utf8' } ) , 'removed empty config' )
226
263
} )
227
264
228
265
t . test ( 'config set key value' , async t => {
229
266
const home = t . testdir ( {
230
- '.npmrc' : 'foo=bar ' ,
267
+ '.npmrc' : 'access=public ' ,
231
268
} )
232
269
233
270
const sandbox = new Sandbox ( t , { home } )
234
271
235
- await sandbox . run ( 'config' , [ 'set' , 'foo ' , 'baz ' ] )
272
+ await sandbox . run ( 'config' , [ 'set' , 'access ' , 'restricted ' ] )
236
273
237
- t . equal ( sandbox . config . get ( 'foo ' ) , 'baz ' , 'set the value for foo ' )
274
+ t . equal ( sandbox . config . get ( 'access ' ) , 'restricted ' , 'set the value for access ' )
238
275
239
- const contents = await readFile ( join ( home , '.npmrc' ) , { encoding : 'utf8' } )
240
- t . ok ( contents . includes ( 'foo=baz' ) , 'wrote foo to disk' )
276
+ const contents = await fs . readFile ( join ( home , '.npmrc' ) , { encoding : 'utf8' } )
277
+ const rc = ini . parse ( contents )
278
+ t . equal ( rc . access , 'restricted' , 'access is set to restricted' )
241
279
} )
242
280
243
281
t . test ( 'config set key=value' , async t => {
244
282
const home = t . testdir ( {
245
- '.npmrc' : 'foo=bar ' ,
283
+ '.npmrc' : 'access=public ' ,
246
284
} )
247
285
248
286
const sandbox = new Sandbox ( t , { home } )
249
287
250
- await sandbox . run ( 'config' , [ 'set' , 'foo=baz ' ] )
288
+ await sandbox . run ( 'config' , [ 'set' , 'access=restricted ' ] )
251
289
252
- t . equal ( sandbox . config . get ( 'foo ' ) , 'baz ' , 'set the value for foo ' )
290
+ t . equal ( sandbox . config . get ( 'access ' ) , 'restricted ' , 'set the value for access ' )
253
291
254
- const contents = await readFile ( join ( home , '.npmrc' ) , { encoding : 'utf8' } )
255
- t . ok ( contents . includes ( 'foo=baz' ) , 'wrote foo to disk' )
292
+ const contents = await fs . readFile ( join ( home , '.npmrc' ) , { encoding : 'utf8' } )
293
+ const rc = ini . parse ( contents )
294
+ t . equal ( rc . access , 'restricted' , 'access is set to restricted' )
256
295
} )
257
296
258
297
t . test ( 'config set key1 value1 key2=value2 key3' , async t => {
259
298
const home = t . testdir ( {
260
- '.npmrc' : 'foo=bar\nbar=baz\nbaz=foo ' ,
299
+ '.npmrc' : 'access=public\nall=true\naudit=true ' ,
261
300
} )
262
301
263
302
const sandbox = new Sandbox ( t , { home } )
264
- await sandbox . run ( 'config' , [ 'set' , 'foo ' , 'oof ' , 'bar=rab ' , 'baz ' ] )
303
+ await sandbox . run ( 'config' , [ 'set' , 'access ' , 'restricted ' , 'all=false ' , 'audit ' ] )
265
304
266
- t . equal ( sandbox . config . get ( 'foo ' ) , 'oof ' , 'foo was set' )
267
- t . equal ( sandbox . config . get ( 'bar ' ) , 'rab' , 'bar was set' )
268
- t . equal ( sandbox . config . get ( 'baz ' ) , '' , 'baz was set' )
305
+ t . equal ( sandbox . config . get ( 'access ' ) , 'restricted ' , 'access was set' )
306
+ t . equal ( sandbox . config . get ( 'all ' ) , false , 'all was set' )
307
+ t . equal ( sandbox . config . get ( 'audit ' ) , false , 'audit was set' )
269
308
270
- const contents = await readFile ( join ( home , '.npmrc' ) , { encoding : 'utf8' } )
271
- t . ok ( contents . includes ( 'foo=oof' ) , 'foo was written to disk' )
272
- t . ok ( contents . includes ( 'bar=rab' ) , 'bar was written to disk' )
273
- t . ok ( contents . includes ( 'baz=' ) , 'baz was written to disk' )
309
+ const contents = await fs . readFile ( join ( home , '.npmrc' ) , { encoding : 'utf8' } )
310
+ const rc = ini . parse ( contents )
311
+ t . equal ( rc . access , 'restricted' , 'access is set to restricted' )
312
+ t . equal ( rc . all , false , 'all is set to false' )
313
+ t . equal ( rc . audit , false , 'audit is set to false' )
274
314
} )
275
315
276
316
t . test ( 'config set invalid key logs warning' , async t => {
@@ -287,30 +327,32 @@ t.test('config set invalid key logs warning', async t => {
287
327
288
328
t . test ( 'config set key=value --location=global' , async t => {
289
329
const global = t . testdir ( {
290
- npmrc : 'foo=bar\nbar=baz ' ,
330
+ npmrc : 'access=public\nall=true ' ,
291
331
} )
292
332
293
333
const sandbox = new Sandbox ( t , { global } )
294
- await sandbox . run ( 'config' , [ 'set' , 'foo=buzz ' , '--location=global' ] )
334
+ await sandbox . run ( 'config' , [ 'set' , 'access=restricted ' , '--location=global' ] )
295
335
296
- t . equal ( sandbox . config . get ( 'foo ' , 'global' ) , 'buzz ' , 'foo should be set' )
336
+ t . equal ( sandbox . config . get ( 'access ' , 'global' ) , 'restricted ' , 'foo should be set' )
297
337
298
- const contents = await readFile ( join ( global , 'npmrc' ) , { encoding : 'utf8' } )
299
- t . not ( contents . includes ( 'foo=buzz' ) , 'foo was saved on disk' )
338
+ const contents = await fs . readFile ( join ( global , 'npmrc' ) , { encoding : 'utf8' } )
339
+ const rc = ini . parse ( contents )
340
+ t . equal ( rc . access , 'restricted' , 'access is set to restricted' )
300
341
} )
301
342
302
343
t . test ( 'config set key=value --global' , async t => {
303
344
const global = t . testdir ( {
304
- npmrc : 'foo=bar\nbar=baz ' ,
345
+ npmrc : 'access=public\nall=true ' ,
305
346
} )
306
347
307
348
const sandbox = new Sandbox ( t , { global } )
308
- await sandbox . run ( 'config' , [ 'set' , 'foo=buzz ' , '--global' ] )
349
+ await sandbox . run ( 'config' , [ 'set' , 'access=restricted ' , '--global' ] )
309
350
310
- t . equal ( sandbox . config . get ( 'foo ' , 'global' ) , 'buzz ' , 'foo should be set' )
351
+ t . equal ( sandbox . config . get ( 'access ' , 'global' ) , 'restricted ' , 'access should be set' )
311
352
312
- const contents = await readFile ( join ( global , 'npmrc' ) , { encoding : 'utf8' } )
313
- t . not ( contents . includes ( 'foo=buzz' ) , 'foo was saved on disk' )
353
+ const contents = await fs . readFile ( join ( global , 'npmrc' ) , { encoding : 'utf8' } )
354
+ const rc = ini . parse ( contents )
355
+ t . equal ( rc . access , 'restricted' , 'access is set to restricted' )
314
356
} )
315
357
316
358
t . test ( 'config get no args' , async t => {
@@ -383,7 +425,7 @@ t.test('config edit', async t => {
383
425
'editor opened the user config file'
384
426
)
385
427
386
- const contents = await readFile ( join ( home , '.npmrc' ) , { encoding : 'utf8' } )
428
+ const contents = await fs . readFile ( join ( home , '.npmrc' ) , { encoding : 'utf8' } )
387
429
t . ok ( contents . includes ( 'foo=bar' ) , 'kept foo' )
388
430
t . ok ( contents . includes ( 'bar=baz' ) , 'kept bar' )
389
431
t . ok ( contents . includes ( 'shown below with default values' ) , 'appends defaults to file' )
@@ -448,7 +490,7 @@ t.test('config fix', (t) => {
448
490
t . not ( sandbox . config . get ( '_authToken' , 'global' ) , '_authToken is not set globally' )
449
491
t . equal ( sandbox . config . get ( `${ registry } :_authToken` , 'global' ) , 'afaketoken' ,
450
492
'global _authToken was scoped' )
451
- const globalConfig = await readFile ( join ( root , 'global' , 'npmrc' ) , { encoding : 'utf8' } )
493
+ const globalConfig = await fs . readFile ( join ( root , 'global' , 'npmrc' ) , { encoding : 'utf8' } )
452
494
t . equal ( globalConfig , `${ registry } :_authToken=afaketoken\n` , 'global config was written' )
453
495
454
496
// user config fixes
@@ -459,7 +501,7 @@ t.test('config fix', (t) => {
459
501
t . not ( sandbox . config . get ( '_authtoken' , 'user' ) , '_authtoken is not set in user config' )
460
502
t . not ( sandbox . config . get ( '_auth' ) , '_auth is not set in user config' )
461
503
t . equal ( sandbox . config . get ( `${ registry } :_auth` , 'user' ) , 'beef' , 'user _auth was scoped' )
462
- const userConfig = await readFile ( join ( root , 'home' , '.npmrc' ) , { encoding : 'utf8' } )
504
+ const userConfig = await fs . readFile ( join ( root , 'home' , '.npmrc' ) , { encoding : 'utf8' } )
463
505
t . equal ( userConfig , `${ registry } :_auth=beef\n` , 'user config was written' )
464
506
} )
465
507
@@ -488,7 +530,7 @@ t.test('config fix', (t) => {
488
530
t . equal ( sandbox . config . get ( '_authtoken' , 'global' ) , 'notatoken' , 'global _authtoken untouched' )
489
531
t . equal ( sandbox . config . get ( '_authToken' , 'global' ) , 'afaketoken' , 'global _authToken untouched' )
490
532
t . not ( sandbox . config . get ( `${ registry } :_authToken` , 'global' ) , 'global _authToken not scoped' )
491
- const globalConfig = await readFile ( join ( root , 'global' , 'npmrc' ) , { encoding : 'utf8' } )
533
+ const globalConfig = await fs . readFile ( join ( root , 'global' , 'npmrc' ) , { encoding : 'utf8' } )
492
534
t . equal ( globalConfig , '_authtoken=notatoken\n_authToken=afaketoken' ,
493
535
'global config was not written' )
494
536
@@ -500,7 +542,7 @@ t.test('config fix', (t) => {
500
542
t . not ( sandbox . config . get ( '_authtoken' , 'user' ) , '_authtoken is not set in user config' )
501
543
t . not ( sandbox . config . get ( '_auth' , 'user' ) , '_auth is not set in user config' )
502
544
t . equal ( sandbox . config . get ( `${ registry } :_auth` , 'user' ) , 'beef' , 'user _auth was scoped' )
503
- const userConfig = await readFile ( join ( root , 'home' , '.npmrc' ) , { encoding : 'utf8' } )
545
+ const userConfig = await fs . readFile ( join ( root , 'home' , '.npmrc' ) , { encoding : 'utf8' } )
504
546
t . equal ( userConfig , `${ registry } :_auth=beef\n` , 'user config was written' )
505
547
} )
506
548
0 commit comments