diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index e9af9149ca..b6a7bc4ea2 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -18,7 +18,8 @@ Thank you for your interest in contributing to PHP_CodeSniffer! * [Finding Something to Work on](#finding-something-to-work-on) * [Getting Started](#getting-started) * [While Working on a Patch](#while-working-on-a-patch) - * [Writing Tests](#writing-tests) + * [Writing Unit/Integration Tests](#writing-unitintegration-tests) + * [Writing End-to-End Tests](#writing-end-to-end-tests) * [Submitting Your Pull Request](#submitting-your-pull-request) * [Licensing](#licensing) @@ -268,7 +269,7 @@ To help you with this, a number of convenience scripts are available: N.B.: You can ignore any skipped tests as these are for external tools. -### Writing Tests +### Writing Unit/Integration Tests Tests for the PHP_CodeSniffer engine can be found in the `tests/Core` directory. Tests for individual sniffs can be found in the `src/Standards/[StandardName]/Tests/[Category]/` directory. @@ -376,7 +377,7 @@ To run the tests specific to the use of `PHP_CODESNIFFER_CBF === true`: vendor/bin/phpunit --group CBF --exclude-group nothing ``` -#### Other notes about writing tests +#### Other notes about writing unit/integration tests * The `Config` class uses a number of static properties and can have a performance impact on the tests too. To get round both these issues, use the `ConfigDouble` class instead. @@ -387,6 +388,24 @@ To run the tests specific to the use of `PHP_CODESNIFFER_CBF === true`: * When using data providers, define them immediately below the corresponding test method. * When a test method has only one data provider, it is considered best practice to closely couple the test and data provider methods via their names. I.e. the data provider's name should match the test method name, replacing the "test" prefix with "data". For example, the data provider for a method named `testSomething()` should be `dataSomething()`. + +### Writing End-to-End Tests + +Bash-based end-to-end tests can be written using the [Bashunit](https://bashunit.typeddevs.com/) test tooling. + +To install bashunit, follow the [installation guide](https://bashunit.typeddevs.com/installation). + +You can then run the bashunit tests on Linux/Mac/WSL, like so: +```bash +./lib/bashunit -p tests/EndToEnd +``` + +> Note: these tests will not run in the Windows native CMD shell. When on Windows, either use WSL or use the "git bash" shell. + +When writing end-to-end tests, please use fixtures for the "files under scan" to make the tests stable. +These fixtures can be placed in the `tests/EndToEnd/Fixtures` subdirectory. + + ### Submitting Your Pull Request Some guidelines for submitting pull requests (PRs) and improving the chance that your PR will be merged: diff --git a/.github/workflows/end-to-end-tests.yml b/.github/workflows/end-to-end-tests.yml new file mode 100644 index 0000000000..ff9efd3729 --- /dev/null +++ b/.github/workflows/end-to-end-tests.yml @@ -0,0 +1,60 @@ +name: E2E Tests + +on: + # Run on pushes to `master`/`4.0` and on all pull requests. + # Prevent the build from running when there are only irrelevant changes. + push: + branches: + - master + - 4.0 + tags: + - '**' + paths-ignore: + - '**.md' + pull_request: + # Allow manually triggering the workflow. + workflow_dispatch: + +jobs: + bash-tests: + # Cancels all previous runs of this particular job for the same branch that have not yet completed. + concurrency: + # The concurrency group contains the workflow name, job name, job index and the branch name. + group: ${{ github.workflow }}-${{ github.job }}-${{ strategy.job-index }}-${{ github.ref }} + cancel-in-progress: true + + runs-on: 'ubuntu-latest' + + strategy: + matrix: + php: ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5'] + + # yamllint disable-line rule:line-length + name: "E2E PHP: ${{ matrix.php }}" + + continue-on-error: ${{ matrix.php == '8.5' }} + + steps: + - name: Prepare git to leave line endings alone + run: git config --global core.autocrlf input + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + ini-values: "error_reporting=-1, display_errors=On" + coverage: none + + - name: "Install bashunit" + shell: bash + run: | + curl -s https://bashunit.typeddevs.com/install.sh > install.sh + chmod +x install.sh + ./install.sh + + - name: "Run bashunit tests" + shell: bash + run: "./lib/bashunit -p tests/EndToEnd" diff --git a/.gitignore b/.gitignore index 3b1c9e6084..3152b53c61 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ composer.lock phpstan.neon /node_modules/ +/tests/EndToEnd/Fixtures/*.fixed +/lib/ diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 0fd2f38d0f..fdb9235f91 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -11,6 +11,7 @@ */src/Standards/*/Tests/*\.(inc|css|js)$ */tests/Core/*/*\.(inc|css|js)$ */tests/Core/*/Fixtures/*\.php$ + */tests/EndToEnd/Fixtures/*\.inc$ diff --git a/src/Runner.php b/src/Runner.php index d527ea575e..7b299c7f84 100644 --- a/src/Runner.php +++ b/src/Runner.php @@ -764,7 +764,7 @@ public function processFile($file) * The reporting information returned by each child process is merged * into the main reporter class. * - * @param array $childProcs An array of child processes to wait for. + * @param array $childProcs An array of child processes to wait for. * * @return bool */ @@ -777,7 +777,8 @@ private function processChildProcs($childProcs) while (count($childProcs) > 0) { $pid = pcntl_waitpid(0, $status); - if ($pid <= 0) { + if ($pid <= 0 || isset($childProcs[$pid]) === false) { + // No child or a child with an unmanaged PID was returned. continue; } diff --git a/tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc b/tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc new file mode 100644 index 0000000000..700fe7a7b0 --- /dev/null +++ b/tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc @@ -0,0 +1,18 @@ + + + The coding standard for end to end tests. + + + + . + + + + + + diff --git a/tests/EndToEnd/phpcbf_test.sh b/tests/EndToEnd/phpcbf_test.sh new file mode 100644 index 0000000000..3886832342 --- /dev/null +++ b/tests/EndToEnd/phpcbf_test.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +function tear_down() { + rm -r tests/EndToEnd/Fixtures/*.fixed +} + +function test_phpcbf_is_working() { + OUTPUT="$(bin/phpcbf --no-cache --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc tests/EndToEnd/Fixtures/ClassTwoWithoutStyleError.inc)" + + assert_successful_code + assert_contains "No violations were found" "$OUTPUT" +} + +function test_phpcbf_is_working_in_parallel() { + OUTPUT="$(bin/phpcbf --no-cache --parallel=2 --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc tests/EndToEnd/Fixtures/ClassTwoWithoutStyleError.inc)" + + assert_successful_code + assert_contains "No violations were found" "$OUTPUT" +} + +function test_phpcbf_returns_error_on_issues() { + OUTPUT="$(bin/phpcbf --no-colors --no-cache --suffix=.fixed --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassWithStyleError.inc)" + assert_exit_code 1 + + assert_contains "F 1 / 1 (100%)" "$OUTPUT" + assert_contains "A TOTAL OF 1 ERROR WERE FIXED IN 1 FILE" "$OUTPUT" +} + +function test_phpcbf_bug_1112() { + # See https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1112 + if [[ "$(uname)" == "Darwin" ]]; then + # Perform some magic with `& fg` to prevent the processes from turning into a background job. + assert_successful_code "$(bash -ic 'bash --init-file <(echo "echo \"Subprocess\"") -c "bin/phpcbf --no-cache --parallel=2 --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc tests/EndToEnd/Fixtures/ClassTwoWithoutStyleError.inc" & fg')" + else + # This is not needed on Linux / GitHub Actions + assert_successful_code "$(bash -ic 'bash --init-file <(echo "echo \"Subprocess\"") -c "bin/phpcbf --no-cache --parallel=2 --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc tests/EndToEnd/Fixtures/ClassTwoWithoutStyleError.inc"')" + fi +} diff --git a/tests/EndToEnd/phpcs_test.sh b/tests/EndToEnd/phpcs_test.sh new file mode 100644 index 0000000000..3bdf41708d --- /dev/null +++ b/tests/EndToEnd/phpcs_test.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +function test_phpcs_is_working() { + assert_successful_code "$(bin/phpcs --no-cache --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc tests/EndToEnd/Fixtures/ClassTwoWithoutStyleError.inc)" +} + +function test_phpcs_is_working_in_parallel() { + assert_successful_code "$(bin/phpcs --no-cache --parallel=2 --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc tests/EndToEnd/Fixtures/ClassTwoWithoutStyleError.inc)" +} + +function test_phpcs_returns_error_on_issues() { + OUTPUT="$(bin/phpcs --no-colors --no-cache --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassWithStyleError.inc)" + assert_exit_code 2 + + assert_contains "E 1 / 1 (100%)" "$OUTPUT" + assert_contains "FOUND 1 ERROR AFFECTING 1 LINE" "$OUTPUT" +} + +function test_phpcs_bug_1112() { + # See https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1112 + if [[ "$(uname)" == "Darwin" ]]; then + # Perform some magic with `& fg` to prevent the processes from turning into a background job. + assert_successful_code "$(bash -ic 'bash --init-file <(echo "echo \"Subprocess\"") -c "bin/phpcs --no-cache --parallel=2 --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc tests/EndToEnd/Fixtures/ClassTwoWithoutStyleError.inc" & fg')" + else + # This is not needed on Linux / GitHub Actions + assert_successful_code "$(bash -ic 'bash --init-file <(echo "echo \"Subprocess\"") -c "bin/phpcs --no-cache --parallel=2 --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc tests/EndToEnd/Fixtures/ClassTwoWithoutStyleError.inc"')" + fi +}