Skip to content

Commit

Permalink
Handle absolute paths #21 + glob #22
Browse files Browse the repository at this point in the history
  • Loading branch information
tenninebt committed Dec 7, 2022
1 parent 61b15e8 commit 61286a3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@
"koverage.coverageFilePaths": {
"type": "array",
"default": [
"coverage"
"**"
],
"description": "coverage file paths for the extensions to automatically search in"
},
"koverage.ignoredPathGlobs": {"type": "string", "default": "**/{node_modules,venv,.venv,vendor}/**"},
"koverage.lowCoverageThreshold": {
"type": "number",
"default": 50,
Expand Down Expand Up @@ -126,4 +127,4 @@
"mocha": "^7.1.1",
"mkdirp": "^0.5.6"
}
}
}
7 changes: 5 additions & 2 deletions src/config-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ export class ConfigStore {
} else {
const coverageFileNames = updatedRawConfig.inspect('coverageFileNames')?.defaultValue as string[];
const coverageFilePaths = updatedRawConfig.inspect('coverageFilePaths')?.defaultValue as string[];
const ignoredPathGlobs = updatedRawConfig.inspect('ignoredPathGlobs')?.defaultValue as string;
const lowCoverageThreshold = updatedRawConfig.inspect('lowCoverageThreshold')?.defaultValue as number;
const sufficientCoverageThreshold = updatedRawConfig.inspect('sufficientCoverageThreshold')?.defaultValue as number;
rollbackConfig = new Config(coverageFileNames, coverageFilePaths, lowCoverageThreshold, sufficientCoverageThreshold);
rollbackConfig = new Config(coverageFileNames, coverageFilePaths, ignoredPathGlobs, lowCoverageThreshold, sufficientCoverageThreshold);
}
this.logger.warn(`Invalid configuration : ${updatedConfig}`);
this.logger.warn(`Last valid configuration will be used : ${rollbackConfig}`);
Expand All @@ -56,9 +57,10 @@ export class ConfigStore {
// Basic configurations
const coverageFileNames = workspaceConfiguration.get("coverageFileNames") as string[];
const coverageFilePaths = workspaceConfiguration.get("coverageFilePaths") as string[];
const ignoredPathGlobs = workspaceConfiguration.get("ignoredPathGlobs") as string;
const lowCoverageThreshold = workspaceConfiguration.get("lowCoverageThreshold") as number;
const sufficientCoverageThreshold = workspaceConfiguration.get("sufficientCoverageThreshold") as number;
return new Config(coverageFileNames, coverageFilePaths, lowCoverageThreshold, sufficientCoverageThreshold);
return new Config(coverageFileNames, coverageFilePaths, ignoredPathGlobs, lowCoverageThreshold, sufficientCoverageThreshold);
}

public subscribe(next?: (value: Config) => void, error?: (error: any) => void, complete?: () => void): rx.Subscription {
Expand All @@ -73,6 +75,7 @@ export class Config {
constructor(
public readonly coverageFileNames: string[],
public readonly coverageFilePaths: string[],
public readonly ignoredPathGlobs: string,
public readonly lowCoverageThreshold: number,
public readonly sufficientCoverageThreshold: number
) {
Expand Down
44 changes: 37 additions & 7 deletions src/files-loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as vscodeLogging from '@vscode-logging/logger';
import * as glob from "glob";
import * as fs from 'fs';
import { readFile } from "fs";
import * as iopath from "path";
Expand All @@ -19,7 +20,7 @@ export class FilesLoader {
const fileNames = this.configStore.current.coverageFileNames;
const filesPaths = this.configStore.current.coverageFilePaths;
const files = await this.loadCoverageInWorkspace(filesPaths, fileNames);
if (!files.size) {
if (!files.size) {
this.logger.warn('Could not find a Coverage file!');
}
return files;
Expand All @@ -30,15 +31,14 @@ export class FilesLoader {
for (const workspaceFolder of vscode.workspace.workspaceFolders) {
for (const filePath of filesPaths) {
for (const fileName of fileNames) {

const coverageFileFullPath = await this.globFind(workspaceFolder, fileName, filePath);

const coverageFileFullPath = iopath.join(workspaceFolder.uri.fsPath, filePath, fileName);

if (fs.existsSync(coverageFileFullPath) && fs.lstatSync(coverageFileFullPath).isFile()) {

if (!coverageFiles.has(workspaceFolder.uri.fsPath)){
for (var f of coverageFileFullPath) {
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)));
coverageFiles.get(workspaceFolder.uri.fsPath)?.coverageFiles.add(new WorkspaceFolderCoverageFile(f, await this.load(f)));
}
}
}
Expand All @@ -50,6 +50,36 @@ export class FilesLoader {
return new Set<WorkspaceFolderCoverageFiles>(coverageFiles.values());
}

private globFind(
workspaceFolder: vscode.WorkspaceFolder,
fileName: string,
filePath: string
) {
return new Promise<Set<string>>((resolve) => {
glob(`${filePath}/${fileName}`,
{
cwd: workspaceFolder.uri.fsPath,
dot: true,
ignore: this.configStore.current.ignoredPathGlobs,
realpath: true,
strict: false,
},
(err, files) => {
if (!files || !files.length) {
// Show any errors if no file was found.
if (err) {
vscode.window.showWarningMessage(`An error occured while looking for the coverage file ${err}`);
}
return resolve(new Set());
}
const setFiles = new Set<string>();
files.forEach((file) => setFiles.add(file));
return resolve(setFiles);
},
);
});
}

private load(path: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
readFile(path, (err, data) => {
Expand Down

0 comments on commit 61286a3

Please sign in to comment.