Skip to content

Commit

Permalink
rust: Fix get_default_version() on system install without toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
Holzhaus committed Oct 4, 2022
1 parent 6791f2c commit 85e147c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
14 changes: 9 additions & 5 deletions pre_commit/languages/rust.py
Expand Up @@ -36,12 +36,16 @@

@functools.lru_cache(maxsize=1)
def get_default_version() -> str:
# if rust is already installed, we can save a bunch of setup time by
# using the installed version
if helpers.exe_exists('cargo'):
# If rust is already installed, we can save a bunch of setup time by
# using the installed version.
#
# Just detecting the executable does not suffice, because if rustup is
# installed but no toolchain is available, then `cargo` exists but
# cannot be used without installing a toolchain first.
retcode, _, _ = cmd_output_b('cargo', '--version', retcode=0)
if retcode == 0:
return 'system'
else:
return C.DEFAULT
return C.DEFAULT


def _envdir(prefix: Prefix, version: str) -> str:
Expand Down
12 changes: 6 additions & 6 deletions tests/languages/rust_test.py
Expand Up @@ -18,18 +18,18 @@


@pytest.fixture
def find_exe_mck():
with mock.patch.object(parse_shebang, 'find_executable') as mck:
def cmd_output_b_mck():
with mock.patch.object(rust, 'cmd_output_b') as mck:
yield mck


def test_sets_system_when_rust_is_available(find_exe_mck):
find_exe_mck.return_value = '/path/to/exe'
def test_sets_system_when_rust_is_available(cmd_output_b_mck):
cmd_output_b_mck.return_value = (0, b'', b'')
assert ACTUAL_GET_DEFAULT_VERSION() == 'system'


def test_uses_default_when_rust_is_not_available(find_exe_mck):
find_exe_mck.return_value = None
def test_uses_default_when_rust_is_not_available(cmd_output_b_mck):
cmd_output_b_mck.return_value = (127, b'', b'error: not found')
assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT


Expand Down

0 comments on commit 85e147c

Please sign in to comment.