@@ -10,11 +10,11 @@ const pacote = require("pacote");
1010const tar = require ( "tar" ) ;
1111const path = require ( "path" ) ;
1212
13- const npmCachePath = "npmCachePath" ;
13+ const defaultPacoteOpts : IPacoteBaseOptions = createPacoteOptions ( { } ) ;
14+ const npmCachePath = defaultPacoteOpts [ 'cache' ] ;
1415const packageName = "testPackage" ;
1516const fullPath = `/Users/username/${ packageName } ` ;
1617const destinationDir = "destinationDir" ;
17- const defaultPacoteOpts : IPacoteBaseOptions = createPacoteOptions ( { cache : npmCachePath } ) ;
1818const errorMessage = "error message" ;
1919const proxySettings : IProxySettings = {
2020 hostname : "hostname" ,
@@ -38,11 +38,17 @@ interface ITestCase extends ITestSetup {
3838 expectedArgs : any [ ] ;
3939}
4040
41- function createPacoteOptions ( options : Object ) : Object {
41+ function createPacoteOptions ( source : Object ) : Object {
42+ let options : { [ index : string ] : any } = { } ;
4243 npmconfig . read ( ) . forEach ( ( value : any , key : string ) => {
4344 // replace env ${VARS} in strings with the process.env value
4445 options [ key ] = typeof value !== 'string' ? value : value . replace ( / \$ { ( [ ^ } ] + ) } / , ( _ , envVar ) => process . env [ envVar ] ) ;
4546 } ) ;
47+
48+ // Copy any original source keys over our defaults
49+ for ( let key in source ) {
50+ options [ key ] = source [ key ] ;
51+ }
4652 return options ;
4753}
4854
@@ -113,8 +119,15 @@ describe("pacoteService", () => {
113119 const setupTest = ( opts ?: ITestSetup ) : IPacoteService => {
114120 opts = opts || { } ;
115121 const testInjector = createTestInjector ( opts ) ;
122+
116123 if ( opts . isLocalPackage ) {
117- sandboxInstance . stub ( path , "resolve" ) . withArgs ( packageName ) . returns ( fullPath ) ;
124+ const oldPath = path . resolve ;
125+ sandboxInstance . stub ( path , "resolve" ) . callsFake ( ( value :string ) => {
126+ if ( value === packageName ) {
127+ return fullPath ;
128+ }
129+ return oldPath ( value ) ;
130+ } ) ;
118131 }
119132
120133 return testInjector . resolve < IPacoteService > ( "pacoteService" ) ;
@@ -126,7 +139,7 @@ describe("pacoteService", () => {
126139 const testData : ITestCase [ ] = [
127140 {
128141 name : "with 'cache' only when no opts are passed" ,
129- expectedArgs : [ packageName , defaultPacoteOpts ]
142+ expectedArgs : [ packageName , _ . extend ( { } , defaultPacoteOpts ) ]
130143 } ,
131144 {
132145 name : "with 'cache' and passed options" ,
@@ -147,7 +160,7 @@ describe("pacoteService", () => {
147160 {
148161 name : "with full path to file when it is local one" ,
149162 isLocalPackage : true ,
150- expectedArgs : [ fullPath , defaultPacoteOpts ]
163+ expectedArgs : [ fullPath , _ . extend ( { } , defaultPacoteOpts ) ]
151164 } ,
152165 {
153166 name : "with full path to file, 'cache' and passed options when local path is passed" ,
0 commit comments