diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 73109113ac5..15cd68599a1 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -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. diff --git a/packages/framework/src/Support/DataCollection.php b/packages/framework/src/Support/DataCollection.php index 21c7a728674..a551eb60c37 100644 --- a/packages/framework/src/Support/DataCollection.php +++ b/packages/framework/src/Support/DataCollection.php @@ -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); diff --git a/packages/framework/tests/Feature/DataCollectionTest.php b/packages/framework/tests/Feature/DataCollectionTest.php index 70587bdd95d..09ac73dacd0 100644 --- a/packages/framework/tests/Feature/DataCollectionTest.php +++ b/packages/framework/tests/Feature/DataCollectionTest.php @@ -33,12 +33,10 @@ public function testYamlCollections() $this->directory('resources/collections/foo'); $this->markdown('resources/collections/foo/foo.yaml', matter: ['title' => 'Foo']); $this->file('resources/collections/foo/bar.yml', "---\ntitle: Bar\n---"); - $this->file('resources/collections/foo/baz.yml'); $this->assertEquals(new DataCollection([ 'foo/foo.yaml' => new FrontMatter(['title' => 'Foo']), 'foo/bar.yml' => new FrontMatter(['title' => 'Bar']), - 'foo/baz.yml' => new FrontMatter([]), ]), DataCollection::yaml('foo')); } diff --git a/packages/framework/tests/Unit/DataCollectionUnitTest.php b/packages/framework/tests/Unit/DataCollectionUnitTest.php index 08b9ffc0404..cb64171a241 100644 --- a/packages/framework/tests/Unit/DataCollectionUnitTest.php +++ b/packages/framework/tests/Unit/DataCollectionUnitTest.php @@ -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([