Skip to content

Commit

Permalink
Change Rust to install environment with cargo add over toml
Browse files Browse the repository at this point in the history
  • Loading branch information
marsha committed Oct 29, 2022
1 parent 5c9e844 commit c6366cb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
19 changes: 6 additions & 13 deletions pre_commit/languages/rust.py
Expand Up @@ -11,8 +11,6 @@
from typing import Generator
from typing import Sequence

import toml

import pre_commit.constants as C
from pre_commit import parse_shebang
from pre_commit.envcontext import envcontext
Expand Down Expand Up @@ -82,18 +80,13 @@ def in_env(


def _add_dependencies(
cargo_toml_path: str,
prefix: Prefix,
additional_dependencies: set[str],
) -> None:
with open(cargo_toml_path, 'r+') as f:
cargo_toml = toml.load(f)
cargo_toml.setdefault('dependencies', {})
for dep in additional_dependencies:
name, _, spec = dep.partition(':')
cargo_toml['dependencies'][name] = spec or '*'
f.seek(0)
toml.dump(cargo_toml, f)
f.truncate()
for dep in additional_dependencies:
name, _, spec = dep.partition(':')
crate = name + (f'@{spec}' if spec else '')
helpers.run_setup_cmd(prefix, ('cargo', 'add', crate))


def install_rust_with_toolchain(toolchain: str) -> None:
Expand Down Expand Up @@ -152,7 +145,7 @@ def install_environment(
lib_deps = set(additional_dependencies) - cli_deps

if len(lib_deps) > 0:
_add_dependencies(prefix.path('Cargo.toml'), lib_deps)
_add_dependencies(prefix, lib_deps)

with clean_path_on_failure(directory):
packages_to_install: set[tuple[str, ...]] = {('--path', '.')}
Expand Down
2 changes: 1 addition & 1 deletion tests/repository_test.py
Expand Up @@ -485,7 +485,7 @@ def test_additional_rust_lib_dependencies_installed(
path = make_repo(tempdir_factory, 'rust_hooks_repo')
config = make_config_from_repo(path)
# A small rust package with no dependencies.
deps = ['shellharden:3.1.0']
deps = ['shellharden:3.1.0', 'git-version']
config['hooks'][0]['additional_dependencies'] = deps
hook = _get_hook(config, store, 'rust-hook')
binaries = os.listdir(
Expand Down

0 comments on commit c6366cb

Please sign in to comment.