Skip to content

Commit 7a330db

Browse files
authored
Merge pull request #83 from kabalin/add-plugin-default-branch
AddPluginCommand should not use master branch by default.
2 parents 0fca38e + 2e429fe commit 7a330db

File tree

11 files changed

+241
-82
lines changed

11 files changed

+241
-82
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@
8989
"padraic/phar-updater": "^1.0"
9090
},
9191
"require-dev": {
92-
"phpunit/phpunit": "^5.7"
92+
"phpunit/phpunit": "^5.7",
93+
"mockery/mockery": "^1.3"
9394
},
9495
"config": {
9596
"platform": {

composer.lock

Lines changed: 113 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/CHANGELOG.md

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

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

2124
## [3.0.4] - 2021-01-29
2225
### Fixed

docs/CLI.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Queue up an additional plugin to be installed in the test site
135135

136136
#### `project`
137137

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

140140
* Is required: no
141141
* Is array: no
@@ -145,16 +145,16 @@ GitHub project, EG: moodlehq/moodle-local_hub
145145

146146
#### `--branch|-b`
147147

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

150150
* Accept value: yes
151151
* Is value required: yes
152152
* Is multiple: no
153-
* Default: `'master'`
153+
* Default: `NULL`
154154

155155
#### `--clone|-c`
156156

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

159159
* Accept value: yes
160160
* Is value required: yes

src/Command/AddPluginCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ protected function configure()
4747
{
4848
$this->setName('add-plugin')
4949
->setDescription('Queue up an additional plugin to be installed in the test site')
50-
->addArgument('project', InputArgument::OPTIONAL, 'GitHub project, EG: moodlehq/moodle-local_hub')
51-
->addOption('branch', 'b', InputOption::VALUE_REQUIRED, 'The branch to checkout within the plugin', 'master')
52-
->addOption('clone', 'c', InputOption::VALUE_REQUIRED, 'Git clone URL')
50+
->addArgument('project', InputArgument::OPTIONAL, 'GitHub project, EG: moodlehq/moodle-local_hub, can\'t be used with --clone option')
51+
->addOption('branch', 'b', InputOption::VALUE_REQUIRED, 'The branch to checkout in plugin repo (if non-default)', null)
52+
->addOption('clone', 'c', InputOption::VALUE_REQUIRED, 'Git clone URL, can\'t be used with --project option')
5353
->addOption('storage', null, InputOption::VALUE_REQUIRED, 'Plugin storage directory', 'moodle-plugin-ci-plugins');
5454
}
5555

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

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

src/Command/ExecuteTrait.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ trait ExecuteTrait
3434
*/
3535
protected function initializeExecute(OutputInterface $output, ProcessHelper $helper)
3636
{
37-
$this->execute = $this->execute ?: new Execute($output, $helper);
37+
if (isset($this->execute)) {
38+
// Define output and process helper.
39+
$this->execute->setOutput($output);
40+
$this->execute->setHelper($helper);
41+
} else {
42+
$this->execute = new Execute($output, $helper);
43+
}
3844
}
3945
}

src/Process/Execute.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212

1313
namespace MoodlePluginCI\Process;
1414

15+
use Symfony\Component\Console\Helper\DebugFormatterHelper;
16+
use Symfony\Component\Console\Helper\HelperSet;
1517
use Symfony\Component\Console\Helper\ProcessHelper;
18+
use Symfony\Component\Console\Output\NullOutput;
1619
use Symfony\Component\Console\Output\OutputInterface;
1720
use Symfony\Component\Process\Exception\ProcessFailedException;
1821
use Symfony\Component\Process\Process;
@@ -25,12 +28,12 @@ class Execute
2528
/**
2629
* @var OutputInterface
2730
*/
28-
private $output;
31+
protected $output;
2932

3033
/**
3134
* @var ProcessHelper
3235
*/
33-
private $helper;
36+
protected $helper;
3437

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

44-
public function __construct(OutputInterface $output, ProcessHelper $helper)
47+
/**
48+
* TODO: Add nullable type declaration for params when we switch to php 7.1.
49+
*
50+
* @param OutputInterface|null $output
51+
* @param ProcessHelper|null $helper
52+
*/
53+
public function __construct($output = null, $helper = null)
54+
{
55+
$this->setOutput($output);
56+
$this->setHelper($helper);
57+
}
58+
59+
/**
60+
* Output setter.
61+
* TODO: Add nullable type declaration for param when we switch to php 7.1.
62+
*
63+
* @param OutputInterface|null $output
64+
*/
65+
public function setOutput($output)
66+
{
67+
$this->output = $output ?? new NullOutput();
68+
}
69+
70+
/**
71+
* Process helper setter.
72+
* TODO: Add nullable type declaration for param when we switch to php 7.1.
73+
*
74+
* @param ProcessHelper|null $helper
75+
*/
76+
public function setHelper($helper)
4577
{
46-
$this->output = $output;
78+
if (empty($helper)) {
79+
$helper = new ProcessHelper();
80+
// Looks like $helper->run is not possible without DebugFormatterHelper.
81+
$helper->setHelperSet(new HelperSet([new DebugFormatterHelper()]));
82+
}
4783
$this->helper = $helper;
4884
}
4985

tests/Command/AddPluginCommandTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use MoodlePluginCI\Tests\Fake\Process\DummyExecute;
1717
use MoodlePluginCI\Tests\FilesystemTestCase;
1818
use Symfony\Component\Console\Application;
19+
use Symfony\Component\Console\Output\OutputInterface;
1920
use Symfony\Component\Console\Tester\CommandTester;
2021

2122
class AddPluginCommandTest extends FilesystemTestCase
@@ -51,12 +52,36 @@ public function testExecute()
5152
public function testExecuteWithClone()
5253
{
5354
$commandTester = $this->getCommandTester();
55+
// Execute with verbosity, so process helper outputs command line.
5456
$commandTester->execute([
5557
'--clone' => 'https://github.com/user/moodle-mod_foo.git',
5658
'--storage' => $this->tempDir.'/plugins',
57-
]);
59+
], ['verbosity' => OutputInterface::VERBOSITY_VERY_VERBOSE]);
60+
61+
$this->assertSame(0, $commandTester->getStatusCode());
62+
$this->assertContains('git clone --depth 1 https://github.com/user/moodle-mod_foo.git',
63+
$commandTester->getDisplay());
64+
$this->assertTrue(is_dir($this->tempDir.'/plugins'));
65+
$this->assertFileExists($this->tempDir.'/.env');
66+
$this->assertSame(
67+
sprintf("EXTRA_PLUGINS_DIR=%s/plugins\n", realpath($this->tempDir)),
68+
file_get_contents($this->tempDir.'/.env')
69+
);
70+
}
71+
72+
public function testExecuteWithCloneAndBranch()
73+
{
74+
$commandTester = $this->getCommandTester();
75+
// Execute with verbosity, so process helper outputs command line.
76+
$commandTester->execute([
77+
'--clone' => 'https://github.com/user/moodle-mod_foo.git',
78+
'--branch' => 'dev',
79+
'--storage' => $this->tempDir.'/plugins',
80+
], ['verbosity' => OutputInterface::VERBOSITY_VERY_VERBOSE]);
5881

5982
$this->assertSame(0, $commandTester->getStatusCode());
83+
$this->assertContains('git clone --depth 1 --branch dev https://github.com/user/moodle-mod_foo.git',
84+
$commandTester->getDisplay());
6085
$this->assertTrue(is_dir($this->tempDir.'/plugins'));
6186
$this->assertFileExists($this->tempDir.'/.env');
6287
$this->assertSame(

0 commit comments

Comments
 (0)