Skip to content

Commit 06ee69b

Browse files
authored
Merge pull request pre-commit#685 from pre-commit/windows_node
Support node on windows with long path hack
2 parents 577f109 + 6e46d6a commit 06ee69b

9 files changed

Lines changed: 54 additions & 36 deletions

File tree

pre_commit/languages/node.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pre_commit.envcontext import envcontext
88
from pre_commit.envcontext import Var
99
from pre_commit.languages import helpers
10+
from pre_commit.languages.python import bin_dir
1011
from pre_commit.util import clean_path_on_failure
1112
from pre_commit.util import cmd_output
1213
from pre_commit.xargs import xargs
@@ -17,40 +18,44 @@
1718
healthy = helpers.basic_healthy
1819

1920

20-
def get_env_patch(venv): # pragma: windows no cover
21+
def _envdir(prefix, version):
22+
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
23+
return prefix.path(directory)
24+
25+
26+
def get_env_patch(venv):
2127
if sys.platform == 'cygwin': # pragma: no cover
2228
_, win_venv, _ = cmd_output('cygpath', '-w', venv)
2329
install_prefix = r'{}\bin'.format(win_venv.strip())
30+
elif sys.platform == 'win32': # pragma: no cover
31+
install_prefix = bin_dir(venv)
2432
else:
2533
install_prefix = venv
2634
return (
2735
('NODE_VIRTUAL_ENV', venv),
2836
('NPM_CONFIG_PREFIX', install_prefix),
2937
('npm_config_prefix', install_prefix),
3038
('NODE_PATH', os.path.join(venv, 'lib', 'node_modules')),
31-
('PATH', (os.path.join(venv, 'bin'), os.pathsep, Var('PATH'))),
39+
('PATH', (bin_dir(venv), os.pathsep, Var('PATH'))),
3240
)
3341

3442

3543
@contextlib.contextmanager
36-
def in_env(prefix, language_version): # pragma: windows no cover
37-
envdir = prefix.path(
38-
helpers.environment_dir(ENVIRONMENT_DIR, language_version),
39-
)
40-
with envcontext(get_env_patch(envdir)):
44+
def in_env(prefix, language_version):
45+
with envcontext(get_env_patch(_envdir(prefix, language_version))):
4146
yield
4247

4348

44-
def install_environment(
45-
prefix, version, additional_dependencies,
46-
): # pragma: windows no cover
49+
def install_environment(prefix, version, additional_dependencies):
4750
additional_dependencies = tuple(additional_dependencies)
4851
assert prefix.exists('package.json')
49-
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
52+
envdir = _envdir(prefix, version)
5053

51-
env_dir = prefix.path(directory)
52-
with clean_path_on_failure(env_dir):
53-
cmd = [sys.executable, '-m', 'nodeenv', '--prebuilt', env_dir]
54+
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx?f=255&MSPPError=-2147217396#maxpath
55+
if sys.platform == 'win32': # pragma: no cover
56+
envdir = '\\\\?\\' + os.path.normpath(envdir)
57+
with clean_path_on_failure(envdir):
58+
cmd = [sys.executable, '-m', 'nodeenv', '--prebuilt', envdir]
5459
if version != 'default':
5560
cmd.extend(['-n', version])
5661
cmd_output(*cmd)
@@ -62,6 +67,6 @@ def install_environment(
6267
)
6368

6469

65-
def run_hook(prefix, hook, file_args): # pragma: windows no cover
70+
def run_hook(prefix, hook, file_args):
6671
with in_env(prefix, hook['language_version']):
6772
return xargs(helpers.to_cmd(hook), file_args)

testing/resources/node_0_11_8_hooks_repo/.pre-commit-hooks.yaml

Lines changed: 0 additions & 6 deletions
This file was deleted.

testing/resources/node_0_11_8_hooks_repo/package.json

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- id: versioned-node-hook
2+
name: Versioned node hook
3+
entry: versioned-node-hook
4+
language: node
5+
language_version: 9.3.0
6+
files: \.js$

testing/resources/node_0_11_8_hooks_repo/bin/main.js renamed to testing/resources/node_versioned_hooks_repo/bin/main.js

File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "versioned-node-hook",
3+
"version": "0.0.1",
4+
"bin": {"versioned-node-hook": "./bin/main.js"}
5+
}

testing/util.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import unicode_literals
22

33
import os.path
4+
import sys
45

56
import pytest
67

@@ -42,9 +43,21 @@ def cmd_output_mocked_pre_commit_home(*args, **kwargs):
4243
reason='Ruby support not yet implemented on windows.',
4344
)
4445

45-
xfailif_windows_no_node = pytest.mark.xfail(
46-
os.name == 'nt',
47-
reason='Node support not yet implemented on windows.',
46+
47+
def broken_deep_listdir(): # pragma: no cover (platform specific)
48+
if sys.platform != 'win32':
49+
return False
50+
try:
51+
os.listdir(str('\\\\?\\') + os.path.abspath(str('.')))
52+
except OSError:
53+
return True
54+
else:
55+
return False
56+
57+
58+
xfailif_broken_deep_listdir = pytest.mark.xfail(
59+
broken_deep_listdir(),
60+
reason='Node on windows requires deep listdir',
4861
)
4962

5063

tests/repository_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
from testing.util import get_resource_path
3232
from testing.util import skipif_cant_run_docker
3333
from testing.util import skipif_cant_run_swift
34+
from testing.util import xfailif_broken_deep_listdir
3435
from testing.util import xfailif_no_pcre_support
35-
from testing.util import xfailif_windows_no_node
3636
from testing.util import xfailif_windows_no_ruby
3737

3838

@@ -186,7 +186,7 @@ def test_run_a_docker_image_hook(tempdir_factory, store, hook_id):
186186
)
187187

188188

189-
@xfailif_windows_no_node
189+
@xfailif_broken_deep_listdir
190190
@pytest.mark.integration
191191
def test_run_a_node_hook(tempdir_factory, store):
192192
_test_hook_repo(
@@ -195,12 +195,12 @@ def test_run_a_node_hook(tempdir_factory, store):
195195
)
196196

197197

198-
@xfailif_windows_no_node
198+
@xfailif_broken_deep_listdir
199199
@pytest.mark.integration
200200
def test_run_versioned_node_hook(tempdir_factory, store):
201201
_test_hook_repo(
202-
tempdir_factory, store, 'node_0_11_8_hooks_repo',
203-
'node-11-8-hook', [os.devnull], b'v0.11.8\nHello World\n',
202+
tempdir_factory, store, 'node_versioned_hooks_repo',
203+
'versioned-node-hook', [os.devnull], b'v9.3.0\nHello World\n',
204204
)
205205

206206

@@ -505,7 +505,7 @@ def test_additional_ruby_dependencies_installed(
505505
assert 'tins' in output
506506

507507

508-
@xfailif_windows_no_node
508+
@xfailif_broken_deep_listdir
509509
@pytest.mark.integration
510510
def test_additional_node_dependencies_installed(
511511
tempdir_factory, store,

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ envlist = py27,py35,py36,pypy
55

66
[testenv]
77
deps = -rrequirements-dev.txt
8-
passenv = GOROOT HOME HOMEPATH PROGRAMDATA TERM
8+
passenv = GOROOT HOME HOMEPATH PROCESSOR_ARCHITECTURE PROGRAMDATA TERM
99
commands =
1010
coverage erase
1111
coverage run -m pytest {posargs:tests}

0 commit comments

Comments
 (0)