Skip to content

Commit 5a25b90

Browse files
authored
[BUG] SARIF report is not generated for server on local host #340 (#344)
- fogging unification for local/remote scenario - global refactoring with renaming - remmove wrong plugin dependences
1 parent 4978445 commit 5a25b90

File tree

5 files changed

+101
-94
lines changed

5 files changed

+101
-94
lines changed

server/src/SARIFGenerator.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace sarif {
4747
void sarifAddTestsToResults(const utbot::ProjectContext &projectContext,
4848
const Tests &tests,
4949
json &results) {
50-
LOG_S(INFO) << "{stack";
50+
LOG_SCOPE_FUNCTION(DEBUG);
5151
for (const auto &it : tests.methods) {
5252
for (const auto &methodTestCase : it.second.testCases) {
5353
json result;
@@ -180,11 +180,9 @@ namespace sarif {
180180
}
181181
}
182182
}
183-
LOG_S(INFO) << "}stack";
184183
}
185184

186185
std::string sarifPackResults(const json &results) {
187-
// POINT 3
188186
json sarifJson;
189187
sarifJson["$schema"] = "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json";
190188
sarifJson["version"] = "2.1.0";

server/src/streams/tests/ServerTestsWriter.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ bool ServerTestsWriter::writeFileAndSendResponse(const tests::Tests &tests,
4848
testSource->set_filepath(tests.testSourceFilePath);
4949
if (synchronizeCode) {
5050
testSource->set_code(tests.code);
51-
testSource->set_errormethodsnumber(tests.errorMethodsNumber);
52-
testSource->set_regressionmethodsnumber(tests.regressionMethodsNumber);
5351
}
52+
testSource->set_errormethodsnumber(tests.errorMethodsNumber);
53+
testSource->set_regressionmethodsnumber(tests.regressionMethodsNumber);
5454

5555
auto testHeader = response.add_testsources();
5656
testHeader->set_filepath(tests.testHeaderFilePath);
@@ -69,19 +69,18 @@ void ServerTestsWriter::writeReport(const std::string &content,
6969
const std::string &message,
7070
const fs::path &pathToStore) const
7171
{
72-
if (synchronizeCode || fs::exists(pathToStore)) {
73-
testsgen::TestsResponse response;
74-
if (synchronizeCode) {
75-
TestsWriter::writeReport(content, message, pathToStore);
76-
}
77-
auto testSource = response.add_testsources();
78-
testSource->set_filepath(pathToStore);
79-
if (synchronizeCode) {
80-
testSource->set_code(content);
81-
}
82-
LOG_S(INFO) << message;
83-
auto progress = GrpcUtils::createProgress(message, 100, false);
84-
response.set_allocated_progress(progress.release());
85-
writeMessage(response);
72+
testsgen::TestsResponse response;
73+
TestsWriter::writeReport(content, message, pathToStore);
74+
75+
auto testSource = response.add_testsources();
76+
testSource->set_filepath(pathToStore);
77+
if (synchronizeCode) {
78+
// write the content only for real data transfer
79+
// `synchronizeCode` is false if client and server share the same FS
80+
testSource->set_code(content);
8681
}
82+
LOG_S(INFO) << message;
83+
auto progress = GrpcUtils::createProgress(message, 100, false);
84+
response.set_allocated_progress(progress.release());
85+
writeMessage(response);
8786
}

vscode-plugin/package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,5 @@
580580
"randomstring": "1.2.2",
581581
"source-map-support": "0.5.21",
582582
"typescript": "3.9.4"
583-
},
584-
"extensionPack": [
585-
"Natizyskunk.sftp",
586-
"MS-SarifVSCode.sarif-viewer"
587-
]
583+
}
588584
}

vscode-plugin/src/responses/responseHandler.ts

Lines changed: 78 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -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

118137
export 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
}

vscode-plugin/src/visualizers/testResultsVisualizer.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ export class TestResultsVisualizer implements Visualizer, DataLoader<TestResultO
3535
});
3636
vs.workspace.onDidDeleteFiles((event) => {
3737
event.files.forEach(fileUri => {
38-
this.clearTestsByTestFileName(fileUri.fsPath);
38+
this.removeFileFromData(fileUri.fsPath);
3939
});
4040
});
4141
vs.workspace.onDidChangeTextDocument((event) => {
4242
this.editors.forEach((editor) => {
4343
if (editor.document.uri === event.document.uri) {
4444
this.hide(editor);
45-
this.clearEditorTests(editor);
45+
this.removeFileFromData(editor.document.fileName, false);
4646
}
4747
});
4848
});
@@ -80,15 +80,11 @@ export class TestResultsVisualizer implements Visualizer, DataLoader<TestResultO
8080
this.testsWithStatuses = [];
8181
}
8282

83-
public clearEditorTests(editor: vs.TextEditor): void {
84-
this.clearTestsByTestFileName(editor.document.fileName, false);
85-
}
83+
public removeFileFromData(localFileName: string, canBeFolder: boolean = true): void {
84+
const serverFileName = Prefs.isRemoteScenario()
85+
? pathUtils.substituteRemotePath(localFileName)
86+
: localFileName;
8687

87-
public clearTestsByTestFileName(filename: string, canBeFolder: boolean = true): void {
88-
let serverFileName = filename;
89-
if (Prefs.isRemoteScenario()) {
90-
serverFileName = pathUtils.substituteRemotePath(serverFileName);
91-
}
9288
if (canBeFolder) {
9389
this.testsWithStatuses = this.testsWithStatuses.filter(test => !test.testInfo.filePath.startsWith(serverFileName));
9490
} else {

0 commit comments

Comments
 (0)