diff --git a/src/Command/SelfUpdateCommand.php b/src/Command/SelfUpdateCommand.php index 1a3322ac..be5a9c88 100644 --- a/src/Command/SelfUpdateCommand.php +++ b/src/Command/SelfUpdateCommand.php @@ -93,13 +93,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int } /** + * Calculate the full path where the old PHAR file will be backup (to be able to roll back to it). + * + * @param string|null $directory The directory where the backup will be stored. * @return string */ - protected function getBackupPath(): string + protected function getBackupPath(?string $directory = null): string { - $directory = getenv('HOME'); + $directory = $directory ?? getenv('HOME'); // Default to $HOME as base directory if not provided. if (empty($directory) || !is_dir($directory)) { - throw new \RuntimeException('Your $HOME enviroment variable is either not set or is not a directory'); + throw new \RuntimeException("The {$directory} path is not an existing directory"); } $directory .= '/.moodle-plugin-ci'; diff --git a/tests/Command/SelfUpdateCommandTest.php b/tests/Command/SelfUpdateCommandTest.php new file mode 100644 index 00000000..4c28e3b8 --- /dev/null +++ b/tests/Command/SelfUpdateCommandTest.php @@ -0,0 +1,63 @@ +expectException(\RuntimeException::class); + + // Use reflection to test the protected method. + $method = new \ReflectionMethod($command, 'getBackupPath'); + $method->setAccessible(true); + $this->assertSame($rollBackFile, $method->invoke($command, $rollBackDir)); + } + + /** + * @covers \MoodlePluginCI\Command\SelfUpdateCommand::getBackupPath + */ + public function testGetBackupPathExists() + { + $command = new SelfUpdateCommand(); + + // Try with a existing directory. + $rollBackDir = sys_get_temp_dir() . '/existing_dir'; + (new Filesystem())->mkdir($rollBackDir); // Let's create the directory. + $rollBackFile = $rollBackDir . '/.moodle-plugin-ci/moodle-plugin-ci-old.phar'; + + + // Use reflection to test the protected method. + $method = new \ReflectionMethod($command, 'getBackupPath'); + $method->setAccessible(true); + $this->assertSame($rollBackFile, $method->invoke($command, $rollBackDir)); + } +}