Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test bootstrap: various minor tweaks #558

Merged
merged 1 commit into from
Jul 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,29 @@
* @license gpl-2.0-or-later
*/

if ( ! \defined( 'PHP_CODESNIFFER_IN_TESTS' ) ) {
if ( ! defined( 'PHP_CODESNIFFER_IN_TESTS' ) ) {
define( 'PHP_CODESNIFFER_IN_TESTS', true );
}

$ds = DIRECTORY_SEPARATOR;

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

// This may be a Composer install.
if ( false === $phpcsDir && is_dir( dirname( __DIR__ ) . $ds . 'vendor' . $ds . 'squizlabs' . $ds . 'php_codesniffer' ) ) {
$phpcsDir = dirname( __DIR__ ) . $ds . 'vendor' . $ds . 'squizlabs' . $ds . 'php_codesniffer';
if ( false === $phpcsDir && is_dir( $composerPHPCSPath ) ) {
$phpcsDir = $composerPHPCSPath;
} elseif ( false !== $phpcsDir ) {
// PHPCS in a custom directory.
$phpcsDir = realpath( $phpcsDir );
}

// Try and load the PHPCS autoloader.
if ( false !== $phpcsDir && file_exists( $phpcsDir . $ds . 'autoload.php' ) ) {
if ( false !== $phpcsDir
&& file_exists( $phpcsDir . $ds . 'autoload.php' )
&& file_exists( $phpcsDir . $ds . 'tests' . $ds . 'bootstrap.php' )
) {
require_once $phpcsDir . $ds . 'autoload.php';

/*
Expand All @@ -39,17 +44,42 @@
*/
require_once $phpcsDir . $ds . 'tests' . $ds . 'bootstrap.php';
} else {
echo 'Uh oh... can\'t find PHPCS. Are you sure you are using PHPCS 3.x ?
echo 'Uh oh... can\'t find PHPCS.

If you use Composer, please run `composer install`.
Otherwise, make sure you set a `PHPCS_DIR` environment variable in your phpunit.xml file
pointing to the PHPCS directory.

Please read the contributors guidelines for more information:
https://is.gd/contributing2WPCS
https://github.com/Automattic/VIP-Coding-Standards/blob/develop/.github/CONTRIBUTING.md
';

die( 1 );
}

unset( $ds, $phpcsDir );
/*
* Set the PHPCS_IGNORE_TEST environment variable to ignore tests from other standards.
*/
$vipcsStandards = [
'WordPressVIPMinimum' => true,
];

$allStandards = PHP_CodeSniffer\Util\Standards::getInstalledStandards();
$allStandards[] = 'Generic';

$standardsToIgnore = [];
foreach ( $allStandards as $standard ) {
if ( isset( $vipcsStandards[ $standard ] ) === true ) {
continue;
}

$standardsToIgnore[] = $standard;
}

$standardsToIgnoreString = implode( ',', $standardsToIgnore );

// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_putenv -- This is test code, not production.
putenv( "PHPCS_IGNORE_TESTS={$standardsToIgnoreString}" );

// Clean up.
unset( $ds, $phpcsDir, $composerPHPCSPath, $allStandards, $standardsToIgnore, $standard, $standardsToIgnoreString );