mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-19 17:14:43 +04:00
allow rust hooks to specify crate features
Now the rust hooks can use the parameter `additional_dependencies` to receive build-time features for the crate. The features must start with the string "--feature=" following the name of the feature; e.g. "--feature=full". Fixes #3230
This commit is contained in:
parent
60db5d78d1
commit
bd153a698a
2 changed files with 30 additions and 1 deletions
|
|
@ -22,6 +22,7 @@ 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
|
||||||
|
|
||||||
|
|
@ -73,6 +74,8 @@ 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,6 +133,12 @@ def install_environment(
|
||||||
cli_deps = {
|
cli_deps = {
|
||||||
dep for dep in additional_dependencies if dep.startswith('cli:')
|
dep for dep in additional_dependencies if dep.startswith('cli:')
|
||||||
}
|
}
|
||||||
|
# Features starts with "--feature="
|
||||||
|
features = {
|
||||||
|
dep.replace(FEATURE_PREFIX, '')
|
||||||
|
for dep in additional_dependencies
|
||||||
|
if dep.startswith('--')
|
||||||
|
}
|
||||||
lib_deps = set(additional_dependencies) - cli_deps
|
lib_deps = set(additional_dependencies) - cli_deps
|
||||||
|
|
||||||
packages_to_install: set[tuple[str, ...]] = {('--path', '.')}
|
packages_to_install: set[tuple[str, ...]] = {('--path', '.')}
|
||||||
|
|
@ -153,8 +162,13 @@ def install_environment(
|
||||||
if len(lib_deps) > 0:
|
if len(lib_deps) > 0:
|
||||||
_add_dependencies(prefix, lib_deps)
|
_add_dependencies(prefix, lib_deps)
|
||||||
|
|
||||||
|
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,
|
||||||
cwd=prefix.prefix_dir,
|
cwd=prefix.prefix_dir,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -44,13 +44,19 @@ def _make_hello_world(tmp_path):
|
||||||
src_dir.joinpath('main.rs').write_text(
|
src_dir.joinpath('main.rs').write_text(
|
||||||
'fn main() {\n'
|
'fn main() {\n'
|
||||||
' println!("Hello, world!");\n'
|
' println!("Hello, world!");\n'
|
||||||
|
' if cfg!(feature = "foo") {\n'
|
||||||
|
' println!("With feature foo");\n'
|
||||||
|
' }\n'
|
||||||
'}\n',
|
'}\n',
|
||||||
)
|
)
|
||||||
tmp_path.joinpath('Cargo.toml').write_text(
|
tmp_path.joinpath('Cargo.toml').write_text(
|
||||||
'[package]\n'
|
'[package]\n'
|
||||||
'name = "hello_world"\n'
|
'name = "hello_world"\n'
|
||||||
'version = "0.1.0"\n'
|
'version = "0.1.0"\n'
|
||||||
'edition = "2021"\n',
|
'edition = "2021"\n'
|
||||||
|
'\n'
|
||||||
|
'[features]\n'
|
||||||
|
'foo = []\n',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -113,3 +119,12 @@ def test_run_lib_additional_dependencies(tmp_path):
|
||||||
assert bin_dir.is_dir()
|
assert bin_dir.is_dir()
|
||||||
assert not bin_dir.joinpath('shellharden').exists()
|
assert not bin_dir.joinpath('shellharden').exists()
|
||||||
assert not bin_dir.joinpath('shellharden.exe').exists()
|
assert not bin_dir.joinpath('shellharden.exe').exists()
|
||||||
|
|
||||||
|
|
||||||
|
def test_run_features_additional_dependencies(tmp_path):
|
||||||
|
_make_hello_world(tmp_path)
|
||||||
|
|
||||||
|
deps = ('shellharden:4.2.0', 'git-version')
|
||||||
|
features = ('--feature=foo',)
|
||||||
|
ret = run_language(tmp_path, rust, 'hello_world', deps=deps + features)
|
||||||
|
assert ret == (0, b'Hello, world!\nWith feature foo\n')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue