Skip to content

Commit ce309ac

Browse files
authored
Merge pull request #250 from stronk7/test_self_update
Add tests to cover what is testable in SelfUpdate command
2 parents d5d6054 + a79ed0a commit ce309ac

File tree

3 files changed

+111
-3
lines changed

3 files changed

+111
-3
lines changed

.github/workflows/test.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,45 @@ jobs:
261261
php build/moodle-plugin-ci.phar behat --profile default
262262
php build/moodle-plugin-ci.phar behat --profile chrome
263263
php build/moodle-plugin-ci.phar behat --profile firefox --tags="@local_ci&&~@app"
264+
265+
selfupdatetest:
266+
name: SelfUpdate tests (PHAR)
267+
runs-on: ubuntu-22.04
268+
env:
269+
lowest_release: '4.1.8'
270+
271+
strategy:
272+
fail-fast: false
273+
matrix:
274+
include:
275+
# Each supported PHP version once. That's enough.
276+
- php: '8.2'
277+
- php: '8.1'
278+
- php: '8.0'
279+
- php: '7.4'
280+
281+
steps:
282+
- name: Setup PHP ${{ matrix.php }}
283+
uses: shivammathur/setup-php@v2
284+
with:
285+
php-version: ${{ matrix.php }}
286+
extensions: pgsql, zip, gd, xmlrpc, soap
287+
ini-values: max_input_vars=5000
288+
coverage: none
289+
290+
- name: Download base ${{ env.lowest_release }} PHAR artifact
291+
uses: robinraju/[email protected]
292+
with:
293+
repository: moodlehq/moodle-plugin-ci
294+
tag: ${{ env.lowest_release }}
295+
fileName: moodle-plugin-ci.phar
296+
297+
- name: Self update PHAR to actual
298+
run: |
299+
php moodle-plugin-ci.phar selfupdate
300+
php moodle-plugin-ci.phar --version | grep -qv "${{ env.lowest_release }}"
301+
302+
- name: Self update rollback PHAR to base ${{ env.lowest_release }}
303+
run: |
304+
php moodle-plugin-ci.phar selfupdate --rollback
305+
php moodle-plugin-ci.phar --version | grep -q "${{ env.lowest_release }}"

src/Command/SelfUpdateCommand.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9393
}
9494

9595
/**
96+
* Calculate the full path where the old PHAR file will be backup (to be able to roll back to it).
97+
*
98+
* @param string|null $directory the directory where the backup will be stored
99+
*
96100
* @return string
97101
*/
98-
protected function getBackupPath(): string
102+
protected function getBackupPath(?string $directory = null): string
99103
{
100-
$directory = getenv('HOME');
104+
$directory = $directory ?? getenv('HOME'); // Default to $HOME as base directory if not provided.
101105
if (empty($directory) || !is_dir($directory)) {
102-
throw new \RuntimeException('Your $HOME enviroment variable is either not set or is not a directory');
106+
throw new \RuntimeException("The {$directory} path is not an existing directory");
103107
}
104108
$directory .= '/.moodle-plugin-ci';
105109

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Moodle Plugin CI package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* @copyright 2023 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
10+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
11+
*/
12+
13+
namespace Command;
14+
15+
use MoodlePluginCI\Command\SelfUpdateCommand;
16+
use Symfony\Component\Filesystem\Filesystem;
17+
18+
/**
19+
* Tests for the SelfUpdateCommand class.
20+
*
21+
* There isn't much to test here, only helper methods. Note that the utility itself
22+
* will be covered by some integration tests @ CIs.
23+
*/
24+
class SelfUpdateCommandTest extends \PHPUnit\Framework\TestCase
25+
{
26+
/**
27+
* @covers \MoodlePluginCI\Command\SelfUpdateCommand::getBackupPath
28+
*/
29+
public function testGetBackupPathNotExists()
30+
{
31+
$command = new SelfUpdateCommand();
32+
33+
// Try with a non-existing directory.
34+
$rollBackDir = sys_get_temp_dir() . '/not_existing_dir';
35+
$rollBackFile = $rollBackDir . '/.moodle-plugin-ci/moodle-plugin-ci-old.phar';
36+
37+
$this->expectException(\RuntimeException::class);
38+
39+
// Use reflection to test the protected method.
40+
$method = new \ReflectionMethod($command, 'getBackupPath');
41+
$method->setAccessible(true);
42+
$this->assertSame($rollBackFile, $method->invoke($command, $rollBackDir));
43+
}
44+
45+
/**
46+
* @covers \MoodlePluginCI\Command\SelfUpdateCommand::getBackupPath
47+
*/
48+
public function testGetBackupPathExists()
49+
{
50+
$command = new SelfUpdateCommand();
51+
52+
// Try with a existing directory.
53+
$rollBackDir = sys_get_temp_dir() . '/existing_dir';
54+
(new Filesystem())->mkdir($rollBackDir); // Let's create the directory.
55+
$rollBackFile = $rollBackDir . '/.moodle-plugin-ci/moodle-plugin-ci-old.phar';
56+
57+
// Use reflection to test the protected method.
58+
$method = new \ReflectionMethod($command, 'getBackupPath');
59+
$method->setAccessible(true);
60+
$this->assertSame($rollBackFile, $method->invoke($command, $rollBackDir));
61+
}
62+
}

0 commit comments

Comments
 (0)