Skip to content

Commit

Permalink
Merge branch '6.4' into 7.0
Browse files Browse the repository at this point in the history
* 6.4: (22 commits)
  fix merge
  [AssetMapper] Check asset/vendor directory is writable
  detect wrong e-mail validation modes
  detect wrong usages of minMessage/maxMessage in options
  [Security] Remove workflow from empty folder
  read form values using the chain data accessor
  [Validator] Review Bulgarian (bg) translation
  Reviewed italian translation
  Fix french translation
  call substr() with integer offsets
  [Finder] Also consider .git inside the basedir of in() directory
  review: FR translation
  [Serializer] Add AbstractNormalizerContextBuilder::defaultConstructorArguments()
  Update spanish and catalan translations
  Update AbstractSchemaListener.php to adjust more database params
  Updated id=113 Arabic translation.
  #53771 Updated validator Lithuanian translations
  review: translation RU
  [PropertyInfo] Fix PHPStan properties type in trait
  explicitly cast boolean SSL stream options
  ...
  • Loading branch information
xabbuh committed Apr 28, 2024
2 parents 791162d + 8f0348b commit 0643f48
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
10 changes: 8 additions & 2 deletions ImportMap/RemotePackageStorage.php
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\AssetMapper\ImportMap;

use Symfony\Component\AssetMapper\Exception\RuntimeException;

/**
* Manages the local storage of remote/vendor importmap packages.
*/
Expand Down Expand Up @@ -52,7 +54,9 @@ public function save(ImportMapEntry $entry, string $contents): void
$vendorPath = $this->getDownloadPath($entry->packageModuleSpecifier, $entry->type);

@mkdir(\dirname($vendorPath), 0777, true);
file_put_contents($vendorPath, $contents);
if (false === @file_put_contents($vendorPath, $contents)) {
throw new RuntimeException(error_get_last()['message'] ?? sprintf('Failed to write file "%s".', $vendorPath));
}
}

public function saveExtraFile(ImportMapEntry $entry, string $extraFilename, string $contents): void
Expand All @@ -64,7 +68,9 @@ public function saveExtraFile(ImportMapEntry $entry, string $extraFilename, stri
$vendorPath = $this->getExtraFileDownloadPath($entry, $extraFilename);

@mkdir(\dirname($vendorPath), 0777, true);
file_put_contents($vendorPath, $contents);
if (false === @file_put_contents($vendorPath, $contents)) {
throw new RuntimeException(error_get_last()['message'] ?? sprintf('Failed to write file "%s".', $vendorPath));
}
}

/**
Expand Down
16 changes: 8 additions & 8 deletions Tests/ImportMap/ImportMapManagerTest.php
Expand Up @@ -201,15 +201,15 @@ public static function getRequirePackageTests(): iterable
];

yield 'single_package_with_a_path' => [
'packages' => [new PackageRequireOptions('some/module', path: self::$writableRoot.'/assets/some_file.js')],
'expectedProviderPackageArgumentCount' => 0,
'resolvedPackages' => [],
'expectedImportMap' => [
'some/module' => [
// converted to relative path
'path' => './assets/some_file.js',
'packages' => [new PackageRequireOptions('some/module', path: self::$writableRoot.'/assets/some_file.js')],
'expectedProviderPackageArgumentCount' => 0,
'resolvedPackages' => [],
'expectedImportMap' => [
'some/module' => [
// converted to relative path
'path' => './assets/some_file.js',
],
],
],
];
}

Expand Down
29 changes: 23 additions & 6 deletions Tests/ImportMap/RemotePackageStorageTest.php
Expand Up @@ -25,7 +25,7 @@ class RemotePackageStorageTest extends TestCase
protected function setUp(): void
{
$this->filesystem = new Filesystem();
if (!file_exists(self::$writableRoot)) {
if (!$this->filesystem->exists(self::$writableRoot)) {
$this->filesystem->mkdir(self::$writableRoot);
}
}
Expand All @@ -41,14 +41,30 @@ public function testGetStorageDir()
$this->assertSame(realpath(self::$writableRoot.'/assets/vendor'), realpath($storage->getStorageDir()));
}

public function testSaveThrowsWhenVendorDirectoryIsNotWritable()
{
$this->filesystem->mkdir($vendorDir = self::$writableRoot.'/assets/acme/vendor');
$this->filesystem->chmod($vendorDir, 0555);

$storage = new RemotePackageStorage($vendorDir);
$entry = ImportMapEntry::createRemote('foo', ImportMapType::JS, '/does/not/matter', '1.0.0', 'module_specifier', false);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('file_put_contents('.$vendorDir.'/module_specifier/module_specifier.index.js): Failed to open stream: No such file or directory');
$storage->save($entry, 'any content');

$this->filesystem->remove($vendorDir);
}

public function testIsDownloaded()
{
$storage = new RemotePackageStorage(self::$writableRoot.'/assets/vendor');
$entry = ImportMapEntry::createRemote('foo', ImportMapType::JS, '/does/not/matter', '1.0.0', 'module_specifier', false);
$this->assertFalse($storage->isDownloaded($entry));

$targetPath = self::$writableRoot.'/assets/vendor/module_specifier/module_specifier.index.js';
@mkdir(\dirname($targetPath), 0777, true);
file_put_contents($targetPath, 'any content');
$this->filesystem->mkdir(\dirname($targetPath));
$this->filesystem->dumpFile($targetPath, 'any content');
$this->assertTrue($storage->isDownloaded($entry));
}

Expand All @@ -57,9 +73,10 @@ public function testIsExtraFileDownloaded()
$storage = new RemotePackageStorage(self::$writableRoot.'/assets/vendor');
$entry = ImportMapEntry::createRemote('foo', ImportMapType::JS, '/does/not/matter', '1.0.0', 'module_specifier', false);
$this->assertFalse($storage->isExtraFileDownloaded($entry, '/path/to/extra.woff'));

$targetPath = self::$writableRoot.'/assets/vendor/module_specifier/path/to/extra.woff';
@mkdir(\dirname($targetPath), 0777, true);
file_put_contents($targetPath, 'any content');
$this->filesystem->mkdir(\dirname($targetPath));
$this->filesystem->dumpFile($targetPath, 'any content');
$this->assertTrue($storage->isExtraFileDownloaded($entry, '/path/to/extra.woff'));
}

Expand Down Expand Up @@ -92,7 +109,7 @@ public function testGetDownloadedPath(string $packageModuleSpecifier, ImportMapT
$this->assertSame($expectedPath, $storage->getDownloadPath($packageModuleSpecifier, $importMapType));
}

public static function getDownloadPathTests()
public static function getDownloadPathTests(): iterable
{
yield 'javascript bare package' => [
'packageModuleSpecifier' => 'foo',
Expand Down

0 comments on commit 0643f48

Please sign in to comment.