Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Bag.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,10 @@ public function package(string $filepath): void
if (!$this->isValid()) {
throw new BagItException("Bag is not valid, cannot package.");
}
$parent = dirname($filepath);
if (!is_dir($parent)) {
BagUtils::checkedMkdir($parent, 0777, true);
}
$this->makePackage($filepath);
}

Expand Down
33 changes: 33 additions & 0 deletions tests/BagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,39 @@ public function testZipBag(): void
);
}

/**
* Archive destinations may include directories that do not exist yet.
*
* @dataProvider nestedArchiveFormats
* @covers ::package
*/
public function testPackageCreatesParentDirectory(string $extension): void
{
mkdir($this->tmpdir);
$bag = Bag::create($this->tmpdir . "/bag");
$bag->addFile(self::TEST_IMAGE['filename'], "data/image.jpg");
$archive = $this->tmpdir . "/new/nested/archive." . $extension;
$this->assertDirectoryDoesNotExist(dirname($archive));
$bag->package($archive);
$this->assertFileExists($archive);
$restored = Bag::load($archive);
try {
$this->assertTrue($restored->isValid());
$this->assertSame(
$bag->getPayloadManifests()['sha512']->getHashes(),
$restored->getPayloadManifests()['sha512']->getHashes()
);
} finally {
self::deleteDirAndContents(dirname($restored->getBagRoot()));
}
}

/** @return array<array<string>> */
public static function nestedArchiveFormats(): array
{
return [['tar'], ['tar.gz'], ['tgz'], ['tar.bz2'], ['zip']];
}

/**
* Test generating a tar.
*
Expand Down
Loading