From 593141c3a5add898ee1906f4812a1131ec750ad6 Mon Sep 17 00:00:00 2001 From: Siad Ardroumli Date: Sat, 14 Sep 2024 18:41:25 +0200 Subject: [PATCH] [ReplaceRegexpTask] Added failOnError support (#1866) * [ReplaceRegexpTask] Added fail on error support * Update ReplaceRegexpTask.php * Update ReplaceRegexpTask.php * Update phing-grammar.rng * Update ReplaceRegexpTaskTest.xml * Update ReplaceRegexpTaskTest.php * Update ReplaceRegexpTaskTest.php --- etc/phing-grammar.rng | 3 +++ src/Phing/Task/System/ReplaceRegexpTask.php | 20 +++++++++++++++++-- .../Task/System/ReplaceRegexpTaskTest.php | 9 +++++++++ .../tasks/system/ReplaceRegexpTaskTest.xml | 6 ++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/etc/phing-grammar.rng b/etc/phing-grammar.rng index 57919974fb..1405e975dc 100644 --- a/etc/phing-grammar.rng +++ b/etc/phing-grammar.rng @@ -5956,6 +5956,9 @@ + + + diff --git a/src/Phing/Task/System/ReplaceRegexpTask.php b/src/Phing/Task/System/ReplaceRegexpTask.php index 38726f4540..799b7426b8 100644 --- a/src/Phing/Task/System/ReplaceRegexpTask.php +++ b/src/Phing/Task/System/ReplaceRegexpTask.php @@ -65,6 +65,16 @@ class ReplaceRegexpTask extends Task */ private $regexp; + private $failonerror = false; + + /** + * If false, note errors but continue. + */ + public function setFailOnError($failonerror) + { + $this->failonerror = $failonerror; + } + /** * File to apply regexp on. */ @@ -200,7 +210,10 @@ public function main() if ($in) { $in->close(); } - $this->log('Error reading file: ' . $e->getMessage(), Project::MSG_WARN); + $this->log('Error reading file: ' . $e->getMessage(), Project::MSG_ERR); + if ($this->failonerror) { + throw new BuildException("Error reading file: '" . $file->getAbsolutePath() . "'", $e); + } } try { @@ -213,7 +226,10 @@ public function main() if ($out) { $out->close(); } - $this->log('Error writing file back: ' . $e->getMessage(), Project::MSG_WARN); + $this->log('Error writing file back: ' . $e->getMessage(), Project::MSG_ERR); + if ($this->failonerror) { + throw new BuildException("Error writing file back: '" . $file->getAbsolutePath() . "'", $e); + } } } } diff --git a/tests/Phing/Test/Task/System/ReplaceRegexpTaskTest.php b/tests/Phing/Test/Task/System/ReplaceRegexpTaskTest.php index b609cd6a49..82eb1f75d5 100644 --- a/tests/Phing/Test/Task/System/ReplaceRegexpTaskTest.php +++ b/tests/Phing/Test/Task/System/ReplaceRegexpTaskTest.php @@ -50,4 +50,13 @@ public function testReplaceRegexp(): void $this->executeTarget(__FUNCTION__); $this->assertStringEqualsFile('test.properties', 'NewProperty=12345'); } + + public function testFailOnError(): void + { + $this->expectBuildExceptionContaining( + __FUNCTION__, + 'failonerror has to fail', + "Error reading file:" + ); + } } diff --git a/tests/etc/tasks/system/ReplaceRegexpTaskTest.xml b/tests/etc/tasks/system/ReplaceRegexpTaskTest.xml index ff410f6f1f..af4da68e82 100644 --- a/tests/etc/tasks/system/ReplaceRegexpTaskTest.xml +++ b/tests/etc/tasks/system/ReplaceRegexpTaskTest.xml @@ -11,4 +11,10 @@ match="OldProperty=(.*)" replace="NewProperty=\1"/> + + +