From ddd11b5ca83981c84941949e1fc5316d14240ab2 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Tue, 10 Oct 2023 20:41:35 +0100 Subject: [PATCH 1/2] handle pest filters --- src/phpunit-command.js | 19 ++++++++-- src/test/suite/pest.test.ts | 40 +++++++++++----------- test/project-stub/tests/SamplePestTest.php | 9 +++++ 3 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 test/project-stub/tests/SamplePestTest.php diff --git a/src/phpunit-command.js b/src/phpunit-command.js index ea4538d..e1ba951 100644 --- a/src/phpunit-command.js +++ b/src/phpunit-command.js @@ -14,6 +14,7 @@ module.exports = class PhpUnitCommand { : false; this.lastOutput; + this.isPest = this._isPest(); } get output() { @@ -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}( .*)?$'` : ''); @@ -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; @@ -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) { diff --git a/src/test/suite/pest.test.ts b/src/test/suite/pest.test.ts index 9fe0cbe..ea68be6 100644 --- a/src/test/suite/pest.test.ts +++ b/src/test/suite/pest.test.ts @@ -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'); @@ -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') ); }); }); @@ -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'); @@ -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'); @@ -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'" ); }); }); @@ -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') @@ -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'); @@ -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') @@ -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') diff --git a/test/project-stub/tests/SamplePestTest.php b/test/project-stub/tests/SamplePestTest.php new file mode 100644 index 0000000..556d1f2 --- /dev/null +++ b/test/project-stub/tests/SamplePestTest.php @@ -0,0 +1,9 @@ + Date: Tue, 10 Oct 2023 20:52:22 +0100 Subject: [PATCH 2/2] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4de5e5c..b718f2b 100644 --- a/package.json +++ b/package.json @@ -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"