@@ -3,6 +3,7 @@ import { DryMongoBinary } from '../DryMongoBinary';
3
3
import * as path from 'path' ;
4
4
import { constants , promises as fspromises } from 'fs' ;
5
5
import { DEFAULT_VERSION , envName , ResolveConfigVariables } from '../resolveConfig' ;
6
+ import * as resConfig from '../resolveConfig' ;
6
7
import * as utils from '../utils' ;
7
8
import * as getOs from '../getos' ;
8
9
import { LinuxOS , OtherOS } from '../getos' ;
@@ -140,6 +141,102 @@ describe('DryBinary', () => {
140
141
modulesCache : '' , // because not being in an project
141
142
} as binary . DryMongoBinaryPaths ) ;
142
143
} ) ;
144
+
145
+ it ( 'should resolve modulesCache relative to the package json that has config options' , async ( ) => {
146
+ const workspacePackagePath = path . resolve ( tmpDir , 'package.json' ) ;
147
+ const packagePackagePath = path . resolve ( tmpDir , 'packages/testy/package.json' ) ;
148
+ await utils . mkdir ( path . dirname ( packagePackagePath ) ) ;
149
+
150
+ // create the shared workspace package.json
151
+ await fspromises . writeFile (
152
+ workspacePackagePath ,
153
+ JSON . stringify ( {
154
+ name : 'testw' ,
155
+ private : true ,
156
+ devDependencies : {
157
+ 'mongodb-memory-server' : '0.0.0' ,
158
+ } ,
159
+ config : {
160
+ mongodbMemoryServer : {
161
+ test : 1 ,
162
+ } ,
163
+ } ,
164
+ } )
165
+ ) ;
166
+ // emulate having resolved the package.json files and found the above file
167
+ jest . spyOn ( resConfig , 'packageJsonPath' ) . mockReturnValue ( path . dirname ( workspacePackagePath ) ) ;
168
+ // emulate having run "npm i" / "yarn install"
169
+ await utils . mkdir ( path . resolve ( path . dirname ( workspacePackagePath ) , 'node_modules' ) ) ;
170
+
171
+ // create the package's package.json
172
+ await fspromises . writeFile (
173
+ packagePackagePath ,
174
+ JSON . stringify ( {
175
+ name : 'testp' ,
176
+ // no custom configuration
177
+ } )
178
+ ) ;
179
+
180
+ process . chdir ( path . dirname ( packagePackagePath ) ) ;
181
+
182
+ const returnValue = await binary . DryMongoBinary . generatePaths ( opts ) ;
183
+ expect ( returnValue ) . toStrictEqual ( {
184
+ resolveConfig : '' , // empty because not having an extra config value
185
+ relative : path . resolve ( path . dirname ( packagePackagePath ) , 'mongodb-binaries' , binaryName ) ,
186
+ homeCache : path . resolve ( tmpDir , 'homedir/.cache/mongodb-binaries' , binaryName ) ,
187
+ modulesCache : path . resolve (
188
+ path . dirname ( workspacePackagePath ) ,
189
+ 'node_modules/.cache/mongodb-memory-server' ,
190
+ binaryName
191
+ ) ,
192
+ } as binary . DryMongoBinaryPaths ) ;
193
+ } ) ;
194
+
195
+ it ( 'should resolve modulesCache relative to cwd if no package.json with config options are found' , async ( ) => {
196
+ const workspacePackagePath = path . resolve ( tmpDir , 'package.json' ) ;
197
+ const packagePackagePath = path . resolve ( tmpDir , 'packages/testy/package.json' ) ;
198
+ await utils . mkdir ( path . dirname ( packagePackagePath ) ) ;
199
+
200
+ // create the shared workspace package.json
201
+ await fspromises . writeFile (
202
+ workspacePackagePath ,
203
+ JSON . stringify ( {
204
+ name : 'testw' ,
205
+ private : true ,
206
+ devDependencies : {
207
+ 'mongodb-memory-server' : '0.0.0' ,
208
+ } ,
209
+ // no custom configuration
210
+ } )
211
+ ) ;
212
+ // emulate having resolved the package.json files and found the above file
213
+ jest . spyOn ( resConfig , 'packageJsonPath' ) . mockReturnValue ( undefined ) ;
214
+ // emulate having run "npm i" / "yarn install"
215
+ await utils . mkdir ( path . resolve ( path . dirname ( workspacePackagePath ) , 'node_modules' ) ) ;
216
+
217
+ // create the package's package.json
218
+ await fspromises . writeFile (
219
+ packagePackagePath ,
220
+ JSON . stringify ( {
221
+ name : 'testp' ,
222
+ // no custom configuration
223
+ } )
224
+ ) ;
225
+
226
+ process . chdir ( path . dirname ( packagePackagePath ) ) ;
227
+
228
+ const returnValue = await binary . DryMongoBinary . generatePaths ( opts ) ;
229
+ expect ( returnValue ) . toStrictEqual ( {
230
+ resolveConfig : '' , // empty because not having an extra config value
231
+ relative : path . resolve ( path . dirname ( packagePackagePath ) , 'mongodb-binaries' , binaryName ) ,
232
+ homeCache : path . resolve ( tmpDir , 'homedir/.cache/mongodb-binaries' , binaryName ) ,
233
+ modulesCache : path . resolve (
234
+ path . dirname ( packagePackagePath ) ,
235
+ 'node_modules/.cache/mongodb-memory-server' ,
236
+ binaryName
237
+ ) ,
238
+ } as binary . DryMongoBinaryPaths ) ;
239
+ } ) ;
143
240
} ) ;
144
241
145
242
describe ( 'getBinaryName' , ( ) => {
0 commit comments