Skip to content

Commit f5bde12

Browse files
authored
Merge pull request #558 from Automattic/fix/test-bootstrap-various-tweaks
Test bootstrap: various minor tweaks
2 parents 77e4258 + 42fb244 commit f5bde12

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

tests/bootstrap.php

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,29 @@
1313
* @license gpl-2.0-or-later
1414
*/
1515

16-
if ( ! \defined( 'PHP_CODESNIFFER_IN_TESTS' ) ) {
16+
if ( ! defined( 'PHP_CODESNIFFER_IN_TESTS' ) ) {
1717
define( 'PHP_CODESNIFFER_IN_TESTS', true );
1818
}
1919

2020
$ds = DIRECTORY_SEPARATOR;
2121

2222
// Get the PHPCS dir from an environment variable.
23-
$phpcsDir = getenv( 'PHPCS_DIR' );
23+
$phpcsDir = getenv( 'PHPCS_DIR' );
24+
$composerPHPCSPath = dirname( __DIR__ ) . $ds . 'vendor' . $ds . 'squizlabs' . $ds . 'php_codesniffer';
2425

2526
// This may be a Composer install.
26-
if ( false === $phpcsDir && is_dir( dirname( __DIR__ ) . $ds . 'vendor' . $ds . 'squizlabs' . $ds . 'php_codesniffer' ) ) {
27-
$phpcsDir = dirname( __DIR__ ) . $ds . 'vendor' . $ds . 'squizlabs' . $ds . 'php_codesniffer';
27+
if ( false === $phpcsDir && is_dir( $composerPHPCSPath ) ) {
28+
$phpcsDir = $composerPHPCSPath;
2829
} elseif ( false !== $phpcsDir ) {
30+
// PHPCS in a custom directory.
2931
$phpcsDir = realpath( $phpcsDir );
3032
}
3133

3234
// Try and load the PHPCS autoloader.
33-
if ( false !== $phpcsDir && file_exists( $phpcsDir . $ds . 'autoload.php' ) ) {
35+
if ( false !== $phpcsDir
36+
&& file_exists( $phpcsDir . $ds . 'autoload.php' )
37+
&& file_exists( $phpcsDir . $ds . 'tests' . $ds . 'bootstrap.php' )
38+
) {
3439
require_once $phpcsDir . $ds . 'autoload.php';
3540

3641
/*
@@ -39,17 +44,42 @@
3944
*/
4045
require_once $phpcsDir . $ds . 'tests' . $ds . 'bootstrap.php';
4146
} else {
42-
echo 'Uh oh... can\'t find PHPCS. Are you sure you are using PHPCS 3.x ?
47+
echo 'Uh oh... can\'t find PHPCS.
4348
4449
If you use Composer, please run `composer install`.
4550
Otherwise, make sure you set a `PHPCS_DIR` environment variable in your phpunit.xml file
4651
pointing to the PHPCS directory.
4752
4853
Please read the contributors guidelines for more information:
49-
https://is.gd/contributing2WPCS
54+
https://github.com/Automattic/VIP-Coding-Standards/blob/develop/.github/CONTRIBUTING.md
5055
';
5156

5257
die( 1 );
5358
}
5459

55-
unset( $ds, $phpcsDir );
60+
/*
61+
* Set the PHPCS_IGNORE_TEST environment variable to ignore tests from other standards.
62+
*/
63+
$vipcsStandards = [
64+
'WordPressVIPMinimum' => true,
65+
];
66+
67+
$allStandards = PHP_CodeSniffer\Util\Standards::getInstalledStandards();
68+
$allStandards[] = 'Generic';
69+
70+
$standardsToIgnore = [];
71+
foreach ( $allStandards as $standard ) {
72+
if ( isset( $vipcsStandards[ $standard ] ) === true ) {
73+
continue;
74+
}
75+
76+
$standardsToIgnore[] = $standard;
77+
}
78+
79+
$standardsToIgnoreString = implode( ',', $standardsToIgnore );
80+
81+
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_putenv -- This is test code, not production.
82+
putenv( "PHPCS_IGNORE_TESTS={$standardsToIgnoreString}" );
83+
84+
// Clean up.
85+
unset( $ds, $phpcsDir, $composerPHPCSPath, $allStandards, $standardsToIgnore, $standard, $standardsToIgnoreString );

0 commit comments

Comments
 (0)