Skip to content

Commit b16a63a

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 b16a63a

File tree

9 files changed

+92
-17
lines changed

9 files changed

+92
-17
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 4 deletions
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

Lines changed: 5 additions & 0 deletions
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 is not required anymore.
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

Lines changed: 11 additions & 1 deletion
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class VendorInstaller extends AbstractInstaller
2727
private Execute $execute;
2828
private bool $noPluginNode;
2929
public ?string $nodeVer;
30+
private bool $noNvm;
31+
3032
/**
3133
* Define legacy Node version to use when .nvmrc is absent (Moodle < 3.5).
3234
*/
@@ -38,17 +40,22 @@ class VendorInstaller extends AbstractInstaller
3840
* @param Execute $execute
3941
* @param string|null $nodeVer
4042
*/
41-
public function __construct(Moodle $moodle, MoodlePlugin $plugin, Execute $execute, bool $noPluginNode, ?string $nodeVer)
43+
public function __construct(Moodle $moodle, MoodlePlugin $plugin, Execute $execute, bool $noPluginNode, ?string $nodeVer, bool $noNvm)
4244
{
4345
$this->moodle = $moodle;
4446
$this->plugin = $plugin;
4547
$this->execute = $execute;
4648
$this->nodeVer = $nodeVer;
4749
$this->noPluginNode = $noPluginNode;
50+
$this->noNvm = $noNvm;
4851
}
4952

5053
public function install(): void
5154
{
55+
if ($this->canInstallNvm()) {
56+
$this->getOutput()->step('Installing nvm');
57+
$this->installNvm();
58+
}
5259
if ($this->canInstallNode()) {
5360
$this->getOutput()->step('Installing Node.js');
5461
$this->installNode();
@@ -85,10 +92,37 @@ public function install(): void
8592
public function stepCount(): int
8693
{
8794
return 2 + // Normally 2 steps: global dependencies and Moodle npm dependencies.
95+
($this->canInstallNvm() ? 1 : 0) + // Plus nvm installation.
8896
($this->canInstallNode() ? 1 : 0) + // Plus Node.js installation.
8997
((!$this->noPluginNode && $this->plugin->hasNodeDependencies()) ? 1 : 0); // Plus plugin npm dependencies step.
9098
}
9199

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

tests/Installer/VendorInstallerTest.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ public function testInstall()
2727
new MoodlePlugin($this->pluginDir),
2828
new DummyExecute(),
2929
false,
30-
null
30+
null,
31+
false,
3132
);
33+
// Unset NVM_DIR.
34+
putenv('NVM_DIR');
35+
3236
$installer->install();
3337

38+
$this->assertNotEmpty(getenv('NVM_DIR'));
3439
$this->assertSame($installer->stepCount(), $installer->getOutput()->getStepCount());
3540
}
3641

@@ -41,7 +46,8 @@ public function testInstallNodeNoNvmrc()
4146
new MoodlePlugin($this->pluginDir),
4247
new DummyExecute(),
4348
false,
44-
null
49+
null,
50+
false,
4551
);
4652

4753
// Remove .nvmrc
@@ -53,6 +59,25 @@ public function testInstallNodeNoNvmrc()
5359
$this->assertSame('lts/carbon', file_get_contents($this->moodleDir . '/.nvmrc'));
5460
}
5561

62+
public function testInstallNoNvm()
63+
{
64+
$installer = new VendorInstaller(
65+
new DummyMoodle($this->moodleDir),
66+
new MoodlePlugin($this->pluginDir),
67+
new DummyExecute(),
68+
false,
69+
null,
70+
true,
71+
);
72+
// Unset NVM_DIR.
73+
putenv('NVM_DIR');
74+
75+
$installer->install();
76+
77+
$this->assertFalse(getenv('NVM_DIR'));
78+
$this->assertSame(2, $installer->getOutput()->getStepCount());
79+
}
80+
5681
public function testInstallNodeUserVersion()
5782
{
5883
$userVersion = '8.9';
@@ -61,8 +86,10 @@ public function testInstallNodeUserVersion()
6186
new MoodlePlugin($this->pluginDir),
6287
new DummyExecute(),
6388
false,
64-
$userVersion
89+
$userVersion,
90+
false,
6591
);
92+
6693
$installer->installNode();
6794

6895
// Expect .nvmrc containing user specified version.
@@ -77,14 +104,15 @@ public function testInstallNodePluginDependencies()
77104
new MoodlePlugin($this->pluginDir),
78105
new DummyExecute(),
79106
false,
80-
null
107+
null,
108+
false,
81109
);
82110

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

85113
$installer->install();
86114

87-
$this->assertSame(4, $installer->getOutput()->getStepCount());
115+
$this->assertSame(5, $installer->getOutput()->getStepCount());
88116
}
89117

90118
public function testSkipNodePluginDependencies()
@@ -94,13 +122,14 @@ public function testSkipNodePluginDependencies()
94122
new MoodlePlugin($this->pluginDir),
95123
new DummyExecute(),
96124
true,
97-
null
125+
null,
126+
false,
98127
);
99128

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

102131
$installer->install();
103132

104-
$this->assertSame(3, $installer->getOutput()->getStepCount());
133+
$this->assertSame(4, $installer->getOutput()->getStepCount());
105134
}
106135
}

0 commit comments

Comments
 (0)