Skip to content

Commit

Permalink
fix(assets): throw exception when manifest is missing (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
QWp6t authored Dec 20, 2021
1 parent f2cefb7 commit 2275787
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Roots/Acorn/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function usePaths(array $paths)
throw new Exception("The {$path_type} path type is not supported.");
}

if (! is_dir($path) || ! is_readable($path)) {
if ($path_type !== 'public' && (! is_dir($path) || ! is_readable($path))) {
throw new Exception("The {$path} directory must be present.");
}

Expand Down
10 changes: 10 additions & 0 deletions src/Roots/Acorn/Assets/Contracts/ManifestNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Roots\Acorn\Assets\Contracts;

use Exception;

class ManifestNotFoundException extends Exception
{
//
}
7 changes: 6 additions & 1 deletion src/Roots/Acorn/Assets/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use InvalidArgumentException;
use Roots\Acorn\Assets\Concerns\Mixable;
use Roots\Acorn\Assets\Contracts\Manifest as ManifestContract;
use Roots\Acorn\Assets\Contracts\ManifestNotFoundException;

/**
* Manage assets manifests
Expand Down Expand Up @@ -99,7 +100,11 @@ protected function resolve(string $name, ?array $config): ManifestContract
*/
protected function getJsonManifest(string $jsonManifest): array
{
return file_exists($jsonManifest) ? json_decode(file_get_contents($jsonManifest), true) : [];
if (! file_exists($jsonManifest)) {
throw new ManifestNotFoundException("The manifest [{$jsonManifest}] cannot be found.");
}

return json_decode(file_get_contents($jsonManifest), true) ?? [];
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Application/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@
]);
})->throws(Exception::class);

it('allows invalid public path', function () {
$app = new Application();

$app->usePaths([
'app' => $this->fixture('use_paths/app'),
'public' => __DIR__ . '/this/does/not/exist',
]);
})->not->throws(Exception::class);

it('accepts an array of custom paths', function () {
$app = new Application(temp('base_path'));

Expand Down
29 changes: 29 additions & 0 deletions tests/Assets/ManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Roots\Acorn\Assets\Contracts\Manifest as ManifestContract;
use Roots\Acorn\Assets\Contracts\ManifestNotFoundException;
use Roots\Acorn\Assets\Manager;
use Roots\Acorn\Assets\Manifest;
use Roots\Acorn\Tests\Test\TestCase;
Expand Down Expand Up @@ -33,6 +34,20 @@
expect($assets->manifest('theme'))->toBeInstanceOf(ManifestContract::class);
});

it('throws an error if an assets manifest does not exist', function () {
$assets = new Manager([
'manifests' => [
'theme' => [
'path' => $this->fixture('bud_single_runtime'),
'url' => 'https://k.jo',
'assets' => __DIR__ . '/does/not/exist/manifest.json',
]
]
]);

$assets->manifest('theme')->asset('app.css')->uri();
})->throws(ManifestNotFoundException::class);

it('reads an assets manifest', function () {
$assets = new Manager([
'manifests' => [
Expand All @@ -48,6 +63,20 @@
assertMatchesSnapshot($assets->manifest('theme')->asset('app.js')->uri());
});

it('throws an error if a bundles manifest does not exist', function () {
$assets = new Manager([
'manifests' => [
'theme' => [
'path' => $this->fixture('bud_single_runtime'),
'url' => 'https://k.jo',
'bundles' => __DIR__ . '/does/not/exist/entrypoints.json',
]
]
]);

$assets->manifest('theme')->bundle('app')->js()->toJson();
})->throws(ManifestNotFoundException::class);

it('reads a bundles manifest', function () {
$assets = new Manager([
'manifests' => [
Expand Down

0 comments on commit 2275787

Please sign in to comment.