Skip to content

Commit 0f52854

Browse files
committed
Default to language_version: system if node and npm are installed
1 parent 0a8ba31 commit 0f52854

File tree

4 files changed

+81
-11
lines changed

4 files changed

+81
-11
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,34 @@ repos:
1717
- id: flake8
1818
additional_dependencies: [flake8-typing-imports==1.6.0]
1919
- repo: https://github.com/pre-commit/mirrors-autopep8
20-
rev: v1.5
20+
rev: v1.5.1
2121
hooks:
2222
- id: autopep8
2323
- repo: https://github.com/pre-commit/pre-commit
24-
rev: v2.1.1
24+
rev: v2.2.0
2525
hooks:
2626
- id: validate_manifest
2727
- repo: https://github.com/asottile/pyupgrade
28-
rev: v2.0.1
28+
rev: v2.1.0
2929
hooks:
3030
- id: pyupgrade
3131
args: [--py36-plus]
3232
- repo: https://github.com/asottile/reorder_python_imports
33-
rev: v1.9.0
33+
rev: v2.1.0
3434
hooks:
3535
- id: reorder-python-imports
3636
args: [--py3-plus]
3737
- repo: https://github.com/asottile/add-trailing-comma
38-
rev: v1.5.0
38+
rev: v2.0.1
3939
hooks:
4040
- id: add-trailing-comma
4141
args: [--py36-plus]
4242
- repo: https://github.com/asottile/setup-cfg-fmt
43-
rev: v1.6.0
43+
rev: v1.8.2
4444
hooks:
4545
- id: setup-cfg-fmt
4646
- repo: https://github.com/pre-commit/mirrors-mypy
47-
rev: v0.761
47+
rev: v0.770
4848
hooks:
4949
- id: mypy
5050
exclude: ^testing/resources/

pre_commit/languages/node.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import contextlib
2+
import functools
23
import os
34
import sys
45
from typing import Generator
56
from typing import Sequence
67
from typing import Tuple
78

89
import pre_commit.constants as C
10+
from pre_commit import parse_shebang
911
from pre_commit.envcontext import envcontext
1012
from pre_commit.envcontext import PatchesT
1113
from pre_commit.envcontext import Var
@@ -18,10 +20,22 @@
1820
from pre_commit.util import cmd_output_b
1921

2022
ENVIRONMENT_DIR = 'node_env'
21-
get_default_version = helpers.basic_get_default_version
2223
healthy = helpers.basic_healthy
2324

2425

26+
@functools.lru_cache(maxsize=1)
27+
def get_default_version() -> str:
28+
# nodeenv does not yet support `-n system` on windows
29+
if sys.platform == 'win32':
30+
return C.DEFAULT
31+
# if node is already installed, we can save a bunch of setup time by
32+
# using the installed version
33+
elif all(parse_shebang.find_executable(exe) for exe in ('node', 'npm')):
34+
return 'system'
35+
else:
36+
return C.DEFAULT
37+
38+
2539
def _envdir(prefix: Prefix, version: str) -> str:
2640
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
2741
return prefix.path(directory)

tests/languages/node_test.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
from unittest import mock
3+
4+
import pytest
5+
6+
import pre_commit.constants as C
7+
from pre_commit import parse_shebang
8+
from pre_commit.languages.node import get_default_version
9+
10+
11+
ACTUAL_GET_DEFAULT_VERSION = get_default_version.__wrapped__
12+
13+
14+
@pytest.fixture
15+
def is_linux():
16+
with mock.patch.object(sys, 'platform', 'linux'):
17+
yield
18+
19+
20+
@pytest.fixture
21+
def is_win32():
22+
with mock.patch.object(sys, 'platform', 'win32'):
23+
yield
24+
25+
26+
@pytest.fixture
27+
def find_exe_mck():
28+
with mock.patch.object(parse_shebang, 'find_executable') as mck:
29+
yield mck
30+
31+
32+
@pytest.mark.usefixtures('is_linux')
33+
def test_sets_system_when_node_and_npm_are_available(find_exe_mck):
34+
find_exe_mck.return_value = '/path/to/exe'
35+
assert ACTUAL_GET_DEFAULT_VERSION() == 'system'
36+
37+
38+
@pytest.mark.usefixtures('is_linux')
39+
def test_uses_default_when_node_and_npm_are_not_available(find_exe_mck):
40+
find_exe_mck.return_value = None
41+
assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT
42+
43+
44+
@pytest.mark.usefixtures('is_win32')
45+
def test_sets_default_on_windows(find_exe_mck):
46+
find_exe_mck.return_value = '/path/to/exe'
47+
assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT

tests/repository_test.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ def test_python_hook(tempdir_factory, store):
131131
def test_python_hook_default_version(tempdir_factory, store):
132132
# make sure that this continues to work for platforms where default
133133
# language detection does not work
134-
with mock.patch.object(
135-
python, 'get_default_version', return_value=C.DEFAULT,
136-
):
134+
returns_default = mock.Mock(return_value=C.DEFAULT)
135+
lang = languages['python']._replace(get_default_version=returns_default)
136+
with mock.patch.dict(languages, python=lang):
137137
test_python_hook(tempdir_factory, store)
138138

139139

@@ -243,6 +243,15 @@ def test_run_a_node_hook(tempdir_factory, store):
243243
)
244244

245245

246+
def test_run_a_node_hook_default_version(tempdir_factory, store):
247+
# make sure that this continues to work for platforms where node is not
248+
# installed at the system
249+
returns_default = mock.Mock(return_value=C.DEFAULT)
250+
lang = languages['node']._replace(get_default_version=returns_default)
251+
with mock.patch.dict(languages, node=lang):
252+
test_run_a_node_hook(tempdir_factory, store)
253+
254+
246255
def test_run_versioned_node_hook(tempdir_factory, store):
247256
_test_hook_repo(
248257
tempdir_factory, store, 'node_versioned_hooks_repo',

0 commit comments

Comments
 (0)