From 9fad129e382e4549dab7823c8ac4125b8f66d0c4 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Wed, 11 Oct 2023 18:50:09 +0200 Subject: [PATCH 1/2] Add tests to cover what is testable in SelfUpdate command Note that only helper methods can be tested for that command, not the execution of the update itself (it has to happen within a PHAR). And it doesn't make much sense to start mocking lots of stuff for that. Some integration tests @ CIS will be in charge of covering the real execution instead. Coming soon. --- src/Command/SelfUpdateCommand.php | 10 ++-- tests/Command/SelfUpdateCommandTest.php | 62 +++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 tests/Command/SelfUpdateCommandTest.php diff --git a/src/Command/SelfUpdateCommand.php b/src/Command/SelfUpdateCommand.php index 1a3322ac..86828470 100644 --- a/src/Command/SelfUpdateCommand.php +++ b/src/Command/SelfUpdateCommand.php @@ -93,13 +93,17 @@ 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..1dd1046c --- /dev/null +++ b/tests/Command/SelfUpdateCommandTest.php @@ -0,0 +1,62 @@ +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)); + } +} From a79ed0a85fd2a444063df309955098334db97796 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Thu, 11 Jan 2024 17:12:35 +0100 Subject: [PATCH 2/2] Add some selfupdate integration tests We just start with the 4.1.8 release that was the first supporting the selfupdate command. Then we upgrade to actual and then rollback to confirm that we are back to the original one. That should be enough to have it continuously covered. --- .github/workflows/test.yml | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index eae59a4a..4d1f66d7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -261,3 +261,45 @@ jobs: php build/moodle-plugin-ci.phar behat --profile default php build/moodle-plugin-ci.phar behat --profile chrome php build/moodle-plugin-ci.phar behat --profile firefox --tags="@local_ci&&~@app" + + selfupdatetest: + name: SelfUpdate tests (PHAR) + runs-on: ubuntu-22.04 + env: + lowest_release: '4.1.8' + + strategy: + fail-fast: false + matrix: + include: + # Each supported PHP version once. That's enough. + - php: '8.2' + - php: '8.1' + - php: '8.0' + - php: '7.4' + + steps: + - name: Setup PHP ${{ matrix.php }} + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: pgsql, zip, gd, xmlrpc, soap + ini-values: max_input_vars=5000 + coverage: none + + - name: Download base ${{ env.lowest_release }} PHAR artifact + uses: robinraju/release-downloader@v1.8 + with: + repository: moodlehq/moodle-plugin-ci + tag: ${{ env.lowest_release }} + fileName: moodle-plugin-ci.phar + + - name: Self update PHAR to actual + run: | + php moodle-plugin-ci.phar selfupdate + php moodle-plugin-ci.phar --version | grep -qv "${{ env.lowest_release }}" + + - name: Self update rollback PHAR to base ${{ env.lowest_release }} + run: | + php moodle-plugin-ci.phar selfupdate --rollback + php moodle-plugin-ci.phar --version | grep -q "${{ env.lowest_release }}"