Skip to content

Commit

Permalink
Merge pull request #136 from joedixon/pest-support
Browse files Browse the repository at this point in the history
Adds Pest support
  • Loading branch information
calebporzio authored Oct 10, 2023
2 parents 81447bd + 9c7efe9 commit 22661a5
Show file tree
Hide file tree
Showing 17 changed files with 2,408 additions and 2,829 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Tests
on: [push, pull_request]
jobs:
build:
strategy:
matrix:
os: [macos-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 16.x
- run: npm install
- run: xvfb-run -a npm test
if: runner.os == 'Linux'
- run: npm test
if: runner.os != 'Linux'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
.vscode-test/
.vsix
.DS_Store
out
4,631 changes: 1,880 additions & 2,751 deletions package-lock.json

Large diffs are not rendered by default.

35 changes: 26 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "1.5.3",
"publisher": "calebporzio",
"engines": {
"vscode": "^1.17.0"
"vscode": "^1.74.0"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -76,6 +76,14 @@
"default": null,
"description": "A custom phpunit binary. Ex: 'phpunit', '/usr/local/bin/phpunit'"
},
"better-phpunit.pestBinary": {
"type": [
"string",
"null"
],
"default": null,
"description": "A custom Pest binary. Ex: 'pest', '/usr/local/bin/pest'"
},
"better-phpunit.xmlConfigFilepath": {
"type": [
"string",
Expand Down Expand Up @@ -203,16 +211,25 @@
]
},
"scripts": {
"postinstall": "node ./node_modules/vscode/bin/install",
"test": "node ./node_modules/vscode/bin/test"
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"pretest": "npm run compile",
"test": "node ./out/test/runTest.js"
},
"devDependencies": {
"@types/mocha": "^2.2.42",
"@types/node": "^7.10.2",
"eslint": "^4.6.1",
"mocha": "^4.1.0",
"typescript": "^2.5.2",
"vscode": "^1.1.22"
"@types/glob": "^7.1.1",
"@types/mocha": "^10.0.1",
"@types/node": "^20.8.4",
"@types/vscode": "^1.74.0",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"@vscode/test-electron": "^2.3.0",
"eslint": "^8.26.0",
"glob": "^7.1.4",
"mocha": "^10.2.0",
"source-map-support": "^0.5.12",
"typescript": "^5.2.2"
},
"dependencies": {
"find-up": "^2.1.0"
Expand Down
1 change: 0 additions & 1 deletion src/extension.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const vscode = require('vscode');
const assert = require('assert');
const PhpUnitCommand = require('./phpunit-command');
const RemotePhpUnitCommand = require('./remote-phpunit-command.js');
const DockerPhpUnitCommand = require('./docker-phpunit-command.js');
Expand Down
23 changes: 19 additions & 4 deletions src/phpunit-command.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const findUp = require('find-up');
const vscode = require('vscode');
const path = require('path');
const fs = require('fs');

module.exports = class PhpUnitCommand {
constructor(options) {
Expand Down Expand Up @@ -67,13 +68,17 @@ module.exports = class PhpUnitCommand {
}

get binary() {
if (vscode.workspace.getConfiguration('better-phpunit').get('phpunitBinary')) {
return vscode.workspace.getConfiguration('better-phpunit').get('phpunitBinary')
const binaryKey = this.isPest ? 'pestBinary' : 'phpunitBinary';

if (vscode.workspace.getConfiguration('better-phpunit').get(binaryKey)) {
return vscode.workspace.getConfiguration('better-phpunit').get(binaryKey)
}

const binary = this.isPest ? 'pest' : 'phpunit';

return this.subDirectory
? this._normalizePath(path.join(this.subDirectory, 'vendor', 'bin', 'phpunit'+this.windowsSuffix))
: this._normalizePath(path.join(vscode.workspace.rootPath, 'vendor', 'bin', 'phpunit'+this.windowsSuffix));
? this._normalizePath(path.join(this.subDirectory, 'vendor', 'bin', binary+this.windowsSuffix))
: this._normalizePath(path.join(vscode.workspace.rootPath, 'vendor', 'bin', binary+this.windowsSuffix));
}

get subDirectory() {
Expand Down Expand Up @@ -102,6 +107,16 @@ module.exports = class PhpUnitCommand {
return method;
}

get isPest() {
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');
}

_normalizePath(path) {
return path
.replace(/\\/g, '/') // Convert backslashes from windows paths to forward slashes, otherwise the shell will ignore them.
Expand Down
24 changes: 24 additions & 0 deletions src/test/runTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as path from 'path';

import { runTests } from '@vscode/test-electron';

async function main() {
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, '../../');

// The path to the extension test script
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, './suite/index');
const testWorkspace = path.resolve(__dirname, '../../test/project-stub');

// Download VS Code, unzip it and run the integration test
await runTests({ extensionDevelopmentPath, extensionTestsPath, launchArgs: [testWorkspace], });
} catch (err) {
console.error('Failed to run tests');
process.exit(1);
}
}

main();
49 changes: 27 additions & 22 deletions test/extension.test.js → src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
const assert = require('assert');
import * as assert from 'assert';
import { beforeEach, afterEach } from 'mocha';
import * as path from 'path';

const vscode = require('vscode');
const path = require('path');
const extension = require('../src/extension');
const extension = require('../../../src/extension');

const waitToAssertInSeconds = 5;

// This is a little helper function to promisify setTimeout, so we can "await" setTimeout.
function timeout(seconds, callback) {
function timeout(seconds: any, callback: any) {
return new Promise(resolve => {
setTimeout(() => {
callback();
resolve();
resolve('');
}, seconds * waitToAssertInSeconds);
});
}

describe("Better PHPUnit Test Suite", function () {
suite("Better PHPUnit Test Suite", function () {
beforeEach(async () => {
// Reset the test/project-stub/.vscode/settings.json settings for each test.
// This allows us to test config options in tests and not harm other tests.
await vscode.workspace.getConfiguration('better-phpunit').update('commandSuffix', null);
await vscode.workspace.getConfiguration('better-phpunit').update('phpunitBinary', null);
await vscode.workspace.getConfiguration('better-phpunit').update('pestBinary', null);
await vscode.workspace.getConfiguration("better-phpunit").update("ssh.enable", false);
await vscode.workspace.getConfiguration("better-phpunit").update("xmlConfigFilepath", null);
await vscode.workspace.getConfiguration("better-phpunit").update("suiteSuffix", null);
Expand All @@ -31,13 +35,14 @@ describe("Better PHPUnit Test Suite", function () {
// This allows us to test config options in tests and not harm other tests.
await vscode.workspace.getConfiguration('better-phpunit').update('commandSuffix', null);
await vscode.workspace.getConfiguration('better-phpunit').update('phpunitBinary', null);
await vscode.workspace.getConfiguration('better-phpunit').update('pestBinary', null);
await vscode.workspace.getConfiguration("better-phpunit").update("ssh.enable", false);
await vscode.workspace.getConfiguration("better-phpunit").update("xmlConfigFilepath", null);
await vscode.workspace.getConfiguration("better-phpunit").update("suiteSuffix", null);
await vscode.workspace.getConfiguration("better-phpunit").update("docker.enable", false);
});

it("Run file outside of method", async () => {
test("Run file outside of method", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
await vscode.window.showTextDocument(document);
await vscode.commands.executeCommand('better-phpunit.run');
Expand All @@ -47,7 +52,7 @@ describe("Better PHPUnit Test Suite", function () {
});
});

it("Run file", async () => {
test("Run file", 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) });
await vscode.commands.executeCommand('better-phpunit.run-file');
Expand All @@ -60,7 +65,7 @@ describe("Better PHPUnit Test Suite", function () {
});
});

it("Run from within first method", async () => {
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) });
await vscode.commands.executeCommand('better-phpunit.run');
Expand All @@ -73,7 +78,7 @@ describe("Better PHPUnit Test Suite", function () {
});
});

it("Run from within second method", async () => {
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) });
await vscode.commands.executeCommand('better-phpunit.run');
Expand All @@ -86,7 +91,7 @@ describe("Better PHPUnit Test Suite", function () {
});
});

it("Detect filename", async () => {
test("Detect filename", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
await vscode.window.showTextDocument(document);
await vscode.commands.executeCommand('better-phpunit.run');
Expand All @@ -99,7 +104,7 @@ describe("Better PHPUnit Test Suite", function () {
});
});

it("Detect filename with a space", async () => {
test("Detect filename with a space", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'File With Spaces Test.php'));
await vscode.window.showTextDocument(document);
await vscode.commands.executeCommand('better-phpunit.run');
Expand All @@ -112,7 +117,7 @@ describe("Better PHPUnit Test Suite", function () {
});
});

it("Detect executable", async () => {
test("Detect executable", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
await vscode.window.showTextDocument(document);
await vscode.commands.executeCommand('better-phpunit.run');
Expand All @@ -125,7 +130,7 @@ describe("Better PHPUnit Test Suite", function () {
});
});

it("Detect executable in sub-directory", async () => {
test("Detect executable in sub-directory", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'sub-directory', 'tests', 'SampleTest.php'));
await vscode.window.showTextDocument(document);
await vscode.commands.executeCommand('better-phpunit.run');
Expand All @@ -138,7 +143,7 @@ describe("Better PHPUnit Test Suite", function () {
});
});

it("Detect configuration in sub-directory", async () => {
test("Detect configuration in sub-directory", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'sub-directory', 'tests', 'SampleTest.php'));
await vscode.window.showTextDocument(document);
await vscode.commands.executeCommand('better-phpunit.run');
Expand All @@ -151,7 +156,7 @@ describe("Better PHPUnit Test Suite", function () {
});
});

it("Uses configuration found in path supplied in settings", async () => {
test("Uses configuration found in path supplied in settings", async () => {
await vscode.workspace.getConfiguration('better-phpunit').update('xmlConfigFilepath', '/var/log/phpunit.xml');
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'sub-directory', 'tests', 'SampleTest.php'));
await vscode.window.showTextDocument(document);
Expand All @@ -165,7 +170,7 @@ describe("Better PHPUnit Test Suite", function () {
});
});

it("Check full command", async () => {
test("Check full command", 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) });
await vscode.commands.executeCommand('better-phpunit.run');
Expand All @@ -178,7 +183,7 @@ describe("Better PHPUnit Test Suite", function () {
});
});

it("Run previous", async () => {
test("Run previous", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'OtherTest.php'));
await vscode.window.showTextDocument(document, { selection: new vscode.Range(12, 0, 12, 0) });
await vscode.commands.executeCommand('better-phpunit.run-previous');
Expand All @@ -191,7 +196,7 @@ describe("Better PHPUnit Test Suite", function () {
});
});

