@@ -8,22 +8,31 @@ import stripAnsi from 'strip-ansi';
8
8
import assert from 'assert' ;
9
9
10
10
const PROMPT_PATTERN = / ^ > / m;
11
- const ERROR_PATTERN = / T h r o w n : \n ( [ ^ > ] * ) / m;
11
+ const ERROR_PATTERN_1 = / T h r o w n : \n ( [ ^ > ] * ) / m; // node <= 12.14
12
+ const ERROR_PATTERN_2 = / U n c a u g h t [: \n ] + ( [ ^ > ] * ) / m;
12
13
13
14
/**
14
15
* Test shell helper class.
15
16
*/
16
17
export class TestShell {
17
18
private static _openShells : TestShell [ ] = [ ] ;
18
19
19
- static start ( options : { args : string [ ] } = { args : [ ] } ) : TestShell {
20
- const execPath = path . resolve ( __dirname , '..' , 'bin' , 'mongosh.js' ) ;
20
+ static start ( options : {
21
+ args : string [ ] ;
22
+ } = { args : [ ] } ) : TestShell {
23
+ let shellProcess : ChildProcess ;
21
24
22
- const process = spawn ( 'node' , [ execPath , ...options . args ] , {
23
- stdio : [ 'pipe' , 'pipe' , 'pipe' ]
24
- } ) ;
25
+ if ( process . env . MONGOSH_TEST_EXECUTABLE_PATH ) {
26
+ shellProcess = spawn ( process . env . MONGOSH_TEST_EXECUTABLE_PATH , [ ...options . args ] , {
27
+ stdio : [ 'pipe' , 'pipe' , 'pipe' ]
28
+ } ) ;
29
+ } else {
30
+ shellProcess = spawn ( 'node' , [ path . resolve ( __dirname , '..' , 'bin' , 'mongosh.js' ) , ...options . args ] , {
31
+ stdio : [ 'pipe' , 'pipe' , 'pipe' ]
32
+ } ) ;
33
+ }
25
34
26
- const shell = new TestShell ( process ) ;
35
+ const shell = new TestShell ( shellProcess ) ;
27
36
TestShell . _openShells . push ( shell ) ;
28
37
29
38
return shell ;
@@ -38,6 +47,7 @@ export class TestShell {
38
47
private _process : ChildProcess ;
39
48
40
49
private _output : string ;
50
+ private _onClose : Promise < number > ;
41
51
42
52
constructor ( shellProcess : ChildProcess ) {
43
53
this . _process = shellProcess ;
@@ -54,6 +64,12 @@ export class TestShell {
54
64
shellProcess . stderr . on ( 'data' , ( chunk ) => {
55
65
this . _output += stripAnsi ( stderrDecoder . write ( chunk ) ) ;
56
66
} ) ;
67
+
68
+ this . _onClose = new Promise ( ( resolve ) => {
69
+ shellProcess . once ( 'close' , ( code ) => {
70
+ resolve ( code ) ;
71
+ } ) ;
72
+ } ) ;
57
73
}
58
74
59
75
get output ( ) : string {
@@ -72,6 +88,10 @@ export class TestShell {
72
88
} ) ;
73
89
}
74
90
91
+ waitForExit ( ) : Promise < number > {
92
+ return this . _onClose ;
93
+ }
94
+
75
95
kill ( ) : void {
76
96
this . _process . kill ( ) ;
77
97
}
@@ -132,7 +152,11 @@ export class TestShell {
132
152
}
133
153
134
154
private _getAllErrors ( ) : string [ ] {
135
- return [ ...( this . _output as any ) . matchAll ( ERROR_PATTERN ) ]
155
+ const output = ( this . _output as any ) ;
156
+ return [
157
+ ...output . matchAll ( ERROR_PATTERN_1 ) ,
158
+ ...output . matchAll ( ERROR_PATTERN_2 )
159
+ ]
136
160
. map ( m => m [ 1 ] . trim ( ) ) ;
137
161
}
138
162
}
0 commit comments