Skip to content

Commit 74cbee7

Browse files
committed
AddPluginCommand should not use master branch by default.
As default branch could be anything, don't explicity set master if --branch param is omitted. Also improves tests. Fixes #58
1 parent 0fca38e commit 74cbee7

File tree

9 files changed

+110
-52
lines changed

9 files changed

+110
-52
lines changed

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: 32 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,34 @@ class Execute
4144
*/
4245
public $parallelWaitTime = 200000;
4346

44-
public function __construct(OutputInterface $output, ProcessHelper $helper)
47+
public function __construct(?OutputInterface $output = null, ?ProcessHelper $helper = null)
4548
{
46-
$this->output = $output;
49+
$this->setOutput($output);
50+
$this->setHelper($helper);
51+
}
52+
53+
/**
54+
* Output setter.
55+
*
56+
* @param OutputInterface|null $output
57+
*/
58+
public function setOutput(?OutputInterface $output)
59+
{
60+
$this->output = $output ?? new NullOutput();
61+
}
62+
63+
/**
64+
* Process helper setter.
65+
*
66+
* @param OutputInterface|null $output
67+
*/
68+
public function setHelper(?ProcessHelper $helper)
69+
{
70+
if (empty($helper)) {
71+
$helper = new ProcessHelper();
72+
// Looks like $helper->run is not possible without DebugFormatterHelper.
73+
$helper->setHelperSet(new HelperSet([new DebugFormatterHelper()]));
74+
}
4775
$this->helper = $helper;
4876
}
4977

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(

tests/Fake/Process/DummyExecute.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,26 @@
1717

1818
class DummyExecute extends Execute
1919
{
20-
/** @noinspection PhpMissingParentConstructorInspection */
21-
public function __construct()
22-
{
23-
// Do nothing.
24-
}
25-
2620
public function run($cmd, $error = null)
2721
{
28-
return new DummyProcess('dummy');
22+
if ($cmd instanceof Process) {
23+
// Get the command line from process.
24+
$cmd = $cmd->getCommandLine();
25+
}
26+
$cmd = new DummyProcess($cmd);
27+
28+
return $this->helper->run($this->output, $cmd, $error);
2929
}
3030

3131
public function mustRun($cmd, $error = null)
3232
{
33-
return new DummyProcess('dummy');
33+
if ($cmd instanceof Process) {
34+
// Get the command line from process.
35+
$cmd = $cmd->getCommandLine();
36+
}
37+
$cmd = new DummyProcess($cmd);
38+
39+
return $this->helper->mustRun($this->output, $cmd, $error);
3440
}
3541

3642
public function runAll($processes)

tests/Fake/Process/DummyProcess.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616

1717
class DummyProcess extends Process
1818
{
19+
private $realcommandline;
20+
21+
public function __construct($commandline)
22+
{
23+
$this->realcommandline = $commandline;
24+
}
25+
26+
public function getCommandLine()
27+
{
28+
return $this->realcommandline;
29+
}
30+
1931
public function getOutput()
2032
{
2133
return '';

tests/Process/ExecuteTest.php

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@
1414

1515
use MoodlePluginCI\Process\Execute;
1616
use MoodlePluginCI\Process\MoodleProcess;
17-
use Symfony\Component\Console\Helper\DebugFormatterHelper;
18-
use Symfony\Component\Console\Helper\HelperSet;
19-
use Symfony\Component\Console\Helper\ProcessHelper;
2017
use Symfony\Component\Console\Output\BufferedOutput;
21-
use Symfony\Component\Console\Output\NullOutput;
2218
use Symfony\Component\Console\Output\OutputInterface;
2319
use Symfony\Component\Process\Exception\ProcessFailedException;
2420
use Symfony\Component\Process\Process;
@@ -34,10 +30,7 @@ protected function setUp()
3430

3531
public function testSetNodeEnv()
3632
{
37-
$helper = new ProcessHelper();
38-
$helper->setHelperSet(new HelperSet([new DebugFormatterHelper()]));
39-
40-
$execute = new Execute(new NullOutput(), $helper);
33+
$execute = new Execute();
4134
$pathenv = getenv('PATH');
4235

4336
// RUNTIME_NVM_BIN in undefined.
@@ -69,10 +62,7 @@ public function testSetNodeEnv()
6962

7063
public function testRun()
7164
{
72-
$helper = new ProcessHelper();
73-
$helper->setHelperSet(new HelperSet([new DebugFormatterHelper()]));
74-
75-
$execute = new Execute(new NullOutput(), $helper);
65+
$execute = new Execute();
7666
$process = $execute->run('env');
7767

7868
$this->assertInstanceOf('Symfony\Component\Process\Process', $process);
@@ -83,10 +73,7 @@ public function testRun()
8373

8474
public function testMustRun()
8575
{
86-
$helper = new ProcessHelper();
87-
$helper->setHelperSet(new HelperSet([new DebugFormatterHelper()]));
88-
89-
$execute = new Execute(new NullOutput(), $helper);
76+
$execute = new Execute();
9077
$process = $execute->mustRun('env');
9178

9279
$this->assertInstanceOf('Symfony\Component\Process\Process', $process);
@@ -97,17 +84,14 @@ public function testMustRun()
9784

9885
public function testRunAllVerbose()
9986
{
100-
$helper = new ProcessHelper();
101-
$helper->setHelperSet(new HelperSet([new DebugFormatterHelper()]));
102-
10387
/** @var Process[] $processes */
10488
$processes = [
10589
new Process('env'),
10690
new Process('env'),
10791
];
10892

10993
$output = new BufferedOutput(OutputInterface::VERBOSITY_VERY_VERBOSE);
110-
$execute = new Execute($output, $helper);
94+
$execute = new Execute($output);
11195
$execute->runAll($processes);
11296

11397
foreach ($processes as $process) {
@@ -120,17 +104,14 @@ public function testRunAllVerbose()
120104

121105
public function testMustRunAll()
122106
{
123-
$helper = new ProcessHelper();
124-
$helper->setHelperSet(new HelperSet([new DebugFormatterHelper()]));
125-
126107
/** @var Process[] $processes */
127108
$processes = [
128109
new Process('env'),
129110
new MoodleProcess('-r "echo \'PATH=\'.getenv(\'PATH\');"'),
130111
new Process('env'),
131112
];
132113

133-
$execute = new Execute(new NullOutput(), $helper);
114+
$execute = new Execute();
134115

135116
$execute->parallelWaitTime = 1;
136117
$execute->mustRunAll($processes);
@@ -146,26 +127,22 @@ public function testMustRunAllFail()
146127
{
147128
$this->expectException(ProcessFailedException::class);
148129

149-
$helper = new ProcessHelper();
150-
$helper->setHelperSet(new HelperSet([new DebugFormatterHelper()]));
151-
152130
/** @var Process[] $processes */
153131
$processes = [
154132
new Process('php -r "echo 42;"'),
155133
new Process('php -r "syntax error"'),
156134
new Process('php -r "echo 42;"'),
157135
];
158136

159-
$execute = new Execute(new NullOutput(), $helper);
160-
137+
$execute = new Execute();
161138
$execute->parallelWaitTime = 1;
162139
$execute->mustRunAll($processes);
163140
}
164141

165142
public function testPassThrough()
166143
{
167144
$output = new BufferedOutput(OutputInterface::VERBOSITY_VERY_VERBOSE);
168-
$execute = new Execute($output, new ProcessHelper());
145+
$execute = new Execute($output);
169146
$process = $execute->passThrough('php -r "echo 42;"');
170147

171148
$this->assertInstanceOf('Symfony\Component\Process\Process', $process);

0 commit comments

Comments
 (0)