Skip to content

Commit

Permalink
[ReplaceRegexpTask] Added failOnError support (#1866)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
siad007 authored Sep 14, 2024
1 parent b2af8ce commit 593141c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
3 changes: 3 additions & 0 deletions etc/phing-grammar.rng
Original file line number Diff line number Diff line change
Expand Up @@ -5956,6 +5956,9 @@
<attribute name="replace"/>
<attribute name="flags"/>
<attribute name="byLine"/>
<attribute name="failonerror" a:defaultValue="false">
<data type="boolean"/>
</attribute>
</optional>
</interleave>
</element>
Expand Down
20 changes: 18 additions & 2 deletions src/Phing/Task/System/ReplaceRegexpTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Phing/Test/Task/System/ReplaceRegexpTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:"
);
}
}
6 changes: 6 additions & 0 deletions tests/etc/tasks/system/ReplaceRegexpTaskTest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@
match="OldProperty=(.*)"
replace="NewProperty=\1"/>
</target>
<target name="testFailOnError">
<replaceregexp file="nofile"
match="OldProperty=(.*)"
replace="NewProperty=\1"
failonerror="true"/>
</target>
</project>

0 comments on commit 593141c

Please sign in to comment.