2016-08-26 00:40:11 +01:00
|
|
|
#!/usr/bin/env php
|
|
|
|
|
<?php
|
|
|
|
|
|
2016-08-26 04:30:43 -05:00
|
|
|
$filename = basename(__FILE__);
|
2016-08-28 15:44:36 -05:00
|
|
|
$install_dir = realpath(__DIR__ . '/..');
|
2016-08-26 04:30:43 -05:00
|
|
|
chdir($install_dir);
|
2016-08-26 00:40:11 +01:00
|
|
|
|
2016-08-28 15:44:36 -05:00
|
|
|
$short_opts = 'lsuphc';
|
2016-08-26 04:30:43 -05:00
|
|
|
$long_opts = array(
|
|
|
|
|
'lint',
|
|
|
|
|
'style',
|
|
|
|
|
'unit',
|
|
|
|
|
'passthru',
|
2016-08-28 15:44:36 -05:00
|
|
|
'commands',
|
2016-08-26 04:30:43 -05:00
|
|
|
'help',
|
|
|
|
|
);
|
|
|
|
|
$options = getopt($short_opts, $long_opts);
|
|
|
|
|
|
|
|
|
|
if (check_opt($options, 'h', 'help')) {
|
|
|
|
|
echo "LibreNMS Code Tests Script
|
|
|
|
|
Running $filename without options runs all checks.
|
2016-08-28 15:44:36 -05:00
|
|
|
-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.
|
|
|
|
|
-p, --passthru Display output from checks as it comes
|
|
|
|
|
-c, --commands Print commands only, no checks
|
|
|
|
|
-h, --help Show this help text.\n";
|
2016-08-26 04:30:43 -05:00
|
|
|
exit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// set up some variables
|
2016-08-28 15:44:36 -05:00
|
|
|
$parameters = array(
|
|
|
|
|
'p' => false,
|
|
|
|
|
'c' => false,
|
|
|
|
|
'passthru' => false,
|
|
|
|
|
'commands' => false,
|
|
|
|
|
);
|
2016-08-26 04:30:43 -05:00
|
|
|
$passthru = check_opt($options, 'p', 'passthru');
|
2016-08-28 15:44:36 -05:00
|
|
|
$command_only = check_opt($options, 'c', 'commands');
|
|
|
|
|
$commands = array_diff_assoc($options, $parameters);
|
2016-08-26 04:30:43 -05:00
|
|
|
$all = empty($commands);
|
|
|
|
|
$ret = 0;
|
|
|
|
|
|
|
|
|
|
// run tests
|
2016-08-28 15:44:36 -05:00
|
|
|
if (($all || check_opt($commands, 'l', 'lint')) && !getenv('SKIP_LINT_CHECK')) {
|
|
|
|
|
$ret += check_lint($passthru, $command_only);
|
2016-08-26 04:30:43 -05:00
|
|
|
}
|
|
|
|
|
|
2016-08-28 15:44:36 -05:00
|
|
|
if (($all || check_opt($commands, 's', 'style')) && !getenv('SKIP_STYLE_CHECK')) {
|
|
|
|
|
$ret += check_style($passthru, $command_only);
|
2016-08-26 04:30:43 -05:00
|
|
|
}
|
|
|
|
|
|
2016-08-28 15:44:36 -05:00
|
|
|
if (($all || check_opt($commands, 'u', 'unit')) && !getenv('SKIP_UNIT_CHECK')) {
|
|
|
|
|
$ret += check_unit($passthru, $command_only);
|
2016-08-26 04:30:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// output Tests ok, if no arguments passed
|
|
|
|
|
if ($all && $ret === 0) {
|
|
|
|
|
echo "\033[32mTests ok, submit away :)\033[0m \n";
|
2016-08-26 00:40:11 +01:00
|
|
|
}
|
2016-08-26 04:30:43 -05:00
|
|
|
exit($ret); //return the combined/single return value of tests
|
2016-08-26 00:40:11 +01:00
|
|
|
|
2016-08-26 04:30:43 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Runs php -l and tests for any syntax errors
|
|
|
|
|
*
|
2016-08-28 15:44:36 -05:00
|
|
|
* @param bool $passthru display the output as comes in
|
|
|
|
|
* @param bool $command_only only display the intended command, no checks
|
2016-08-26 04:30:43 -05:00
|
|
|
* @return int the return value from running php -l (0 = success)
|
|
|
|
|
*/
|
2016-08-28 15:44:36 -05:00
|
|
|
function check_lint($passthru = false, $command_only = false)
|
2016-08-26 04:30:43 -05:00
|
|
|
{
|
2016-08-28 15:44:36 -05:00
|
|
|
// 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/';
|
|
|
|
|
}
|
2016-08-26 04:30:43 -05:00
|
|
|
|
2016-08-28 15:44:36 -05:00
|
|
|
$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;
|
2016-08-26 04:30:43 -05:00
|
|
|
}
|
|
|
|
|
|
2016-08-28 15:44:36 -05:00
|
|
|
echo 'Running lint check... ';
|
2016-08-26 04:30:43 -05:00
|
|
|
|
2016-08-28 15:44:36 -05:00
|
|
|
if ($passthru) {
|
|
|
|
|
echo PHP_EOL;
|
|
|
|
|
passthru($lint_cmd, $lint_ret);
|
2016-08-26 04:30:43 -05:00
|
|
|
} else {
|
2016-08-28 15:44:36 -05:00
|
|
|
exec($lint_cmd, $lint_output, $lint_ret);
|
|
|
|
|
|
|
|
|
|
if ($lint_ret > 0) {
|
|
|
|
|
print(implode(PHP_EOL, $lint_output) . PHP_EOL);
|
|
|
|
|
} else {
|
|
|
|
|
echo "success\n";
|
|
|
|
|
}
|
2016-08-26 04:30:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $lint_ret;
|
2016-08-26 00:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
2016-08-26 04:30:43 -05:00
|
|
|
/**
|
|
|
|
|
* Runs phpcs --standard=PSR2 against the code base
|
|
|
|
|
*
|
|
|
|
|
* @param bool $passthru display the output as comes in
|
2016-08-28 15:44:36 -05:00
|
|
|
* @param bool $command_only only display the intended command, no checks
|
2016-08-26 04:30:43 -05:00
|
|
|
* @return int the return value from phpcs (0 = success)
|
|
|
|
|
*/
|
2016-08-28 15:44:36 -05:00
|
|
|
function check_style($passthru = false, $command_only = false)
|
2016-08-26 04:30:43 -05:00
|
|
|
{
|
2016-08-28 15:44:36 -05:00
|
|
|
// matches a substring of the full path
|
|
|
|
|
$cs_excludes = array(
|
|
|
|
|
'/vendor/',
|
|
|
|
|
'/lib/',
|
|
|
|
|
'/html/plugins/',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$cs_exclude = build_excludes('--ignore=', $cs_excludes);
|
2016-08-28 17:32:55 -05:00
|
|
|
$cs_cmd = "./vendor/bin/phpcs -n -p --colors --extensions=php --standard=PSR2 $cs_exclude ./";
|
2016-08-28 15:44:36 -05:00
|
|
|
|
|
|
|
|
if ($command_only) {
|
|
|
|
|
echo $cs_cmd . PHP_EOL;
|
|
|
|
|
return 250;
|
|
|
|
|
}
|
2016-08-26 00:40:11 +01:00
|
|
|
|
2016-08-28 15:44:36 -05:00
|
|
|
echo 'Running style check... ';
|
2016-08-26 04:30:43 -05:00
|
|
|
|
|
|
|
|
if ($passthru) {
|
2016-08-28 15:44:36 -05:00
|
|
|
echo PHP_EOL;
|
2016-08-26 04:30:43 -05:00
|
|
|
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;
|
2016-08-26 00:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
2016-08-26 04:30:43 -05:00
|
|
|
/**
|
|
|
|
|
* Runs phpunit
|
|
|
|
|
*
|
|
|
|
|
* @param bool $passthru display the output as comes in
|
2016-08-28 15:44:36 -05:00
|
|
|
* @param bool $command_only only display the intended command, no checks
|
2016-08-26 04:30:43 -05:00
|
|
|
* @return int the return value from phpunit (0 = success)
|
|
|
|
|
*/
|
2016-08-28 15:44:36 -05:00
|
|
|
function check_unit($passthru = false, $command_only = false)
|
2016-08-26 04:30:43 -05:00
|
|
|
{
|
|
|
|
|
$phpunit_cmd = './vendor/bin/phpunit --colors=always';
|
|
|
|
|
|
2016-08-28 15:44:36 -05:00
|
|
|
if ($command_only) {
|
|
|
|
|
echo $phpunit_cmd . PHP_EOL;
|
|
|
|
|
return 250;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
echo 'Running unit tests... ';
|
2016-08-26 04:30:43 -05:00
|
|
|
if ($passthru) {
|
2016-08-28 15:44:36 -05:00
|
|
|
echo PHP_EOL;
|
2016-08-26 04:30:43 -05:00
|
|
|
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;
|
2016-08-26 00:40:11 +01:00
|
|
|
}
|
2016-08-28 15:44:36 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Build a list of exclude arguments from an array
|
|
|
|
|
*
|
|
|
|
|
* @param string $exclude_string such as "--exclude"
|
|
|
|
|
* @param array $excludes array of directories to exclude
|
|
|
|
|
* @return string resulting string
|
|
|
|
|
*/
|
|
|
|
|
function build_excludes($exclude_string, $excludes)
|
|
|
|
|
{
|
|
|
|
|
$result = '';
|
|
|
|
|
foreach ($excludes as $exclude) {
|
|
|
|
|
$result .= $exclude_string . $exclude . ' ';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|