Skip to content

Commit

Permalink
Throw parse exception for empty Yaml to match json_validate
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Jun 25, 2024
1 parent b217795 commit 5dcb64a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,4 @@ InvalidArgumentException: Invalid JSON in file: 'foo/baz.json' (Syntax error)
```

In order to normalize the thrown exceptions, we now rethrow Yaml `ParseException` as `InvalidArgumentException` to match the JSON validation.
Additionally, an exception will be thrown if a JSON or YAML file is empty, as this is unlikely to be intentional.
4 changes: 4 additions & 0 deletions packages/framework/src/Support/DataCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public static function yaml(string $name): static
$content = Str::between($content, '---', '---');

try {
if (blank(trim($content))) {
throw new ParseException('File is empty');
}

$parsed = Yaml::parse($content) ?: [];
} catch (ParseException $exception) {
throw new InvalidArgumentException(sprintf("Invalid YAML in file: '%s' (%s)", $file, $exception->getMessage()), previous: $exception);
Expand Down
24 changes: 24 additions & 0 deletions packages/framework/tests/Unit/DataCollectionUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,30 @@ public function testYamlCollectionsThrowExceptionForInvalidYaml()
MockableDataCollection::yaml('foo');
}

public function testYamlCollectionsThrowExceptionForEmptyYaml()
{
MockableDataCollection::mockFiles([
'foo/bar.yml' => '',
]);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Invalid YAML in file: 'foo/bar.yml' (File is empty)");

MockableDataCollection::yaml('foo');
}

public function testYamlCollectionsThrowExceptionForBlankYaml()
{
MockableDataCollection::mockFiles([
'foo/bar.yml' => ' ',
]);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Invalid YAML in file: 'foo/bar.yml' (File is empty)");

MockableDataCollection::yaml('foo');
}

public function testYamlCollectionsThrowExceptionForOtherReasonsThanSyntaxErrorWithUtfError()
{
MockableDataCollection::mockFiles([
Expand Down

0 comments on commit 5dcb64a

Please sign in to comment.