Skip to content

Commit d98bce3

Browse files
committed
Add nvm installation to install step.
Install 0.39.7 explicitly as part of vendor installation, so we have control over the version we use. For cases where nvm is not required (pre-installed in the image, use --no-nvm param for install command) Fixes #309
1 parent 3842ad1 commit d98bce3

File tree

9 files changed

+84
-17
lines changed

9 files changed

+84
-17
lines changed

.github/workflows/test.yml

-4
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ jobs:
120120
echo "CI_BUILD_DIR="$(cd ../moodle-local_ci; pwd) >> $GITHUB_ENV
121121
# PHPUnit depends on en_AU.UTF-8 locale
122122
sudo locale-gen en_AU.UTF-8
123-
# Define NVM_DIR pointing to nvm installation.
124-
echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV
125123
126124
- name: Install moodle-plugin-ci
127125
run: moodle-plugin-ci install -vvv
@@ -238,8 +236,6 @@ jobs:
238236
echo "CI_BUILD_DIR="$(cd ../moodle-local_ci; pwd) >> $GITHUB_ENV
239237
# PHPUnit depends on en_AU.UTF-8 locale
240238
sudo locale-gen en_AU.UTF-8
241-
# Define NVM_DIR pointing to nvm installation.
242-
echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV
243239
244240
- name: Download PHAR artifact
245241
uses: actions/download-artifact@v4

