FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat: allow rust hooks to specify crate features by Terseus · Pull Request #3235 · pre-commit/pre-commit · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
27 changes: 20 additions & 7 deletions pre_commit/languages/rust.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,25 @@ def install_environment(
#
# Because of this, we allow specifying "cli" dependencies by prefixing
# with 'cli:'.
cli_deps = {
dep for dep in additional_dependencies if dep.startswith('cli:')
}
lib_deps = set(additional_dependencies) - cli_deps
#
# We can pass arbitrary arguments to the `cargo install` invocation by
# adding '--' before the arguments.
cli_deps = []
lib_deps = []
cargo_params = []
for index, dep in enumerate(additional_dependencies):
if dep == '--':
cargo_params = list(additional_dependencies[index + 1:])
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', '.')}
for cli_dep in cli_deps:
for cli_dep in cli_deps_set:
cli_dep = cli_dep.removeprefix('cli:')
package, _, crate_version = cli_dep.partition(':')
if crate_version != '':
Expand All @@ -150,11 +162,12 @@ def install_environment(
tmpdir = ctx.enter_context(tempfile.TemporaryDirectory())
ctx.enter_context(envcontext((('RUSTUP_HOME', tmpdir),)))

if len(lib_deps) > 0:
_add_dependencies(prefix, lib_deps)
if len(lib_deps_set) > 0:
_add_dependencies(prefix, lib_deps_set)

for args in packages_to_install:
cmd_output_b(
'cargo', 'install', '--bins', '--root', envdir, *args,
*cargo_params,
cwd=prefix.prefix_dir,
)
17 changes: 16 additions & 1 deletion tests/languages/rust_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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')
cargo_params = ('--', '--features', 'foo')
ret = run_language(tmp_path, rust, 'hello_world', deps=deps + cargo_params)
assert ret == (0, b'Hello, world!\nWith feature foo\n')

Back | FazBrowse Home | New Git URL