Skip to content

Commit ede8151

Browse files
hype09sebastianfeldmann
authored andcommitted
Fix missing error message with tar (fixes #314)
When backing up from a `tar` source, phpbu used to check if the specified path exists during the `::setup()` stage, not the actual `::backup()` stage. This might cause phpbu to die by an "uncaught" exception (aka. no logging / email being sent). This commit removes the existence check from the `::setup()`, which already doubled an existing check in the `::backup()` stage (through `\phpbu\App\Backup\Source\Tar::validatePath`).
1 parent cc121aa commit ede8151

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/Backup/Source/Tar.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,6 @@ protected function setupPath(array $conf)
224224
throw new Exception('path option is mandatory');
225225
}
226226
$this->path = Util\Path::toAbsolutePath($path, Configuration::getWorkingDirectory());
227-
if (!file_exists($this->path)) {
228-
throw new Exception('could not find directory to compress');
229-
}
230227
}
231228

232229
/**
@@ -329,10 +326,14 @@ private function handleIncrementalBackup(Executable\Tar $executable): void
329326
*
330327
* @throws \phpbu\App\Exception
331328
*/
332-
private function validatePath()
329+
private function validatePath(): void
333330
{
331+
if (!file_exists($this->path)) {
332+
throw new Exception(sprintf('Could not find directory to compress at "%s".', $this->path));
333+
}
334+
334335
if (!is_dir($this->path)) {
335-
throw new Exception('path to compress has to be a directory');
336+
throw new Exception(sprintf('Cannot compress at path "%s": not a directory.', $this->path));
336337
}
337338
}
338339

tests/phpbu/Backup/Source/TarTest.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@ public function testSetupPathMissing()
4040
/**
4141
* Tests Tar::setUp
4242
*/
43-
public function testSetupPathDoesNotExist()
43+
public function testSetupPathDoesNotExistDoesNotThrowException(): void
4444
{
45-
$this->expectException('phpbu\App\Exception');
45+
$this->expectNotToPerformAssertions();
46+
4647
$tar = new Tar();
4748
$tar->setup(['path' => getcwd() . '/foo']);
48-
49-
$this->assertFalse(true, 'exception should be thrown');
5049
}
5150

5251
/**
@@ -519,9 +518,11 @@ public function testBackupOkUnsupportedCompression()
519518
/**
520519
* Tests Tar::backup
521520
*/
522-
public function testBackupInvalidPath()
521+
public function testBackupThrowsExceptionWhenPathPointsToFile(): void
523522
{
524523
$this->expectException('phpbu\App\Exception');
524+
$this->expectExceptionMessage(sprintf('Cannot compress at path "%s": not a directory.', __FILE__));
525+
525526
$runner = $this->getRunnerMock();
526527
$tar = new Tar($runner);
527528
$tar->setup(['pathToTar' => PHPBU_TEST_BIN, 'path' => __FILE__]);
@@ -532,6 +533,21 @@ public function testBackupInvalidPath()
532533
$tar->backup($target, $appResult);
533534
}
534535

536+
public function testBackupThrowsExceptionWhenNoDirectoryExistsAtPath(): void
537+
{
538+
$this->expectException('phpbu\App\Exception');
539+
$this->expectExceptionMessage('Could not find directory to compress at "/some/directory/that/does/not/exist".');
540+
541+
$runner = $this->getRunnerMock();
542+
$tar = new Tar($runner);
543+
$tar->setup(['pathToTar' => PHPBU_TEST_BIN, 'path' => '/some/directory/that/does/not/exist']);
544+
545+
$target = $this->createTargetMock('/tmp/backup.tar');
546+
$appResult = $this->getAppResultMock();
547+
548+
$tar->backup($target, $appResult);
549+
}
550+
535551
/**
536552
* Tests Tar::backup
537553
*/

0 commit comments

Comments
 (0)