@@ -10,11 +10,11 @@ const pacote = require("pacote");
10
10
const tar = require ( "tar" ) ;
11
11
const path = require ( "path" ) ;
12
12
13
- const npmCachePath = "npmCachePath" ;
13
+ const defaultPacoteOpts : IPacoteBaseOptions = createPacoteOptions ( { } ) ;
14
+ const npmCachePath = defaultPacoteOpts [ 'cache' ] ;
14
15
const packageName = "testPackage" ;
15
16
const fullPath = `/Users/username/${ packageName } ` ;
16
17
const destinationDir = "destinationDir" ;
17
- const defaultPacoteOpts : IPacoteBaseOptions = createPacoteOptions ( { cache : npmCachePath } ) ;
18
18
const errorMessage = "error message" ;
19
19
const proxySettings : IProxySettings = {
20
20
hostname : "hostname" ,
@@ -38,11 +38,17 @@ interface ITestCase extends ITestSetup {
38
38
expectedArgs : any [ ] ;
39
39
}
40
40
41
- function createPacoteOptions ( options : Object ) : Object {
41
+ function createPacoteOptions ( source : Object ) : Object {
42
+ let options : { [ index : string ] : any } = { } ;
42
43
npmconfig . read ( ) . forEach ( ( value : any , key : string ) => {
43
44
// replace env ${VARS} in strings with the process.env value
44
45
options [ key ] = typeof value !== 'string' ? value : value . replace ( / \$ { ( [ ^ } ] + ) } / , ( _ , envVar ) => process . env [ envVar ] ) ;
45
46
} ) ;
47
+
48
+ // Copy any original source keys over our defaults
49
+ for ( let key in source ) {
50
+ options [ key ] = source [ key ] ;
51
+ }
46
52
return options ;
47
53
}
48
54
@@ -113,8 +119,15 @@ describe("pacoteService", () => {
113
119
const setupTest = ( opts ?: ITestSetup ) : IPacoteService => {
114
120
opts = opts || { } ;
115
121
const testInjector = createTestInjector ( opts ) ;
122
+
116
123
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
+ } ) ;
118
131
}
119
132
120
133
return testInjector . resolve < IPacoteService > ( "pacoteService" ) ;
@@ -126,7 +139,7 @@ describe("pacoteService", () => {
126
139
const testData : ITestCase [ ] = [
127
140
{
128
141
name : "with 'cache' only when no opts are passed" ,
129
- expectedArgs : [ packageName , defaultPacoteOpts ]
142
+ expectedArgs : [ packageName , _ . extend ( { } , defaultPacoteOpts ) ]
130
143
} ,
131
144
{
132
145
name : "with 'cache' and passed options" ,
@@ -147,7 +160,7 @@ describe("pacoteService", () => {
147
160
{
148
161
name : "with full path to file when it is local one" ,
149
162
isLocalPackage : true ,
150
- expectedArgs : [ fullPath , defaultPacoteOpts ]
163
+ expectedArgs : [ fullPath , _ . extend ( { } , defaultPacoteOpts ) ]
151
164
} ,
152
165
{
153
166
name : "with full path to file, 'cache' and passed options when local path is passed" ,
0 commit comments