it("Run entire suite", async () => {
test("Run entire suite", 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) });
await vscode.commands.executeCommand('better-phpunit.run-suite')
Expand All @@ -204,7 +209,7 @@ describe("Better PHPUnit Test Suite", function () {
});
});

it("Run entire suite with specified options", async () => {
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'));
await vscode.window.showTextDocument(document, { selection: new vscode.Range(7, 0, 7, 0) });
Expand All @@ -218,7 +223,7 @@ describe("Better PHPUnit Test Suite", function () {
});
});

it("Run with commandSuffix config", async () => {
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'));
Expand All @@ -233,7 +238,7 @@ describe("Better PHPUnit Test Suite", function () {
});
});

it("Run with phpunitBinary config", async () => {
test("Run with phpunitBinary config", async () => {
await vscode.workspace.getConfiguration('better-phpunit').update('phpunitBinary', 'vendor/foo/bar');

let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
Expand Down
37 changes: 37 additions & 0 deletions src/test/suite/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as path from 'path';
import * as Mocha from 'mocha';
import * as glob from 'glob';

export function run(): Promise<void> {
// Create the mocha test
const mocha = new Mocha({
ui: 'tdd'
});

const testsRoot = path.resolve(__dirname, '..');

return new Promise((c, e) => {
glob('**/**.test.js', { cwd: testsRoot }, (err, files) => {
if (err) {
return e(err);
}

// Add files to the test suite
files.forEach(f => mocha.addFile(path.resolve(testsRoot, f)));

try {
// Run the mocha test
mocha.run(failures => {
if (failures > 0) {
e(new Error(`${failures} tests failed.`));
} else {
c();
}
});
} catch (err) {
console.error(err);
e(err);
}
});
});
}
Loading

0 comments on commit 22661a5

Please sign in to comment.