diff --git a/Makefile b/Makefile index 6f4a8cc0fa..2009bdc54d 100644 --- a/Makefile +++ b/Makefile @@ -334,6 +334,11 @@ install-ansible: install-force: @bash scripts/install/all.sh --force +.PHONY: install-node +# Install the Node.js runtime the JavaScript unit suite runs on. +install-node: + @bash scripts/install/node.sh + .PHONY: install-lint # Install lint dependencies. # Note: host or docker selected via INFINITO_LINT_RUNNER; incremental via a per-env stamp. @@ -819,8 +824,8 @@ test-signed: test-unit: test-unit-python test-unit-javascript test-unit-php test-unit-ruby .PHONY: test-unit-javascript -# Run the JavaScript unit test suite (node:test, no extra dependency). -test-unit-javascript: +# Run the JavaScript unit test suite (node:test, no npm dependency). +test-unit-javascript: install-node @bash scripts/tests/unit/javascript.sh .PHONY: test-unit-php diff --git a/scripts/install/node.sh b/scripts/install/node.sh new file mode 100755 index 0000000000..b0601a0399 --- /dev/null +++ b/scripts/install/node.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +# shellcheck shell=bash +set -euo pipefail +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" +REPO_ROOT="$(cd -- "${SCRIPT_DIR}/../.." && pwd)" +cd "${REPO_ROOT}" +exec python3 -m utils.install.node "$@" diff --git a/tests/unit/python/utils/install/test_node.py b/tests/unit/python/utils/install/test_node.py new file mode 100644 index 0000000000..18b0667d54 --- /dev/null +++ b/tests/unit/python/utils/install/test_node.py @@ -0,0 +1,41 @@ +"""Unit tests for :mod:`utils.install.node`.""" + +from __future__ import annotations + +import unittest +import unittest.mock as mock + +from utils.install import node as node_mod + + +class TestEnsureNodePresent(unittest.TestCase): + def test_present_noop(self) -> None: + with ( + mock.patch.object(node_mod.shutil, "which", return_value="/usr/bin/node"), + mock.patch.object(node_mod, "install_command_via_pkg") as install_pkg, + ): + node_mod.ensure_node_present() + install_pkg.assert_not_called() + + def test_installs_via_system_pkg(self) -> None: + whiches = iter([None, "/usr/bin/node"]) + with ( + mock.patch.object( + node_mod.shutil, "which", side_effect=lambda _x: next(whiches) + ), + mock.patch.object(node_mod, "install_command_via_pkg") as install_pkg, + ): + node_mod.ensure_node_present() + install_pkg.assert_called_once_with("node") + + def test_raises_when_still_missing(self) -> None: + with ( + mock.patch.object(node_mod.shutil, "which", return_value=None), + mock.patch.object(node_mod, "install_command_via_pkg"), + self.assertRaises(RuntimeError), + ): + node_mod.ensure_node_present() + + +if __name__ == "__main__": + unittest.main() diff --git a/utils/install/node.py b/utils/install/node.py new file mode 100644 index 0000000000..c1a38db647 --- /dev/null +++ b/utils/install/node.py @@ -0,0 +1,33 @@ +"""Node.js runtime provisioning for the JavaScript unit suite. + +``make test-unit-javascript`` runs ``node --test``; the runtime is a hard +requirement of that target, not an optional extra, so a host that cannot +install it fails loudly instead of silently skipping the suite. +""" + +from __future__ import annotations + +import shutil + +from utils.install.primitives import log +from utils.install.system_pkg import install_command_via_pkg + + +def ensure_node_present() -> None: + """Install the Node.js runtime through the system package manager. + + Raises: + RuntimeError: node is still absent after the install attempt. + """ + if shutil.which("node") is not None: + return + + log("node missing; attempting Node.js install via system package manager.") + install_command_via_pkg("node") + + if shutil.which("node") is None: + raise RuntimeError("node not found and could not be installed") + + +if __name__ == "__main__": + ensure_node_present() diff --git a/utils/install/system_pkg.py b/utils/install/system_pkg.py index 3886d874ea..72c5dc95a2 100644 --- a/utils/install/system_pkg.py +++ b/utils/install/system_pkg.py @@ -117,6 +117,13 @@ _COMMAND_PACKAGES: dict[str, dict[str, list[str]]] = { "yum": ["npm", "nodejs"], "brew": ["node"], }, + "node": { + "pacman": ["nodejs"], + "apt-get": ["nodejs"], + "dnf": ["nodejs"], + "yum": ["nodejs"], + "brew": ["node"], + }, }