Skip to content

Commit

Permalink
Merge pull request #83 from kabalin/add-plugin-default-branch
Browse files Browse the repository at this point in the history
AddPluginCommand should not use master branch by default.
  • Loading branch information
stronk7 authored Feb 3, 2021
2 parents 0fca38e + 2e429fe commit 7a330db
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 82 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
"padraic/phar-updater": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
"phpunit/phpunit": "^5.7",
"mockery/mockery": "^1.3"
},
"config": {
"platform": {
Expand Down
114 changes: 113 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ The format of this change log follows the advice given at [Keep a CHANGELOG](htt
[gha.dist.yml](https://github.com/moodlehq/moodle-plugin-ci/blob/master/gha.dist.yml)
and add missing `NVM_DIR` line your plugin's GHA workflow file.

### Changed
- `moodle-plugin-ci add-plugin` command now uses default banch to checkout
instead of `master` if `--branch` param is not specified..

## [3.0.4] - 2021-01-29
### Fixed
Expand Down
8 changes: 4 additions & 4 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Queue up an additional plugin to be installed in the test site

#### `project`

GitHub project, EG: moodlehq/moodle-local_hub
GitHub project, EG: moodlehq/moodle-local_hub, can't be used with --clone option

* Is required: no
* Is array: no
Expand All @@ -145,16 +145,16 @@ GitHub project, EG: moodlehq/moodle-local_hub

#### `--branch|-b`

The branch to checkout within the plugin
The branch to checkout in plugin repo (if non-default)

* Accept value: yes
* Is value required: yes
* Is multiple: no
* Default: `'master'`
* Default: `NULL`

#### `--clone|-c`

Git clone URL
Git clone URL, can't be used with --project option

* Accept value: yes
* Is value required: yes
Expand Down
9 changes: 5 additions & 4 deletions src/Command/AddPluginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ protected function configure()
{
$this->setName('add-plugin')
->setDescription('Queue up an additional plugin to be installed in the test site')
->addArgument('project', InputArgument::OPTIONAL, 'GitHub project, EG: moodlehq/moodle-local_hub')
->addOption('branch', 'b', InputOption::VALUE_REQUIRED, 'The branch to checkout within the plugin', 'master')
->addOption('clone', 'c', InputOption::VALUE_REQUIRED, 'Git clone URL')
->addArgument('project', InputArgument::OPTIONAL, 'GitHub project, EG: moodlehq/moodle-local_hub, can\'t be used with --clone option')
->addOption('branch', 'b', InputOption::VALUE_REQUIRED, 'The branch to checkout in plugin repo (if non-default)', null)
->addOption('clone', 'c', InputOption::VALUE_REQUIRED, 'Git clone URL, can\'t be used with --project option')
->addOption('storage', null, InputOption::VALUE_REQUIRED, 'Plugin storage directory', 'moodle-plugin-ci-plugins');
}

Expand Down Expand Up @@ -82,8 +82,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$filesystem->mkdir($storage);
$storageDir = realpath($validate->directory($storage));

$branch = $branch !== null ? '--branch '.$branch : '';
/** @psalm-suppress PossiblyInvalidArgument */
$cloneUrl = sprintf('git clone --depth 1 --branch %s %s', $branch, $cloneUrl);
$cloneUrl = sprintf('git clone --depth 1 %s %s', $branch, $cloneUrl);
$process = new Process($cloneUrl, $storageDir);
$this->execute->mustRun($process);

Expand Down
8 changes: 7 additions & 1 deletion src/Command/ExecuteTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ trait ExecuteTrait
*/
protected function initializeExecute(OutputInterface $output, ProcessHelper $helper)
{
$this->execute = $this->execute ?: new Execute($output, $helper);
if (isset($this->execute)) {
// Define output and process helper.
$this->execute->setOutput($output);
$this->execute->setHelper($helper);
} else {
$this->execute = new Execute($output, $helper);
}
}
}
44 changes: 40 additions & 4 deletions src/Process/Execute.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

namespace MoodlePluginCI\Process;

use Symfony\Component\Console\Helper\DebugFormatterHelper;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\ProcessHelper;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
Expand All @@ -25,12 +28,12 @@ class Execute
/**
* @var OutputInterface
*/
private $output;
protected $output;

/**
* @var ProcessHelper
*/
private $helper;
protected $helper;

/**
* Sleep for .2 seconds to avoid race conditions in Moodle scripts when running them in parallel.
Expand All @@ -41,9 +44,42 @@ class Execute
*/
public $parallelWaitTime = 200000;

public function __construct(OutputInterface $output, ProcessHelper $helper)
/**
* TODO: Add nullable type declaration for params when we switch to php 7.1.
*
* @param OutputInterface|null $output
* @param ProcessHelper|null $helper
*/
public function __construct($output = null, $helper = null)
{
$this->setOutput($output);
$this->setHelper($helper);
}

/**
* Output setter.
* TODO: Add nullable type declaration for param when we switch to php 7.1.
*
* @param OutputInterface|null $output
*/
public function setOutput($output)
{
$this->output = $output ?? new NullOutput();
}

/**
* Process helper setter.
* TODO: Add nullable type declaration for param when we switch to php 7.1.
*
* @param ProcessHelper|null $helper
*/
public function setHelper($helper)
{
$this->output = $output;
if (empty($helper)) {
$helper = new ProcessHelper();
// Looks like $helper->run is not possible without DebugFormatterHelper.
$helper->setHelperSet(new HelperSet([new DebugFormatterHelper()]));
}
$this->helper = $helper;
}

Expand Down
27 changes: 26 additions & 1 deletion tests/Command/AddPluginCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use MoodlePluginCI\Tests\Fake\Process\DummyExecute;
use MoodlePluginCI\Tests\FilesystemTestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;

class AddPluginCommandTest extends FilesystemTestCase
Expand Down Expand Up @@ -51,12 +52,36 @@ public function testExecute()
public function testExecuteWithClone()
{
$commandTester = $this->getCommandTester();
// Execute with verbosity, so process helper outputs command line.
$commandTester->execute([
'--clone' => 'https://github.com/user/moodle-mod_foo.git',
'--storage' => $this->tempDir.'/plugins',
]);
], ['verbosity' => OutputInterface::VERBOSITY_VERY_VERBOSE]);

$this->assertSame(0, $commandTester->getStatusCode());
$this->assertContains('git clone --depth 1 https://github.com/user/moodle-mod_foo.git',
$commandTester->getDisplay());
$this->assertTrue(is_dir($this->tempDir.'/plugins'));
$this->assertFileExists($this->tempDir.'/.env');
$this->assertSame(
sprintf("EXTRA_PLUGINS_DIR=%s/plugins\n", realpath($this->tempDir)),
file_get_contents($this->tempDir.'/.env')
);
}

public function testExecuteWithCloneAndBranch()
{
$commandTester = $this->getCommandTester();
// Execute with verbosity, so process helper outputs command line.
$commandTester->execute([
'--clone' => 'https://github.com/user/moodle-mod_foo.git',
'--branch' => 'dev',
'--storage' => $this->tempDir.'/plugins',
], ['verbosity' => OutputInterface::VERBOSITY_VERY_VERBOSE]);

$this->assertSame(0, $commandTester->getStatusCode());
$this->assertContains('git clone --depth 1 --branch dev https://github.com/user/moodle-mod_foo.git',
$commandTester->getDisplay());
$this->assertTrue(is_dir($this->tempDir.'/plugins'));
$this->assertFileExists($this->tempDir.'/.env');
$this->assertSame(
Expand Down
Loading

0 comments on commit 7a330db

Please sign in to comment.