Skip to content

Commit e1e6a32

Browse files
committed
skip rbenv if ruby and gem are installed with default language_version
1 parent 0e5eb19 commit e1e6a32

File tree

5 files changed

+78
-39
lines changed

5 files changed

+78
-39
lines changed

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ jobs:
1919
toxenvs: [py37]
2020
os: windows
2121
pre_test:
22+
- task: UseRubyVersion@0
2223
- powershell: Write-Host "##vso[task.prependpath]$env:CONDA\Scripts"
2324
displayName: Add conda to PATH
2425
- powershell: |

pre_commit/languages/ruby.py

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import contextlib
2+
import functools
23
import os.path
34
import shutil
45
import tarfile
@@ -7,6 +8,7 @@
78
from typing import Tuple
89

910
import pre_commit.constants as C
11+
from pre_commit import parse_shebang
1012
from pre_commit.envcontext import envcontext
1113
from pre_commit.envcontext import PatchesT
1214
from pre_commit.envcontext import UNSET
@@ -19,33 +21,51 @@
1921
from pre_commit.util import resource_bytesio
2022

2123
ENVIRONMENT_DIR = 'rbenv'
22-
get_default_version = helpers.basic_get_default_version
2324
healthy = helpers.basic_healthy
2425

2526

27+
@functools.lru_cache(maxsize=1)
28+
def get_default_version() -> str:
29+
if all(parse_shebang.find_executable(exe) for exe in ('ruby', 'gem')):
30+
return 'system'
31+
else:
32+
return C.DEFAULT
33+
34+
2635
def get_env_patch(
2736
venv: str,
2837
language_version: str,
29-
) -> PatchesT: # pragma: win32 no cover
38+
) -> PatchesT:
3039
patches: PatchesT = (
3140
('GEM_HOME', os.path.join(venv, 'gems')),
3241
('GEM_PATH', UNSET),
33-
('RBENV_ROOT', venv),
3442
('BUNDLE_IGNORE_CONFIG', '1'),
35-
(
36-
'PATH', (
37-
os.path.join(venv, 'gems', 'bin'), os.pathsep,
38-
os.path.join(venv, 'shims'), os.pathsep,
39-
os.path.join(venv, 'bin'), os.pathsep, Var('PATH'),
40-
),
41-
),
4243
)
43-
if language_version != C.DEFAULT:
44-
patches += (('RBENV_VERSION', language_version),)
44+
if language_version == 'system':
45+
patches += (
46+
(
47+
'PATH', (
48+
os.path.join(venv, 'gems', 'bin'), os.pathsep,
49+
Var('PATH'),
50+
),
51+
),
52+
)
53+
else: # pragma: win32 no cover
54+
patches += (
55+
('RBENV_ROOT', venv),
56+
('RBENV_VERSION', language_version),
57+
(
58+
'PATH', (
59+
os.path.join(venv, 'gems', 'bin'), os.pathsep,
60+
os.path.join(venv, 'shims'), os.pathsep,
61+
os.path.join(venv, 'bin'), os.pathsep, Var('PATH'),
62+
),
63+
),
64+
)
4565
return patches
4666

4767

48-
@contextlib.contextmanager # pragma: win32 no cover
68+
@contextlib.contextmanager
4969
def in_env(
5070
prefix: Prefix,
5171
language_version: str,
@@ -65,7 +85,7 @@ def _extract_resource(filename: str, dest: str) -> None:
6585

6686
def _install_rbenv(
6787
prefix: Prefix,
68-
version: str = C.DEFAULT,
88+
version: str,
6989
) -> None: # pragma: win32 no cover
7090
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
7191

@@ -92,21 +112,22 @@ def _install_ruby(
92112

93113
def install_environment(
94114
prefix: Prefix, version: str, additional_dependencies: Sequence[str],
95-
) -> None: # pragma: win32 no cover
115+
) -> None:
96116
additional_dependencies = tuple(additional_dependencies)
97117
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
98118
with clean_path_on_failure(prefix.path(directory)):
99-
# TODO: this currently will fail if there's no version specified and
100-
# there's no system ruby installed. Is this ok?
101-
_install_rbenv(prefix, version=version)
102-
with in_env(prefix, version):
103-
# Need to call this before installing so rbenv's directories are
104-
# set up
105-
helpers.run_setup_cmd(prefix, ('rbenv', 'init', '-'))
106-
if version != C.DEFAULT:
119+
if version != 'system': # pragma: win32 no cover
120+
_install_rbenv(prefix, version)
121+
with in_env(prefix, version):
122+
# Need to call this before installing so rbenv's directories
123+
# are set up
124+
helpers.run_setup_cmd(prefix, ('rbenv', 'init', '-'))
125+
# XXX: this will *always* fail if `version == C.DEFAULT`
107126
_install_ruby(prefix, version)
108-
# Need to call this after installing to set up the shims
109-
helpers.run_setup_cmd(prefix, ('rbenv', 'rehash'))
127+
# Need to call this after installing to set up the shims
128+
helpers.run_setup_cmd(prefix, ('rbenv', 'rehash'))
129+
130+
with in_env(prefix, version):
110131
helpers.run_setup_cmd(
111132
prefix, ('gem', 'build', *prefix.star('.gemspec')),
112133
)
@@ -123,6 +144,6 @@ def run_hook(
123144
hook: Hook,
124145
file_args: Sequence[str],
125146
color: bool,
126-
) -> Tuple[int, bytes]: # pragma: win32 no cover
147+
) -> Tuple[int, bytes]:
127148
with in_env(hook.prefix, hook.language_version):
128149
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

