@@ -9,8 +9,19 @@ const fs = require('fs');
99const mongoose = require ( '../../lib' ) ;
1010const { Map } = require ( '../../lib/types' ) ;
1111
12+ const { stop, start } = require ( '../../scripts/setup-encryption-tests' ) ;
13+
1214const LOCAL_KEY = Buffer . from ( 'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk' , 'base64' ) ;
1315
16+ const exists = path => {
17+ try {
18+ fs . statSync ( path ) ;
19+ return true ;
20+ } catch {
21+ return false ;
22+ }
23+ } ;
24+
1425/**
1526 * @param {object } object
1627 * @param {string } property
@@ -25,19 +36,29 @@ describe('encryption integration tests', () => {
2536 const cachedUri = process . env . MONGOOSE_TEST_URI ;
2637 const cachedLib = process . env . CRYPT_SHARED_LIB_PATH ;
2738
28- before ( function ( ) {
39+ before ( async function ( ) {
40+ this . timeout ( 0 ) ;
2941 const cwd = process . cwd ( ) ;
30- const file = fs . readFileSync ( cwd + '/data/mo-expansion.yml' , { encoding : 'utf-8' } ) . trim ( ) . split ( '\n' ) ;
31-
32- // matches `key="value"` and extracts key and value.
33- const regex = / ^ (?< key > .* ) : " (?< value > .* ) " $ / ;
34- const variables = Object . fromEntries ( file . map ( ( line ) => regex . exec ( line . trim ( ) ) . groups ) . map ( ( { key, value } ) => [ key , value ] ) ) ;
35-
36- process . env . CRYPT_SHARED_LIB_PATH ??= variables . CRYPT_SHARED_LIB_PATH ;
37- process . env . MONGOOSE_TEST_URI ??= variables . MONGODB_URI ;
42+ if ( ! exists ( cwd + '/mo-expansion.yml' ) ) {
43+ const { uri, cryptShared } = await start ( ) ;
44+ process . env . CRYPT_SHARED_LIB_PATH = cryptShared ;
45+ process . env . MONGOOSE_TEST_URI = uri ;
46+ } else {
47+ const file = fs . readFileSync ( cwd + '/mo-expansion.yml' , { encoding : 'utf-8' } ) . trim ( ) . split ( '\n' ) ;
48+
49+ const regex = / ^ (?< key > .* ) : " (?< value > .* ) " $ / ;
50+ const variables = Object . fromEntries ( file . map ( ( line ) => regex . exec ( line . trim ( ) ) . groups ) . map ( ( { key, value } ) => [ key , value ] ) ) ;
51+
52+ process . env . CRYPT_SHARED_LIB_PATH = variables . CRYPT_SHARED_LIB_PATH ;
53+ process . env . MONGOOSE_TEST_URI = variables . MONGODB_URI ;
54+ }
3855 } ) ;
3956
40- after ( function ( ) {
57+ after ( async function ( ) {
58+ if ( ! exists ( process . cwd ( ) + '/mo-expansion.yml' ) ) {
59+ await stop ( ) ;
60+ }
61+
4162 process . env . CRYPT_SHARED_LIB_PATH = cachedLib ;
4263 process . env . MONGOOSE_TEST_URI = cachedUri ;
4364 } ) ;
0 commit comments