Skip to content

Commit

Permalink
CM-873 fix test and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasfejfar committed Jan 10, 2024
1 parent 76afdee commit 362e56f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/CsvReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class CsvReader extends AbstractCsvFile implements Iterator
* @deprecated use Keboola\Csv\CsvOptions::DEFAULT_ENCLOSURE
*/
const DEFAULT_ESCAPED_BY = CsvOptions::DEFAULT_ESCAPED_BY;
const SAMPLE_SIZE = 10000;

/**
* @var int
Expand Down Expand Up @@ -124,7 +125,12 @@ protected function openCsvFile($fileName)
protected function detectLineBreak()
{
@rewind($this->getFilePointer());
$sample = @fread($this->getFilePointer(), 10000);
$sample = @fread($this->getFilePointer(), self::SAMPLE_SIZE);
if (substr($sample, -1) === "\r") {
// we might have hit the file in the middle of CR+LF, only getting CR
@rewind($this->getFilePointer());
$sample = @fread($this->getFilePointer(), self::SAMPLE_SIZE+1);
}

return LineBreaksHelper::detectLineBreaks($sample, $this->getEnclosure(), $this->getEscapedBy());
}
Expand Down
3 changes: 2 additions & 1 deletion tests/CsvReadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public function testColumnsCount()

public function testNewlineDetectionEdgecaseWithCrLf()
{
$this->expectExceptionMessage('Invalid line break. Please use unix \n or win \r\n line breaks.');
$this->expectNotToPerformAssertions();
// this used to throw "Invalid line break. Please use unix \n or win \r\n line breaks." before the fix
new CsvReader(__DIR__ . '/data/test-input-edgecase.crlf.csv');
}

Expand Down

0 comments on commit 362e56f

Please sign in to comment.