Unverified Commit 45f97bcf authored by alexpott's avatar alexpott

Issue #3046298 by Gogowitsch, voleger, jhedstrom, larowlan: DX: tell developer...

Issue #3046298 by Gogowitsch, voleger, jhedstrom, larowlan: DX: tell developer which yaml file caused a ParseException
parent ffb1e1e5
......@@ -2,8 +2,9 @@
namespace Drupal\Component\Discovery;
use Drupal\Component\Serialization\Yaml;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Component\Serialization\Yaml;
/**
* Provides discovery for YAML files within a given set of directories.
......@@ -74,11 +75,17 @@ public function findAll() {
*
* @param string $file
* Yaml file path.
*
* @return array
*/
protected function decode($file) {
try {
return Yaml::decode(file_get_contents($file)) ?: [];
}
catch (InvalidDataTypeException $e) {
throw new InvalidDataTypeException($file . ': ' . $e->getMessage(), $e->getCode(), $e);
}
}
/**
* Returns an array of file paths, keyed by provider.
......
......@@ -15,6 +15,8 @@ interface SerializationInterface {
*
* @return string
* The encoded data.
*
* @throws \Drupal\Component\Serialization\Exception\InvalidDataTypeException
*/
public static function encode($data);
......@@ -26,6 +28,8 @@ public static function encode($data);
*
* @return mixed
* The decoded data.
*
* @throws \Drupal\Component\Serialization\Exception\InvalidDataTypeException
*/
public static function decode($raw);
......
......@@ -3,6 +3,7 @@
namespace Drupal\Core\Discovery;
use Drupal\Component\Discovery\YamlDiscovery as ComponentYamlDiscovery;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Core\Serialization\Yaml;
/**
......@@ -16,7 +17,12 @@ class YamlDiscovery extends ComponentYamlDiscovery {
* {@inheritdoc}
*/
protected function decode($file) {
try {
return Yaml::decode(file_get_contents($file)) ?: [];
}
catch (InvalidDataTypeException $e) {
throw new InvalidDataTypeException($file . ': ' . $e->getMessage(), $e->getCode(), $e);
}
}
}
......@@ -4,9 +4,10 @@
use Drupal\Component\Discovery\YamlDiscovery;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamWrapper;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamWrapper;
use PHPUnit\Framework\TestCase;
/**
......@@ -67,4 +68,24 @@ public function testDiscovery() {
$this->assertSame([], $data['test_4']);
}
/**
* Tests if filename is output for a broken YAML file.
*/
public function testForBrokenYml() {
vfsStreamWrapper::register();
$root = new vfsStreamDirectory('modules');
vfsStreamWrapper::setRoot($root);
$url = vfsStream::url('modules');
mkdir($url . '/test_broken');
file_put_contents($url . '/test_broken/test_broken.test.yml', "broken:\n:");
$this->expectException(InvalidDataTypeException::class);
$this->expectExceptionMessage('vfs://modules/test_broken/test_broken.test.yml');
$directories = ['test_broken' => $url . '/test_broken'];
$discovery = new YamlDiscovery('test', $directories);
$discovery->findAll();
}
}
<?php
namespace Drupal\Tests\Core\Discovery;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Core\Discovery\YamlDiscovery;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamWrapper;
use PHPUnit\Framework\TestCase;
/**
* YamlDiscovery component unit tests.
*
* @group Discovery
*/
class YamlDiscoveryTest extends TestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
// Ensure that FileCacheFactory has a prefix.
FileCacheFactory::setPrefix('prefix');
}
/**
* Tests if filename is output for a broken YAML file.
*/
public function testFilenameForBrokenYml() {
vfsStreamWrapper::register();
$root = new vfsStreamDirectory('modules');
vfsStreamWrapper::setRoot($root);
$url = vfsStream::url('modules');
mkdir($url . '/test_broken');
file_put_contents($url . '/test_broken/test_broken.test.yml', "broken:\n:");
$this->expectException(InvalidDataTypeException::class);
$this->expectExceptionMessage('vfs://modules/test_broken/test_broken.test.yml');
$directories = ['test_broken' => $url . '/test_broken'];
$discovery = new YamlDiscovery('test', $directories);
$discovery->findAll();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment