Skip to content

Commit

Permalink
Merge pull request #137 from joedixon/fix/pest-filter
Browse files Browse the repository at this point in the history
Handles Pest filters
  • Loading branch information
calebporzio authored Oct 10, 2023
2 parents 5da3769 + 9eb7c13 commit a920e47
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "better-phpunit",
"displayName": "Better PHPUnit",
"description": "A better PHPUnit test runner",
"version": "1.6.0",
"version": "1.6.1",
"publisher": "calebporzio",
"engines": {
"vscode": "^1.74.0"
Expand Down
19 changes: 16 additions & 3 deletions src/phpunit-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = class PhpUnitCommand {
: false;

this.lastOutput;
this.isPest = this._isPest();
}

get output() {
Expand All @@ -40,6 +41,10 @@ module.exports = class PhpUnitCommand {
}

get filter() {
if (this.isPest) {
return this.method ? ` --filter ${this.method}` : '';
}

return process.platform === "win32"
? (this.method ? ` --filter '^.*::${this.method}'` : '')
: (this.method ? ` --filter '^.*::${this.method}( .*)?$'` : '');
Expand Down Expand Up @@ -96,7 +101,10 @@ module.exports = class PhpUnitCommand {

while (line > 0) {
const lineText = vscode.window.activeTextEditor.document.lineAt(line).text;
const match = lineText.match(/^\s*(?:public|private|protected)?\s*function\s*(\w+)\s*\(.*$/);
const match = this.isPest ?
lineText.match(/^\s*(?:it|test)\(([^,)]+)/m) :
lineText.match(/^\s*(?:public|private|protected)?\s*function\s*(\w+)\s*\(.*$/);

if (match) {
method = match[1];
break;
Expand All @@ -107,14 +115,19 @@ module.exports = class PhpUnitCommand {
return method;
}

get isPest() {
_isPest() {
const start = Date.now();
const composerJson = findUp.sync('composer.json', { cwd: vscode.window.activeTextEditor.document.fileName });

if (!fs.existsSync(composerJson)) {
return false;
}

return fs.readFileSync(composerJson, 'utf8').includes('pestphp/pest');
const isPest = fs.readFileSync(composerJson, 'utf8').includes('pestphp/pest');
const end = Date.now();
console.log(`Checking for Pest: ${end - start}ms`)

return isPest;
}

_normalizePath(path) {
Expand Down
40 changes: 20 additions & 20 deletions src/test/suite/pest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ suite("Better PHPUnit Test Suite", function () {
});

test("Run file outside of method", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SamplePestTest.php'));
await vscode.window.showTextDocument(document, { selection: new vscode.Range(0, 0, 0, 0) });
await vscode.commands.executeCommand('better-phpunit.run');

Expand All @@ -70,53 +70,53 @@ suite("Better PHPUnit Test Suite", function () {
});

test("Run file", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SamplePestTest.php'));
await vscode.window.showTextDocument(document, { selection: new vscode.Range(7, 0, 7, 0) });
await vscode.commands.executeCommand('better-phpunit.run-file');

await timeout(waitToAssertInSeconds, () => {
assert.equal(
extension.getGlobalCommandInstance().output,
path.join(vscode.workspace.rootPath, '/vendor/bin/pest') + ' ' + path.join(vscode.workspace.rootPath, '/tests/SampleTest.php')
path.join(vscode.workspace.rootPath, '/vendor/bin/pest') + ' ' + path.join(vscode.workspace.rootPath, '/tests/SamplePestTest.php')
);
});
});

test("Run from within first method", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
await vscode.window.showTextDocument(document, { selection: new vscode.Range(7, 0, 7, 0) });
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SamplePestTest.php'));
await vscode.window.showTextDocument(document, { selection: new vscode.Range(3, 0, 3, 0) });
await vscode.commands.executeCommand('better-phpunit.run');

await timeout(waitToAssertInSeconds, () => {
assert.equal(
extension.getGlobalCommandInstance().method,
'test_first'
"'first'"
);
});
});

test("Run from within second method", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
await vscode.window.showTextDocument(document, { selection: new vscode.Range(12, 0, 12, 0) });
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SamplePestTest.php'));
await vscode.window.showTextDocument(document, { selection: new vscode.Range(7, 0, 7, 0) });
await vscode.commands.executeCommand('better-phpunit.run');

await timeout(waitToAssertInSeconds, () => {
assert.equal(
extension.getGlobalCommandInstance().method,
'test_second'
"'second'"
);
});
});

test("Detect filename", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SamplePestTest.php'));
await vscode.window.showTextDocument(document);
await vscode.commands.executeCommand('better-phpunit.run');

await timeout(waitToAssertInSeconds, () => {
assert.equal(
extension.getGlobalCommandInstance().file,
path.join(vscode.workspace.rootPath, '/tests/SampleTest.php')
path.join(vscode.workspace.rootPath, '/tests/SamplePestTest.php')
);
});
});
Expand All @@ -135,7 +135,7 @@ suite("Better PHPUnit Test Suite", function () {
});

test("Detect executable", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SamplePestTest.php'));
await vscode.window.showTextDocument(document);
await vscode.commands.executeCommand('better-phpunit.run');

Expand All @@ -150,7 +150,7 @@ suite("Better PHPUnit Test Suite", function () {
test("Fallback to default executable if composer.json not detected", async () => {
fs.renameSync(path.join(path.join(vscode.workspace.rootPath, 'composer.json')), path.join(path.join(vscode.workspace.rootPath, 'composer.json.pest')));

let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SamplePestTest.php'));
await vscode.window.showTextDocument(document);
await vscode.commands.executeCommand('better-phpunit.run');

Expand Down Expand Up @@ -211,14 +211,14 @@ suite("Better PHPUnit Test Suite", function () {
});

test("Check full command", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SamplePestTest.php'));
await vscode.window.showTextDocument(document, { selection: new vscode.Range(7, 0, 7, 0) });
await vscode.commands.executeCommand('better-phpunit.run');

await timeout(waitToAssertInSeconds, () => {
assert.equal(
extension.getGlobalCommandInstance().output,
path.join(vscode.workspace.rootPath, '/vendor/bin/pest ') + path.join(vscode.workspace.rootPath, '/tests/SampleTest.php') + " --filter '^.*::test_first( .*)?$'"
path.join(vscode.workspace.rootPath, '/vendor/bin/pest ') + path.join(vscode.workspace.rootPath, '/tests/SamplePestTest.php') + " --filter 'second'"
);
});
});
Expand All @@ -231,13 +231,13 @@ suite("Better PHPUnit Test Suite", function () {
await timeout(waitToAssertInSeconds, () => {
assert.equal(
extension.getGlobalCommandInstance().output,
path.join(vscode.workspace.rootPath, '/vendor/bin/pest ') + path.join(vscode.workspace.rootPath, '/tests/SampleTest.php') + " --filter '^.*::test_first( .*)?$'"
path.join(vscode.workspace.rootPath, '/vendor/bin/pest ') + path.join(vscode.workspace.rootPath, '/tests/SamplePestTest.php') + " --filter 'second'"
);
});
});

test("Run entire suite", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SamplePestTest.php'));
await vscode.window.showTextDocument(document, { selection: new vscode.Range(7, 0, 7, 0) });
await vscode.commands.executeCommand('better-phpunit.run-suite')

Expand All @@ -251,7 +251,7 @@ suite("Better PHPUnit Test Suite", function () {

test("Run entire suite with specified options", async () => {
await vscode.workspace.getConfiguration('better-phpunit').update('suiteSuffix', '--testsuite unit --coverage');
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SamplePestTest.php'));
await vscode.window.showTextDocument(document, { selection: new vscode.Range(7, 0, 7, 0) });
await vscode.commands.executeCommand('better-phpunit.run-suite');

Expand All @@ -266,7 +266,7 @@ suite("Better PHPUnit Test Suite", function () {
test("Run with commandSuffix config", async () => {
await vscode.workspace.getConfiguration('better-phpunit').update('commandSuffix', '--foo=bar');

let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SamplePestTest.php'));
await vscode.window.showTextDocument(document, { selection: new vscode.Range(7, 0, 7, 0) });
await vscode.commands.executeCommand('better-phpunit.run-suite')

Expand All @@ -281,7 +281,7 @@ suite("Better PHPUnit Test Suite", function () {
test("Run with pestBinary config", async () => {
await vscode.workspace.getConfiguration('better-phpunit').update('pestBinary', 'vendor/foo/bar');

let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SamplePestTest.php'));
await vscode.window.showTextDocument(document, { selection: new vscode.Range(7, 0, 7, 0) });
await vscode.commands.executeCommand('better-phpunit.run-suite')

Expand Down
9 changes: 9 additions & 0 deletions test/project-stub/tests/SamplePestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

test('first', function () {
expect(true).toBeTrue();
});

test('second', function () {
expect(true).toBeTrue();
});

0 comments on commit a920e47

Please sign in to comment.