Skip to content

Commit

Permalink
feat: allow rust hooks to specify crate features
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Terseus committed Jun 15, 2024
1 parent 60db5d7 commit 19f46ae
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
46 changes: 36 additions & 10 deletions pre_commit/languages/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from pre_commit.util import win_exe

ENVIRONMENT_DIR = 'rustenv'
FEATURE_PREFIX = '--feature='
health_check = lang_base.basic_health_check
run_hook = lang_base.basic_run_hook

Expand Down Expand Up @@ -55,7 +56,8 @@ def get_env_patch(target_dir: str, version: str) -> PatchesT:
# toolchain
*(
(('RUSTUP_TOOLCHAIN', _rust_toolchain(version)),)
if version != 'system' else ()
if version != 'system'
else ()
),
)

Expand All @@ -68,11 +70,13 @@ def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:


def _add_dependencies(
prefix: Prefix,
additional_dependencies: set[str],
prefix: Prefix,
additional_dependencies: set[str],
) -> None:
crates = []
for dep in additional_dependencies:
if dep.startswith(FEATURE_PREFIX):
continue
name, _, spec = dep.partition(':')
crate = f'{name}@{spec or "*"}'
crates.append(crate)
Expand Down Expand Up @@ -100,20 +104,27 @@ def install_rust_with_toolchain(toolchain: str, envdir: str) -> None:

# install rustup into `$CARGO_HOME/bin`
cmd_output_b(
rustup_init, '-y', '--quiet', '--no-modify-path',
'--default-toolchain', 'none',
rustup_init,
'-y',
'--quiet',
'--no-modify-path',
'--default-toolchain',
'none',
)

cmd_output_b(
'rustup', 'toolchain', 'install', '--no-self-update',
'rustup',
'toolchain',
'install',
'--no-self-update',
toolchain,
)


def install_environment(
prefix: Prefix,
version: str,
additional_dependencies: Sequence[str],
prefix: Prefix,
version: str,
additional_dependencies: Sequence[str],
) -> None:
envdir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)

Expand All @@ -130,6 +141,12 @@ def install_environment(
cli_deps = {
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(FEATURE_PREFIX)
}
lib_deps = set(additional_dependencies) - cli_deps

packages_to_install: set[tuple[str, ...]] = {('--path', '.')}
Expand All @@ -153,8 +170,17 @@ def install_environment(
if len(lib_deps) > 0:
_add_dependencies(prefix, lib_deps)

features_params = []
if features:
features_params = ['--features', *features]
for args in packages_to_install:
cmd_output_b(
'cargo', 'install', '--bins', '--root', envdir, *args,
'cargo',
'install',
'--bins',
'--root',
envdir,
*args,
*features_params,
cwd=prefix.prefix_dir,
)
17 changes: 16 additions & 1 deletion tests/languages/rust_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@ def _make_hello_world(tmp_path):
src_dir.joinpath('main.rs').write_text(
'fn main() {\n'
' println!("Hello, world!");\n'
' if cfg!(feature = "foo") {\n'
' println!("With feature foo");\n'
' }\n'
'}\n',
)
tmp_path.joinpath('Cargo.toml').write_text(
'[package]\n'
'name = "hello_world"\n'
'version = "0.1.0"\n'
'edition = "2021"\n',
'edition = "2021"\n'
'\n'
'[features]\n'
'foo = []\n',
)


Expand Down Expand Up @@ -113,3 +119,12 @@ def test_run_lib_additional_dependencies(tmp_path):
assert bin_dir.is_dir()
assert not bin_dir.joinpath('shellharden').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')

0 comments on commit 19f46ae

Please sign in to comment.