Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #76 from kavod-io/exclude-test-files-using-pattern
Browse files Browse the repository at this point in the history
Exclude test files using pattern
  • Loading branch information
rossknudsen authored Jul 30, 2020
2 parents 7cb77e4 + 9a3f85b commit e3c50ce
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 17 deletions.
8 changes: 8 additions & 0 deletions src/ProjectManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class ProjectManager {
suite: this.workspaceTestState,
type: "projectAppUpdated",
});

this.log.info(`Application file changed: ${JSON.stringify(event)}`)
break;

case "Test":
Expand All @@ -102,6 +104,8 @@ class ProjectManager {
testEvent: event,
type: "projectTestsUpdated",
});

this.log.info(`Test file changed: ${JSON.stringify(event)}`)
break;
}
}
Expand All @@ -126,6 +130,8 @@ class ProjectManager {
suite: this.workspaceTestState,
type: "projectAdded",
});

this.log.info(`New project added: ${event.config} ${newProject}`)
break;

case "removed":
Expand All @@ -138,6 +144,8 @@ class ProjectManager {
suite: this.workspaceTestState,
type: "projectRemoved",
});

this.log.info(`Project removed: ${event}`)
break;
}
}
Expand Down
33 changes: 25 additions & 8 deletions src/TestParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import * as mm from "micromatch";
import * as path from "path";
import * as vscode from "vscode";
import { Log } from "vscode-test-adapter-util";
import { lowerCaseDriveLetter } from "./helpers/mapAssertionResultToTestId";
import { cancellationTokenNone, Matcher, TestFileParseResult } from "./types";
import { convertErrorToString } from './utils';
import { convertErrorToString } from "./utils";

/**
* Glob patterns to globally ignore when searching for tests.
Expand Down Expand Up @@ -172,14 +173,30 @@ class TestParser {
* @param settings The Jest settings.
*/
const createMatcher = (settings: JestSettings): Matcher => {
// TODO what to do if there is more than one config?...
return value => {
return settings.configs.some(c => {
// first check if there are any ignored paths.
const isIgnored = c.testPathIgnorePatterns
?.map(lowerCaseDriveLetter)
?.some(p => {
const matches = value.match(p);
return matches && matches.length > 0;
});

if (isIgnored) {
return false;
}

if (settings?.configs?.length > 0 && settings.configs[0].testRegex?.length > 0) {
const regex = new RegExp(settings.configs[0].testRegex[0], process.platform === "win32" ? "i" : undefined);
return value => regex.test(value);
} else {
return value => mm.any(value, settings.configs[0].testMatch, { nocase: process.platform === "win32" });
}
if (c.testRegex?.length > 0) {
return c.testRegex.some(r => {
const regex = new RegExp(r, process.platform === "win32" ? "i" : undefined);
return regex.test(value);
});
}

return mm.any(value, c.testMatch, { nocase: process.platform === "win32" });
});
};
};

export { TestParser as default, createMatcher };
45 changes: 39 additions & 6 deletions src/__tests__/TestParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ const getTestMatchData = (): Array<[string, string, boolean]> => {
["C:\\testfile.test.js", "C:/**/*.test.js", true],
];
}
return [
["/folder/app.test.js", "**/*.test.js", true]
];
return [["/folder/app.test.js", "**/*.test.js", true]];
};

const getTestRegexData = (): Array<[string, string, boolean]> => {
Expand All @@ -29,9 +27,7 @@ const getTestRegexData = (): Array<[string, string, boolean]> => {
["C:\\testfile.test.js", "C:\\\\.*\\.test\\.[tj]sx?", true],
];
}
return [
["/folder/app.test.js", ".*/.*\\.test\\.[tj]sx?", true]
];
return [["/folder/app.test.js", ".*/.*\\.test\\.[tj]sx?", true]];
};

describe("Matcher tests", () => {
Expand All @@ -50,6 +46,43 @@ describe("Matcher tests", () => {
expect(matcher(filePath)).toBe(expectedToMatch);
},
);

test.each`
ignorePattern | filePath | expectedResult
${"test"} | ${"c:\\folder\\testfile.test.js"} | ${false}
${"file"} | ${"c:\\folder\\testfile.test.js"} | ${false}
${"folder"} | ${"c:\\folder\\testfile.test.js"} | ${false}
${"c:\\\\"} | ${"c:\\folder\\testfile.test.js"} | ${false}
${"c:\\\\folder\\\\testfile.test.js"} | ${"c:\\folder\\testfile.test.js"} | ${false}
${"c:\\\\folder"} | ${"c:\\folder\\testfile.test.js"} | ${false}
${"c:\\\\another_folder"} | ${"c:\\folder\\testfile.test.js"} | ${true}
${"another_string"} | ${"c:\\folder\\testfile.test.js"} | ${true}
`(
`Given an ignore pattern of '$ignorePattern' and a file path of '$filePath'
When the matcher is invoked
Then the result is $expectedResult`,
({
ignorePattern,
filePath,
expectedResult,
}: {
ignorePattern: string;
filePath: string;
expectedResult: boolean;
}) => {
const config = createConfig({ testRegex: [".*.js$"] });
config.configs[0].rootDir = "c:\\";

// run the matcher before to confirm that it works without the ignore pattern.
let matcher = createMatcher(config);
expect(matcher(filePath)).toBe(true);

// now include the ignore pattern.
config.configs[0].testPathIgnorePatterns = [ignorePattern];
matcher = createMatcher(config);
expect(matcher(filePath)).toBe(expectedResult);
},
);
});

const createConfig = ({ testRegex, testMatch }: { testRegex?: string[]; testMatch?: string[] }): JestSettings => {
Expand Down
2 changes: 2 additions & 0 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export default class JestTestAdapter implements TestAdapter {
) {
this.jestManager = new JestManager();

this.log.info(`Created adapter for workspace: ${workspace.uri.fsPath} with options: ${JSON.stringify(options)}`)

this.disposables.push(this.testsEmitter);
this.disposables.push(this.testStatesEmitter);
this.disposables.push(this.retireEmitter);
Expand Down
4 changes: 3 additions & 1 deletion src/repo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const getRepoParser = async (workspaceRoot: string, log: Log, pathToJest: string
repoParsers.map(async p => ({ parser: p, match: await p.isMatch() })),
).then(x => x.filter(z => z.match).map(z => z.parser));

return matchingParsers[0] ?? null;
const parser = matchingParsers[0] ?? null;
log.info(`Selected parser: ${parser.type}`)
return parser;
};

export { ProjectConfig, RepoParser, getRepoParser };
4 changes: 2 additions & 2 deletions src/utils/convertErrorToString.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { inspect } from "util";

const convertErrorToString = (error: Error): string => {
return inspect(error, false, 2, true);
const convertErrorToString = (error: Error, color = false): string => {
return inspect(error, false, 2, color);
};

export { convertErrorToString };

0 comments on commit e3c50ce

Please sign in to comment.