summaryrefslogtreecommitdiffstats
path: root/tests/languages/node_test.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2020-11-03 06:17:33 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2020-11-03 06:17:33 +0000
commit22b6d3abcec9046e294d08c741aa7b6338984e84 (patch)
treea0a9c823a7d82771ffb8a8b39f7143d5ba2f6b30 /tests/languages/node_test.py
parentAdding upstream version 2.7.1. (diff)
downloadpre-commit-22b6d3abcec9046e294d08c741aa7b6338984e84.tar.xz
pre-commit-22b6d3abcec9046e294d08c741aa7b6338984e84.zip
Adding upstream version 2.8.2.upstream/2.8.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/languages/node_test.py')
-rw-r--r--tests/languages/node_test.py65
1 files changed, 63 insertions, 2 deletions
diff --git a/tests/languages/node_test.py b/tests/languages/node_test.py
index fd30046..8e52268 100644
--- a/tests/languages/node_test.py
+++ b/tests/languages/node_test.py
@@ -1,14 +1,21 @@
+import json
+import os
+import shutil
import sys
from unittest import mock
import pytest
import pre_commit.constants as C
+from pre_commit import envcontext
from pre_commit import parse_shebang
-from pre_commit.languages.node import get_default_version
+from pre_commit.languages import node
+from pre_commit.prefix import Prefix
+from pre_commit.util import cmd_output
+from testing.util import xfailif_windows
-ACTUAL_GET_DEFAULT_VERSION = get_default_version.__wrapped__
+ACTUAL_GET_DEFAULT_VERSION = node.get_default_version.__wrapped__
@pytest.fixture
@@ -45,3 +52,57 @@ def test_uses_default_when_node_and_npm_are_not_available(find_exe_mck):
def test_sets_default_on_windows(find_exe_mck):
find_exe_mck.return_value = '/path/to/exe'
assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT
+
+
+@xfailif_windows # pragma: win32 no cover
+def test_healthy_system_node(tmpdir):
+ tmpdir.join('package.json').write('{"name": "t", "version": "1.0.0"}')
+
+ prefix = Prefix(str(tmpdir))
+ node.install_environment(prefix, 'system', ())
+ assert node.healthy(prefix, 'system')
+
+
+@xfailif_windows # pragma: win32 no cover
+def test_unhealthy_if_system_node_goes_missing(tmpdir):
+ bin_dir = tmpdir.join('bin').ensure_dir()
+ node_bin = bin_dir.join('node')
+ node_bin.mksymlinkto(shutil.which('node'))
+
+ prefix_dir = tmpdir.join('prefix').ensure_dir()
+ prefix_dir.join('package.json').write('{"name": "t", "version": "1.0.0"}')
+
+ path = ('PATH', (str(bin_dir), os.pathsep, envcontext.Var('PATH')))
+ with envcontext.envcontext((path,)):
+ prefix = Prefix(str(prefix_dir))
+ node.install_environment(prefix, 'system', ())
+ assert node.healthy(prefix, 'system')
+
+ node_bin.remove()
+ assert not node.healthy(prefix, 'system')
+
+
+@xfailif_windows # pragma: win32 no cover
+def test_installs_without_links_outside_env(tmpdir):
+ tmpdir.join('bin/main.js').ensure().write(
+ '#!/usr/bin/env node\n'
+ '_ = require("lodash"); console.log("success!")\n',
+ )
+ tmpdir.join('package.json').write(
+ json.dumps({
+ 'name': 'foo',
+ 'version': '0.0.1',
+ 'bin': {'foo': './bin/main.js'},
+ 'dependencies': {'lodash': '*'},
+ }),
+ )
+
+ prefix = Prefix(str(tmpdir))
+ node.install_environment(prefix, 'system', ())
+ assert node.healthy(prefix, 'system')
+
+ # this directory shouldn't exist, make sure we succeed without it existing
+ cmd_output('rm', '-rf', str(tmpdir.join('node_modules')))
+
+ with node.in_env(prefix, 'system'):
+ assert cmd_output('foo')[1] == 'success!\n'