Skip to content

Commit

Permalink
Merge pull request #747 from catalyst/722-csv-reader-choke=when-no-file
Browse files Browse the repository at this point in the history
Issue 722: Throw exception when cannot open input.
keevan authored May 15, 2023
2 parents 47cbcc7 + c0aaeb1 commit 932f84a
Showing 2 changed files with 50 additions and 19 deletions.
40 changes: 21 additions & 19 deletions classes/local/step/reader_csv.php
Original file line number Diff line number Diff line change
@@ -64,28 +64,30 @@ public function csv_contents_generator() {
$strheaders = $config->headers;
$path = $this->enginestep->engine->resolve_path($config->path);

if (($handle = fopen($path, 'r')) !== false) {
try {
// Prepare and resolve headers.
if (empty($strheaders)) {
$strheaders = fgets($handle);
}
if (($handle = @fopen($path, 'r')) === false) {
throw new \moodle_exception('writer_stream:failed_to_open_stream', 'tool_dataflows', '', $path);
}

try {
// Prepare and resolve headers.
if (empty($strheaders)) {
$strheaders = fgets($handle);
}

// At this point, if headers is false, then the file is empty, and
// so it should continue as if the file finished.
if ($strheaders === false) {
return;
}
// At this point, if headers is false, then the file is empty, and
// so it should continue as if the file finished.
if ($strheaders === false) {
return;
}

// Convert header string to an actual headers array.
$headers = str_getcsv($strheaders, $config->delimiter);
while (($data = fgetcsv($handle, $maxlinelength, $config->delimiter)) !== false) {
$record = array_combine($headers, $data);
yield (object) $record;
}
} finally {
fclose($handle);
// Convert header string to an actual headers array.
$headers = str_getcsv($strheaders, $config->delimiter);
while (($data = fgetcsv($handle, $maxlinelength, $config->delimiter)) !== false) {
$record = array_combine($headers, $data);
yield (object) $record;
}
} finally {
fclose($handle);
}
}

29 changes: 29 additions & 0 deletions tests/tool_dataflows_reader_csv_test.php
Original file line number Diff line number Diff line change
@@ -36,13 +36,17 @@
*/
class tool_dataflows_reader_csv_test extends \advanced_testcase {

/** @var string|null Input path for reader to read from. */
protected $inputpath = null;

/**
* Set up before each test
*/
protected function setUp(): void {
parent::setUp();
$this->resetAfterTest();

$this->inputpath = null;
set_config('permitted_dirs', '/tmp', 'tool_dataflows');
}

@@ -127,6 +131,31 @@ public function test_csv_with_custom_headers_configured() {
$this->assertEquals($expected, $output);
}

/**
* Tests for an invalid input stream.
*/
public function test_bad_input_stream() {
[$dataflow, $steps] = $this->create_dataflow();
$path = 'path/to/nowhere';

$reader = $steps[$dataflow->steps->reader->id];
$reader->config = Yaml::dump([
'path' => $path,
'headers' => '',
'delimiter' => ',',
]);

$this->expectException('\moodle_exception');

// Execute.
ob_start();
$engine = new engine($dataflow);
$this->expectExceptionMessage(get_string('writer_stream:failed_to_open_stream',
'tool_dataflows', $engine->resolve_path($path)));
$engine->execute();
ob_get_clean();
}

/**
* Dataflow creation helper function
*

0 comments on commit 932f84a

Please sign in to comment.