docs/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ This project adheres to [Semantic Versioning](https://semver.org/).
99
The format of this change log follows the advice given at [Keep a CHANGELOG](https://keepachangelog.com).
1010

1111
## [Unreleased]
12+
### Added
13+
- Install stable release of nvm as part of install command. This is now a default
14+
behaviour, to bypass nvm installation use `--no-nvm` param for install command.
15+
- ACTION SUGGESTED: Update your workflows and remove `echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV` in "Initialise moodle-plugin-ci" step, this not required.
16+
1217
## [4.5.3] - 2024-07-05
1318
### Added
1419
- Support for version 4.4 of the app, that uses new defaults and Chrome (Selenium 4) version.

docs/CLI.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ Install everything required for CI testing
859859

860860
### Usage
861861

862-
* `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]`
862+
* `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-nvm] [--no-plugin-node] [--node-version NODE-VERSION]`
863863

864864
Install everything required for CI testing
865865

@@ -1015,6 +1015,16 @@ Prevent PHPUnit and Behat initialization
10151015
* Is negatable: no
10161016
* Default: `false`
10171017

1018+
#### `--no-nvm`
1019+
1020+
Prevent nvm installation
1021+
1022+
* Accept value: no
1023+
* Is value required: no
1024+
* Is multiple: no
1025+
* Is negatable: no
1026+
* Default: `false`
1027+
10181028
#### `--no-plugin-node`
10191029

10201030
Prevent Node.js plugin dependencies installation

docs/GHAFileExplained.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,13 @@ jobs:
9797
coverage: none
9898

9999
# Install this project into a directory called "ci", updating PATH and
100-
# locale, define nvm location.
100+
# locale.
101101
- name: Initialise moodle-plugin-ci
102102
run: |
103103
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^4
104104
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
105105
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
106106
sudo locale-gen en_AU.UTF-8
107-
echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV
108107
109108
# Run the default install.
110109
# Optionally, it is possible to specify a different Moodle repo to use

gha.dist.yml

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ jobs:
5656
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
5757
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
5858
sudo locale-gen en_AU.UTF-8
59-
echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV
6059
6160
- name: Install moodle-plugin-ci
6261
run: moodle-plugin-ci install --plugin ./plugin --db-host=127.0.0.1

src/Command/InstallCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ protected function configure(): void
102102
->addOption('not-names', null, InputOption::VALUE_REQUIRED, 'CSV of file names to exclude', $names)
103103
->addOption('extra-plugins', null, InputOption::VALUE_REQUIRED, 'Directory of extra plugins to install', $extra)
104104
->addOption('no-init', null, InputOption::VALUE_NONE, 'Prevent PHPUnit and Behat initialization')
105+
->addOption('no-nvm', null, InputOption::VALUE_NONE, 'Prevent nvm installation')
105106
->addOption('no-plugin-node', null, InputOption::VALUE_NONE, 'Prevent Node.js plugin dependencies installation')
106107
->addOption('node-version', null, InputOption::VALUE_REQUIRED, 'Node.js version to use for nvm install (this will override one defined in .nvmrc)', $node);
107108
}
@@ -175,6 +176,7 @@ public function initializeInstallerFactory(InputInterface $input): InstallerFact
175176
$factory->dumper = $this->initializePluginConfigDumper($input);
176177
$factory->pluginsDir = $pluginsDir;
177178
$factory->noInit = $input->getOption('no-init');
179+
$factory->noNvm = $input->getOption('no-nvm');
178180
$factory->noPluginNode = $input->getOption('no-plugin-node');
179181
$factory->nodeVer = $input->getOption('node-version');
180182
$factory->database = $resolver->resolveDatabase(

src/Installer/InstallerFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class InstallerFactory
3333
public ConfigDumper $dumper;
3434
public ?string $pluginsDir;
3535
public bool $noInit;
36+
public bool $noNvm;
3637
public bool $noPluginNode;
3738
public ?string $nodeVer;
3839

@@ -52,7 +53,7 @@ public function addInstallers(InstallerCollection $installers): void
5253
}
5354

5455
$installers->add(new PluginInstaller($this->moodle, $this->plugin, $this->pluginsDir, $this->dumper));
55-
$installers->add(new VendorInstaller($this->moodle, $this->plugin, $this->execute, $this->noPluginNode, $this->nodeVer));
56+
$installers->add(new VendorInstaller($this->moodle, $this->plugin, $this->execute, $this->noPluginNode, $this->nodeVer, $this->noNvm));
5657

5758
if ($this->noInit) {
5859
return;

src/Installer/VendorInstaller.php

+34-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class VendorInstaller extends AbstractInstaller
2525
private Moodle $moodle;
2626
private MoodlePlugin $plugin;
2727
private Execute $execute;
28+
private bool $noNvm;
2829
private bool $noPluginNode;
2930
public ?string $nodeVer;
3031
/**
@@ -38,17 +39,22 @@ class VendorInstaller extends AbstractInstaller
3839
* @param Execute $execute
3940
* @param string|null $nodeVer
4041
*/
41-
public function __construct(Moodle $moodle, MoodlePlugin $plugin, Execute $execute, bool $noPluginNode, ?string $nodeVer)
42+
public function __construct(Moodle $moodle, MoodlePlugin $plugin, Execute $execute, bool $noPluginNode, ?string $nodeVer, bool $noNvm)
4243
{
4344
$this->moodle = $moodle;
4445
$this->plugin = $plugin;
4546
$this->execute = $execute;
4647
$this->nodeVer = $nodeVer;
4748
$this->noPluginNode = $noPluginNode;
49+
$this->noNvm = $noNvm;
4850
}
4951

5052
public function install(): void
5153
{
54+
if ($this->canInstallNvm()) {
55+
$this->getOutput()->step('Installing nvm');
56+
$this->installNvm();
57+
}
5258
if ($this->canInstallNode()) {
5359
$this->getOutput()->step('Installing Node.js');
5460
$this->installNode();
@@ -85,10 +91,37 @@ public function install(): void
8591
public function stepCount(): int
8692
{
8793
return 2 + // Normally 2 steps: global dependencies and Moodle npm dependencies.
94+
($this->canInstallNvm() ? 1 : 0) + // Plus nvm installation.
8895
($this->canInstallNode() ? 1 : 0) + // Plus Node.js installation.
8996
((!$this->noPluginNode && $this->plugin->hasNodeDependencies()) ? 1 : 0); // Plus plugin npm dependencies step.
9097
}
9198

99+
/**
100+
* Check if we have to install nvm.
101+
*
102+
* @return bool
103+
*/
104+
public function canInstallNvm(): bool
105+
{
106+
return !$this->noNvm;
107+
}
108+
109+
/**
110+
* Install nvm.
111+
*/
112+
public function installNvm(): void
113+
{
114+
$cmd = 'curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash';
115+
$process = $this->execute->passThroughProcess(
116+
Process::fromShellCommandline($cmd, $this->moodle->directory, null, null, null)
117+
);
118+
if (!$process->isSuccessful()) {
119+
throw new \RuntimeException('nvm installation failed.');
120+
}
121+
$home = getenv('HOME');
122+
putenv("NVM_DIR={$home}/.nvm");
123+
}
124+
92125
/**
93126
* Check if we have nvm to proceed with Node.js installation step.
94127
*

tests/Installer/VendorInstallerTest.php

+29-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public function testInstall()
2727
new MoodlePlugin($this->pluginDir),
2828
new DummyExecute(),
2929
false,
30-
null
30+
null,
31+
false,
3132
);
3233
$installer->install();
3334

@@ -41,7 +42,8 @@ public function testInstallNodeNoNvmrc()
4142
new MoodlePlugin($this->pluginDir),
4243
new DummyExecute(),
4344
false,
44-
null
45+
null,
46+
false,
4547
);
4648

4749
// Remove .nvmrc
@@ -53,6 +55,22 @@ public function testInstallNodeNoNvmrc()
5355
$this->assertSame('lts/carbon', file_get_contents($this->moodleDir . '/.nvmrc'));
5456
}
5557

58+
public function testInstallNoNvm()
59+
{
60+
$installer = new VendorInstaller(
61+
new DummyMoodle($this->moodleDir),
62+
new MoodlePlugin($this->pluginDir),
63+
new DummyExecute(),
64+
false,
65+
null,
66+
true,
67+
);
68+
69+
$installer->install();
70+
71+
$this->assertSame(3, $installer->getOutput()->getStepCount());
72+
}
73+
5674
public function testInstallNodeUserVersion()
5775
{
5876
$userVersion = '8.9';
@@ -61,8 +79,10 @@ public function testInstallNodeUserVersion()
6179
new MoodlePlugin($this->pluginDir),
6280
new DummyExecute(),
6381
false,
64-
$userVersion
82+
$userVersion,
83+
false,
6584
);
85+
6686
$installer->installNode();
6787

6888
// Expect .nvmrc containing user specified version.
@@ -77,14 +97,15 @@ public function testInstallNodePluginDependencies()
7797
new MoodlePlugin($this->pluginDir),
7898
new DummyExecute(),
7999
false,
80-
null
100+
null,
101+
false,
81102
);
82103

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

85106
$installer->install();
86107

87-
$this->assertSame(4, $installer->getOutput()->getStepCount());
108+
$this->assertSame(5, $installer->getOutput()->getStepCount());
88109
}
89110

90111
public function testSkipNodePluginDependencies()
@@ -94,13 +115,14 @@ public function testSkipNodePluginDependencies()
94115
new MoodlePlugin($this->pluginDir),
95116
new DummyExecute(),
96117
true,
97-
null
118+
null,
119+
false,
98120
);
99121

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

102124
$installer->install();
103125

104-
$this->assertSame(3, $installer->getOutput()->getStepCount());
126+
$this->assertSame(4, $installer->getOutput()->getStepCount());
105127
}
106128
}

0 commit comments

Comments
 (0)