Skip to content

Commit

Permalink
Rename option to no-plugin-node plus unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 authored and mchurchward committed May 30, 2024
1 parent d246909 commit 9785474
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 38 deletions.
22 changes: 11 additions & 11 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ Install everything required for CI testing

### Usage

* `install [--moodle MOODLE] [--data DATA] [--repo REPO] [--branch BRANCH] [--plugin PLUGIN] [--db-type DB-TYPE] [--db-user DB-USER] [--db-pass DB-PASS] [--db-name DB-NAME] [--db-host DB-HOST] [--db-port DB-PORT] [--not-paths NOT-PATHS] [--not-names NOT-NAMES] [--extra-plugins EXTRA-PLUGINS] [--no-init] [--node-version NODE-VERSION] [--ignore-npm-dependencies]`
* `install [--moodle MOODLE] [--data DATA] [--repo REPO] [--branch BRANCH] [--plugin PLUGIN] [--db-type DB-TYPE] [--db-user DB-USER] [--db-pass DB-PASS] [--db-name DB-NAME] [--db-host DB-HOST] [--db-port DB-PORT] [--not-paths NOT-PATHS] [--not-names NOT-NAMES] [--extra-plugins EXTRA-PLUGINS] [--no-init] [--no-plugin-node] [--node-version NODE-VERSION]`

Install everything required for CI testing

Expand Down Expand Up @@ -1015,25 +1015,25 @@ Prevent PHPUnit and Behat initialization
* Is negatable: no
* Default: `false`

#### `--node-version`
#### `--no-plugin-node`

Node.js version to use for nvm install (this will override one defined in .nvmrc)
Prevent Node.js plugin dependencies installation

* Accept value: yes
* Is value required: yes
* Accept value: no
* Is value required: no
* Is multiple: no
* Is negatable: no
* Default: `NULL`
* Default: `false`

#### `--ignore-npm-dependencies`
#### `--node-version`

Does not execute npm install for the plugin
Node.js version to use for nvm install (this will override one defined in .nvmrc)

* Accept value: no
* Is value required: no
* Accept value: yes
* Is value required: yes
* Is multiple: no
* Is negatable: no
* Default: `false`
* Default: `NULL`

#### `--help|-h`

Expand Down
30 changes: 15 additions & 15 deletions src/Command/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ protected function configure(): void
->addOption('not-names', null, InputOption::VALUE_REQUIRED, 'CSV of file names to exclude', $names)
->addOption('extra-plugins', null, InputOption::VALUE_REQUIRED, 'Directory of extra plugins to install', $extra)
->addOption('no-init', null, InputOption::VALUE_NONE, 'Prevent PHPUnit and Behat initialization')
->addOption('node-version', null, InputOption::VALUE_REQUIRED, 'Node.js version to use for nvm install (this will override one defined in .nvmrc)', $node)
->addOption('ignore-npm-dependencies', null, InputOption::VALUE_NONE, 'Does not execute npm install for the plugin');
->addOption('no-plugin-node', null, InputOption::VALUE_NONE, 'Prevent Node.js plugin dependencies installation')
->addOption('node-version', null, InputOption::VALUE_REQUIRED, 'Node.js version to use for nvm install (this will override one defined in .nvmrc)', $node);
}

protected function initialize(InputInterface $input, OutputInterface $output): void
Expand Down Expand Up @@ -165,19 +165,19 @@ public function initializeInstallerFactory(InputInterface $input): InstallerFact
$pluginsDir = realpath($validate->directory($pluginsDir));
}

