mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 00:04:42 +04:00
Support language_version for ruby as well.
This commit is contained in:
parent
511de4fe32
commit
02a79bf1c3
8 changed files with 72 additions and 15 deletions
|
|
@ -2,7 +2,6 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
import contextlib
|
import contextlib
|
||||||
import io
|
import io
|
||||||
import os
|
|
||||||
|
|
||||||
from pre_commit.languages import helpers
|
from pre_commit.languages import helpers
|
||||||
from pre_commit.util import clean_path_on_failure
|
from pre_commit.util import clean_path_on_failure
|
||||||
|
|
@ -16,25 +15,23 @@ class RubyEnv(helpers.Environment):
|
||||||
def env_prefix(self):
|
def env_prefix(self):
|
||||||
return '. {{prefix}}{0}/bin/activate &&'.format(ENVIRONMENT_DIR)
|
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-p547')
|
|
||||||
return super(RubyEnv, self).run(*args, env=env, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def in_env(repo_cmd_runner):
|
def in_env(repo_cmd_runner):
|
||||||
yield RubyEnv(repo_cmd_runner)
|
yield RubyEnv(repo_cmd_runner)
|
||||||
|
|
||||||
|
|
||||||
def _install_rbenv(repo_cmd_runner):
|
def _install_rbenv(repo_cmd_runner, version='default'):
|
||||||
repo_cmd_runner.run([
|
repo_cmd_runner.run([
|
||||||
'git', 'clone', 'git://github.com/sstephenson/rbenv', '{prefix}rbenv',
|
'git', 'clone', 'git://github.com/sstephenson/rbenv', '{prefix}rbenv',
|
||||||
])
|
])
|
||||||
repo_cmd_runner.run([
|
|
||||||
'git', 'clone', 'git://github.com/sstephenson/ruby-build',
|
# Only install ruby-build if the version is specified
|
||||||
'{prefix}rbenv/plugins/ruby-build',
|
if version != 'default':
|
||||||
])
|
repo_cmd_runner.run([
|
||||||
|
'git', 'clone', 'git://github.com/sstephenson/ruby-build',
|
||||||
|
'{prefix}rbenv/plugins/ruby-build',
|
||||||
|
])
|
||||||
|
|
||||||
activate_path = repo_cmd_runner.path('rbenv', 'bin', 'activate')
|
activate_path = repo_cmd_runner.path('rbenv', 'bin', 'activate')
|
||||||
with io.open(activate_path, 'w') as activate_file:
|
with io.open(activate_path, 'w') as activate_file:
|
||||||
|
|
@ -55,13 +52,19 @@ def _install_rbenv(repo_cmd_runner):
|
||||||
'\n'.format(repo_cmd_runner.path('rbenv'))
|
'\n'.format(repo_cmd_runner.path('rbenv'))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# If we aren't using the system ruby, add a version here
|
||||||
|
if version != 'default':
|
||||||
|
activate_file.write('export RBENV_VERSION="{0}"\n'.format(version))
|
||||||
|
|
||||||
|
|
||||||
def install_environment(repo_cmd_runner, version='default'):
|
def install_environment(repo_cmd_runner, version='default'):
|
||||||
with clean_path_on_failure(repo_cmd_runner.path('rbenv')):
|
with clean_path_on_failure(repo_cmd_runner.path('rbenv')):
|
||||||
_install_rbenv(repo_cmd_runner)
|
# TODO: this currently will fail if there's no version specified and
|
||||||
|
# there's no system ruby installed. Is this ok?
|
||||||
|
_install_rbenv(repo_cmd_runner, version=version)
|
||||||
with in_env(repo_cmd_runner) as ruby_env:
|
with in_env(repo_cmd_runner) as ruby_env:
|
||||||
# TODO: hardcoded version smell
|
if version != 'default':
|
||||||
ruby_env.run('rbenv install 1.9.3-p547')
|
ruby_env.run('rbenv install {0}'.format(version))
|
||||||
ruby_env.run(
|
ruby_env.run(
|
||||||
'cd {prefix} && gem build *.gemspec && gem install *.gem',
|
'cd {prefix} && gem build *.gemspec && gem install *.gem',
|
||||||
)
|
)
|
||||||
|
|
|
||||||
1
testing/resources/ruby_1_9_3_p547_hooks_repo/.gitignore
vendored
Normal file
1
testing/resources/ruby_1_9_3_p547_hooks_repo/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
*.gem
|
||||||
5
testing/resources/ruby_1_9_3_p547_hooks_repo/bin/ruby_hook
Executable file
5
testing/resources/ruby_1_9_3_p547_hooks_repo/bin/ruby_hook
Executable file
|
|
@ -0,0 +1,5 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
puts RUBY_VERSION
|
||||||
|
puts RUBY_PATCHLEVEL
|
||||||
|
puts 'Hello world from a ruby hook'
|
||||||
5
testing/resources/ruby_1_9_3_p547_hooks_repo/hooks.yaml
Normal file
5
testing/resources/ruby_1_9_3_p547_hooks_repo/hooks.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
- id: ruby_hook
|
||||||
|
name: Ruby Hook
|
||||||
|
entry: ruby_hook
|
||||||
|
language: ruby
|
||||||
|
language_version: 1.9.3-p547
|
||||||
|
|
@ -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
|
||||||
|
|
@ -92,6 +92,11 @@ def ruby_hooks_repo(dummy_git_repo):
|
||||||
yield _make_repo(dummy_git_repo, 'ruby_hooks_repo')
|
yield _make_repo(dummy_git_repo, 'ruby_hooks_repo')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.yield_fixture
|
||||||
|
def ruby_1_9_3_p547_hooks_repo(dummy_git_repo):
|
||||||
|
yield _make_repo(dummy_git_repo, 'ruby_1_9_3_p547_hooks_repo')
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def consumer_repo(dummy_git_repo):
|
def consumer_repo(dummy_git_repo):
|
||||||
yield _make_repo(dummy_git_repo, 'consumer_repo')
|
yield _make_repo(dummy_git_repo, 'consumer_repo')
|
||||||
|
|
@ -143,6 +148,11 @@ def config_for_ruby_hooks_repo(ruby_hooks_repo):
|
||||||
yield _make_config(ruby_hooks_repo, 'ruby_hook', '\\.rb$')
|
yield _make_config(ruby_hooks_repo, 'ruby_hook', '\\.rb$')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.yield_fixture
|
||||||
|
def config_for_ruby_1_9_3_p547_hooks_repo(ruby_1_9_3_p547_hooks_repo):
|
||||||
|
yield _make_config(ruby_1_9_3_p547_hooks_repo, 'ruby_hook', '\\.rb$')
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def config_for_python_hooks_repo(python_hooks_repo):
|
def config_for_python_hooks_repo(python_hooks_repo):
|
||||||
yield _make_config(python_hooks_repo, 'foo', '\\.py$')
|
yield _make_config(python_hooks_repo, 'foo', '\\.py$')
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,21 @@ def test_install_rbenv(cmd_runner):
|
||||||
activate_path = cmd_runner.path('rbenv', 'bin', 'activate')
|
activate_path = cmd_runner.path('rbenv', 'bin', 'activate')
|
||||||
assert os.path.exists(activate_path)
|
assert os.path.exists(activate_path)
|
||||||
|
|
||||||
# Should be able to activate using our script and access the install method
|
# Should be able to activate using our script and access rbenv
|
||||||
|
cmd_runner.run(
|
||||||
|
[
|
||||||
|
'bash',
|
||||||
|
'-c',
|
||||||
|
'. {prefix}/rbenv/bin/activate && rbenv --help',
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@skipif_slowtests_false
|
||||||
|
def test_install_rbenv_with_version(cmd_runner):
|
||||||
|
_install_rbenv(cmd_runner, version='1.9.3p547')
|
||||||
|
|
||||||
|
# Should be able to activate and use rbenv install
|
||||||
cmd_runner.run(
|
cmd_runner.run(
|
||||||
[
|
[
|
||||||
'bash',
|
'bash',
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,16 @@ def test_run_versioned_node_hook(config_for_node_0_11_8_hooks_repo, store):
|
||||||
assert ret[1] == 'v0.11.8\nHello World\n'
|
assert ret[1] == 'v0.11.8\nHello World\n'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.herpderp
|
||||||
|
@skipif_slowtests_false
|
||||||
|
@pytest.mark.integration
|
||||||
|
def test_run_versioned_ruby_hook(config_for_ruby_1_9_3_p547_hooks_repo, store):
|
||||||
|
repo = Repository.create(config_for_ruby_1_9_3_p547_hooks_repo, store)
|
||||||
|
ret = repo.run_hook('ruby_hook', [])
|
||||||
|
assert ret[0] == 0
|
||||||
|
assert ret[1] == '1.9.3\n547\nHello world from a ruby hook\n'
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.integration
|
@pytest.mark.integration
|
||||||
def test_lots_of_files(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)
|
repo = Repository.create(config_for_python_hooks_repo, store)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue