Skip to content

Commit

Permalink
Merge pull request #3201 from pre-commit/rust-default-language-version
Browse files Browse the repository at this point in the history
determine rust default language version independent of rust-toolchain.toml
  • Loading branch information
asottile committed May 11, 2024
2 parents 1602328 + 296f592 commit eeac061
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pre_commit/languages/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_default_version() -> str:
# 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.
if cmd_output_b('cargo', '--version', check=False)[0] == 0:
if cmd_output_b('cargo', '--version', check=False, cwd='/')[0] == 0:
return 'system'
else:
return C.DEFAULT
Expand Down
9 changes: 9 additions & 0 deletions tests/languages/rust_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pre_commit.languages import rust
from pre_commit.store import _make_local_repo
from testing.language_helpers import run_language
from testing.util import cwd

ACTUAL_GET_DEFAULT_VERSION = rust.get_default_version.__wrapped__

Expand All @@ -29,6 +30,14 @@ def test_uses_default_when_rust_is_not_available(cmd_output_b_mck):
assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT


def test_selects_system_even_if_rust_toolchain_toml(tmp_path):
toolchain_toml = '[toolchain]\nchannel = "wtf"\n'
tmp_path.joinpath('rust-toolchain.toml').write_text(toolchain_toml)

with cwd(tmp_path):
assert ACTUAL_GET_DEFAULT_VERSION() == 'system'


def _make_hello_world(tmp_path):
src_dir = tmp_path.joinpath('src')
src_dir.mkdir()
Expand Down

0 comments on commit eeac061

Please sign in to comment.