diff --git a/README.md b/README.md index b19670d7..400e2b2c 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ Setup PHP with required extensions, php.ini configuration, code-coverage support - [Matrix Setup](#matrix-setup) - [Experimental Setup](#experimental-setup) - [Cache dependencies](#cache-dependencies) + - [Problem Matchers](#problem-matchers) - [Examples](#examples) - [License](#scroll-license) - [Contributions](#1-contributions) @@ -215,6 +216,15 @@ You can persist composer's internal cache directory using the [`action/cache`](h run: composer install --prefer-dist ``` +### Problem Matchers + +You can setup problem matchers for your `PHPUnit` output. This will scan the errors in your tests and surface that information prominently in the GitHub Actions UI by creating annotations and log file decorations. + +```yaml +- name: Setup Problem Matchers for PHPUnit + run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" +``` + ### Examples Examples for setting up this GitHub Action with different PHP Frameworks/Packages. diff --git a/__tests__/install.test.ts b/__tests__/install.test.ts index 5dd5dcab..9842a38e 100644 --- a/__tests__/install.test.ts +++ b/__tests__/install.test.ts @@ -1,6 +1,4 @@ import * as install from '../src/install'; -import * as matchers from '../src/matchers'; -import * as path from 'path'; /** * Mock install.ts @@ -152,34 +150,4 @@ describe('Install', () => { expect(script).toContain('set coverage driver'); expect(script).toContain('sh script.sh 7.3 ' + __dirname); }); - - describe('Matchers', () => { - let originalLogMethod: any; - let outputData: any[] = []; - - beforeAll(() => { - originalLogMethod = console.log; - console['log'] = jest.fn(inputs => outputData.push(inputs)); - }); - - beforeEach(() => { - outputData = []; - }); - - afterAll(() => { - console['log'] = originalLogMethod; - }); - - it('Add matchers', async () => { - matchers.addMatchers(); - - expect(outputData).toEqual([ - `##[add-matcher]${path.join( - __dirname, - '..', - '.github/matchers/phpunit.json' - )}` - ]); - }); - }); }); diff --git a/__tests__/matchers.test.ts b/__tests__/matchers.test.ts new file mode 100644 index 00000000..23e74830 --- /dev/null +++ b/__tests__/matchers.test.ts @@ -0,0 +1,34 @@ +import * as io from '@actions/io'; +import * as path from 'path'; +import * as fs from 'fs'; +import * as matchers from '../src/matchers'; + +async function cleanup(path: string): Promise { + fs.unlink(path, error => { + if (error) { + console.log(error); + } + }); +} + +jest.mock('@actions/io'); + +describe('Matchers', () => { + it('Add matchers', async () => { + process.env['RUNNER_TOOL_CACHE'] = __dirname; + await matchers.addMatchers(); + const spy = jest.spyOn(io, 'cp'); + expect(spy).toHaveBeenCalledTimes(1); + }); + + it('Test Regex', async () => { + const regex1 = /^\d+\)\s.*$/; + const regex2 = /^(.*)$/; + const regex3 = /^\s*$/; + const regex4 = /^(.*):(\d+)$/; + expect(regex1.test('1) Tests\\Test::it_tests')).toBe(true); + expect(regex2.test('Failed asserting that false is true')).toBe(true); + expect(regex3.test('\n')).toBe(true); + expect(regex4.test('/path/to/file.php:42')).toBe(true); + }); +}); diff --git a/dist/index.js b/dist/index.js index 97611682..e943c162 100644 --- a/dist/index.js +++ b/dist/index.js @@ -932,6 +932,46 @@ class ExecState extends events.EventEmitter { } //# sourceMappingURL=toolrunner.js.map +/***/ }), + +/***/ 86: +/***/ (function(__unusedmodule, exports, __webpack_require__) { + +"use strict"; + +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const path = __importStar(__webpack_require__(622)); +const utils = __importStar(__webpack_require__(163)); +const io = __importStar(__webpack_require__(1)); +/** + * Cache json files for problem matchers + */ +function addMatchers() { + return __awaiter(this, void 0, void 0, function* () { + const config_path = path.join(__dirname, '..', 'src', 'configs', 'phpunit.json'); + const runner_dir = yield utils.getInput('RUNNER_TOOL_CACHE', false); + yield io.cp(config_path, runner_dir); + }); +} +exports.addMatchers = addMatchers; + + /***/ }), /***/ 87: @@ -1795,6 +1835,7 @@ const config = __importStar(__webpack_require__(641)); const coverage = __importStar(__webpack_require__(635)); const extensions = __importStar(__webpack_require__(911)); const utils = __importStar(__webpack_require__(163)); +const matchers = __importStar(__webpack_require__(86)); /** * Build the script * @@ -1810,6 +1851,7 @@ function build(filename, version, os_version) { const ini_values_csv = (yield utils.getInput('ini-values', false)) || (yield utils.getInput('ini-values-csv', false)); const coverage_driver = yield utils.getInput('coverage', false); + const setup_matchers = yield utils.getInput('matchers', false); let script = yield utils.readScript(filename, version, os_version); if (extension_csv) { script += yield extensions.addExtension(extension_csv, version, os_version); @@ -1851,6 +1893,7 @@ function run() { yield exec_1.exec('pwsh ' + script_path + ' -version ' + version + ' -dir ' + __dirname); break; } + yield matchers.addMatchers(); } catch (error) { core.setFailed(error.message); diff --git a/.github/matchers/phpunit.json b/src/configs/phpunit.json similarity index 100% rename from .github/matchers/phpunit.json rename to src/configs/phpunit.json diff --git a/src/install.ts b/src/install.ts index 6b88d3d7..b36434d8 100644 --- a/src/install.ts +++ b/src/install.ts @@ -4,7 +4,7 @@ import * as config from './config'; import * as coverage from './coverage'; import * as extensions from './extensions'; import * as utils from './utils'; -import {addMatchers} from './matchers'; +import * as matchers from './matchers'; /** * Build the script @@ -26,6 +26,7 @@ export async function build( (await utils.getInput('ini-values', false)) || (await utils.getInput('ini-values-csv', false)); const coverage_driver: string = await utils.getInput('coverage', false); + const setup_matchers: string = await utils.getInput('matchers', false); let script: string = await utils.readScript(filename, version, os_version); if (extension_csv) { @@ -69,8 +70,7 @@ export async function run(): Promise { ); break; } - - addMatchers(); + await matchers.addMatchers(); } catch (error) { core.setFailed(error.message); } diff --git a/src/matchers.ts b/src/matchers.ts index 18116ccb..500f2101 100644 --- a/src/matchers.ts +++ b/src/matchers.ts @@ -1,10 +1,18 @@ import * as path from 'path'; +import * as utils from './utils'; +import * as io from '@actions/io'; /** - * Add matches using the Actions Toolkit problem matchers syntax - * https://github.com/actions/toolkit/blob/master/docs/problem-matchers.md + * Cache json files for problem matchers */ -export function addMatchers(): void { - const matchersPath = path.join(__dirname, '..', '.github/matchers'); - console.log(`##[add-matcher]${path.join(matchersPath, 'phpunit.json')}`); +export async function addMatchers(): Promise { + const config_path = path.join( + __dirname, + '..', + 'src', + 'configs', + 'phpunit.json' + ); + const runner_dir: string = await utils.getInput('RUNNER_TOOL_CACHE', false); + await io.cp(config_path, runner_dir); }