Skip to content

Commit 054315d

Browse files
committed
feat: Introduce bashunit test suite
This introduces the bashunit test suite as requested by @jrfnl. This tests both a simple happy and a simple unhappy flow, as well as a test case for #1112.
1 parent 1408155 commit 054315d

File tree

9 files changed

+153
-1
lines changed

9 files changed

+153
-1
lines changed

.github/workflows/test.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,21 @@ jobs:
247247
env:
248248
PHP_CODESNIFFER_CBF: '1'
249249

250+
- name: "Install bashunit"
251+
if: ${{ matrix.custom_ini == false && matrix.os == 'ubuntu-latest' }}
252+
run: |
253+
curl -s https://bashunit.typeddevs.com/install.sh > install.sh
254+
chmod +x install.sh
255+
./install.sh
256+
257+
- name: "Run bashunit tests"
258+
if: ${{ matrix.custom_ini == false && matrix.os == 'ubuntu-latest' }}
259+
run: "./lib/bashunit -p tests/EndToEnd"
260+
250261
# Note: The code style check is run multiple times against every PHP version
251262
# as it also acts as an integration test.
252263
- name: 'PHPCS: check code style without cache, no parallel'
253-
if: ${{ matrix.custom_ini == false }}
264+
if: ${{ matrix.custom_ini == false && matrix.os == 'windows-latest' }}
254265
run: php "bin/phpcs" --no-cache --parallel=1
255266

256267
- name: Download the PHPCS phar

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<exclude-pattern>*/src/Standards/*/Tests/*\.(inc|css|js)$</exclude-pattern>
1212
<exclude-pattern>*/tests/Core/*/*\.(inc|css|js)$</exclude-pattern>
1313
<exclude-pattern>*/tests/Core/*/Fixtures/*\.php$</exclude-pattern>
14+
<exclude-pattern>*/tests/EndToEnd/Files/*\.inc$</exclude-pattern>
1415

1516
<arg name="basepath" value="."/>
1617
<arg name="colors"/>

tests/EndToEnd/Files/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.fixed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/**
4+
* Class containing no style errors according to the end-to-end tests phpcs.xml.dist.
5+
*
6+
* @copyright 2025 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\EndToEnd\Files;
11+
12+
class ClassOneWithoutStyleError
13+
{
14+
private function foo()
15+
{
16+
return 'bar';
17+
}
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/**
4+
* Class containing no style errors.
5+
*
6+
* @copyright 2025 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\EndToEnd\Files;
11+
12+
class ClassTwoWithoutStyleError
13+
{
14+
/**
15+
* A property.
16+
*
17+
* @var string
18+
*/
19+
private $bar = 'baz';
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* Class containing a simple style error that phpcbf can fix.
5+
*
6+
* @copyright 2025 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\EndToEnd\Files;
11+
12+
class ClassWithStyleError
13+
{
14+
/**
15+
* The bracket for this function is misaligned and this can be automatically fixed by phpcbf.
16+
*
17+
* @return string
18+
*/
19+
private function foo() {
20+
return 'bar';
21+
}
22+
}

tests/EndToEnd/Files/phpcs.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="phpcs.xsd">
3+
<description>The coding standard for end to end tests.</description>
4+
5+
<rule ref="PSR12"/>
6+
7+
<file>.</file>
8+
9+
<arg name="basepath" value="."/>
10+
<arg name="colors"/>
11+
<arg name="parallel" value="75"/>
12+
<arg value="p"/>
13+
</ruleset>

tests/EndToEnd/phpcbf_test.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
3+
function tear_down() {
4+
rm -r tests/EndToEnd/Files/*.fixed
5+
}
6+
7+
function test_phpcbf_is_working() {
8+
OUTPUT="$(bin/phpcbf --no-cache --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassOneWithoutStyleError.inc tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc)"
9+
10+
assert_successful_code
11+
assert_contains "No violations were found" "$OUTPUT"
12+
}
13+
14+
function test_phpcbf_is_working_in_parallel() {
15+
OUTPUT="$(bin/phpcbf --no-cache --parallel=2 --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassOneWithoutStyleError.inc tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc)"
16+
17+
assert_successful_code
18+
assert_contains "No violations were found" "$OUTPUT"
19+
}
20+
21+
function test_phpcbf_returns_error_on_issues() {
22+
OUTPUT="$(bin/phpcbf --no-colors --no-cache --suffix=.fixed --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassWithStyleError.inc)"
23+
assert_exit_code 1
24+
25+
assert_contains "F 1 / 1 (100%)" "$OUTPUT"
26+
assert_contains "A TOTAL OF 1 ERROR WERE FIXED IN 1 FILE" "$OUTPUT"
27+
}
28+
29+
function test_phpcbf_bug_1112() {
30+
# See https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1112
31+
if [[ "$(uname)" == "Darwin" ]]; then
32+
# Perform some magic with `& fg` to prevent the processes from turning into a background job.
33+
assert_successful_code "$(bash -ic 'bash --init-file <(echo "echo \"Subprocess\"") -c "bin/phpcbf --no-cache --parallel=2 --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassOneWithoutStyleError.inc tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc" & fg')"
34+
else
35+
# This is not needed on Linux / GitHub Actions
36+
assert_successful_code "$(bash -ic 'bash --init-file <(echo "echo \"Subprocess\"") -c "bin/phpcbf --no-cache --parallel=2 --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassOneWithoutStyleError.inc tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc"')"
37+
fi
38+
}

tests/EndToEnd/phpcs_test.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
3+
function test_phpcs_is_working() {
4+
assert_successful_code "$(bin/phpcs --no-cache --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassOneWithoutStyleError.inc tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc)"
5+
}
6+
7+
function test_phpcs_is_working_in_parallel() {
8+
assert_successful_code "$(bin/phpcs --no-cache --parallel=2 --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassOneWithoutStyleError.inc tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc)"
9+
}
10+
11+
function test_phpcs_returns_error_on_issues() {
12+
OUTPUT="$(bin/phpcs --no-colors --no-cache --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassWithStyleError.inc)"
13+
assert_exit_code 2
14+
15+
assert_contains "E 1 / 1 (100%)" "$OUTPUT"
16+
assert_contains "FOUND 1 ERROR AFFECTING 1 LINE" "$OUTPUT"
17+
}
18+
19+
function test_phpcs_bug_1112() {
20+
# See https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1112
21+
if [[ "$(uname)" == "Darwin" ]]; then
22+
# Perform some magic with `& fg` to prevent the processes from turning into a background job.
23+
assert_successful_code "$(bash -ic 'bash --init-file <(echo "echo \"Subprocess\"") -c "bin/phpcs --no-cache --parallel=2 --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassOneWithoutStyleError.inc tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc" & fg')"
24+
else
25+
# This is not needed on Linux / GitHub Actions
26+
assert_successful_code "$(bash -ic 'bash --init-file <(echo "echo \"Subprocess\"") -c "bin/phpcs --no-cache --parallel=2 --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassOneWithoutStyleError.inc tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc"')"
27+
fi
28+
}

0 commit comments

Comments
 (0)