From edfc9dd2dc988cab858c60c987a7b62248eeea4f Mon Sep 17 00:00:00 2001 From: dtodt Date: Wed, 18 May 2022 22:49:44 -0300 Subject: [PATCH 1/2] chore: linter fix --- src/data-provider.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/data-provider.ts b/src/data-provider.ts index 738d78b..7322c77 100644 --- a/src/data-provider.ts +++ b/src/data-provider.ts @@ -184,7 +184,7 @@ export class FileCoverageDataProvider implements vscode.TreeDataProvider Date: Wed, 18 May 2022 22:53:57 -0300 Subject: [PATCH 2/2] fix: monorepo coverage resolves Accept 2 or more coverage files #4 --- src/coverage-parser.ts | 26 ++++++++++++++++++-------- src/files-loader.ts | 19 ++++++++++--------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/coverage-parser.ts b/src/coverage-parser.ts index 126b1b3..ae12728 100644 --- a/src/coverage-parser.ts +++ b/src/coverage-parser.ts @@ -60,19 +60,24 @@ export class CoverageParser { private recomputeStats(data: any[] ) { data.forEach((section) => { - if(!section.hit || !section.found){ + if(!section.lines.hit || !section.lines.found){ section.lines.hit = section.lines.details.reduce((a, b) => a + (b.hit > 0 ? 1 : 0), 0); section.lines.found = section.lines.details.length; } }); } - private async convertSectionsToMap(workspaceFolder: vscode.WorkspaceFolder, data: Section[]): Promise> { + private async convertSectionsToMap(workspaceFolder: vscode.WorkspaceFolder, filename: string, data: Section[]): Promise> { + this.recomputeStats(data); + const sections = new Map(); const addToSectionsMap = async (section: { title: string; file: string; }) => { - this.recomputeStats(data); + const intermediatePath = this.stripPath(filename); let filePath = section.file; - if(iopath.isAbsolute(section.file)){ + if (!!intermediatePath) { + filePath = iopath.join(intermediatePath, filePath); + } + if(iopath.isAbsolute(section.file) || filePath.includes(workspaceFolder.uri.fsPath)){ //Convert to a path relative to the workspace root filePath = filePath.replace(workspaceFolder.uri.fsPath, ""); } @@ -100,7 +105,7 @@ export class CoverageParser { try { parseContentCobertura(xmlFile, async (err: any, data: any[]) => { checkError(err); - const sections = await this.convertSectionsToMap(workspaceFolder, data); + const sections = await this.convertSectionsToMap(workspaceFolder, filename, data); return resolve(sections); }, true); } catch (error) { @@ -122,7 +127,7 @@ export class CoverageParser { try { parseContentJacoco(xmlFile, async (err: any, data: any[]) => { checkError(err); - const sections = await this.convertSectionsToMap(workspaceFolder, data); + const sections = await this.convertSectionsToMap(workspaceFolder, filename, data); return resolve(sections); }); } catch (error) { @@ -134,7 +139,7 @@ export class CoverageParser { private async xmlExtractClover(workspaceFolder: vscode.WorkspaceFolder, filename: string, xmlFile: string) { try { const data = await parseContentClover(xmlFile); - const sections = await this.convertSectionsToMap(workspaceFolder, data); + const sections = await this.convertSectionsToMap(workspaceFolder, filename, data); return sections; } catch (error) { error.message = `filename: ${filename} ${error.message}`; @@ -156,7 +161,7 @@ export class CoverageParser { try { source(lcovFile, async (err: Error, data: any[]) => { checkError(err); - const sections = await this.convertSectionsToMap(workspaceFolder, data); + const sections = await this.convertSectionsToMap(workspaceFolder, filename, data); return resolve(sections); }); } catch (error) { @@ -172,4 +177,9 @@ export class CoverageParser { `[${Date.now()}][coverageparser][${system}]: Error: ${message}\n${stackTrace}`, ); } + + // Removes the coverage dir and file from coverage path + private stripPath(filename: String) { + return (filename || '').split('/').slice(0, -2).join('/'); + } } diff --git a/src/files-loader.ts b/src/files-loader.ts index 509937e..fb44b7c 100644 --- a/src/files-loader.ts +++ b/src/files-loader.ts @@ -1,5 +1,5 @@ import * as vscodeLogging from '@vscode-logging/logger'; -import * as fs from 'fs'; +import * as glob from 'glob'; import { readFile } from "fs"; import * as iopath from "path"; import * as vscode from 'vscode'; @@ -30,15 +30,16 @@ export class FilesLoader { for (const workspaceFolder of vscode.workspace.workspaceFolders) { for (const filePath of filesPaths) { for (const fileName of fileNames) { + const testPath = iopath.join(filePath, fileName); + const coveragePaths: string[] = glob.sync(testPath, { cwd: workspaceFolder.uri.fsPath }) || []; + this.logger.debug(`total files found: ${coveragePaths.length}`); - const coverageFileFullPath = iopath.join(workspaceFolder.uri.fsPath, filePath, fileName); - - if (fs.existsSync(coverageFileFullPath) && fs.lstatSync(coverageFileFullPath).isFile()) { - - if (!coverageFiles.has(workspaceFolder.uri.fsPath)){ - coverageFiles.set(workspaceFolder.uri.fsPath, new WorkspaceFolderCoverageFiles(workspaceFolder)); - } - coverageFiles.get(workspaceFolder.uri.fsPath)?.coverageFiles.add(new WorkspaceFolderCoverageFile(coverageFileFullPath, await this.load(coverageFileFullPath))); + if (!!coveragePaths.length && !coverageFiles.has(workspaceFolder.uri.fsPath)){ + coverageFiles.set(workspaceFolder.uri.fsPath, new WorkspaceFolderCoverageFiles(workspaceFolder)); + } + for (const coveragePath of coveragePaths) { + const filePath = iopath.join(workspaceFolder.uri.fsPath, coveragePath); + coverageFiles.get(workspaceFolder.uri.fsPath)?.coverageFiles.add(new WorkspaceFolderCoverageFile(filePath, await this.load(filePath))); } } }