mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
allow rust hooks to specify arbitrary cargo install parameters
We've replaced the previous "--feature=" format with a single parameter "--" which marks the start of the "cargo install" arguments, so now we can use "-- --features foo", "-- --locked", etc. Fixes #3162
This commit is contained in:
parent
bd153a698a
commit
558793bbc2
2 changed files with 22 additions and 23 deletions
|
|
@ -22,7 +22,6 @@ from pre_commit.util import make_executable
|
||||||
from pre_commit.util import win_exe
|
from pre_commit.util import win_exe
|
||||||
|
|
||||||
ENVIRONMENT_DIR = 'rustenv'
|
ENVIRONMENT_DIR = 'rustenv'
|
||||||
FEATURE_PREFIX = '--feature='
|
|
||||||
health_check = lang_base.basic_health_check
|
health_check = lang_base.basic_health_check
|
||||||
run_hook = lang_base.basic_run_hook
|
run_hook = lang_base.basic_run_hook
|
||||||
|
|
||||||
|
|
@ -74,8 +73,6 @@ def _add_dependencies(
|
||||||
) -> None:
|
) -> None:
|
||||||
crates = []
|
crates = []
|
||||||
for dep in additional_dependencies:
|
for dep in additional_dependencies:
|
||||||
if dep.startswith(FEATURE_PREFIX):
|
|
||||||
continue
|
|
||||||
name, _, spec = dep.partition(':')
|
name, _, spec = dep.partition(':')
|
||||||
crate = f'{name}@{spec or "*"}'
|
crate = f'{name}@{spec or "*"}'
|
||||||
crates.append(crate)
|
crates.append(crate)
|
||||||
|
|
@ -130,19 +127,25 @@ def install_environment(
|
||||||
#
|
#
|
||||||
# Because of this, we allow specifying "cli" dependencies by prefixing
|
# Because of this, we allow specifying "cli" dependencies by prefixing
|
||||||
# with 'cli:'.
|
# with 'cli:'.
|
||||||
cli_deps = {
|
#
|
||||||
dep for dep in additional_dependencies if dep.startswith('cli:')
|
# We can pass arbitrary arguments to the `cargo install` invocation by
|
||||||
}
|
# adding '--' before the arguments.
|
||||||
# Features starts with "--feature="
|
cli_deps = []
|
||||||
features = {
|
lib_deps = []
|
||||||
dep.replace(FEATURE_PREFIX, '')
|
cargo_params = []
|
||||||
for dep in additional_dependencies
|
for index, dep in enumerate(additional_dependencies):
|
||||||
if dep.startswith('--')
|
if dep == '--':
|
||||||
}
|
cargo_params = list(additional_dependencies[index + 1:])
|
||||||
lib_deps = set(additional_dependencies) - cli_deps
|
break
|
||||||
|
if dep.startswith('cli:'):
|
||||||
|
cli_deps.append(dep)
|
||||||
|
continue
|
||||||
|
lib_deps.append(dep)
|
||||||
|
cli_deps_set = set(cli_deps)
|
||||||
|
lib_deps_set = set(lib_deps)
|
||||||
|
|
||||||
packages_to_install: set[tuple[str, ...]] = {('--path', '.')}
|
packages_to_install: set[tuple[str, ...]] = {('--path', '.')}
|
||||||
for cli_dep in cli_deps:
|
for cli_dep in cli_deps_set:
|
||||||
cli_dep = cli_dep.removeprefix('cli:')
|
cli_dep = cli_dep.removeprefix('cli:')
|
||||||
package, _, crate_version = cli_dep.partition(':')
|
package, _, crate_version = cli_dep.partition(':')
|
||||||
if crate_version != '':
|
if crate_version != '':
|
||||||
|
|
@ -159,16 +162,12 @@ def install_environment(
|
||||||
tmpdir = ctx.enter_context(tempfile.TemporaryDirectory())
|
tmpdir = ctx.enter_context(tempfile.TemporaryDirectory())
|
||||||
ctx.enter_context(envcontext((('RUSTUP_HOME', tmpdir),)))
|
ctx.enter_context(envcontext((('RUSTUP_HOME', tmpdir),)))
|
||||||
|
|
||||||
if len(lib_deps) > 0:
|
if len(lib_deps_set) > 0:
|
||||||
_add_dependencies(prefix, lib_deps)
|
_add_dependencies(prefix, lib_deps_set)
|
||||||
|
|
||||||
features_params = []
|
|
||||||
if features:
|
|
||||||
features_params = ['--features', *features]
|
|
||||||
|
|
||||||
for args in packages_to_install:
|
for args in packages_to_install:
|
||||||
cmd_output_b(
|
cmd_output_b(
|
||||||
'cargo', 'install', '--bins', '--root', envdir, *args,
|
'cargo', 'install', '--bins', '--root', envdir, *args,
|
||||||
*features_params,
|
*cargo_params,
|
||||||
cwd=prefix.prefix_dir,
|
cwd=prefix.prefix_dir,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -125,6 +125,6 @@ def test_run_features_additional_dependencies(tmp_path):
|
||||||
_make_hello_world(tmp_path)
|
_make_hello_world(tmp_path)
|
||||||
|
|
||||||
deps = ('shellharden:4.2.0', 'git-version')
|
deps = ('shellharden:4.2.0', 'git-version')
|
||||||
features = ('--feature=foo',)
|
cargo_params = ('--', '--features', 'foo')
|
||||||
ret = run_language(tmp_path, rust, 'hello_world', deps=deps + features)
|
ret = run_language(tmp_path, rust, 'hello_world', deps=deps + cargo_params)
|
||||||
assert ret == (0, b'Hello, world!\nWith feature foo\n')
|
assert ret == (0, b'Hello, world!\nWith feature foo\n')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue