@@ -5,12 +5,12 @@ const which = require('which')
55const spawn = require ( '@npmcli/promise-spawn' )
66const MockRegistry = require ( '@npmcli/mock-registry' )
77const http = require ( 'http' )
8- const httpProxy = require ( 'http-proxy' )
8+ const { createProxy } = require ( 'proxy' )
9+
10+ const { SMOKE_PUBLISH_NPM , SMOKE_PUBLISH_TARBALL , CI , PATH , Path } = process . env
911
10- const { SMOKE_PUBLISH_NPM , SMOKE_PUBLISH_TARBALL , CI , PATH , Path, TAP_CHILD_ID = '0' } = process . env
11- const PROXY_PORT = 12345 + ( + TAP_CHILD_ID )
12- const HTTP_PROXY = `http://localhost:${ PROXY_PORT } /`
1312const DEFAULT_REGISTRY = new URL ( 'https://registry.npmjs.org/' )
13+ const MOCK_REGISTRY = new URL ( 'http://smoke-test-registry.club/' )
1414
1515const NODE_PATH = process . execPath
1616const CLI_ROOT = resolve ( process . cwd ( ) , '..' )
@@ -73,25 +73,7 @@ const getCleanPaths = async () => {
7373 } )
7474}
7575
76- const createRegistry = async ( t , { debug, ...opts } = { } ) => {
77- const registry = new MockRegistry ( {
78- tap : t ,
79- registry : 'http://smoke-test-registry.club/' ,
80- debug,
81- strict : true ,
82- ...opts ,
83- } )
84-
85- const proxy = httpProxy . createProxyServer ( { } )
86- const server = http . createServer ( ( req , res ) => proxy . web ( req , res , { target : registry . origin } ) )
87- await new Promise ( res => server . listen ( PROXY_PORT , res ) )
88-
89- t . teardown ( ( ) => server . close ( ) )
90-
91- return registry
92- }
93-
94- module . exports = async ( t , { testdir = { } , debug, registry : _registry = { } } = { } ) => {
76+ module . exports = async ( t , { testdir = { } , debug, mockRegistry = true , useProxy = false } = { } ) => {
9577 const debugLog = debug || CI ? ( ...a ) => console . error ( ...a ) : ( ) => { }
9678 const cleanPaths = await getCleanPaths ( )
9779
@@ -115,11 +97,21 @@ module.exports = async (t, { testdir = {}, debug, registry: _registry = {} } = {
11597 globalNodeModules : join ( root , 'global' , GLOBAL_NODE_MODULES ) ,
11698 }
11799
118- const liveRegistry = _registry === false
119- const USE_PROXY = ! liveRegistry
120- const registry = liveRegistry
121- ? DEFAULT_REGISTRY
122- : await createRegistry ( t , { ..._registry , debug } )
100+ const registry = ! mockRegistry ? DEFAULT_REGISTRY : new MockRegistry ( {
101+ tap : t ,
102+ registry : MOCK_REGISTRY ,
103+ debug,
104+ strict : true ,
105+ } )
106+
107+ const proxyEnv = { }
108+ if ( useProxy || mockRegistry ) {
109+ useProxy = true
110+ const proxyServer = createProxy ( http . createServer ( ) )
111+ await new Promise ( res => proxyServer . listen ( 0 , res ) )
112+ t . teardown ( ( ) => proxyServer . close ( ) )
113+ proxyEnv . HTTP_PROXY = new URL ( `http://localhost:${ proxyServer . address ( ) . port } ` )
114+ }
123115
124116 // update notifier should never be written
125117 t . afterEach ( ( t ) => {
@@ -143,7 +135,6 @@ module.exports = async (t, { testdir = {}, debug, registry: _registry = {} } = {
143135 }
144136 return s
145137 . split ( relative ( CLI_ROOT , t . testdirName ) ) . join ( '{TESTDIR}' )
146- . split ( HTTP_PROXY ) . join ( '{PROXY_REGISTRY}' )
147138 . split ( registry . origin ) . join ( '{REGISTRY}' )
148139 . replace ( / \\ + / g, '/' )
149140 . replace ( / \r \n / g, '\n' )
@@ -188,12 +179,12 @@ module.exports = async (t, { testdir = {}, debug, registry: _registry = {} } = {
188179 }
189180
190181 const baseNpm = async ( ...a ) => {
191- const [ { cwd, cmd, argv = [ ] , proxy = USE_PROXY , ...opts } , args ] = getOpts ( ...a )
182+ const [ { cwd, cmd, argv = [ ] , proxy = useProxy , ...opts } , args ] = getOpts ( ...a )
192183
193184 const isGlobal = args . some ( arg => [ '-g' , '--global' , '--global=true' ] . includes ( arg ) )
194185
195186 const defaultFlags = [
196- proxy ? `--registry=${ HTTP_PROXY } ` : null ,
187+ `--registry=${ registry . origin } ` ,
197188 `--cache=${ paths . cache } ` ,
198189 `--prefix=${ isGlobal ? paths . global : cwd } ` ,
199190 `--userconfig=${ paths . userConfig } ` ,
@@ -217,7 +208,7 @@ module.exports = async (t, { testdir = {}, debug, registry: _registry = {} } = {
217208 cwd,
218209 env : {
219210 ...opts . env ,
220- ...proxy ? { HTTP_PROXY } : { } ,
211+ ...proxy ? proxyEnv : { } ,
221212 } ,
222213 ...opts ,
223214 } )
@@ -274,5 +265,4 @@ module.exports.CLI_ROOT = CLI_ROOT
274265module . exports . WINDOWS = WINDOWS
275266module . exports . SMOKE_PUBLISH = ! ! SMOKE_PUBLISH_NPM
276267module . exports . SMOKE_PUBLISH_TARBALL = SMOKE_PUBLISH_TARBALL
277- module . exports . HTTP_PROXY = HTTP_PROXY
278- module . exports . PROXY_PORT = PROXY_PORT
268+ module . exports . MOCK_REGISTRY = MOCK_REGISTRY
0 commit comments