From b381bb68b72915c1b55cba8cca61d715004275e7 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 3 May 2014 23:24:28 -0700 Subject: [PATCH] Support ruby through a combination of rbenv, ruby-build, and GEM_HOME --- pre_commit/languages/ruby.py | 58 +++++++++++++++---- testing/resources/ruby_hooks_repo/.gitignore | 1 + .../resources/ruby_hooks_repo/bin/ruby_hook | 3 + testing/resources/ruby_hooks_repo/hooks.yaml | 4 ++ .../ruby_hooks_repo/ruby_hook.gemspec | 9 +++ testing/util.py | 8 +++ tests/conftest.py | 16 +++++ tests/languages/ruby_test.py | 23 ++++++++ tests/repository_test.py | 20 ++++--- tests/staged_files_only_test.py | 6 -- 10 files changed, 124 insertions(+), 24 deletions(-) create mode 100644 testing/resources/ruby_hooks_repo/.gitignore create mode 100755 testing/resources/ruby_hooks_repo/bin/ruby_hook create mode 100644 testing/resources/ruby_hooks_repo/hooks.yaml create mode 100644 testing/resources/ruby_hooks_repo/ruby_hook.gemspec create mode 100644 tests/languages/ruby_test.py diff --git a/pre_commit/languages/ruby.py b/pre_commit/languages/ruby.py index 083cd7f7..86a09912 100644 --- a/pre_commit/languages/ruby.py +++ b/pre_commit/languages/ruby.py @@ -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): diff --git a/testing/resources/ruby_hooks_repo/.gitignore b/testing/resources/ruby_hooks_repo/.gitignore new file mode 100644 index 00000000..c111b331 --- /dev/null +++ b/testing/resources/ruby_hooks_repo/.gitignore @@ -0,0 +1 @@ +*.gem diff --git a/testing/resources/ruby_hooks_repo/bin/ruby_hook b/testing/resources/ruby_hooks_repo/bin/ruby_hook new file mode 100755 index 00000000..5a7e5ed2 --- /dev/null +++ b/testing/resources/ruby_hooks_repo/bin/ruby_hook @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby + +puts 'Hello world from a ruby hook' diff --git a/testing/resources/ruby_hooks_repo/hooks.yaml b/testing/resources/ruby_hooks_repo/hooks.yaml new file mode 100644 index 00000000..2496e5e4 --- /dev/null +++ b/testing/resources/ruby_hooks_repo/hooks.yaml @@ -0,0 +1,4 @@ +- id: ruby_hook + name: Ruby Hook + entry: ruby_hook + language: ruby diff --git a/testing/resources/ruby_hooks_repo/ruby_hook.gemspec b/testing/resources/ruby_hooks_repo/ruby_hook.gemspec new file mode 100644 index 00000000..75f4e8f7 --- /dev/null +++ b/testing/resources/ruby_hooks_repo/ruby_hook.gemspec @@ -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 diff --git a/testing/util.py b/testing/util.py index 7af18e71..009b9eb5 100644 --- a/testing/util.py +++ b/testing/util.py @@ -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) diff --git a/tests/conftest.py b/tests/conftest.py index 9d031692..ba6b46d1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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()) diff --git a/tests/languages/ruby_test.py b/tests/languages/ruby_test.py new file mode 100644 index 00000000..72f2e671 --- /dev/null +++ b/tests/languages/ruby_test.py @@ -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', + ], + ) diff --git a/tests/repository_test.py b/tests/repository_test.py index 1a8fa6e4..7f9e9338 100644 --- a/tests/repository_test.py +++ b/tests/repository_test.py @@ -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) diff --git a/tests/staged_files_only_test.py b/tests/staged_files_only_test.py index 726265b8..a70c6447 100644 --- a/tests/staged_files_only_test.py +++ b/tests/staged_files_only_test.py @@ -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