diff --git a/Makefile b/Makefile index dc7194e..4581296 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,24 @@ -analyze: analyze-phpmd analyze-phpstan analyze-psalm analyze-phpcs - +analyze: analyze-yaml analyze-phpmd analyze-phpstan analyze-psalm analyze-phpcs + +analyze-yaml: + vendor/bin/yaml-lint config.yml analyze-phpmd: - ./vendor/bin/phpmd src text cleancode,codesize,controversial,design,naming,unusedcode + vendor/bin/phpmd src text cleancode,codesize,controversial,design,naming,unusedcode analyze-phpstan: - ./vendor/bin/phpstan analyze --level=7 --error-format=raw src/ backup + vendor/bin/phpstan analyze --level=7 --error-format=raw src/ backup analyze-psalm: - ./vendor/bin/psalm + vendor/bin/psalm analyze-phpcs: - ./vendor/bin/phpcs src backup --report=emacs --standard=PSR12 + vendor/bin/phpcs src backup --report=emacs --standard=PSR12 install: php composer.phar install --no-dev install-dev: php composer.phar install + sudo apt update + sudo apt install rclone test: - ./vendor/bin/phpunit tests --testdox --coverage-filter src --coverage-html output/coverage + vendor/bin/phpunit tests +test-coverage: + vendor/bin/phpunit tests --testdox --coverage-filter src --coverage-html output/coverage --coverage-text \ No newline at end of file diff --git a/composer.lock b/composer.lock index 50bb8cb..38b9af3 100644 --- a/composer.lock +++ b/composer.lock @@ -2922,16 +2922,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.18", + "version": "1.10.19", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "52b6416c579663eebdd2f1d97df21971daf3b43f" + "reference": "af5a296ff02610c1bfb4ddfac9fd4a08657b9046" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/52b6416c579663eebdd2f1d97df21971daf3b43f", - "reference": "52b6416c579663eebdd2f1d97df21971daf3b43f", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/af5a296ff02610c1bfb4ddfac9fd4a08657b9046", + "reference": "af5a296ff02610c1bfb4ddfac9fd4a08657b9046", "shasum": "" }, "require": { @@ -2980,7 +2980,7 @@ "type": "tidelift" } ], - "time": "2023-06-07T22:00:43+00:00" + "time": "2023-06-14T15:26:58+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/src/CommandBackup.php b/src/CommandBackup.php index 6ca06dc..bd23081 100644 --- a/src/CommandBackup.php +++ b/src/CommandBackup.php @@ -62,17 +62,13 @@ class CommandBackup extends Command $template['rclone_version'] = $rclone->getVersion(); $template['destination_size_before'] = $rclone->getSize($conf['destination']); - $template['stdout'] = $rclone->copy( - $conf['source'], - $conf['destination'], - $app->getConfig()['rclone']['options'] - ); + $rclone->copy($conf['source'], $conf['destination'], $app->getConfig()['rclone']['options']); $template['destination_size_after'] = $rclone->getSize($conf['destination']); $template['end'] = new DateTime(); $message = $twig->render('notify', $template); - } catch (\Throwable $e) { + } catch (\Exception $e) { $message = $e->getMessage(); } diff --git a/src/Rclone/Rclone.php b/src/Rclone/Rclone.php index d100bed..704e664 100644 --- a/src/Rclone/Rclone.php +++ b/src/Rclone/Rclone.php @@ -43,13 +43,13 @@ class Rclone $this->rclonePath = $rclonePath; $this->setLogger(new NullLogger()); - try { - $version = $this->exec('--version'); - $this->version = explode("\n", $version)[0]; - } catch (ProcessFailedException $e) { + $process = $this->exec('--version'); + if (!$process->isSuccessful()) { throw new Exception("Check installation of rclone"); } + $this->version = explode("\n", $process->getOutput())[0]; + if (!\str_contains($this->version, 'rclone')) { throw new Exception("Rclone not recognized"); } @@ -74,8 +74,12 @@ class Rclone */ public function getSize(string $path): int { - $output = $this->exec('size', ['--json', $path]); - return (int)json_decode($output)->bytes; + $process = $this->exec('size', ['--json', $path]); + + if (!$process->isSuccessful()) { + throw new Exception($process->getErrorOutput()); + } + return (int)json_decode($process->getOutput())->bytes; } /** @@ -84,10 +88,8 @@ class Rclone * @param $src Source mount and path * @param $dest Destination mount and path * @param string[] $additionalOptions Additional options - * - * @return string Stdout from command */ - public function copy(string $src, string $dest, array $additionalOptions = array()): string + public function copy(string $src, string $dest, array $additionalOptions = array()): void { $options = array(); @@ -95,16 +97,14 @@ class Rclone $options[] = $dest; foreach ($additionalOptions as $key => $value) { - if (strlen($key) == 1) { - $options[] = '-' . $key; - $options[] = $value; - } elseif (strlen($key) > 2) { - $options[] = '--' . $key; - $options[] = $value; - } + $options[] = '--' . $key; + $options[] = $value; } - return $this->exec('copy', $options); + $process = $this->exec('copy', $options); + if (!$process->isSuccessful()) { + throw new Exception($process->getErrorOutput()); + } } /** @@ -113,9 +113,9 @@ class Rclone * @param string $command Top level Rclone command * @param array $options Array of additional options * - * @return string stdout data. + * @return Process Instance. */ - protected function exec(string $command, array $options = array()): string + protected function exec(string $command, array $options = array()): Process { $process = new Process( array_merge( @@ -137,11 +137,10 @@ class Rclone if ($this->logger instanceof LoggerInterface) { $this->logger->error("Failed execution"); } - throw new ProcessFailedException($process); } if ($this->logger instanceof LoggerInterface) { $this->logger->info("Return code", [$process->getExitCode()]); } - return $process->getOutput(); + return $process; } } diff --git a/tests/CommandBackupTest.php b/tests/CommandBackupTest.php new file mode 100644 index 0000000..b91935b --- /dev/null +++ b/tests/CommandBackupTest.php @@ -0,0 +1,29 @@ +add(new CommandBackup()); + + + $command = $applicationd->find('backup'); + $commandTester = new CommandTester($command); + $commandTester->execute([]); + + $commandTester->assertCommandIsSuccessful(); + + // the output of the command in the console + $output = $commandTester->getDisplay(); + $this->assertStringContainsString('backup', $output); + } +} \ No newline at end of file diff --git a/tests/Rclone/RcloneTest.php b/tests/Rclone/RcloneTest.php index 89b44ef..e895065 100644 --- a/tests/Rclone/RcloneTest.php +++ b/tests/Rclone/RcloneTest.php @@ -1,23 +1,65 @@ &1'); + } + + protected function tearDown(): void + { + exec('rclone purge temp 2>&1', $output); + } + + public function testRclonePath(): void { $this->expectException(\Exception::class); $rclone = new \App\Rclone\Rclone('invalid'); + $this->assertString('', $rclone->getVersion()); } public function testRcloneInvalidVersion(): void { $this->expectException(\Exception::class); $rclone = new \App\Rclone\Rclone('uname'); + $this->assertString('', $rclone->getVersion()); } public function testRcloneValidVersion(): void { - $rclone = new \App\Rclone\Rclone('./tests/mock-rclone'); + $rclone = new \App\Rclone\Rclone(); $this->assertStringContainsString('rclone', $rclone->getVersion()); } + + public function testRcloneSize(): void + { + $rclone = new \App\Rclone\Rclone(); + $size = $rclone->getSize('temp/source'); + $this->assertGreaterThan(10000, $size); + + $this->expectException(\Exception::class); + $this->expectExceptionMessage("ERROR"); + $size = $rclone->getSize('temp/bogus-source'); + $this->assertEquals(0, $size); + } + + public function testRcloneCopy(): void + { + $rclone = new \App\Rclone\Rclone(); + $result = $rclone->copy('temp/source','temp/destination'); + $this->assertDirectoryExists('temp/destination'); + + $rclone = new \App\Rclone\Rclone(); + $result = $rclone->copy('temp/source','temp/destination',['bwlimit'=>'6M']); + $this->assertDirectoryExists('temp/destination'); + + $this->expectException(\Exception::class); + $this->expectExceptionMessage("ERROR"); + $result = $rclone->copy('temp/bogus-source','temp/bogus-destination'); + $this->assertDirectoryDoesNotExist('temp/bogus-destination'); + } } \ No newline at end of file diff --git a/tests/mock-rclone b/tests/mock-rclone deleted file mode 100755 index 645f1a3..0000000 --- a/tests/mock-rclone +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -echo rclone v1.53.3-DEV