Support hooks with custom node version.

This commit is contained in:
Anthony Sottile
2014-06-02 16:10:59 -07:00
parent 4b98b39cea
commit 511de4fe32
8 changed files with 61 additions and 31 deletions

View File

@@ -2,9 +2,9 @@
sha: ca93f6834f2afc8a8f7de46c0e02076419077c7a
hooks:
- id: trailing-whitespace
files: \.(py|sh|yaml)$
files: \.(js|py|sh|yaml)$
- id: end-of-file-fixer
files: \.(py|sh|yaml)$
files: \.(js|py|sh|yaml)$
- id: check-yaml
files: \.(yaml|yml)$
- id: debug-statements

View File

@@ -1,7 +1,6 @@
import contextlib
from pre_commit.languages import helpers
from pre_commit.languages import python
from pre_commit.prefixed_command_runner import CalledProcessError
from pre_commit.util import clean_path_on_failure
@@ -9,14 +8,10 @@ from pre_commit.util import clean_path_on_failure
ENVIRONMENT_DIR = 'node_env'
class NodeEnv(python.PythonEnv):
class NodeEnv(helpers.Environment):
@property
def env_prefix(self):
base = super(NodeEnv, self).env_prefix
return ' '.join([
base,
'. {{prefix}}{0}/bin/activate &&'.format(ENVIRONMENT_DIR)]
)
return '. {{prefix}}{0}/bin/activate &&'.format(ENVIRONMENT_DIR)
@contextlib.contextmanager
@@ -27,29 +22,31 @@ def in_env(repo_cmd_runner):
def install_environment(repo_cmd_runner, version='default'):
assert repo_cmd_runner.exists('package.json')
with clean_path_on_failure(repo_cmd_runner.path(python.ENVIRONMENT_DIR)):
repo_cmd_runner.run(
['virtualenv', '{{prefix}}{0}'.format(python.ENVIRONMENT_DIR)],
)
env_dir = repo_cmd_runner.path(ENVIRONMENT_DIR)
with clean_path_on_failure(env_dir):
if version == 'default':
# In the default case we attempt to install system node and if that
# doesn't work we use --prebuilt
try:
with clean_path_on_failure(env_dir):
repo_cmd_runner.run([
'nodeenv', '-n', 'system',
'{{prefix}}{0}'.format(ENVIRONMENT_DIR),
])
except CalledProcessError:
# TODO: log failure here
repo_cmd_runner.run([
'nodeenv', '--prebuilt',
'{{prefix}}{0}'.format(ENVIRONMENT_DIR)
])
else:
repo_cmd_runner.run([
'nodeenv', '--prebuilt', '-n', version,
'{{prefix}}{0}'.format(ENVIRONMENT_DIR)
])
with python.in_env(repo_cmd_runner) as python_env:
python_env.run('pip install nodeenv')
with clean_path_on_failure(repo_cmd_runner.path(ENVIRONMENT_DIR)):
# Try and use the system level node executable first
try:
with clean_path_on_failure(repo_cmd_runner.path(ENVIRONMENT_DIR)):
python_env.run(
'nodeenv -n system {{prefix}}{0}'.format(ENVIRONMENT_DIR),
)
except CalledProcessError:
# TODO: log failure here
python_env.run(
'nodeenv --jobs 4 {{prefix}}{0}'.format(ENVIRONMENT_DIR),
)
with in_env(repo_cmd_runner) as node_env:
node_env.run('cd {prefix} && npm install -g')
with in_env(repo_cmd_runner) as node_env:
node_env.run('cd {prefix} && npm install -g')
def run_hook(repo_cmd_runner, hook, file_args):

View File

@@ -33,6 +33,7 @@ setup(
'asottile.ordereddict',
'asottile.yaml',
'jsonschema',
'nodeenv>=0.9.4',
'plumbum',
'pyyaml',
'simplejson',

View File

@@ -0,0 +1,4 @@
#!/usr/bin/env node
console.log(process.version);
console.log('Hello World');

View File

@@ -0,0 +1,5 @@
- id: node-11-8-hook
name: Node 0.11.8 hook
entry: node-11-8-hook
language: node
language_version: 0.11.8

View File

@@ -0,0 +1,5 @@
{
"name": "node-11-8-hook",
"version": "0.0.1",
"bin": {"node-11-8-hook": "./bin/main.js"}
}

View File

@@ -82,6 +82,11 @@ def node_hooks_repo(dummy_git_repo):
yield _make_repo(dummy_git_repo, 'node_hooks_repo')
@pytest.yield_fixture
def node_0_11_8_hooks_repo(dummy_git_repo):
yield _make_repo(dummy_git_repo, 'node_0_11_8_hooks_repo')
@pytest.yield_fixture
def ruby_hooks_repo(dummy_git_repo):
yield _make_repo(dummy_git_repo, 'ruby_hooks_repo')
@@ -128,6 +133,11 @@ def config_for_node_hooks_repo(node_hooks_repo):
yield _make_config(node_hooks_repo, 'foo', '\\.js$')
@pytest.yield_fixture
def config_for_node_0_11_8_hooks_repo(node_0_11_8_hooks_repo):
yield _make_config(node_0_11_8_hooks_repo, 'node-11-8-hook', '\\.js$')
@pytest.yield_fixture
def config_for_ruby_hooks_repo(ruby_hooks_repo):
yield _make_config(ruby_hooks_repo, 'ruby_hook', '\\.rb$')

View File

@@ -32,6 +32,14 @@ def test_run_versioned_hook(config_for_python3_hooks_repo, store):
assert ret[1] == "3.3\n['/dev/null']\nHello World\n"
@pytest.mark.integration
def test_run_versioned_node_hook(config_for_node_0_11_8_hooks_repo, store):
repo = Repository.create(config_for_node_0_11_8_hooks_repo, store)
ret = repo.run_hook('node-11-8-hook', ['/dev/null'])
assert ret[0] == 0
assert ret[1] == 'v0.11.8\nHello World\n'
@pytest.mark.integration
def test_lots_of_files(config_for_python_hooks_repo, store):
repo = Repository.create(config_for_python_hooks_repo, store)