33 * See 'LICENSE' in the project root for license information.
44 * ------------------------------------------------------------------------------------------ */
55
6- import { readdir } from 'fs/promises' ;
7- import { IOptions , glob as globSync } from 'glob' ;
8- import Mocha from 'mocha' ;
6+ import { readdir , readFile } from 'fs/promises' ;
7+ import { glob as globSync , IOptions } from 'glob' ;
8+ import * as Mocha from 'mocha' ;
99import { basename , dirname , resolve } from 'path' ;
1010import { env } from 'process' ;
1111import { promisify } from 'util' ;
@@ -73,6 +73,41 @@ export async function getTestInfo(...scenarioOptions: (string | undefined)[]) {
7373 return undefined ;
7474}
7575
76+ /**
77+ * When running tests on GitHub, this function determines if the tests should be skipped based on
78+ * whether the binary version copied for tests is compatible with the minimum required version.
79+ * The minimum required binary version is defined in the `binaryCompat.json` file and changes when
80+ * there are breaking changes in the communication protocol or messages.
81+ *
82+ * When running locally, the function will always return false since you're expected to have the
83+ * correct binaries available.
84+ * @returns A promise that resolves to a boolean indicating whether the tests should be skipped.
85+ */
86+ async function shouldSkipTests ( ) : Promise < boolean > {
87+ try {
88+ const binaryVersion = JSON . parse ( await readFile ( `${ $root } /bin/binaryVersion.json` , 'utf-8' ) ) as { version : string } | undefined ;
89+ const binaryCompat = JSON . parse ( await readFile ( `${ $root } /test/minBinaryVersion.json` , 'utf-8' ) ) as { minBinaryVersion : string } | undefined ;
90+ if ( binaryCompat ?. minBinaryVersion && binaryVersion ?. version ) {
91+ const minParts = binaryCompat . minBinaryVersion . split ( '.' ) . map ( Number ) ;
92+ const actualParts = binaryVersion . version . split ( '.' ) . map ( Number ) ;
93+ const maxLen = Math . max ( minParts . length , actualParts . length ) ;
94+ let tooOld = false ;
95+ for ( let i = 0 ; i < maxLen ; i ++ ) {
96+ const diff = ( actualParts [ i ] ?? 0 ) - ( minParts [ i ] ?? 0 ) ;
97+ if ( diff < 0 ) { tooOld = true ; break ; }
98+ if ( diff > 0 ) { break ; }
99+ }
100+ if ( tooOld ) {
101+ console . warn ( `\nBinary-dependent tests SKIPPED: installed binary version ${ binaryVersion . version } is below the required minimum ${ binaryCompat . minBinaryVersion } .` ) ;
102+ console . warn ( `Tests will re-enable automatically once binaries >= ${ binaryCompat . minBinaryVersion } are installed.\n` ) ;
103+ return true ;
104+ }
105+ }
106+ } catch {
107+ }
108+ return false ;
109+ }
110+
76111export function run ( testsRoot : string , cb : ( error : any , failures ?: number ) => void ) : void {
77112 /**
78113 * This code runs in the extension host process, and not in the launch (main.ts) process.
@@ -89,32 +124,35 @@ export function run(testsRoot: string, cb: (error: any, failures?: number) => vo
89124 }
90125 const { name } = testInfo ;
91126
92- void glob ( `${ $root } /dist/test/scenarios/${ name } /tests/**/**.test.js` ) . then ( ( files ) => {
93-
94- try {
95- if ( ! files . length ) {
96- throw new Error ( `Unable to find unit tests for ${ name } at '${ $root } /dist/test/scenarios/${ name } /tests/**/**.test.js'` ) ;
97- }
98- const mocha = new Mocha ( {
99- ui : 'tdd' ,
100- timeout : 500000 ,
101- require : [ 'source-map-support/register' ] ,
102- color : true
103- } ) ;
104-
105- // Add files to the test suite
106- files . forEach ( f => mocha . addFile ( resolve ( testsRoot , f ) ) ) ;
107-
108- console . log ( '\n\n=============================================\n Test Output\n\n' ) ;
109- // Run the mocha test
110- mocha . run ( ( failures : any ) => {
111- cb ( null , failures ) ;
112- console . log ( '\n\n=============================================\n\n' ) ;
113- } ) ;
114- } catch ( err ) {
115- console . error ( err ) ;
116- cb ( err ) ;
127+ if ( await shouldSkipTests ( ) ) {
128+ cb ( null , 0 ) ;
129+ return ;
130+ }
131+
132+ const files = await glob ( `${ $root } /dist/test/scenarios/${ name } /tests/**/**.test.js` ) ;
133+ try {
134+ if ( ! files . length ) {
135+ throw new Error ( `Unable to find unit tests for ${ name } at '${ $root } /dist/test/scenarios/${ name } /tests/**/**.test.js'` ) ;
117136 }
118- } ) ;
137+ const mocha = new Mocha ( {
138+ ui : 'tdd' ,
139+ timeout : 500000 ,
140+ require : [ 'source-map-support/register' ] ,
141+ color : true
142+ } ) ;
143+
144+ // Add files to the test suite
145+ files . forEach ( f => mocha . addFile ( resolve ( testsRoot , f ) ) ) ;
146+
147+ console . log ( '\n\n=============================================\n Test Output\n\n' ) ;
148+ // Run the mocha test
149+ mocha . run ( ( failures : any ) => {
150+ cb ( null , failures ) ;
151+ console . log ( '\n\n=============================================\n\n' ) ;
152+ } ) ;
153+ } catch ( err ) {
154+ console . error ( err ) ;
155+ cb ( err ) ;
156+ }
119157 } ) ;
120158}
0 commit comments