Skip to content

Commit

Permalink
Merge pull request #297 from evan-bradley/master
Browse files Browse the repository at this point in the history
Allow named workspace folders
  • Loading branch information
ryanluker authored Apr 11, 2021
2 parents 8308347 + b3b5779 commit 73af483
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/coverage-system/sectionfinder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Section} from "lcov-parse";
import {extname} from "path";
import {basename} from "path";
import {TextEditor, Uri, workspace} from "vscode";
import {OutputChannel} from "vscode";
import {Config} from "../extension/config";
Expand Down Expand Up @@ -116,7 +116,7 @@ export class SectionFinder {
const editorFileAbs = normalizeFileName(fileName);
const workspaceFile = normalizeFileName(workspaceFsPath);
const editorFileRelative = editorFileAbs.substring(workspaceFile.length);
const workspaceFolderName = normalizeFileName(workspaceFolder.name);
const workspaceFolderName = normalizeFileName(basename(workspaceFsPath));
return { relativePath: editorFileRelative, workspaceFolder: workspaceFolderName};
}

Expand Down
89 changes: 78 additions & 11 deletions test/coverage-system/sectionfinder.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import {Section} from "lcov-parse";
import {TextEditor} from "vscode";
import * as assert from "assert";
import { Section } from "lcov-parse";
import { basename, join } from "path";
import { TextEditor, Uri, workspace, WorkspaceFolder } from "vscode";

import {SectionFinder} from "../../src/coverage-system/sectionfinder";
import {fakeConfig} from "../mocks/fakeConfig";
import { SectionFinder } from "../../src/coverage-system/sectionfinder";
import { fakeConfig } from "../mocks/fakeConfig";

const getWorkspaceFolder = workspace.getWorkspaceFolder;

suite("SectionFinder Tests", function() {
teardown(function() {
(workspace as any).getWorkspaceFolder = getWorkspaceFolder;
});

const fakeOutput = {
append: () => {},
Expand All @@ -15,14 +22,74 @@ suite("SectionFinder Tests", function() {
name: "fake",
show: () => {},
};
const filename = "test123.ts";
const title = `00-${filename}`;
const testFolderPath = "/path/to/test/folder";
const filePath = join(testFolderPath, "test123.ts");
const testWorkspaceFolder: WorkspaceFolder = {
index: 0,
name: basename(testFolderPath),
uri: Uri.file(testFolderPath),
};
const textEditor: TextEditor = {} as TextEditor;
(textEditor as any).document = {};
(textEditor as any).document.fileName = filePath;
const sectionMap: Map<string, Section> = new Map<string, Section>([
[
`${title}::${filePath}`,
{
file: filePath,
functions: {
details: [],
found: 0,
hit: 0,
},
lines: {
details: [],
found: 0,
hit: 0,
},
title,
},
],
]);
const sectionFinder: SectionFinder = new SectionFinder(
fakeConfig,
fakeOutput,
);
const createWorkspaceFolderMock = (workspaceFolder: WorkspaceFolder): (uri: Uri) => WorkspaceFolder => {
return (uri: Uri): WorkspaceFolder => {
if (uri.path === textEditor.document.fileName) {
return workspaceFolder;
}

throw new Error("Invalid filename given");
};
};

test("Should handle workspace folders without names", function(done) {
(workspace as any).getWorkspaceFolder = createWorkspaceFolderMock(testWorkspaceFolder);
const sections = sectionFinder.findSectionsForEditor(
textEditor,
sectionMap,
);

assert.equal(sections.length, 1);
return done();
});

test("Should handle workspace folders with names", function(done) {
const workspaceFolderWithName: WorkspaceFolder = {
...testWorkspaceFolder,
name: "Test Folder",
};
(workspace as any).getWorkspaceFolder = createWorkspaceFolderMock(workspaceFolderWithName);
const sections = sectionFinder.findSectionsForEditor(
textEditor,
sectionMap,
);

test("Should not throw an error @unit", function(done) {
const textEditor: TextEditor = {} as TextEditor;
(textEditor as any).document = {};
(textEditor as any).document.fileName = "test123.ts";
const sectionMap: Map<string, Section> = new Map<string, Section>();
const sectionFinder: SectionFinder = new SectionFinder(fakeConfig, fakeOutput);
sectionFinder.findSectionsForEditor(textEditor, sectionMap);
assert.equal(sections.length, 1);
return done();
});
});

0 comments on commit 73af483

Please sign in to comment.