11import { expect } from 'chai' ;
2- import { sep } from 'path' ;
2+ import { mkdir , rm } from 'fs/promises' ;
3+ import { tmpdir } from 'os' ;
4+ import { join , sep } from 'path' ;
35
46import { Package } from '../../lib/common' ;
5- import { clearTestedDeps } from '../utils' ;
7+ import { clearTestedDeps , exists } from '../utils' ;
68
79describe ( 'common functionality' , function ( ) {
810 const BSON_PATH = process . env . BSON_PATH ;
911
1012 context ( 'Package' , function ( ) {
11- beforeEach ( clearTestedDeps ) ;
12- after ( clearTestedDeps ) ;
13+ let installDir : string ;
14+
15+ after ( async function ( ) {
16+ await rm ( installDir , { recursive : true , force : true } ) ;
17+ } ) ;
18+
19+ beforeEach ( async function ( ) {
20+ await clearTestedDeps ( installDir ) ;
21+ } ) ;
22+
23+ before ( async function ( ) {
24+ installDir = join ( tmpdir ( ) , 'bsonBenchTest' ) ;
25+ await mkdir ( installDir ) ;
26+ } ) ;
1327
1428 context ( 'constructor()' , function ( ) {
29+ //github.com/mongodb-js/dbx-js-tools/pull/24/files
1530 context ( 'when given a correctly formatted npm package' , function ( ) {
1631 it ( 'sets computedModuleName correctly' , function ( ) {
17- const pack = new Package ( '[email protected] ' ) ; 32+ const pack = new Package ( '[email protected] ' , installDir ) ; 1833 expect ( pack ) . to . haveOwnProperty ( 'computedModuleName' , 'bson-6.0.0' ) ;
1934 } ) ;
2035 } ) ;
2136
2237 context ( 'when given a correctly formatted git repository' , function ( ) {
2338 it ( 'sets computedModuleName correctly' , function ( ) {
24- const pack = new Package ( 'bson#eb98b8c39d6d5ba4ce7231ab9e0f29495d74b994' ) ;
39+ const pack = new Package ( 'bson#eb98b8c39d6d5ba4ce7231ab9e0f29495d74b994' , installDir ) ;
2540 expect ( pack ) . to . haveOwnProperty (
2641 'computedModuleName' ,
2742 'bson-git-eb98b8c39d6d5ba4ce7231ab9e0f29495d74b994'
@@ -31,13 +46,16 @@ describe('common functionality', function () {
3146
3247 context ( 'when trying to install an npm package apart from bson or bson-ext' , function ( ) {
3348 it ( 'throws an error' , function ( ) {
34- expect ( ( ) => new Package ( '[email protected] ' ) ) . to . throw ( Error , / u n k n o w n p a c k a g e s p e c i f i e r / ) ; 49+ expect ( ( ) => new Package ( '[email protected] ' , installDir ) ) . to . throw ( 50+ Error ,
51+ / u n k n o w n p a c k a g e s p e c i f i e r /
52+ ) ;
3553 } ) ;
3654 } ) ;
3755
3856 context ( 'when trying to install a git package apart from bson or bson-ext' , function ( ) {
3957 it ( 'throws an error' , function ( ) {
40- expect ( ( ) => new Package ( 'notBson#abcdabcdabcd' ) ) . to . throw (
58+ expect ( ( ) => new Package ( 'notBson#abcdabcdabcd' , installDir ) ) . to . throw (
4159 Error ,
4260 / u n k n o w n p a c k a g e s p e c i f i e r /
4361 ) ;
@@ -50,7 +68,7 @@ describe('common functionality', function () {
5068 console . log ( 'Skipping since BSON_PATH is undefined' ) ;
5169 this . skip ( ) ;
5270 }
53- const pack = new Package ( `bson:${ BSON_PATH } ` ) ;
71+ const pack = new Package ( `bson:${ BSON_PATH } ` , installDir ) ;
5472 expect ( pack ) . to . haveOwnProperty (
5573 'computedModuleName' ,
5674 `bson-local-${ BSON_PATH . replaceAll ( sep , '_' ) } `
@@ -62,14 +80,14 @@ describe('common functionality', function () {
6280 context ( '#check()' , function ( ) {
6381 context ( 'when package is not installed' , function ( ) {
6482 it ( 'returns undefined' , function ( ) {
65- const pack = new Package ( 'bson@6' ) ;
83+ const pack = new Package ( 'bson@6' , installDir ) ;
6684 expect ( pack . check ( ) ) . to . be . undefined ;
6785 } ) ;
6886 } ) ;
6987
7088 context ( 'when package is installed' , function ( ) {
7189 it ( 'returns the module' , async function ( ) {
72- const pack = new Package ( '[email protected] ' ) ; 90+ const pack = new Package ( '[email protected] ' , installDir ) ; 7391 await pack . install ( ) ;
7492 expect ( pack . check ( ) ) . to . not . be . undefined ;
7593 } ) ;
@@ -79,26 +97,31 @@ describe('common functionality', function () {
7997 context ( '#install()' , function ( ) {
8098 context ( 'when given a correctly formatted npm package that exists' , function ( ) {
8199 for ( const lib of [ '[email protected] ' , '[email protected] ' , 'bson@latest' , 'bson-ext@latest' ] ) { 82- it ( `installs ${ lib } successfully` , async function ( ) {
83- const pack = new Package ( lib ) ;
100+ it ( `installs ${ lib } successfully to the specified install directory ` , async function ( ) {
101+ const pack = new Package ( lib , installDir ) ;
84102 await pack . install ( ) ;
103+
104+ expect ( await exists ( join ( installDir , 'node_modules' , pack . computedModuleName ) ) ) . to . be
105+ . true ;
85106 } ) ;
86107 }
87108 } ) ;
88109
89110 context ( 'when given a correctly formatted npm package that does not exist' , function ( ) {
90111 it ( 'throws an error' , async function ( ) {
91- const bson9000 = new Package ( 'bson@9000' ) ;
112+ const bson9000 = new Package ( 'bson@9000' , installDir ) ;
92113 const error = await bson9000 . install ( ) . catch ( error => error ) ;
93114 expect ( error ) . to . be . instanceOf ( Error ) ;
94115 } ) ;
95116 } ) ;
96117
97118 context ( 'when given a correctly formatted git package using commit that exists' , function ( ) {
98- it ( 'installs successfully' , async function ( ) {
99- const bson6Git = new Package ( 'bson#58c002d' ) ;
119+ it ( 'installs successfully to specified install directory ' , async function ( ) {
120+ const bson6Git = new Package ( 'bson#58c002d' , installDir ) ;
100121 const maybeError = await bson6Git . install ( ) . catch ( error => error ) ;
101122 expect ( maybeError ) . to . be . undefined ;
123+ expect ( await exists ( join ( installDir , 'node_modules' , bson6Git . computedModuleName ) ) ) . to . be
124+ . true ;
102125 } ) ;
103126 } ) ;
104127
@@ -107,7 +130,10 @@ describe('common functionality', function () {
107130 function ( ) {
108131 // TODO: NODE-6361: Unskip and fix this test.
109132 it . skip ( 'throws an error' , async function ( ) {
110- const bson6Git = new Package ( 'bson#58c002d87bca9bbe7c7001cc6acae54e90a951bcf' ) ;
133+ const bson6Git = new Package (
134+ 'bson#58c002d87bca9bbe7c7001cc6acae54e90a951bcf' ,
135+ installDir
136+ ) ;
111137 const maybeError = await bson6Git . install ( ) . catch ( error => error ) ;
112138 expect ( maybeError ) . to . be . instanceOf ( Error ) ;
113139 } ) ;
@@ -118,9 +144,11 @@ describe('common functionality', function () {
118144 'when given a correctly formatted git package using git tag that exists' ,
119145 function ( ) {
120146 it ( 'installs successfully' , async function ( ) {
121- const bson6Git = new Package ( 'bson#v6.0.0' ) ;
147+ const bson6Git = new Package ( 'bson#v6.0.0' , installDir ) ;
122148 const maybeError = await bson6Git . install ( ) . catch ( error => error ) ;
123149 expect ( maybeError ) . to . be . undefined ;
150+ expect ( await exists ( join ( installDir , 'node_modules' , bson6Git . computedModuleName ) ) ) . to
151+ . be . true ;
124152 } ) ;
125153 }
126154 ) ;
@@ -129,7 +157,7 @@ describe('common functionality', function () {
129157 'when given a correctly formatted git package using git tag that does not exist' ,
130158 function ( ) {
131159 it ( 'throws an error' , async function ( ) {
132- const bson6Git = new Package ( 'bson#v999.999.9' ) ;
160+ const bson6Git = new Package ( 'bson#v999.999.9' , installDir ) ;
133161 const maybeError = await bson6Git . install ( ) . catch ( error => error ) ;
134162 expect ( maybeError ) . to . be . instanceOf ( Error ) ;
135163 } ) ;
@@ -143,16 +171,19 @@ describe('common functionality', function () {
143171 this . skip ( ) ;
144172 }
145173
146- const bsonLocal = new Package ( `bson:${ BSON_PATH } ` ) ;
174+ const bsonLocal = new Package ( `bson:${ BSON_PATH } ` , installDir ) ;
147175 const maybeError = await bsonLocal . install ( ) . catch ( error => error ) ;
148176 expect ( maybeError ) . to . not . be . instanceOf ( Error , maybeError . message ) ;
177+ expect ( await exists ( join ( installDir , 'node_modules' , bsonLocal . computedModuleName ) ) ) . to . be
178+ . true ;
149179 } ) ;
150180 } ) ;
151181
152182 context ( 'when given a path that does not exist' , function ( ) {
153183 it ( 'throws an error' , async function ( ) {
154184 const bsonLocal = new Package (
155- `bson:/highly/unlikely/path/to/exist/that/should/point/to/bson`
185+ `bson:/highly/unlikely/path/to/exist/that/should/point/to/bson` ,
186+ installDir
156187 ) ;
157188 const maybeError = await bsonLocal . install ( ) . catch ( error => error ) ;
158189 expect ( maybeError ) . to . be . instanceOf ( Error ) ;
0 commit comments