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' ;
57import { clearTestedDeps } from '../utils' ;
@@ -8,20 +10,32 @@ describe('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 ( ) {
1529 context ( 'when given a correctly formatted npm package' , function ( ) {
1630 it ( 'sets computedModuleName correctly' , function ( ) {
17- const pack = new Package ( '[email protected] ' ) ; 31+ const pack = new Package ( '[email protected] ' , installDir ) ; 1832 expect ( pack ) . to . haveOwnProperty ( 'computedModuleName' , 'bson-6.0.0' ) ;
1933 } ) ;
2034 } ) ;
2135
2236 context ( 'when given a correctly formatted git repository' , function ( ) {
2337 it ( 'sets computedModuleName correctly' , function ( ) {
24- const pack = new Package ( 'bson#eb98b8c39d6d5ba4ce7231ab9e0f29495d74b994' ) ;
38+ const pack = new Package ( 'bson#eb98b8c39d6d5ba4ce7231ab9e0f29495d74b994' , installDir ) ;
2539 expect ( pack ) . to . haveOwnProperty (
2640 'computedModuleName' ,
2741 'bson-git-eb98b8c39d6d5ba4ce7231ab9e0f29495d74b994'
@@ -31,13 +45,16 @@ describe('common functionality', function () {
3145
3246 context ( 'when trying to install an npm package apart from bson or bson-ext' , function ( ) {
3347 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 / ) ; 48+ expect ( ( ) => new Package ( '[email protected] ' , installDir ) ) . to . throw ( 49+ Error ,
50+ / u n k n o w n p a c k a g e s p e c i f i e r /
51+ ) ;
3552 } ) ;
3653 } ) ;
3754
3855 context ( 'when trying to install a git package apart from bson or bson-ext' , function ( ) {
3956 it ( 'throws an error' , function ( ) {
40- expect ( ( ) => new Package ( 'notBson#abcdabcdabcd' ) ) . to . throw (
57+ expect ( ( ) => new Package ( 'notBson#abcdabcdabcd' , installDir ) ) . to . throw (
4158 Error ,
4259 / u n k n o w n p a c k a g e s p e c i f i e r /
4360 ) ;
@@ -50,7 +67,7 @@ describe('common functionality', function () {
5067 console . log ( 'Skipping since BSON_PATH is undefined' ) ;
5168 this . skip ( ) ;
5269 }
53- const pack = new Package ( `bson:${ BSON_PATH } ` ) ;
70+ const pack = new Package ( `bson:${ BSON_PATH } ` , installDir ) ;
5471 expect ( pack ) . to . haveOwnProperty (
5572 'computedModuleName' ,
5673 `bson-local-${ BSON_PATH . replaceAll ( sep , '_' ) } `
@@ -62,14 +79,14 @@ describe('common functionality', function () {
6279 context ( '#check()' , function ( ) {
6380 context ( 'when package is not installed' , function ( ) {
6481 it ( 'returns undefined' , function ( ) {
65- const pack = new Package ( 'bson@6' ) ;
82+ const pack = new Package ( 'bson@6' , installDir ) ;
6683 expect ( pack . check ( ) ) . to . be . undefined ;
6784 } ) ;
6885 } ) ;
6986
7087 context ( 'when package is installed' , function ( ) {
7188 it ( 'returns the module' , async function ( ) {
72- const pack = new Package ( '[email protected] ' ) ; 89+ const pack = new Package ( '[email protected] ' , installDir ) ; 7390 await pack . install ( ) ;
7491 expect ( pack . check ( ) ) . to . not . be . undefined ;
7592 } ) ;
@@ -80,23 +97,23 @@ describe('common functionality', function () {
8097 context ( 'when given a correctly formatted npm package that exists' , function ( ) {
8198 for ( const lib of [ '[email protected] ' , '[email protected] ' , 'bson@latest' , 'bson-ext@latest' ] ) { 8299 it ( `installs ${ lib } successfully` , async function ( ) {
83- const pack = new Package ( lib ) ;
100+ const pack = new Package ( lib , installDir ) ;
84101 await pack . install ( ) ;
85102 } ) ;
86103 }
87104 } ) ;
88105
89106 context ( 'when given a correctly formatted npm package that does not exist' , function ( ) {
90107 it ( 'throws an error' , async function ( ) {
91- const bson9000 = new Package ( 'bson@9000' ) ;
108+ const bson9000 = new Package ( 'bson@9000' , installDir ) ;
92109 const error = await bson9000 . install ( ) . catch ( error => error ) ;
93110 expect ( error ) . to . be . instanceOf ( Error ) ;
94111 } ) ;
95112 } ) ;
96113
97114 context ( 'when given a correctly formatted git package using commit that exists' , function ( ) {
98115 it ( 'installs successfully' , async function ( ) {
99- const bson6Git = new Package ( 'bson#58c002d' ) ;
116+ const bson6Git = new Package ( 'bson#58c002d' , installDir ) ;
100117 const maybeError = await bson6Git . install ( ) . catch ( error => error ) ;
101118 expect ( maybeError ) . to . be . undefined ;
102119 } ) ;
@@ -107,7 +124,10 @@ describe('common functionality', function () {
107124 function ( ) {
108125 // TODO: NODE-6361: Unskip and fix this test.
109126 it . skip ( 'throws an error' , async function ( ) {
110- const bson6Git = new Package ( 'bson#58c002d87bca9bbe7c7001cc6acae54e90a951bcf' ) ;
127+ const bson6Git = new Package (
128+ 'bson#58c002d87bca9bbe7c7001cc6acae54e90a951bcf' ,
129+ installDir
130+ ) ;
111131 const maybeError = await bson6Git . install ( ) . catch ( error => error ) ;
112132 expect ( maybeError ) . to . be . instanceOf ( Error ) ;
113133 } ) ;
@@ -118,7 +138,7 @@ describe('common functionality', function () {
118138 'when given a correctly formatted git package using git tag that exists' ,
119139 function ( ) {
120140 it ( 'installs successfully' , async function ( ) {
121- const bson6Git = new Package ( 'bson#v6.0.0' ) ;
141+ const bson6Git = new Package ( 'bson#v6.0.0' , installDir ) ;
122142 const maybeError = await bson6Git . install ( ) . catch ( error => error ) ;
123143 expect ( maybeError ) . to . be . undefined ;
124144 } ) ;
@@ -129,7 +149,7 @@ describe('common functionality', function () {
129149 'when given a correctly formatted git package using git tag that does not exist' ,
130150 function ( ) {
131151 it ( 'throws an error' , async function ( ) {
132- const bson6Git = new Package ( 'bson#v999.999.9' ) ;
152+ const bson6Git = new Package ( 'bson#v999.999.9' , installDir ) ;
133153 const maybeError = await bson6Git . install ( ) . catch ( error => error ) ;
134154 expect ( maybeError ) . to . be . instanceOf ( Error ) ;
135155 } ) ;
@@ -143,7 +163,7 @@ describe('common functionality', function () {
143163 this . skip ( ) ;
144164 }
145165
146- const bsonLocal = new Package ( `bson:${ BSON_PATH } ` ) ;
166+ const bsonLocal = new Package ( `bson:${ BSON_PATH } ` , installDir ) ;
147167 const maybeError = await bsonLocal . install ( ) . catch ( error => error ) ;
148168 expect ( maybeError ) . to . not . be . instanceOf ( Error , maybeError . message ) ;
149169 } ) ;
@@ -152,7 +172,8 @@ describe('common functionality', function () {
152172 context ( 'when given a path that does not exist' , function ( ) {
153173 it ( 'throws an error' , async function ( ) {
154174 const bsonLocal = new Package (
155- `bson:/highly/unlikely/path/to/exist/that/should/point/to/bson`
175+ `bson:/highly/unlikely/path/to/exist/that/should/point/to/bson` ,
176+ installDir
156177 ) ;
157178 const maybeError = await bsonLocal . install ( ) . catch ( error => error ) ;
158179 expect ( maybeError ) . to . be . instanceOf ( Error ) ;
0 commit comments