testing/util.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ def cmd_output_mocked_pre_commit_home(
3838
parse_shebang.find_executable('swift') is None,
3939
reason="swift isn't installed or can't be found",
4040
)
41-
xfailif_windows_no_ruby = pytest.mark.xfail(
42-
os.name == 'nt',
43-
reason='Ruby support not yet implemented on windows.',
44-
)
4541
xfailif_windows = pytest.mark.xfail(os.name == 'nt', reason='windows')
4642

4743

tests/languages/ruby_test.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
11
import os.path
2+
from unittest import mock
23

4+
import pytest
5+
6+
import pre_commit.constants as C
7+
from pre_commit import parse_shebang
38
from pre_commit.languages import ruby
49
from pre_commit.prefix import Prefix
510
from pre_commit.util import cmd_output
6-
from testing.util import xfailif_windows_no_ruby
11+
from testing.util import xfailif_windows
12+
13+
14+
ACTUAL_GET_DEFAULT_VERSION = ruby.get_default_version.__wrapped__
15+
16+
17+
@pytest.fixture
18+
def find_exe_mck():
19+
with mock.patch.object(parse_shebang, 'find_executable') as mck:
20+
yield mck
21+
22+
23+
def test_uses_default_version_when_not_available(find_exe_mck):
24+
find_exe_mck.return_value = None
25+
assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT
26+
27+
28+
def test_uses_system_if_both_gem_and_ruby_are_available(find_exe_mck):
29+
find_exe_mck.return_value = '/path/to/exe'
30+
assert ACTUAL_GET_DEFAULT_VERSION() == 'system'
731

832

9-
@xfailif_windows_no_ruby
33+
@xfailif_windows # pragma: win32 no cover
1034
def test_install_rbenv(tempdir_factory):
1135
prefix = Prefix(tempdir_factory.get())
12-
ruby._install_rbenv(prefix)
36+
ruby._install_rbenv(prefix, C.DEFAULT)
1337
# Should have created rbenv directory
1438
assert os.path.exists(prefix.path('rbenv-default'))
1539

@@ -18,7 +42,7 @@ def test_install_rbenv(tempdir_factory):
1842
cmd_output('rbenv', '--help')
1943

2044

21-
@xfailif_windows_no_ruby
45+
@xfailif_windows # pragma: win32 no cover
2246
def test_install_rbenv_with_version(tempdir_factory):
2347
prefix = Prefix(tempdir_factory.get())
2448
ruby._install_rbenv(prefix, version='1.9.3p547')

tests/repository_test.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
from testing.util import skipif_cant_run_docker
3535
from testing.util import skipif_cant_run_swift
3636
from testing.util import xfailif_windows
37-
from testing.util import xfailif_windows_no_ruby
3837

3938

4039
def _norm_out(b):
@@ -260,15 +259,14 @@ def test_run_versioned_node_hook(tempdir_factory, store):
260259
)
261260

262261

263-
@xfailif_windows_no_ruby
264262
def test_run_a_ruby_hook(tempdir_factory, store):
265263
_test_hook_repo(
266264
tempdir_factory, store, 'ruby_hooks_repo',
267265
'ruby_hook', [os.devnull], b'Hello world from a ruby hook\n',
268266
)
269267

270268

271-
@xfailif_windows_no_ruby
269+
@xfailif_windows # pragma: win32 no cover
272270
def test_run_versioned_ruby_hook(tempdir_factory, store):
273271
_test_hook_repo(
274272
tempdir_factory, store, 'ruby_versioned_hooks_repo',
@@ -278,7 +276,7 @@ def test_run_versioned_ruby_hook(tempdir_factory, store):
278276
)
279277

280278

281-
@xfailif_windows_no_ruby
279+
@xfailif_windows # pragma: win32 no cover
282280
def test_run_ruby_hook_with_disable_shared_gems(
283281
tempdir_factory,
284282
store,
@@ -524,7 +522,6 @@ def test_additional_dependencies_roll_forward(tempdir_factory, store):
524522
assert 'mccabe' not in cmd_output('pip', 'freeze', '-l')[1]
525523

526524

527-
@xfailif_windows_no_ruby # pragma: win32 no cover
528525
def test_additional_ruby_dependencies_installed(tempdir_factory, store):
529526
path = make_repo(tempdir_factory, 'ruby_hooks_repo')
530527
config = make_config_from_repo(path)

0 commit comments

Comments
 (0)