$factory = new InstallerFactory();
$factory->moodle = new Moodle($input->getOption('moodle'));
$factory->plugin = new MoodlePlugin($pluginDir);
$factory->execute = $this->execute;
$factory->repo = $validate->gitUrl($input->getOption('repo'));
$factory->branch = $validate->gitBranch($input->getOption('branch'));
$factory->dataDir = $input->getOption('data');
$factory->dumper = $this->initializePluginConfigDumper($input);
$factory->pluginsDir = $pluginsDir;
$factory->noInit = $input->getOption('no-init');
$factory->nodeVer = $input->getOption('node-version');
$factory->ignoreNpmDependencies = $input->getOption('ignore-npm-dependencies');
$factory->database = $resolver->resolveDatabase(
$factory = new InstallerFactory();
$factory->moodle = new Moodle($input->getOption('moodle'));
$factory->plugin = new MoodlePlugin($pluginDir);
$factory->execute = $this->execute;
$factory->repo = $validate->gitUrl($input->getOption('repo'));
$factory->branch = $validate->gitBranch($input->getOption('branch'));
$factory->dataDir = $input->getOption('data');
$factory->dumper = $this->initializePluginConfigDumper($input);
$factory->pluginsDir = $pluginsDir;
$factory->noInit = $input->getOption('no-init');
$factory->noPluginNode = $input->getOption('no-plugin-node');
$factory->nodeVer = $input->getOption('node-version');
$factory->database = $resolver->resolveDatabase(
$input->getOption('db-type'),
$input->getOption('db-name'),
$input->getOption('db-user'),
Expand Down
4 changes: 2 additions & 2 deletions src/Installer/InstallerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class InstallerFactory
public ConfigDumper $dumper;
public ?string $pluginsDir;
public bool $noInit;
public bool $noPluginNode;
public ?string $nodeVer;
public ?string $ignoreNpmDependencies;

/**
* Given a big bag of install options, add installers to the collection.
Expand All @@ -52,7 +52,7 @@ public function addInstallers(InstallerCollection $installers): void
}

$installers->add(new PluginInstaller($this->moodle, $this->plugin, $this->pluginsDir, $this->dumper));
$installers->add(new VendorInstaller($this->moodle, $this->plugin, $this->execute, $this->nodeVer, $this->ignoreNpmDependencies));
$installers->add(new VendorInstaller($this->moodle, $this->plugin, $this->execute, $this->noPluginNode, $this->nodeVer));

if ($this->noInit) {
return;
Expand Down
23 changes: 13 additions & 10 deletions src/Installer/VendorInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class VendorInstaller extends AbstractInstaller
private Moodle $moodle;
private MoodlePlugin $plugin;
private Execute $execute;
private bool $noPluginNode;
public ?string $nodeVer;
private ?string $ignoreNpmDependencies;
/**
* Define legacy Node version to use when .nvmrc is absent (Moodle < 3.5).
*/
Expand All @@ -38,13 +38,13 @@ class VendorInstaller extends AbstractInstaller
* @param Execute $execute
* @param string|null $nodeVer
*/
public function __construct(Moodle $moodle, MoodlePlugin $plugin, Execute $execute, ?string $nodeVer, ?string $ignoreNpmDependencies)
public function __construct(Moodle $moodle, MoodlePlugin $plugin, Execute $execute, bool $noPluginNode, ?string $nodeVer)
{
$this->moodle = $moodle;
$this->plugin = $plugin;
$this->execute = $execute;
$this->nodeVer = $nodeVer;
$this->ignoreNpmDependencies = $ignoreNpmDependencies;
$this->moodle = $moodle;
$this->plugin = $plugin;
$this->execute = $execute;
$this->nodeVer = $nodeVer;
$this->noPluginNode = $noPluginNode;
}

public function install(): void
Expand All @@ -65,12 +65,13 @@ public function install(): void

$this->execute->mustRunAll($processes);

$this->getOutput()->step('Install npm dependencies');
$this->getOutput()->step('Install Moodle npm dependencies');

$this->execute->mustRun(
Process::fromShellCommandline('npm install --no-progress', $this->moodle->directory, null, null, null)
);
if (is_null($this->ignoreNpmDependencies) && $this->plugin->hasNodeDependencies()) {
if (!$this->noPluginNode && $this->plugin->hasNodeDependencies()) {
$this->getOutput()->step('Install plugin npm dependencies');
$this->execute->mustRun(
Process::fromShellCommandline('npm install --no-progress', $this->plugin->directory, null, null, null)
);
Expand All @@ -83,7 +84,9 @@ public function install(): void

public function stepCount(): int
{
return ($this->canInstallNode()) ? 3 : 2;
return 2 + // Normally 2 steps: global dependencies and Moodle npm dependencies.
($this->canInstallNode() ? 1 : 0) + // Plus Node.js installation.
((!$this->noPluginNode && $this->plugin->hasNodeDependencies()) ? 1 : 0); // Plus plugin npm dependencies step.
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/Fixture/plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "VendorInstallerFixture",
"description": "",
"version": "0.1.0",
"dependencies": {},
"devDependencies": {}
}
37 changes: 37 additions & 0 deletions tests/Installer/VendorInstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function testInstall()
new DummyMoodle($this->moodleDir),
new MoodlePlugin($this->pluginDir),
new DummyExecute(),
false,
null
);
$installer->install();
Expand All @@ -39,6 +40,7 @@ public function testInstallNodeNoNvmrc()
new DummyMoodle($this->moodleDir),
new MoodlePlugin($this->pluginDir),
new DummyExecute(),
false,
null
);

Expand All @@ -58,6 +60,7 @@ public function testInstallNodeUserVersion()
new DummyMoodle($this->moodleDir),
new MoodlePlugin($this->pluginDir),
new DummyExecute(),
false,
$userVersion
);
$installer->installNode();
Expand All @@ -66,4 +69,38 @@ public function testInstallNodeUserVersion()
$this->assertTrue(is_file($this->moodleDir . '/.nvmrc'));
$this->assertSame($userVersion, file_get_contents($this->moodleDir . '/.nvmrc'));
}

public function testInstallNodePluginDependencies()
{
$installer = new VendorInstaller(
new DummyMoodle($this->moodleDir),
new MoodlePlugin($this->pluginDir),
new DummyExecute(),
false,
null
);

$this->fs->copy(__DIR__ . '/../Fixture/plugin/package.json', $this->pluginDir . '/package.json');

$installer->install();

$this->assertSame(4, $installer->getOutput()->getStepCount());
}

public function testSkipNodePluginDependencies()
{
$installer = new VendorInstaller(
new DummyMoodle($this->moodleDir),
new MoodlePlugin($this->pluginDir),
new DummyExecute(),
true,
null
);

$this->fs->copy(__DIR__ . '/../Fixture/plugin/package.json', $this->pluginDir . '/package.json');

$installer->install();

$this->assertSame(3, $installer->getOutput()->getStepCount());
}
}

0 comments on commit 9785474

Please sign in to comment.