Skip to content

Commit

Permalink
MDL-71036 phpunit: xml config - change generation code to follow xsd
Browse files Browse the repository at this point in the history
This performs all the changes needed in the util generator to
produce XML files compliant with the new schema (see previous
commit for description of changes).

- All the occurrences in code of filter => coverage.
- All the occurrences in code of whitelist => include.
- Apply all the changes to comply with the new schema.

- Remove processUncoveredFilesFromWhitelist attribute, useless (false
    is its default value, and now have another name).
- Move from 4-spaces indented XML to 2-spaces indented.
- Small linefeed tweaks to generate better-looking XML.
  • Loading branch information
stronk7 committed Mar 11, 2021
1 parent bec0db9 commit 682ce8f
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions lib/phpunit/classes/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,18 +490,16 @@ public static function install_site() {
public static function build_config_file() {
global $CFG;

$template = '
<testsuite name="@component@_testsuite">
<directory suffix="_test.php">@dir@</directory>
</testsuite>';
$filtertemplate = '
<testsuite name="@component@_testsuite">
<directory suffix="_test.php">@dir@</directory>
</testsuite>';
$template = <<<EOF
<testsuite name="@component@_testsuite">
<directory suffix="_test.php">@dir@</directory>
</testsuite>
EOF;
$data = file_get_contents("$CFG->dirroot/phpunit.xml.dist");

$suites = '';
$whitelists = [];
$includelists = [];
$excludelists = [];

$subsystems = core_component::get_core_subsystems();
Expand All @@ -517,7 +515,7 @@ public static function build_config_file() {

$dir = substr($fulldir, strlen($CFG->dirroot) + 1);
if ($coverageinfo = self::get_coverage_info($fulldir)) {
$whitelists = array_merge($whitelists, $coverageinfo->get_whitelists($dir));
$includelists = array_merge($includelists, $coverageinfo->get_includelists($dir));
$excludelists = array_merge($excludelists, $coverageinfo->get_excludelists($dir));
}
}
Expand All @@ -544,7 +542,7 @@ public static function build_config_file() {

if ($coverageinfo = self::get_coverage_info($plugindir)) {

$whitelists = array_merge($whitelists, $coverageinfo->get_whitelists($dir));
$includelists = array_merge($includelists, $coverageinfo->get_includelists($dir));
$excludelists = array_merge($excludelists, $coverageinfo->get_excludelists($dir));
}
}
Expand All @@ -555,14 +553,14 @@ public static function build_config_file() {
// end up being placed in phpunit or behat test code.
$sequencestart = 100000 + mt_rand(0, 99) * 1000;

$data = preg_replace('|<!--@plugin_suites_start@-->.*<!--@plugin_suites_end@-->|s', $suites, $data, 1);
$data = preg_replace('| *<!--@plugin_suites_start@-->.*<!--@plugin_suites_end@-->|s', trim($suites, "\n"), $data, 1);
$data = str_replace(
'<const name="PHPUNIT_SEQUENCE_START" value=""/>',
'<const name="PHPUNIT_SEQUENCE_START" value="' . $sequencestart . '"/>',
$data);

$filters = self::get_filter_config($whitelists, $excludelists);
$data = str_replace('<!--@filterlist@-->', $filters, $data);
$coverages = self::get_coverage_config($includelists, $excludelists);
$data = preg_replace('| *<!--@coveragelist@-->|s', trim($coverages, "\n"), $data);

$result = false;
if (is_writable($CFG->dirroot)) {
Expand All @@ -583,19 +581,21 @@ public static function build_config_file() {
public static function build_component_config_files() {
global $CFG;

$template = '
<testsuites>
<testsuite name="@component@_testsuite">
<directory suffix="_test.php">.</directory>
</testsuite>
</testsuites>';
$filterdefault = '
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">.</directory>
<exclude>
<directory suffix="_test.php">.</directory>
</exclude>
</whitelist>';
$template = <<<EOT
<testsuites>
<testsuite name="@component@_testsuite">
<directory suffix="_test.php">.</directory>
</testsuite>
</testsuites>
EOT;
$coveragedefault = <<<EOT
<include>
<directory suffix=".php">.</directory>
</include>
<exclude>
<directory suffix="_test.php">.</directory>
</exclude>
EOT;

// Start a sequence between 100000 and 199000 to ensure each call to init produces
// different ids in the database. This reduces the risk that hard coded values will
Expand All @@ -604,7 +604,7 @@ public static function build_component_config_files() {

// Use the upstream file as source for the distributed configurations
$ftemplate = file_get_contents("$CFG->dirroot/phpunit.xml.dist");
$ftemplate = preg_replace('|<!--All core suites.*</testsuites>|s', '<!--@component_suite@-->', $ftemplate);
$ftemplate = preg_replace('| *<!--All core suites.*</testsuites>|s', '<!--@component_suite@-->', $ftemplate);

// Gets all the components with tests
$components = tests_finder::get_components_with_tests('phpunit');
Expand All @@ -617,13 +617,13 @@ public static function build_component_config_files() {

$fcontents = str_replace('<!--@component_suite@-->', $ctemplate, $ftemplate);

// Check for filter configurations.
// Check for coverage configurations.
if ($coverageinfo = self::get_coverage_info($cpath)) {
$filters = self::get_filter_config($coverageinfo->get_whitelists(''), $coverageinfo->get_excludelists(''));
$coverages = self::get_coverage_config($coverageinfo->get_includelists(''), $coverageinfo->get_excludelists(''));
} else {
$filters = $filterdefault;
$coverages = $coveragedefault;
}
$fcontents = str_replace('<!--@filterlist@-->', $filters, $fcontents);
$fcontents = preg_replace('| *<!--@coveragelist@-->|s', trim($coverages, "\n"), $fcontents);

// Apply it to the file template.
$fcontents = str_replace(
Expand Down Expand Up @@ -931,34 +931,34 @@ public static function call_internal_method($object, $methodname, array $params
* @return string
*/
protected static function pad(string $string, int $level) : string {
return str_repeat(" ", $level * 4) . "{$string}\n";
return str_repeat(" ", $level * 2) . "{$string}\n";
}

/**
* Get the filter config for the supplied whitelist and excludelist configuration.
* Get the coverage config for the supplied includelist and excludelist configuration.
*
* @param array[] $whitelists The list of files/folders in the whitelist.
* @param array[] $includelists The list of files/folders in the includelist.
* @param array[] $excludelists The list of files/folders in the excludelist.
* @return string
*/
protected static function get_filter_config(array $whitelists, array $excludelists) : string {
$filters = '';
if (!empty($whitelists)) {
$filters .= self::pad("<whitelist>", 2);
foreach ($whitelists as $line) {
$filters .= self::pad($line, 3);
protected static function get_coverage_config(array $includelists, array $excludelists) : string {
$coverages = '';
if (!empty($includelists)) {
$coverages .= self::pad("<include>", 2);
foreach ($includelists as $line) {
$coverages .= self::pad($line, 3);
}
$coverages .= self::pad("</include>", 2);
if (!empty($excludelists)) {
$filters .= self::pad("<exclude>", 3);
$coverages .= self::pad("<exclude>", 2);
foreach ($excludelists as $line) {
$filters .= self::pad($line, 4);
$coverages .= self::pad($line, 3);
}
$filters .= self::pad("</exclude>", 3);
$coverages .= self::pad("</exclude>", 2);
}
$filters .= self::pad("</whitelist>", 2);
}

return $filters;
return $coverages;
}

/**
Expand Down

0 comments on commit 682ce8f

Please sign in to comment.