mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge pull request #1388 from pre-commit/node_default_system
Default to `language_version: system` if node and npm are installed
This commit is contained in:
commit
282527ef16
4 changed files with 81 additions and 11 deletions
|
|
@ -17,34 +17,34 @@ repos:
|
||||||
- id: flake8
|
- id: flake8
|
||||||
additional_dependencies: [flake8-typing-imports==1.6.0]
|
additional_dependencies: [flake8-typing-imports==1.6.0]
|
||||||
- repo: https://github.com/pre-commit/mirrors-autopep8
|
- repo: https://github.com/pre-commit/mirrors-autopep8
|
||||||
rev: v1.5
|
rev: v1.5.1
|
||||||
hooks:
|
hooks:
|
||||||
- id: autopep8
|
- id: autopep8
|
||||||
- repo: https://github.com/pre-commit/pre-commit
|
- repo: https://github.com/pre-commit/pre-commit
|
||||||
rev: v2.1.1
|
rev: v2.2.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: validate_manifest
|
- id: validate_manifest
|
||||||
- repo: https://github.com/asottile/pyupgrade
|
- repo: https://github.com/asottile/pyupgrade
|
||||||
rev: v2.0.1
|
rev: v2.1.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: pyupgrade
|
- id: pyupgrade
|
||||||
args: [--py36-plus]
|
args: [--py36-plus]
|
||||||
- repo: https://github.com/asottile/reorder_python_imports
|
- repo: https://github.com/asottile/reorder_python_imports
|
||||||
rev: v1.9.0
|
rev: v2.1.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: reorder-python-imports
|
- id: reorder-python-imports
|
||||||
args: [--py3-plus]
|
args: [--py3-plus]
|
||||||
- repo: https://github.com/asottile/add-trailing-comma
|
- repo: https://github.com/asottile/add-trailing-comma
|
||||||
rev: v1.5.0
|
rev: v2.0.1
|
||||||
hooks:
|
hooks:
|
||||||
- id: add-trailing-comma
|
- id: add-trailing-comma
|
||||||
args: [--py36-plus]
|
args: [--py36-plus]
|
||||||
- repo: https://github.com/asottile/setup-cfg-fmt
|
- repo: https://github.com/asottile/setup-cfg-fmt
|
||||||
rev: v1.6.0
|
rev: v1.8.2
|
||||||
hooks:
|
hooks:
|
||||||
- id: setup-cfg-fmt
|
- id: setup-cfg-fmt
|
||||||
- repo: https://github.com/pre-commit/mirrors-mypy
|
- repo: https://github.com/pre-commit/mirrors-mypy
|
||||||
rev: v0.761
|
rev: v0.770
|
||||||
hooks:
|
hooks:
|
||||||
- id: mypy
|
- id: mypy
|
||||||
exclude: ^testing/resources/
|
exclude: ^testing/resources/
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import contextlib
|
import contextlib
|
||||||
|
import functools
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from typing import Generator
|
from typing import Generator
|
||||||
|
|
@ -6,6 +7,7 @@ from typing import Sequence
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
|
from pre_commit import parse_shebang
|
||||||
from pre_commit.envcontext import envcontext
|
from pre_commit.envcontext import envcontext
|
||||||
from pre_commit.envcontext import PatchesT
|
from pre_commit.envcontext import PatchesT
|
||||||
from pre_commit.envcontext import Var
|
from pre_commit.envcontext import Var
|
||||||
|
|
@ -18,10 +20,22 @@ from pre_commit.util import cmd_output
|
||||||
from pre_commit.util import cmd_output_b
|
from pre_commit.util import cmd_output_b
|
||||||
|
|
||||||
ENVIRONMENT_DIR = 'node_env'
|
ENVIRONMENT_DIR = 'node_env'
|
||||||
get_default_version = helpers.basic_get_default_version
|
|
||||||
healthy = helpers.basic_healthy
|
healthy = helpers.basic_healthy
|
||||||
|
|
||||||
|
|
||||||
|
@functools.lru_cache(maxsize=1)
|
||||||
|
def get_default_version() -> str:
|
||||||
|
# nodeenv does not yet support `-n system` on windows
|
||||||
|
if sys.platform == 'win32':
|
||||||
|
return C.DEFAULT
|
||||||
|
# if node is already installed, we can save a bunch of setup time by
|
||||||
|
# using the installed version
|
||||||
|
elif all(parse_shebang.find_executable(exe) for exe in ('node', 'npm')):
|
||||||
|
return 'system'
|
||||||
|
else:
|
||||||
|
return C.DEFAULT
|
||||||
|
|
||||||
|
|
||||||
def _envdir(prefix: Prefix, version: str) -> str:
|
def _envdir(prefix: Prefix, version: str) -> str:
|
||||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
|
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
|
||||||
return prefix.path(directory)
|
return prefix.path(directory)
|
||||||
|
|
|
||||||
47
tests/languages/node_test.py
Normal file
47
tests/languages/node_test.py
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
import sys
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
import pre_commit.constants as C
|
||||||
|
from pre_commit import parse_shebang
|
||||||
|
from pre_commit.languages.node import get_default_version
|
||||||
|
|
||||||
|
|
||||||
|
ACTUAL_GET_DEFAULT_VERSION = get_default_version.__wrapped__
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def is_linux():
|
||||||
|
with mock.patch.object(sys, 'platform', 'linux'):
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def is_win32():
|
||||||
|
with mock.patch.object(sys, 'platform', 'win32'):
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def find_exe_mck():
|
||||||
|
with mock.patch.object(parse_shebang, 'find_executable') as mck:
|
||||||
|
yield mck
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures('is_linux')
|
||||||
|
def test_sets_system_when_node_and_npm_are_available(find_exe_mck):
|
||||||
|
find_exe_mck.return_value = '/path/to/exe'
|
||||||
|
assert ACTUAL_GET_DEFAULT_VERSION() == 'system'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures('is_linux')
|
||||||
|
def test_uses_default_when_node_and_npm_are_not_available(find_exe_mck):
|
||||||
|
find_exe_mck.return_value = None
|
||||||
|
assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures('is_win32')
|
||||||
|
def test_sets_default_on_windows(find_exe_mck):
|
||||||
|
find_exe_mck.return_value = '/path/to/exe'
|
||||||
|
assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT
|
||||||
|
|
@ -131,9 +131,9 @@ def test_python_hook(tempdir_factory, store):
|
||||||
def test_python_hook_default_version(tempdir_factory, store):
|
def test_python_hook_default_version(tempdir_factory, store):
|
||||||
# make sure that this continues to work for platforms where default
|
# make sure that this continues to work for platforms where default
|
||||||
# language detection does not work
|
# language detection does not work
|
||||||
with mock.patch.object(
|
returns_default = mock.Mock(return_value=C.DEFAULT)
|
||||||
python, 'get_default_version', return_value=C.DEFAULT,
|
lang = languages['python']._replace(get_default_version=returns_default)
|
||||||
):
|
with mock.patch.dict(languages, python=lang):
|
||||||
test_python_hook(tempdir_factory, store)
|
test_python_hook(tempdir_factory, store)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -243,6 +243,15 @@ def test_run_a_node_hook(tempdir_factory, store):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_run_a_node_hook_default_version(tempdir_factory, store):
|
||||||
|
# make sure that this continues to work for platforms where node is not
|
||||||
|
# installed at the system
|
||||||
|
returns_default = mock.Mock(return_value=C.DEFAULT)
|
||||||
|
lang = languages['node']._replace(get_default_version=returns_default)
|
||||||
|
with mock.patch.dict(languages, node=lang):
|
||||||
|
test_run_a_node_hook(tempdir_factory, store)
|
||||||
|
|
||||||
|
|
||||||
def test_run_versioned_node_hook(tempdir_factory, store):
|
def test_run_versioned_node_hook(tempdir_factory, store):
|
||||||
_test_hook_repo(
|
_test_hook_repo(
|
||||||
tempdir_factory, store, 'node_versioned_hooks_repo',
|
tempdir_factory, store, 'node_versioned_hooks_repo',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue