Merge pull request #94 from pre-commit/ruby_for_realz

Support ruby through a combination of rbenv, ruby-build, and GEM_HOME
This commit is contained in:
Anthony Sottile
2014-05-04 21:59:17 -07:00
10 changed files with 124 additions and 24 deletions

View File

@@ -1,16 +1,25 @@
from __future__ import unicode_literals
import contextlib
import io
import os
from pre_commit.languages import helpers
from pre_commit.util import clean_path_on_failure
ENVIRONMENT_DIR = 'rvm_env'
ENVIRONMENT_DIR = 'rbenv'
class RubyEnv(helpers.Environment):
@property
def env_prefix(self):
raise NotImplementedError
return '. {{prefix}}{0}/bin/activate &&'.format(ENVIRONMENT_DIR)
def run(self, *args, **kwargs):
# TODO: hardcoded version smell
env = dict(os.environ, RBENV_VERSION='1.9.3-p545')
return super(RubyEnv, self).run(*args, env=env, **kwargs)
@contextlib.contextmanager
@@ -18,15 +27,44 @@ def in_env(repo_cmd_runner):
yield RubyEnv(repo_cmd_runner)
def install_environment(repo_cmd_runner):
# Return immediately if we already have a virtualenv
if repo_cmd_runner.exists(ENVIRONMENT_DIR):
return
def _install_rbenv(repo_cmd_runner):
repo_cmd_runner.run([
'git', 'clone', 'git://github.com/sstephenson/rbenv', '{prefix}rbenv',
])
repo_cmd_runner.run([
'git', 'clone', 'git://github.com/sstephenson/ruby-build',
'{prefix}rbenv/plugins/ruby-build',
])
with clean_path_on_failure(repo_cmd_runner.path(ENVIRONMENT_DIR)):
repo_cmd_runner.run(['__rvm-env.sh', '{{prefix}}{0}'.format(ENVIRONMENT_DIR)])
with in_env(repo_cmd_runner) as env:
env.run('cd {prefix} && bundle install')
activate_path = repo_cmd_runner.path('rbenv', 'bin', 'activate')
with io.open(activate_path, 'w') as activate_file:
# This is similar to how you would install rbenv to your home directory
# However we do a couple things to make the executables exposed and
# configure it to work in our directory.
# We also modify the PS1 variable for manual debugging sake.
activate_file.write(
'#!/usr/bin/env bash\n'
"export RBENV_ROOT='{0}'\n"
'export PATH="$RBENV_ROOT/bin:$PATH"\n'
'eval "$(rbenv init -)"\n'
'export PS1="(rbenv)$PS1"\n'
# This lets us install gems in an isolated and repeatable
# directory
"export GEM_HOME='{0}/gems'\n"
'export PATH="$GEM_HOME/bin:$PATH"\n'
'\n'.format(repo_cmd_runner.path('rbenv'))
)
def install_environment(repo_cmd_runner):
with clean_path_on_failure(repo_cmd_runner.path('rbenv')):
_install_rbenv(repo_cmd_runner)
with in_env(repo_cmd_runner) as ruby_env:
# TODO: hardcoded version smell
ruby_env.run('rbenv install 1.9.3-p545')
ruby_env.run(
'cd {prefix} && gem build *.gemspec && gem install *.gem',
)
def run_hook(repo_cmd_runner, hook, file_args):

View File

@@ -0,0 +1 @@
*.gem

View File

@@ -0,0 +1,3 @@
#!/usr/bin/env ruby
puts 'Hello world from a ruby hook'

View File

@@ -0,0 +1,4 @@
- id: ruby_hook
name: Ruby Hook
entry: ruby_hook
language: ruby

View File

@@ -0,0 +1,9 @@
Gem::Specification.new do |s|
s.name = 'ruby_hook'
s.version = '0.1.0'
s.authors = ['Anthony Sottile']
s.summary = 'A ruby hook!'
s.description = 'A ruby hook!'
s.files = ['bin/ruby_hook']
s.executables = ['ruby_hook']
end

View File

@@ -1,6 +1,7 @@
import jsonschema
import os
import os.path
import pytest
import shutil
from plumbum import local
@@ -41,3 +42,10 @@ def is_valid_according_to_schema(obj, schema):
return True
except jsonschema.exceptions.ValidationError:
return False
def skipif_slowtests_false(func):
return pytest.mark.skipif(
os.environ.get('slowtests') == 'false',
reason='slowtests=false',
)(func)

View File

@@ -13,6 +13,7 @@ from pre_commit import five
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
from pre_commit.clientlib.validate_config import validate_config_extra
from pre_commit.jsonschema_extensions import apply_defaults
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
from pre_commit.store import Store
from testing.util import copy_tree_to_path
from testing.util import get_head_sha
@@ -76,6 +77,11 @@ def node_hooks_repo(dummy_git_repo):
yield _make_repo(dummy_git_repo, 'node_hooks_repo')
@pytest.yield_fixture
def ruby_hooks_repo(dummy_git_repo):
yield _make_repo(dummy_git_repo, 'ruby_hooks_repo')
@pytest.yield_fixture
def consumer_repo(dummy_git_repo):
yield _make_repo(dummy_git_repo, 'consumer_repo')
@@ -112,6 +118,11 @@ def config_for_node_hooks_repo(node_hooks_repo):
yield _make_config(node_hooks_repo, 'foo', '\\.js$')
@pytest.yield_fixture
def config_for_ruby_hooks_repo(ruby_hooks_repo):
yield _make_config(ruby_hooks_repo, 'ruby_hook', '\\.rb$')
@pytest.yield_fixture
def config_for_python_hooks_repo(python_hooks_repo):
yield _make_config(python_hooks_repo, 'foo', '\\.py$')
@@ -206,3 +217,8 @@ def mock_out_store_directory(tmpdir_factory):
@pytest.yield_fixture
def store(tmpdir_factory):
yield Store(os.path.join(tmpdir_factory.get(), '.pre-commit'))
@pytest.yield_fixture
def cmd_runner(tmpdir_factory):
yield PrefixedCommandRunner(tmpdir_factory.get())

View File

@@ -0,0 +1,23 @@
import os.path
from pre_commit.languages.ruby import _install_rbenv
def test_install_rbenv(cmd_runner):
_install_rbenv(cmd_runner)
# Should have created rbenv directory
assert os.path.exists(cmd_runner.path('rbenv'))
# It should be a git checkout
assert os.path.exists(cmd_runner.path('rbenv', '.git'))
# We should have created our `activate` script
activate_path = cmd_runner.path('rbenv', 'bin', 'activate')
assert os.path.exists(activate_path)
# Should be able to activate using our script and access the install method
cmd_runner.run(
[
'bash',
'-c',
'. {prefix}/rbenv/bin/activate && rbenv install --help',
],
)

View File

@@ -6,6 +6,7 @@ from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
from pre_commit.clientlib.validate_config import validate_config_extra
from pre_commit.jsonschema_extensions import apply_defaults
from pre_commit.repository import Repository
from testing.util import skipif_slowtests_false
@pytest.mark.integration
@@ -19,7 +20,6 @@ def test_install_python_repo_in_env(config_for_python_hooks_repo, store):
def test_run_a_python_hook(config_for_python_hooks_repo, store):
repo = Repository.create(config_for_python_hooks_repo, store)
ret = repo.run_hook('foo', ['/dev/null'])
assert ret[0] == 0
assert ret[1] == "['/dev/null']\nHello World\n"
@@ -28,7 +28,6 @@ def test_run_a_python_hook(config_for_python_hooks_repo, store):
def test_lots_of_files(config_for_python_hooks_repo, store):
repo = Repository.create(config_for_python_hooks_repo, store)
ret = repo.run_hook('foo', ['/dev/null'] * 15000)
assert ret[0] == 0
@@ -37,24 +36,29 @@ def test_cwd_of_hook(config_for_prints_cwd_repo, store):
# Note: this doubles as a test for `system` hooks
repo = Repository.create(config_for_prints_cwd_repo, store)
ret = repo.run_hook('prints_cwd', [])
assert ret[0] == 0
assert ret[1] == repo.repo_url + '\n'
@pytest.mark.skipif(
os.environ.get('slowtests', None) == 'false',
reason="TODO: make this test not super slow",
)
@skipif_slowtests_false
@pytest.mark.integration
def test_run_a_node_hook(config_for_node_hooks_repo, store):
repo = Repository.create(config_for_node_hooks_repo, store)
ret = repo.run_hook('foo', [])
assert ret[0] == 0
assert ret[1] == 'Hello World\n'
@pytest.mark.herpderp
@skipif_slowtests_false
@pytest.mark.integration
def test_run_a_ruby_hook(config_for_ruby_hooks_repo, store):
repo = Repository.create(config_for_ruby_hooks_repo, store)
ret = repo.run_hook('ruby_hook', [])
assert ret[0] == 0
assert ret[1] == 'Hello world from a ruby hook\n'
@pytest.mark.integration
def test_run_a_script_hook(config_for_script_hooks_repo, store):
repo = Repository.create(config_for_script_hooks_repo, store)

View File

@@ -8,7 +8,6 @@ import pytest
import shutil
from plumbum import local
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
from pre_commit.staged_files_only import staged_files_only
from testing.auto_namedtuple import auto_namedtuple
from testing.util import get_resource_path
@@ -31,11 +30,6 @@ def foo_staged(empty_git_dir):
yield auto_namedtuple(path=empty_git_dir, foo_filename=foo_filename)
@pytest.yield_fixture
def cmd_runner(tmpdir_factory):
yield PrefixedCommandRunner(tmpdir_factory.get())
def _test_foo_state(path, foo_contents=FOO_CONTENTS, status='A'):
assert os.path.exists(path.foo_filename)
assert io.open(path.foo_filename, encoding='utf-8').read() == foo_contents