@@ -28,91 +28,110 @@ export class TestsResponseHandler implements ResponseHandler<TestsResponse> {
2828 constructor (
2929 private readonly client : Client ,
3030 private readonly testsRunner : TestsRunner ,
31- private readonly batched : boolean ) {
31+ private readonly generateForMultipleSources : boolean ) {
3232 }
3333
3434 public async handle ( response : TestsResponse ) : Promise < void > {
3535 const testsSourceList = response . getTestsourcesList ( ) ;
3636
37- testsSourceList . forEach ( testsSourceInfo => {
38- this . testsRunner . testResultsVizualizer . clearTestsByTestFileName ( testsSourceInfo . getFilepath ( ) , false ) ;
39- } ) ;
37+ // Delete/backup old info
38+ for ( const test of testsSourceList ) {
39+ const localPath = pathUtils . substituteLocalPath ( test . getFilepath ( ) ) ;
40+ if ( isSarifReportFile ( localPath ) ) {
41+ if ( Prefs . isRemoteScenario ( ) ) {
42+ // do not back up the SARIF for local scenario - server did it
43+ await backupPreviousClientSarifReport ( localPath ) ;
44+ }
45+ }
46+ else {
47+ this . testsRunner . testResultsVizualizer . removeFileFromData ( localPath , false ) ;
48+ }
49+ }
50+
51+ // Transfer files' code if need
4052 if ( Prefs . isRemoteScenario ( ) ) {
53+ // do not write files for local scenario - server did it
4154 const stubs = response . getStubs ( ) ;
4255 if ( stubs ) {
4356 const stubsFiles = stubs . getStubsourcesList ( ) ;
44- await Promise . all ( stubsFiles . map ( async ( stub ) => {
57+ for ( const stub of stubsFiles ) {
4558 const localPath = pathUtils . substituteLocalPath ( stub . getFilepath ( ) ) ;
46- const stubfile = vs . Uri . file ( localPath ) ;
4759 logger . info ( `Write stub file ${ stub . getFilepath ( ) } to ${ localPath } ` ) ;
48- await vs . workspace . fs . writeFile ( stubfile , Buffer . from ( stub . getCode ( ) ) ) ;
49- } ) ) ;
60+ await vs . workspace . fs . writeFile ( vs . Uri . file ( localPath ) , Buffer . from ( stub . getCode ( ) ) ) ;
61+ }
5062 }
51- await Promise . all ( ( testsSourceList ) . map ( async ( test ) => {
63+ for ( const test of testsSourceList ) {
5264 const localPath = pathUtils . substituteLocalPath ( test . getFilepath ( ) ) ;
53- const testfile = vs . Uri . file ( localPath ) ;
54-
55- if ( isTestFileSourceFile ( testfile ) ) {
65+ await vs . workspace . fs . writeFile ( vs . Uri . file ( localPath ) , Buffer . from ( test . getCode ( ) ) ) ;
66+ }
67+ }
68+
69+ // Show and log the results in UI
70+ {
71+ let firstTest = true ;
72+ const SarifReportFiles = [ ] ;
73+ for ( const test of testsSourceList ) {
74+ const localPath = pathUtils . substituteLocalPath ( test . getFilepath ( ) ) ;
75+
76+ if ( isSarifReportFile ( localPath ) ) {
77+ logger . info ( `Generated SARIF file ${ localPath } ` ) ;
78+ SarifReportFiles . push ( vs . Uri . file ( localPath ) ) ;
79+ } else if ( isTestFileSourceFile ( localPath ) ) {
5680 const testsNumberInErrorSuite = test . getErrormethodsnumber ( ) ;
5781 const testsNumberInRegressionSuite = test . getRegressionmethodsnumber ( ) ;
5882 logger . info ( `Generated test file ${ localPath } with ${ testsNumberInRegressionSuite } tests in regression suite and ${ testsNumberInErrorSuite } tests in error suite` ) ;
83+ if ( ! this . generateForMultipleSources && firstTest ) {
84+ // show generated test file for line, class, function, single source file
85+ firstTest = false ;
86+ await vs . window . showTextDocument ( vs . Uri . file ( localPath ) , { preview : false } ) ;
87+ }
5988 } else {
6089 logger . info ( `Generated test file ${ localPath } ` ) ;
6190 }
62-
63- const isSarifReport = testfile . path . endsWith ( "project_code_analysis.sarif" ) ;
64- if ( isSarifReport && fs . existsSync ( testfile . fsPath ) ) {
65- const ctime = fs . lstatSync ( testfile . fsPath ) . ctime ;
66-
67- // eslint-disable-next-line no-inner-declarations
68- function pad2 ( num : number ) : string {
69- return ( "0" + num ) . slice ( - 2 ) ;
91+ }
92+ if ( SarifReportFiles . length > 0 ) {
93+ const sarifExt = vs . extensions . getExtension ( messages . defaultSARIFViewer ) ;
94+ // eslint-disable-next-line eqeqeq
95+ if ( sarifExt == null ) {
96+ messages . showWarningMessage ( messages . intstallSARIFViewer ) ;
97+ } else {
98+ if ( ! sarifExt . isActive ) {
99+ await sarifExt . activate ( ) ;
70100 }
71-
72- const newName = "project_code_analysis-"
73- + ctime . getFullYear ( )
74- + pad2 ( ctime . getMonth ( ) + 1 )
75- + pad2 ( ctime . getDate ( ) )
76- + pad2 ( ctime . getHours ( ) )
77- + pad2 ( ctime . getMinutes ( ) )
78- + pad2 ( ctime . getSeconds ( ) )
79- + ".sarif" ;
80- await vs . workspace . fs . rename ( testfile , Uri . file ( path . join ( path . dirname ( testfile . fsPath ) , newName ) ) ) ;
81- }
82-
83- await vs . workspace . fs . writeFile ( testfile , Buffer . from ( test . getCode ( ) ) ) ;
84- if ( isSarifReport ) {
85- const sarifExt = vs . extensions . getExtension ( messages . defaultSARIFViewer ) ;
86- // eslint-disable-next-line eqeqeq
87- if ( sarifExt == null ) {
88- messages . showWarningMessage ( messages . intstallSARIFViewer ) ;
89- } else {
90- if ( ! sarifExt . isActive ) {
91- await sarifExt . activate ( ) ;
92- }
93- await sarifExt . exports . openLogs ( [
94- testfile ,
95- ] ) ;
96- }
97- }
98- return testfile ;
99- } ) ) ;
100- }
101- if ( ! this . batched ) {
102- const localPaths = testsSourceList . map ( testsSourceInfo => pathUtils . substituteLocalPath ( testsSourceInfo . getFilepath ( ) ) ) ;
103- if ( localPaths . length > 0 ) {
104- const cppLocalPaths = localPaths . filter ( fileName => fileName . endsWith ( '_test.cpp' ) ) ;
105- if ( cppLocalPaths . length > 0 ) {
106- const fileToOpen = vs . Uri . file ( cppLocalPaths [ 0 ] ) ;
107- await vs . window . showTextDocument ( fileToOpen , { preview : false } ) ;
101+ sarifExt . exports . openLogs ( SarifReportFiles ) ;
108102 }
109103 }
110104 }
111105 }
112106}
113107
114- function isTestFileSourceFile ( testfile : any ) : boolean {
115- return testfile . path . endsWith ( '_test.cpp' ) ;
108+ function isSarifReportFile ( testfile : string ) : boolean {
109+ return testfile . endsWith ( "project_code_analysis.sarif" ) ;
110+ }
111+
112+ async function backupPreviousClientSarifReport ( localPath : string ) : Promise < void > {
113+ if ( fs . existsSync ( localPath ) ) {
114+ const ctime = fs . lstatSync ( localPath ) . ctime ;
115+
116+ // eslint-disable-next-line no-inner-declarations
117+ function pad2 ( num : number ) : string {
118+ return ( "0" + num ) . slice ( - 2 ) ;
119+ }
120+
121+ const newName = "project_code_analysis-"
122+ + ctime . getFullYear ( )
123+ + pad2 ( ctime . getMonth ( ) + 1 )
124+ + pad2 ( ctime . getDate ( ) )
125+ + pad2 ( ctime . getHours ( ) )
126+ + pad2 ( ctime . getMinutes ( ) )
127+ + pad2 ( ctime . getSeconds ( ) )
128+ + ".sarif" ;
129+ await vs . workspace . fs . rename ( vs . Uri . file ( localPath ) , Uri . file ( path . join ( path . dirname ( localPath ) , newName ) ) ) ;
130+ }
131+ }
132+
133+ function isTestFileSourceFile ( localpath : string ) : boolean {
134+ return localpath . endsWith ( '_test.cpp' ) ;
116135}
117136
118137export class SnippetResponseHandler implements ResponseHandler < TestsResponse > {
@@ -133,5 +152,4 @@ export class SnippetResponseHandler implements ResponseHandler<TestsResponse> {
133152 await vs . window . showTextDocument ( doc , { preview : false } ) ;
134153 } ) ;
135154 }
136-
137155}
0 commit comments