diff --git a/.travis.yml b/.travis.yml index 0ab420a850..86c04a8549 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,13 +4,21 @@ matrix: include: - php: 7.0 - php: 5.3 + env: + SKIP_STYLE_CHECK=1 - php: 5.4 + env: + SKIP_STYLE_CHECK=1 - php: 5.5 + env: + SKIP_STYLE_CHECK=1 - php: 5.6 env: - PHP_CS=1 + SKIP_STYLE_CHECK=1 EXECUTE_BUILD_DOCS=true - php: hhvm + env: + SKIP_STYLE_CHECK=1 allow_failures: - php: hhvm @@ -27,6 +35,6 @@ after_success: - test $TRAVIS_PULL_REQUEST == "false" && test $TRAVIS_BRANCH == "master" && test $EXECUTE_BUILD_DOCS == "true" && bash scripts/deploy-docs.sh script: - - php scripts/pre-commit.php -l - - if [[ $PHP_CS == 1 ]]; then php scripts/pre-commit.php -p -s; fi + - php scripts/pre-commit.php -p -l + - php scripts/pre-commit.php -p -s - phpunit diff --git a/composer.json b/composer.json index 6ca49b6e1a..88137f6892 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,8 @@ { "require-dev": { "squizlabs/php_codesniffer": "*", - "phpunit/phpunit": "4.*" + "phpunit/phpunit": "4.*", + "jakub-onderka/php-parallel-lint": "*", + "jakub-onderka/php-console-highlighter": "*" } } - diff --git a/scripts/pre-commit.php b/scripts/pre-commit.php index 592f6c5933..37ce5ab42b 100755 --- a/scripts/pre-commit.php +++ b/scripts/pre-commit.php @@ -2,52 +2,56 @@ false, + 'c' => false, + 'passthru' => false, + 'commands' => false, +); $passthru = check_opt($options, 'p', 'passthru'); -$commands = array_diff($options, $parameters); +$command_only = check_opt($options, 'c', 'commands'); +$commands = array_diff_assoc($options, $parameters); $all = empty($commands); $ret = 0; - // run tests -if ($all || check_opt($commands, 'l', 'lint')) { - $ret += check_lint(); +if (($all || check_opt($commands, 'l', 'lint')) && !getenv('SKIP_LINT_CHECK')) { + $ret += check_lint($passthru, $command_only); } -if ($all || check_opt($commands, 's', 'style')) { - $ret += check_style($passthru); +if (($all || check_opt($commands, 's', 'style')) && !getenv('SKIP_STYLE_CHECK')) { + $ret += check_style($passthru, $command_only); } -if ($all || check_opt($commands, 'u', 'unit')) { - $ret += check_unit($passthru); +if (($all || check_opt($commands, 'u', 'unit')) && !getenv('SKIP_UNIT_CHECK')) { + $ret += check_unit($passthru, $command_only); } @@ -58,6 +62,124 @@ if ($all && $ret === 0) { exit($ret); //return the combined/single return value of tests +/** + * Runs php -l and tests for any syntax errors + * + * @param bool $passthru display the output as comes in + * @param bool $command_only only display the intended command, no checks + * @return int the return value from running php -l (0 = success) + */ +function check_lint($passthru = false, $command_only = false) +{ + // matches a substring of the relative path, leading / is treated as absolute path + $lint_excludes = array('vendor/'); + if (defined('HHVM_VERSION') || version_compare(PHP_VERSION, '5.6', '<')) { + $lint_excludes[] = 'lib/influxdb-php/'; + } + + $lint_exclude = build_excludes('--exclude ', $lint_excludes); + $lint_cmd = "./vendor/bin/parallel-lint $lint_exclude ./"; + + if ($command_only) { + echo $lint_cmd . PHP_EOL; + return 250; + } + + echo 'Running lint check... '; + + if ($passthru) { + echo PHP_EOL; + passthru($lint_cmd, $lint_ret); + } else { + exec($lint_cmd, $lint_output, $lint_ret); + + if ($lint_ret > 0) { + print(implode(PHP_EOL, $lint_output) . PHP_EOL); + } else { + echo "success\n"; + } + } + + return $lint_ret; +} + +/** + * Runs phpcs --standard=PSR2 against the code base + * + * @param bool $passthru display the output as comes in + * @param bool $command_only only display the intended command, no checks + * @return int the return value from phpcs (0 = success) + */ +function check_style($passthru = false, $command_only = false) +{ + // matches a substring of the full path + $cs_excludes = array( + '/vendor/', + '/lib/', + '/html/plugins/', + ); + + $cs_exclude = build_excludes('--ignore=', $cs_excludes); + $cs_cmd = "./vendor/bin/phpcs -n -p --colors --extensions=php --standard=PSR2 $cs_exclude ./html ./includes"; + + if ($command_only) { + echo $cs_cmd . PHP_EOL; + return 250; + } + + echo 'Running style check... '; + + if ($passthru) { + echo PHP_EOL; + passthru($cs_cmd, $cs_ret); + } else { + exec($cs_cmd, $cs_output, $cs_ret); + + if ($cs_ret > 0) { + echo "failed\n"; + print(implode(PHP_EOL, $cs_output) . PHP_EOL); + } else { + echo "success\n"; + } + } + + return $cs_ret; +} + +/** + * Runs phpunit + * + * @param bool $passthru display the output as comes in + * @param bool $command_only only display the intended command, no checks + * @return int the return value from phpunit (0 = success) + */ +function check_unit($passthru = false, $command_only = false) +{ + $phpunit_cmd = './vendor/bin/phpunit --colors=always'; + + if ($command_only) { + echo $phpunit_cmd . PHP_EOL; + return 250; + } + + echo 'Running unit tests... '; + if ($passthru) { + echo PHP_EOL; + passthru($phpunit_cmd, $phpunit_ret); + } else { + exec($phpunit_cmd, $phpunit_output, $phpunit_ret); + + if ($phpunit_ret > 0) { + echo "failed\n"; + print(implode(PHP_EOL, $phpunit_output) . PHP_EOL); + } else { + echo "success\n"; + } + } + + return $phpunit_ret; +} + /** * Check if the given options array contains any of the $opts specified * @@ -74,96 +196,19 @@ function check_opt($options) return !empty($intersect); } - /** - * Runs php -l and tests for any syntax errors + * Build a list of exclude arguments from an array * - * @return int the return value from running php -l (0 = success) + * @param string $exclude_string such as "--exclude" + * @param array $excludes array of directories to exclude + * @return string resulting string */ -function check_lint() +function build_excludes($exclude_string, $excludes) { - echo "Running lint check... \n"; - - if (version_compare(PHP_VERSION, '5.6') >= 0) { - $lint_exclude = 'vendor'; - } else { - $lint_exclude = 'vendor|lib/influxdb-php'; - } - $lint_cmd = 'find . -regextype posix-extended -regex "\./('; - $lint_cmd .= $lint_exclude; - $lint_cmd .= ')" -prune -o -name "*.php" -print0 | xargs -0 -n1 -P8 php -l '; - $lint_cmd .= '| grep -v "^No syntax errors detected"; test $? -eq 1'; - - exec($lint_cmd, $lint_output, $lint_ret); - - if ($lint_ret > 0) { - print(implode(PHP_EOL, $lint_output) . PHP_EOL); - } else { - echo "success\n"; + $result = ''; + foreach ($excludes as $exclude) { + $result .= $exclude_string . $exclude . ' '; } - return $lint_ret; -} - -/** - * Runs phpcs --standard=PSR2 against the code base - * - * @param bool $passthru display the output as comes in - * @return int the return value from phpcs (0 = success) - */ -function check_style($passthru = false) -{ - echo 'Checking PSR-2 style...'.($passthru ? "\n" : ' '); - - - $cs_exclude = '--ignore=html/lib/* --ignore=html/plugins/'; - $cs_cmd = "./vendor/bin/phpcs -n -p --colors --extensions=php --standard=PSR2 $cs_exclude html includes"; - - $cs_output = ''; - if ($passthru) { - passthru($cs_cmd, $cs_ret); - } else { - exec($cs_cmd, $cs_output, $cs_ret); - } - - if (!$passthru) { - if ($cs_ret > 0) { - echo "failed\n"; - print(implode(PHP_EOL, $cs_output) . PHP_EOL); - } else { - echo "success\n"; - } - } - - return $cs_ret; -} - -/** - * Runs phpunit - * - * @param bool $passthru display the output as comes in - * @return int the return value from phpunit (0 = success) - */ -function check_unit($passthru = false) -{ - echo 'Running unit tests...'.($passthru ? "\n" : ' '); - $phpunit_cmd = './vendor/bin/phpunit --colors=always'; - - $phpunit_output = ''; - if ($passthru) { - passthru($phpunit_cmd, $phpunit_ret); - } else { - exec($phpunit_cmd, $phpunit_output, $phpunit_ret); - } - - if (!$passthru) { - if ($phpunit_ret > 0) { - echo "failed\n"; - print(implode(PHP_EOL, $phpunit_output) . PHP_EOL); - } else { - echo "success\n"; - } - } - - return $phpunit_ret; + return $result; }