@@ -2,6 +2,7 @@ const t = require('tap')
2
2
const { load : loadMockNpm } = require ( '../../fixtures/mock-npm.js' )
3
3
const MockRegistry = require ( '../../fixtures/mock-registry.js' )
4
4
5
+ const path = require ( 'path' )
5
6
const npa = require ( 'npm-package-arg' )
6
7
const packageName = '@npmcli/test-package'
7
8
const spec = npa ( packageName )
@@ -12,6 +13,42 @@ const maintainers = [
12
13
{ email : 'test-user-b@npmjs.org' , name : 'test-user-b' } ,
13
14
]
14
15
16
+ const workspaceFixture = {
17
+ 'package.json' : JSON . stringify ( {
18
+ name : packageName ,
19
+ version : '1.2.3-test' ,
20
+ workspaces : [ 'workspace-a' , 'workspace-b' , 'workspace-c' ] ,
21
+ } ) ,
22
+ 'workspace-a' : {
23
+ 'package.json' : JSON . stringify ( {
24
+ name : 'workspace-a' ,
25
+ version : '1.2.3-a' ,
26
+ } ) ,
27
+ } ,
28
+ 'workspace-b' : {
29
+ 'package.json' : JSON . stringify ( {
30
+ name : 'workspace-b' ,
31
+ version : '1.2.3-n' ,
32
+ } ) ,
33
+ } ,
34
+ 'workspace-c' : JSON . stringify ( {
35
+ 'package.json' : {
36
+ name : 'workspace-n' ,
37
+ version : '1.2.3-n' ,
38
+ } ,
39
+ } ) ,
40
+ }
41
+
42
+ function registryPackage ( t , registry , name ) {
43
+ const mockRegistry = new MockRegistry ( { tap : t , registry } )
44
+
45
+ const manifest = mockRegistry . manifest ( {
46
+ name,
47
+ packuments : [ { maintainers, version : '1.0.0' } ] ,
48
+ } )
49
+ mockRegistry . package ( { manifest } )
50
+ }
51
+
15
52
t . test ( 'owner no args' , async t => {
16
53
const { npm } = await loadMockNpm ( t )
17
54
await t . rejects (
@@ -429,6 +466,158 @@ t.test('owner rm <user> no cwd package', async t => {
429
466
)
430
467
} )
431
468
469
+ t . test ( 'workspaces' , async t => {
470
+ t . test ( 'owner no args --workspace' , async t => {
471
+ const { npm } = await loadMockNpm ( t , {
472
+ prefixDir : workspaceFixture ,
473
+ } )
474
+ npm . config . set ( 'workspace' , [ 'workspace-a' ] )
475
+ await t . rejects (
476
+ npm . exec ( 'owner' , [ ] ) ,
477
+ { code : 'EUSAGE' } ,
478
+ 'rejects with usage'
479
+ )
480
+ } )
481
+
482
+ t . test ( 'owner ls implicit workspace' , async t => {
483
+ const { npm, joinedOutput } = await loadMockNpm ( t , {
484
+ prefixDir : workspaceFixture ,
485
+ globals : ( { prefix } ) => ( {
486
+ 'process.cwd' : ( ) => path . join ( prefix , 'workspace-a' ) ,
487
+ } ) ,
488
+ } )
489
+ registryPackage ( t , npm . config . get ( 'registry' ) , 'workspace-a' )
490
+ await npm . exec ( 'owner' , [ 'ls' ] )
491
+ t . match ( joinedOutput ( ) , maintainers . map ( m => `${ m . name } <${ m . email } >` ) . join ( '\n' ) )
492
+ } )
493
+
494
+ t . test ( 'owner ls explicit workspace' , async t => {
495
+ const { npm, joinedOutput } = await loadMockNpm ( t , {
496
+ prefixDir : workspaceFixture ,
497
+ globals : ( { prefix } ) => ( {
498
+ 'process.cwd' : ( ) => prefix ,
499
+ } ) ,
500
+ } )
501
+ npm . config . set ( 'workspace' , [ 'workspace-a' ] )
502
+ registryPackage ( t , npm . config . get ( 'registry' ) , 'workspace-a' )
503
+ await npm . exec ( 'owner' , [ 'ls' ] )
504
+ t . match ( joinedOutput ( ) , maintainers . map ( m => `${ m . name } <${ m . email } >` ) . join ( '\n' ) )
505
+ } )
506
+
507
+ t . test ( 'owner ls <pkg> implicit workspace' , async t => {
508
+ const { npm, joinedOutput } = await loadMockNpm ( t , {
509
+ prefixDir : workspaceFixture ,
510
+ globals : ( { prefix } ) => ( {
511
+ 'process.cwd' : ( ) => path . join ( prefix , 'workspace-a' ) ,
512
+ } ) ,
513
+ } )
514
+ registryPackage ( t , npm . config . get ( 'registry' ) , packageName )
515
+ await npm . exec ( 'owner' , [ 'ls' , packageName ] )
516
+ t . match ( joinedOutput ( ) , maintainers . map ( m => `${ m . name } <${ m . email } >` ) . join ( '\n' ) )
517
+ } )
518
+
519
+ t . test ( 'owner ls <pkg> explicit workspace' , async t => {
520
+ const { npm, joinedOutput } = await loadMockNpm ( t , {
521
+ prefixDir : workspaceFixture ,
522
+ globals : ( { prefix } ) => ( {
523
+ 'process.cwd' : ( ) => prefix ,
524
+ } ) ,
525
+ } )
526
+ npm . config . set ( 'workspace' , [ 'workspace-a' ] )
527
+ registryPackage ( t , npm . config . get ( 'registry' ) , packageName )
528
+ await npm . exec ( 'owner' , [ 'ls' , packageName ] )
529
+ t . match ( joinedOutput ( ) , maintainers . map ( m => `${ m . name } <${ m . email } >` ) . join ( '\n' ) )
530
+ } )
531
+
532
+ t . test ( 'owner add implicit workspace' , async t => {
533
+ const { npm, joinedOutput } = await loadMockNpm ( t , {
534
+ prefixDir : workspaceFixture ,
535
+ globals : ( { prefix } ) => ( {
536
+ 'process.cwd' : ( ) => path . join ( prefix , 'workspace-a' ) ,
537
+ } ) ,
538
+ } )
539
+ const username = 'foo'
540
+ const registry = new MockRegistry ( { tap : t , registry : npm . config . get ( 'registry' ) } )
541
+
542
+ const manifest = registry . manifest ( {
543
+ name : 'workspace-a' ,
544
+ packuments : [ { maintainers, version : '1.0.0' } ] ,
545
+ } )
546
+ registry . package ( { manifest } )
547
+ registry . couchuser ( { username } )
548
+ registry . nock . put ( `/workspace-a/-rev/${ manifest . _rev } ` , body => {
549
+ t . match ( body , {
550
+ _id : manifest . _id ,
551
+ _rev : manifest . _rev ,
552
+ maintainers : [
553
+ ...manifest . maintainers ,
554
+ { name : username , email : '' } ,
555
+ ] ,
556
+ } )
557
+ return true
558
+ } ) . reply ( 200 , { } )
559
+ await npm . exec ( 'owner' , [ 'add' , username ] )
560
+ t . equal ( joinedOutput ( ) , `+ ${ username } (workspace-a)` )
561
+ } )
562
+
563
+ t . test ( 'owner add --workspace' , async t => {
564
+ const { npm, joinedOutput } = await loadMockNpm ( t , {
565
+ prefixDir : workspaceFixture ,
566
+ } )
567
+ npm . config . set ( 'workspace' , [ 'workspace-a' ] )
568
+ const username = 'foo'
569
+ const registry = new MockRegistry ( { tap : t , registry : npm . config . get ( 'registry' ) } )
570
+
571
+ const manifest = registry . manifest ( {
572
+ name : 'workspace-a' ,
573
+ packuments : [ { maintainers, version : '1.0.0' } ] ,
574
+ } )
575
+ registry . package ( { manifest } )
576
+ registry . couchuser ( { username } )
577
+ registry . nock . put ( `/workspace-a/-rev/${ manifest . _rev } ` , body => {
578
+ t . match ( body , {
579
+ _id : manifest . _id ,
580
+ _rev : manifest . _rev ,
581
+ maintainers : [
582
+ ...manifest . maintainers ,
583
+ { name : username , email : '' } ,
584
+ ] ,
585
+ } )
586
+ return true
587
+ } ) . reply ( 200 , { } )
588
+ await npm . exec ( 'owner' , [ 'add' , username ] )
589
+ t . equal ( joinedOutput ( ) , `+ ${ username } (workspace-a)` )
590
+ } )
591
+
592
+ t . test ( 'owner rm --workspace' , async t => {
593
+ const { npm, joinedOutput } = await loadMockNpm ( t , {
594
+ prefixDir : workspaceFixture ,
595
+ globals : ( { prefix } ) => ( {
596
+ 'process.cwd' : ( ) => path . join ( prefix , 'workspace-a' ) ,
597
+ } ) ,
598
+ } )
599
+ const registry = new MockRegistry ( { tap : t , registry : npm . config . get ( 'registry' ) } )
600
+
601
+ const username = maintainers [ 0 ] . name
602
+ const manifest = registry . manifest ( {
603
+ name : 'workspace-a' ,
604
+ packuments : [ { maintainers, version : '1.0.0' } ] ,
605
+ } )
606
+ registry . package ( { manifest } )
607
+ registry . couchuser ( { username } )
608
+ registry . nock . put ( `/workspace-a/-rev/${ manifest . _rev } ` , body => {
609
+ t . match ( body , {
610
+ _id : manifest . _id ,
611
+ _rev : manifest . _rev ,
612
+ maintainers : maintainers . slice ( 1 ) ,
613
+ } )
614
+ return true
615
+ } ) . reply ( 200 , { } )
616
+ await npm . exec ( 'owner' , [ 'rm' , username ] )
617
+ t . equal ( joinedOutput ( ) , `- ${ username } (workspace-a)` )
618
+ } )
619
+ } )
620
+
432
621
t . test ( 'completion' , async t => {
433
622
t . test ( 'basic commands' , async t => {
434
623
const { npm } = await loadMockNpm ( t )
0 commit comments