Skip to content

Commit 89613c2

Browse files
authored
Merge pull request #216 from stronk7/dont_overwite_phpunit_xml
Don't overwrite the phpunit.xml file so much
2 parents c57d48e + 0779be4 commit 89613c2

File tree

6 files changed

+49
-51
lines changed

6 files changed

+49
-51
lines changed

docs/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
99
The format of this change log follows the advice given at [Keep a CHANGELOG](http://keepachangelog.com).
1010

1111
## [Unreleased]
12+
### Changed
13+
- Plugin bundled `phpunit.xml` files are not overwritten or modified ever.
14+
- For Moodle 3.9 and up, when the plugin is missing any `tests/coverage.php` file, core defaults (`lib.php`, `locallib.php`, `classes/`, ...) will be applied. Previously, all the `*.php` files were applied by default (note that older Moodle versions will continue getting them).
1215

1316
## [3.4.10] - 2023-03-14
1417
### Changed

src/Bridge/MoodlePlugin.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ public function hasFilesWithName($pattern): bool
197197
return count($result) !== 0;
198198
}
199199

200+
/**
201+
* Determine if the plugin has a (phpunit.xml) configuration file.
202+
*
203+
* @return bool
204+
*/
205+
public function hasPhpUnitConfig(): bool
206+
{
207+
return is_file($this->directory . '/phpunit.xml');
208+
}
209+
200210
/**
201211
* Determine if the plugin has any Nodejs dependencies.
202212
*

src/Installer/TestSuiteInstaller.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,15 @@ public function getPostInstallProcesses(): array
134134
$this->moodle->directory . '/admin/tool/phpunit/cli/util.php',
135135
'--buildconfig',
136136
]);
137-
$processes[] = new MoodleProcess([
138-
$this->moodle->directory . '/admin/tool/phpunit/cli/util.php',
139-
'--buildcomponentconfigs',
140-
]);
137+
// Only create the PHPUnit config file (phpunit.xml) if it does not exist.
138+
// This is to avoid overwriting the file if it has been customized by the developer.
139+
if (!$this->plugin->hasPHPUnitConfig()) {
140+
$this->getOutput()->debug('Build PHPUnit component config');
141+
$processes[] = new MoodleProcess([
142+
$this->moodle->directory . '/admin/tool/phpunit/cli/util.php',
143+
'--buildcomponentconfigs',
144+
]);
145+
}
141146
}
142147

143148
return $processes;
@@ -157,24 +162,35 @@ public function injectPHPUnitFilter(): void
157162
// section is already configured following it. Nothing to do here.
158163
$coverage = $this->plugin->directory . '/tests/coverage.php';
159164
// If the file exists and we are Moodle >= 3.7.
160-
// TODO: Remove the branch condition when 3.6 becomes unsupported by moodle-local-ci.
165+
// TODO: Remove the branch condition when 3.6 becomes unsupported by moodle-plugin-ci.
161166
if ($this->moodle->getBranch() >= 37 && is_readable($coverage)) {
162167
return;
163168
}
164169

170+
// If the coverage.php file does not exist, and we are Moodle < 3.9, then try to inject the default
171+
// filter/coverage information. Note that, for Moodle 3.9 and up, the filter/coverage information
172+
// with good defaults is already present in the phpunit.xml file. See MDL-72701.
173+
// TODO: Remove the whole code in the function after this line, when 3.8 becomes unsupported by moodle-plugin-ci.
174+
if ($this->moodle->getBranch() >= 39) {
175+
return;
176+
}
177+
165178
$files = $this->getCoverageFiles();
166179
$filterXml = $this->getFilterXml($files);
167180
$subject = file_get_contents($config);
168181
$count = 0;
169182

170183
// Replace existing filter.
171184
$contents = preg_replace('/<coverage>(.|\n)*<\/coverage>/m', trim($filterXml), $subject, 1, $count);
172-
// TODO: Remove this when 3.10 becomes unsupported by moodle-local-ci.
185+
// TODO: Remove this when 3.10 becomes unsupported by moodle-plugin-ci.
173186
if ($this->moodle->getBranch() < 311) {
174187
$contents = preg_replace('/<filter>(.|\n)*<\/filter>/m', trim($filterXml), $subject, 1, $count);
175188
}
176189

177190
// Or if no existing filter, inject the filter.
191+
// We only do this for Moodle < 3.9, because in Moodle 3.9+ the filter with better defaults
192+
// is already present in the phpunit.xml file. See MDL-72701.
193+
// TODO: Remove this when 3.8 becomes unsupported by moodle-plugin-ci.
178194
if ($count === 0) {
179195
$contents = str_replace('</phpunit>', $filterXml . '</phpunit>', $subject, $count);
180196
}

tests/Bridge/MoodlePluginTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ public function testHasUnitTests()
4545
$this->assertTrue($plugin->hasUnitTests());
4646
}
4747

48+
public function testHasPhpUnitConfig()
49+
{
50+
// Our plugins doesn't have a phpunit.xml file.
51+
$plugin = new MoodlePlugin($this->pluginDir);
52+
$this->assertFalse($plugin->hasPhpUnitConfig());
53+
54+
// Let's create one.
55+
$this->fs->dumpFile($this->pluginDir . '/phpunit.xml', '');
56+
$this->assertTrue($plugin->hasPhpUnitConfig());
57+
}
58+
4859
public function testNoUnitTests()
4960
{
5061
// Remove the only unit test file.

tests/Fixture/phpunit/phpunit-expected-311.xml

Lines changed: 0 additions & 42 deletions
This file was deleted.

tests/Installer/TestSuiteInstallerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ public function testPHPUnitXMLFile311()
112112
new DummyExecute()
113113
);
114114

115-
// Test Moodle 3.11 PHPUnit XML file.
115+
// Test Moodle 3.11 PHPUnit XML file. Nothing is changed.
116116
$this->fs->copy(__DIR__ . '/../Fixture/phpunit/phpunit-311.xml', $xmlFile, true);
117117
$installer->injectPHPUnitFilter();
118-
$this->assertXmlFileEqualsXmlFile(__DIR__ . '/../Fixture/phpunit/phpunit-expected-311.xml', $xmlFile);
118+
$this->assertXmlFileEqualsXmlFile(__DIR__ . '/../Fixture/phpunit/phpunit-311.xml', $xmlFile);
119119

120-
// Test Moodle 3.11 PHPUnit XML file when coverage.php is available.
120+
// Test Moodle 3.11 PHPUnit XML file when coverage.php is available. Nothing is changed either.
121121
$this->fs->copy(__DIR__ . '/../Fixture/phpunit/phpunit-311.xml', $xmlFile, true);
122122
$this->fs->copy(__DIR__ . '/../Fixture/phpunit/coverage.php', $this->pluginDir . '/tests/coverage.php', true);
123123
$installer->injectPHPUnitFilter();

0 commit comments

Comments
 (0)