mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Use pre-commit.php in for travis-ci tests (#4212)
This commit is contained in:
committed by
Neil Lathwood
parent
562737a528
commit
f042f9aa56
21
.travis.yml
21
.travis.yml
@@ -3,30 +3,14 @@ matrix:
|
|||||||
fast_finish: true
|
fast_finish: true
|
||||||
include:
|
include:
|
||||||
- php: 7.0
|
- php: 7.0
|
||||||
env:
|
|
||||||
PHP_L=1
|
|
||||||
EXECUTE_BUILD_DOCS=false
|
|
||||||
- php: 5.3
|
- php: 5.3
|
||||||
env:
|
|
||||||
PHP_L_OLD=1
|
|
||||||
EXECUTE_BUILD_DOCS=false
|
|
||||||
- php: 5.4
|
- php: 5.4
|
||||||
env:
|
|
||||||
PHP_L_OLD=1
|
|
||||||
EXECUTE_BUILD_DOCS=false
|
|
||||||
- php: 5.5
|
- php: 5.5
|
||||||
env:
|
|
||||||
PHP_L=1
|
|
||||||
EXECUTE_BUILD_DOCS=false
|
|
||||||
- php: 5.6
|
- php: 5.6
|
||||||
env:
|
env:
|
||||||
PHP_L=1
|
|
||||||
PHP_CS=1
|
PHP_CS=1
|
||||||
EXECUTE_BUILD_DOCS=true
|
EXECUTE_BUILD_DOCS=true
|
||||||
- php: hhvm
|
- php: hhvm
|
||||||
env:
|
|
||||||
PHP_L_OLD=1
|
|
||||||
EXECUTE_BUILD_DOCS=false
|
|
||||||
|
|
||||||
allow_failures:
|
allow_failures:
|
||||||
- php: hhvm
|
- php: hhvm
|
||||||
@@ -43,7 +27,6 @@ after_success:
|
|||||||
- test $TRAVIS_PULL_REQUEST == "false" && test $TRAVIS_BRANCH == "master" && test $EXECUTE_BUILD_DOCS == "true" && bash scripts/deploy-docs.sh
|
- test $TRAVIS_PULL_REQUEST == "false" && test $TRAVIS_BRANCH == "master" && test $EXECUTE_BUILD_DOCS == "true" && bash scripts/deploy-docs.sh
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- if [[ $PHP_L == 1 ]]; then find . -path './vendor' -prune -o -name "*.php" -print0 | xargs -0 -n1 -P8 php -l | grep -v '^No syntax errors detected' ; test $? -eq 1; fi
|
- php scripts/pre-commit.php -l
|
||||||
- if [[ $PHP_L_OLD == 1 ]]; then find . -regextype posix-extended -regex "\./(lib/influxdb-php|vendor)" -prune -o -name "*.php" -print0 | xargs -0 -n1 -P8 php -l | grep -v '^No syntax errors detected' ; test $? -eq 1; fi
|
- if [[ $PHP_CS == 1 ]]; then php scripts/pre-commit.php -p -s; fi
|
||||||
- if [[ $PHP_CS == 1 ]]; then vendor/bin/phpcs -n -p --colors --extensions=php --standard=PSR2 --ignore=html/lib/* --ignore=html/plugins/* html; fi
|
|
||||||
- phpunit
|
- phpunit
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
{
|
{
|
||||||
"require": {
|
|
||||||
"squizlabs/php_codesniffer": "*"
|
|
||||||
},
|
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"squizlabs/php_codesniffer": "*",
|
"squizlabs/php_codesniffer": "*",
|
||||||
"phpunit/phpunit": "4.*"
|
"phpunit/phpunit": "4.*"
|
||||||
|
|||||||
@@ -1,36 +1,169 @@
|
|||||||
#!/usr/bin/env php
|
#!/usr/bin/env php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
$failed = false;
|
$filename = basename(__FILE__);
|
||||||
|
$install_dir = realpath(__DIR__.'/..');
|
||||||
|
chdir($install_dir);
|
||||||
|
|
||||||
if (version_compare(PHP_VERSION, '5.6') >= 0) {
|
$short_opts = 'lsuph';
|
||||||
$lint = `find . -path './vendor' -prune -o -name "*.php" -print0 | xargs -0 -n1 -P8 php -l | grep -v '^No syntax errors detected' ; test $? -eq 1`;
|
$long_opts = array(
|
||||||
} else {
|
'lint',
|
||||||
$lint = `find . -regextype posix-extended -regex "\./(lib/influxdb-php|vendor)" -prune -o -name "*.php" -print0 | xargs -0 -n1 -P8 php -l | grep -v '^No syntax errors detected' ; test $? -eq 1`;
|
'style',
|
||||||
|
'unit',
|
||||||
|
'passthru',
|
||||||
|
'help',
|
||||||
|
);
|
||||||
|
$parameters = array(
|
||||||
|
'p',
|
||||||
|
'passthru',
|
||||||
|
);
|
||||||
|
|
||||||
|
$options = getopt($short_opts, $long_opts);
|
||||||
|
|
||||||
|
if (check_opt($options, 'h', 'help')) {
|
||||||
|
echo "LibreNMS Code Tests Script
|
||||||
|
Running $filename without options runs all checks.
|
||||||
|
-l, --lint Run php lint checks to test for valid syntax.
|
||||||
|
-s, --style Run phpcs check to check for PSR-2 compliance.
|
||||||
|
-u, --unit Run phpunit tests.
|
||||||
|
-h, --help Show this help text.\n";
|
||||||
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($lint)) {
|
// set up some variables
|
||||||
echo "lint check has failed\n";
|
$passthru = check_opt($options, 'p', 'passthru');
|
||||||
print_r($lint);
|
$commands = array_diff($options, $parameters);
|
||||||
$failed = true;
|
$all = empty($commands);
|
||||||
|
$ret = 0;
|
||||||
|
|
||||||
|
|
||||||
|
// run tests
|
||||||
|
if ($all || check_opt($commands, 'l', 'lint')) {
|
||||||
|
$ret += check_lint();
|
||||||
}
|
}
|
||||||
|
|
||||||
$phpcs = `./vendor/bin/phpcs -n -p --colors --extensions=php --standard=PSR2 --ignore=html/lib/* --ignore=html/plugins/ html`;
|
if ($all || check_opt($commands, 's', 'style')) {
|
||||||
|
$ret += check_style($passthru);
|
||||||
if (!empty($phpcs)) {
|
|
||||||
echo "PSR2 check has failed\n";
|
|
||||||
print_r($phpcs);
|
|
||||||
$failed = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$phpunit = `./vendor/bin/phpunit`;
|
if ($all || check_opt($commands, 'u', 'unit')) {
|
||||||
|
$ret += check_unit($passthru);
|
||||||
if(!strstr($phpunit, "OK")) {
|
|
||||||
echo "phpunit tests have failed\n";
|
|
||||||
print_r($phpunit);
|
|
||||||
$failed = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($failed === false) {
|
|
||||||
echo "Tests ok, submit away :)\n";
|
// output Tests ok, if no arguments passed
|
||||||
|
if ($all && $ret === 0) {
|
||||||
|
echo "\033[32mTests ok, submit away :)\033[0m \n";
|
||||||
|
}
|
||||||
|
exit($ret); //return the combined/single return value of tests
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the given options array contains any of the $opts specified
|
||||||
|
*
|
||||||
|
* @param array $options the array from getopt()
|
||||||
|
* @param string $opts,... options to check for
|
||||||
|
* @return bool If one of the specified options is set
|
||||||
|
*/
|
||||||
|
function check_opt($options)
|
||||||
|
{
|
||||||
|
$args = func_get_args();
|
||||||
|
array_shift($args);
|
||||||
|
|
||||||
|
$intersect = array_intersect(array_keys($options), $args);
|
||||||
|
return !empty($intersect);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs php -l and tests for any syntax errors
|
||||||
|
*
|
||||||
|
* @return int the return value from running php -l (0 = success)
|
||||||
|
*/
|
||||||
|
function check_lint()
|
||||||
|
{
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
|
||||||
|
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";
|
||||||
|
|
||||||
|
$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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user