Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PrefixLines test #1168

Merged
merged 2 commits into from
Oct 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion classes/phing/filters/BaseFilterReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function read($len = null)
* Reads a line of text ending with '\n' (or until the end of the stream).
* The returned String retains the '\n'.
*
* @return string the line read, or <code>null</code> if the end of the
* @return string|null the line read, or <code>null</code> if the end of the
* stream has already been reached
*
* @throws IOException if the underlying reader throws one during
Expand Down
40 changes: 26 additions & 14 deletions classes/phing/filters/PrefixLines.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ class PrefixLines extends BaseParamFilterReader implements ChainableReader
*
* @var string
*/
private $prefix = null;
private $prefix;

/** @var string|null $queuedData */
private $queuedData;

/**
* Adds a prefix to each line of input stream and returns resulting stream.
*
* @param int $len
* @param int $len
* @return mixed buffer, -1 on EOF
* @throws IOException
*/
public function read($len = null)
{
Expand All @@ -64,23 +68,31 @@ public function read($len = null)
$this->setInitialized(true);
}

$buffer = $this->in->read($len);
$ch = -1;

if ($buffer === -1) {
return -1;
if ($this->queuedData !== null && $this->queuedData === '') {
$this->queuedData = null;
}

$lines = explode("\n", $buffer);
$filtered = [];

foreach ($lines as $line) {
$line = $this->prefix . $line;
$filtered[] = $line;
if ($this->queuedData !== null) {
$ch = $this->queuedData[0];
$this->queuedData = (string) substr($this->queuedData, 1);
if ($this->queuedData === '') {
$this->queuedData = null;
}
} else {
$this->queuedData = $this->readLine();
if ($this->queuedData === null) {
$ch = -1;
} else {
if ($this->prefix !== null) {
$this->queuedData = $this->prefix . $this->queuedData;
}
return $this->read();
}
}

$filtered_buffer = implode("\n", $filtered);

return $filtered_buffer;
return $ch;
}

/**
Expand Down
47 changes: 47 additions & 0 deletions test/classes/phing/filters/PrefixLinesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

/**
* @author Siad Ardroumli <siad.ardroumli@gmail.com>
* @package phing.filters
*/
class PrefixLinesTest extends BuildFileTest
{
public function setUp(): void
{
$this->configureProject(PHING_TEST_BASE . "/etc/filters/prefixlines.xml");
}

public function tearDown(): void
{
$this->executeTarget("cleanup");
}

public function testPrefixLines()
{
$this->executeTarget("testPrefixLines");

$result = $this->getProject()->resolveFile("result/prefixlines.test");

$this->assertStringEqualsFile(
$result->getAbsolutePath(),
'FooThis is line 1 with alpha.FooThis is line 2 with beta.FooThis is line 3 with gamma.Foo'
);
}
}
3 changes: 3 additions & 0 deletions test/etc/filters/input/prefixlines.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is line 1 with alpha.
This is line 2 with beta.
This is line 3 with gamma.
42 changes: 42 additions & 0 deletions test/etc/filters/prefixlines.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0"?>
<!--
~ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
~ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
~ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
~ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
~ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
~ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
~ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
~ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
~ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
~ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
~ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
~
~ This software consists of voluntary contributions made by many individuals
~ and is licensed under the LGPL. For more information please see
~ <http://phing.info>.
-->

<project default="cleanup">

<target name="init">
<mkdir dir="result" />
</target>

<target name="cleanup">
<delete dir="result"/>
</target>

<target name="testPrefixLines" depends="init">
<copy todir="result">
<fileset dir="input"/>
<filterchain>
<filterreader classname="phing.filters.PrefixLines">
<param name="lines" value="Foo"/>
</filterreader>
<striplinebreaks />
</filterchain>
</copy>
</target>

</project>