@@ -10,40 +10,48 @@ const which = async (cmd, opts) => {
10
10
return path ? join ( dirname ( path ) , basename ( path , extname ( path ) ) ) : null
11
11
}
12
12
13
- t . test ( 'npm replace global' , async t => {
13
+ const setupNpmGlobal = async ( t , opts ) => {
14
+ const mock = await setup ( t , opts )
15
+
16
+ return {
17
+ ...mock ,
18
+ getPaths : async ( ) => {
19
+ const binContents = await fs . readdir ( mock . paths . globalBin )
20
+ . then ( r => r . filter ( p => p !== '.npmrc' && p !== 'node_modules' ) )
21
+ . catch ( ( ) => null )
22
+
23
+ const nodeModulesContents = await fs . readdir ( join ( mock . paths . globalNodeModules , 'npm' ) )
24
+ . catch ( ( ) => null )
25
+
26
+ return {
27
+ npmRoot : await mock . npmPath ( 'help' ) . then ( setup . getNpmRoot ) ,
28
+ pathNpm : await which ( 'npm' , { path : mock . getPath ( ) , nothrow : true } ) ,
29
+ globalNpm : await which ( 'npm' , { nothrow : true } ) ,
30
+ pathNpx : await which ( 'npx' , { path : mock . getPath ( ) , nothrow : true } ) ,
31
+ globalNpx : await which ( 'npx' , { nothrow : true } ) ,
32
+ binContents,
33
+ nodeModulesContents,
34
+ }
35
+ } ,
36
+ }
37
+ }
38
+
39
+ t . test ( 'pack and replace global self' , async t => {
14
40
const {
15
41
npm,
16
- npmLocal ,
42
+ npmLocalTarball ,
17
43
npmPath,
18
- getPath ,
19
- paths : { root , globalBin, globalNodeModules } ,
20
- } = await setup ( t , {
44
+ getPaths ,
45
+ paths : { globalBin, globalNodeModules } ,
46
+ } = await setupNpmGlobal ( t , {
21
47
testdir : {
22
48
project : {
23
49
'package.json' : { name : 'npm' , version : '999.999.999' } ,
24
50
} ,
25
51
} ,
26
52
} )
27
53
28
- const getPaths = async ( ) => {
29
- const binContents = await fs . readdir ( globalBin ) . then ( results => results
30
- . filter ( p => p !== '.npmrc' && p !== 'node_modules' )
31
- . map ( p => basename ( p , extname ( p ) ) )
32
- . reduce ( ( set , p ) => set . add ( p ) , new Set ( ) ) )
33
-
34
- return {
35
- npmRoot : await npmPath ( 'help' ) . then ( setup . getNpmRoot ) ,
36
- pathNpm : await which ( 'npm' , { path : getPath ( ) , nothrow : true } ) ,
37
- globalNpm : await which ( 'npm' , { nothrow : true } ) ,
38
- pathNpx : await which ( 'npx' , { path : getPath ( ) , nothrow : true } ) ,
39
- globalNpx : await which ( 'npx' , { nothrow : true } ) ,
40
- binContents : [ ...binContents ] ,
41
- nodeModulesContents : await fs . readdir ( join ( globalNodeModules , 'npm' ) ) ,
42
- }
43
- }
44
-
45
- const tarball = setup . SMOKE_PUBLISH_TARBALL ??
46
- await npmLocal ( 'pack' , `--pack-destination=${ root } ` ) . then ( r => join ( root , r ) )
54
+ const tarball = await npmLocalTarball ( )
47
55
48
56
await npm ( 'install' , tarball , '--global' )
49
57
@@ -64,7 +72,7 @@ t.test('npm replace global', async t => {
64
72
t . equal ( prePaths . pathNpx , join ( globalBin , 'npx' ) , 'npx bin is in the testdir' )
65
73
t . not ( prePaths . pathNpm , prePaths . globalNpm , 'npm bin is not the same as the global one' )
66
74
t . not ( prePaths . pathNpx , prePaths . globalNpx , 'npm bin is not the same as the global one' )
67
- t . match ( prePaths . binContents , [ 'npm' , 'npx' ] , 'bin has npm and npx' )
75
+ t . strictSame ( prePaths . binContents , [ 'npm' , 'npx' ] , 'bin has npm and npx' )
68
76
t . ok ( prePaths . nodeModulesContents . length > 1 , 'node modules has npm contents' )
69
77
t . ok ( prePaths . nodeModulesContents . includes ( 'node_modules' ) , 'npm has its node_modules' )
70
78
@@ -80,3 +88,62 @@ t.test('npm replace global', async t => {
80
88
t . strictSame ( postPaths . binContents , [ ] , 'bin is empty' )
81
89
t . strictSame ( postPaths . nodeModulesContents , [ 'package.json' ] , 'contents is only package.json' )
82
90
} )
91
+
92
+ t . test ( 'publish and replace global self' , async t => {
93
+ const {
94
+ npm,
95
+ npmPath,
96
+ registry,
97
+ npmLocal,
98
+ npmLocalTarball,
99
+ getPaths,
100
+ paths : { globalBin, globalNodeModules } ,
101
+ } = await setupNpmGlobal ( t , {
102
+ testdir : {
103
+ home : {
104
+ '.npmrc' : `${ setup . HTTP_PROXY . slice ( 5 ) } :_authToken = test-token` ,
105
+ } ,
106
+ } ,
107
+ } )
108
+
109
+ const tarball = await npmLocalTarball ( )
110
+
111
+ let publishedPackument = null
112
+ const pkg = require ( '../../package.json' )
113
+ const { name, version } = pkg
114
+
115
+ registry . nock . put ( '/npm' , body => {
116
+ if ( body . _id === 'npm' && body . versions [ version ] ) {
117
+ publishedPackument = body . versions [ version ]
118
+ return true
119
+ }
120
+ return false
121
+ } ) . reply ( 201 , { } )
122
+ await npmLocal ( 'publish' , { proxy : true } )
123
+
124
+ await registry . package ( {
125
+ manifest : registry . manifest ( { name, packuments : [ publishedPackument ] } ) ,
126
+ tarballs : { [ version ] : tarball } ,
127
+ times : 2 ,
128
+ } )
129
+
130
+ await npm ( 'install' , 'npm' , '--global' , '--prefer-online' )
131
+
132
+ const paths = await getPaths ( )
133
+ t . equal ( paths . npmRoot , join ( globalNodeModules , 'npm' ) , 'npm root is in the testdir' )
134
+ t . equal ( paths . pathNpm , join ( globalBin , 'npm' ) , 'npm bin is in the testdir' )
135
+ t . equal ( paths . pathNpx , join ( globalBin , 'npx' ) , 'npx bin is in the testdir' )
136
+ t . strictSame ( paths . binContents , [ 'npm' , 'npx' ] , 'bin has npm and npx' )
137
+ t . ok ( paths . nodeModulesContents . length > 1 , 'node modules has npm contents' )
138
+ t . ok ( paths . nodeModulesContents . includes ( 'node_modules' ) , 'npm has its node_modules' )
139
+
140
+ await registry . package ( {
141
+ manifest : registry . manifest ( { name, packuments : [ publishedPackument ] } ) ,
142
+ tarballs : { [ version ] : tarball } ,
143
+ times : 2 ,
144
+ } )
145
+
146
+ await npmPath ( 'install' , 'npm' , '--global' , '--prefer-online' )
147
+
148
+ t . strictSame ( await getPaths ( ) , paths )
149
+ } )
0 commit comments