| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,6 @@ | |||
| 3 | 3 | import os | |
| 4 | 4 | from typing import Sequence | |
| 5 | 5 | ||
| 6 | - import pre_commit.constants as C | ||
| 7 | 6 | from pre_commit.languages.all import Language | |
| 8 | 7 | from pre_commit.prefix import Prefix | |
| 9 | 8 | ||
@@ -14,11 +13,12 @@ def run_language( | |||
| 14 | 13 | exe: str, | |
| 15 | 14 | args: Sequence[str] = (), | |
| 16 | 15 | file_args: Sequence[str] = (), | |
| 17 | - version: str = C.DEFAULT, | ||
| 16 | + version: str | None = None, | ||
| 18 | 17 | deps: Sequence[str] = (), | |
| 19 | 18 | is_local: bool = False, | |
| 20 | 19 | ) -> tuple[int, bytes]: | |
| 21 | 20 | prefix = Prefix(str(path)) | |
| 21 | + version = version or language.get_default_version() | ||
| 22 | 22 | ||
| 23 | 23 | language.install_environment(prefix, version, deps) | |
| 24 | 24 | with language.in_env(prefix, version): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,15 +1,14 @@ | |||
| 1 | 1 | from __future__ import annotations | |
| 2 | 2 | ||
| 3 | - from typing import Mapping | ||
| 4 | 3 | from unittest import mock | |
| 5 | 4 | ||
| 6 | 5 | import pytest | |
| 7 | 6 | ||
| 8 | 7 | import pre_commit.constants as C | |
| 9 | 8 | from pre_commit import parse_shebang | |
| 10 | 9 | from pre_commit.languages import rust | |
| 11 | - from pre_commit.prefix import Prefix | ||
| 12 | - from pre_commit.util import cmd_output | ||
| 10 | + from pre_commit.store import _make_local_repo | ||
| 11 | + from testing.language_helpers import run_language | ||
| 13 | 12 | ||
| 14 | 13 | ACTUAL_GET_DEFAULT_VERSION = rust.get_default_version.__wrapped__ | |
| 15 | 14 | ||
@@ -30,64 +29,78 @@ def test_uses_default_when_rust_is_not_available(cmd_output_b_mck): | |||
| 30 | 29 | assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT | |
| 31 | 30 | ||
| 32 | 31 | ||
| 33 | - @pytest.mark.parametrize('language_version', (C.DEFAULT, '1.56.0')) | ||
| 34 | - def test_installs_with_bootstrapped_rustup(tmpdir, language_version): | ||
| 35 | - tmpdir.join('src', 'main.rs').ensure().write( | ||
| 32 | + def _make_hello_world(tmp_path): | ||
| 33 | + src_dir = tmp_path.joinpath('src') | ||
| 34 | + src_dir.mkdir() | ||
| 35 | + src_dir.joinpath('main.rs').write_text( | ||
| 36 | 36 | 'fn main() {\n' | |
| 37 | 37 | ' println!("Hello, world!");\n' | |
| 38 | 38 | '}\n', | |
| 39 | 39 | ) | |
| 40 | - tmpdir.join('Cargo.toml').ensure().write( | ||
| 40 | + tmp_path.joinpath('Cargo.toml').write_text( | ||
| 41 | 41 | '[package]\n' | |
| 42 | 42 | 'name = "hello_world"\n' | |
| 43 | 43 | 'version = "0.1.0"\n' | |
| 44 | 44 | 'edition = "2021"\n', | |
| 45 | 45 | ) | |
| 46 | - prefix = Prefix(str(tmpdir)) | ||
| 47 | 46 | ||
| 48 | - find_executable_exes = [] | ||
| 49 | 47 | ||
| 50 | - original_find_executable = parse_shebang.find_executable | ||
| 48 | + def test_installs_rust_missing_rustup(tmp_path): | ||
| 49 | + _make_hello_world(tmp_path) | ||
| 51 | 50 | ||
| 52 | - def mocked_find_executable( | ||
| 53 | - exe: str, *, env: Mapping[str, str] | None = None, | ||
| 54 | - ) -> str | None: | ||
| 55 | - """ | ||
| 56 | - Return `None` the first time `find_executable` is called to ensure | ||
| 57 | - that the bootstrapping code is executed, then just let the function | ||
| 58 | - work as normal. | ||
| 51 | + # pretend like `rustup` doesn't exist so it gets bootstrapped | ||
| 52 | + calls = [] | ||
| 53 | + orig = parse_shebang.find_executable | ||
| 59 | 54 | ||
| 60 | - Also log the arguments to ensure that everything works as expected. | ||
| 61 | - """ | ||
| 62 | - find_executable_exes.append(exe) | ||
| 63 | - if len(find_executable_exes) == 1: | ||
| 55 | + def mck(exe, env=None): | ||
| 56 | + calls.append(exe) | ||
| 57 | + if len(calls) == 1: | ||
| 58 | + assert exe == 'rustup' | ||
| 64 | 59 | return None | |
| 65 | - return original_find_executable(exe, env=env) | ||
| 60 | + return orig(exe, env=env) | ||
| 66 | 61 | ||
| 67 | - with mock.patch.object(parse_shebang, 'find_executable') as find_exe_mck: | ||
| 68 | - find_exe_mck.side_effect = mocked_find_executable | ||
| 69 | - rust.install_environment(prefix, language_version, ()) | ||
| 70 | - assert find_executable_exes == ['rustup', 'rustup', 'cargo'] | ||
| 62 | + with mock.patch.object(parse_shebang, 'find_executable', side_effect=mck): | ||
| 63 | + ret = run_language(tmp_path, rust, 'hello_world', version='1.56.0') | ||
| 64 | + assert calls == ['rustup', 'rustup', 'cargo', 'hello_world'] | ||
| 65 | + assert ret == (0, b'Hello, world!\n') | ||
| 71 | 66 | ||
| 72 | - with rust.in_env(prefix, language_version): | ||
| 73 | - assert cmd_output('hello_world')[1] == 'Hello, world!\n' | ||
| 74 | 67 | ||
| 68 | + @pytest.mark.parametrize('version', (C.DEFAULT, '1.56.0')) | ||
| 69 | + def test_language_version_with_rustup(tmp_path, version): | ||
| 70 | + assert parse_shebang.find_executable('rustup') is not None | ||
| 75 | 71 | ||
| 76 | - def test_installs_with_existing_rustup(tmpdir): | ||
| 77 | - tmpdir.join('src', 'main.rs').ensure().write( | ||
| 78 | - 'fn main() {\n' | ||
| 79 | - ' println!("Hello, world!");\n' | ||
| 80 | - '}\n', | ||
| 81 | - ) | ||
| 82 | - tmpdir.join('Cargo.toml').ensure().write( | ||
| 83 | - '[package]\n' | ||
| 84 | - 'name = "hello_world"\n' | ||
| 85 | - 'version = "0.1.0"\n' | ||
| 86 | - 'edition = "2021"\n', | ||
| 72 | + _make_hello_world(tmp_path) | ||
| 73 | + | ||
| 74 | + ret = run_language(tmp_path, rust, 'hello_world', version=version) | ||
| 75 | + assert ret == (0, b'Hello, world!\n') | ||
| 76 | + | ||
| 77 | + | ||
| 78 | + @pytest.mark.parametrize('dep', ('cli:shellharden:4.2.0', 'cli:shellharden')) | ||
| 79 | + def test_rust_cli_additional_dependencies(tmp_path, dep): | ||
| 80 | + _make_local_repo(str(tmp_path)) | ||
| 81 | + | ||
| 82 | + t_sh = tmp_path.joinpath('t.sh') | ||
| 83 | + t_sh.write_text('echo $hi\n') | ||
| 84 | + | ||
| 85 | + assert rust.get_default_version() == 'system' | ||
| 86 | + ret = run_language( | ||
| 87 | + tmp_path, | ||
| 88 | + rust, | ||
| 89 | + 'shellharden --transform', | ||
| 90 | + deps=(dep,), | ||
| 91 | + args=(str(t_sh),), | ||
| 87 | 92 | ) | |
| 88 | - prefix = Prefix(str(tmpdir)) | ||
| 93 | + assert ret == (0, b'echo "$hi"\n') | ||
| 89 | 94 | ||
| 90 | - assert parse_shebang.find_executable('rustup') is not None | ||
| 91 | - rust.install_environment(prefix, '1.56.0', ()) | ||
| 92 | - with rust.in_env(prefix, '1.56.0'): | ||
| 93 | - assert cmd_output('hello_world')[1] == 'Hello, world!\n' | ||
| 95 | + | ||
| 96 | + def test_run_lib_additional_dependencies(tmp_path): | ||
| 97 | + _make_hello_world(tmp_path) | ||
| 98 | + | ||
| 99 | + deps = ('shellharden:4.2.0', 'git-version') | ||
| 100 | + ret = run_language(tmp_path, rust, 'hello_world', deps=deps) | ||
| 101 | + assert ret == (0, b'Hello, world!\n') | ||
| 102 | + | ||
| 103 | + bin_dir = tmp_path.joinpath('rustenv-system', 'bin') | ||
| 104 | + assert bin_dir.is_dir() | ||
| 105 | + assert not bin_dir.joinpath('shellharden').exists() | ||
| 106 | + assert not bin_dir.joinpath('shellharden.exe').exists() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,7 +20,6 @@ | |||
| 20 | 20 | from pre_commit.languages import node | |
| 21 | 21 | from pre_commit.languages import python | |
| 22 | 22 | from pre_commit.languages import ruby | |
| 23 | - from pre_commit.languages import rust | ||
| 24 | 23 | from pre_commit.languages.all import languages | |
| 25 | 24 | from pre_commit.prefix import Prefix | |
| 26 | 25 | from pre_commit.repository import _hook_installed | |
@@ -367,54 +366,6 @@ def test_golang_with_recursive_submodule(tmpdir, tempdir_factory, store): | |||
| 367 | 366 | assert _norm_out(out) == b'hello hello world\n' | |
| 368 | 367 | ||
| 369 | 368 | ||
| 370 | - def test_rust_hook(tempdir_factory, store): | ||
| 371 | - _test_hook_repo( | ||
| 372 | - tempdir_factory, store, 'rust_hooks_repo', | ||
| 373 | - 'rust-hook', [], b'hello world\n', | ||
| 374 | - ) | ||
| 375 | - | ||
| 376 | - | ||
| 377 | - @pytest.mark.parametrize('dep', ('cli:shellharden:3.1.0', 'cli:shellharden')) | ||
| 378 | - def test_additional_rust_cli_dependencies_installed( | ||
| 379 | - tempdir_factory, store, dep, | ||
| 380 | - ): | ||
| 381 | - path = make_repo(tempdir_factory, 'rust_hooks_repo') | ||
| 382 | - config = make_config_from_repo(path) | ||
| 383 | - # A small rust package with no dependencies. | ||
| 384 | - config['hooks'][0]['additional_dependencies'] = [dep] | ||
| 385 | - hook = _get_hook(config, store, 'rust-hook') | ||
| 386 | - envdir = helpers.environment_dir( | ||
| 387 | - hook.prefix, | ||
| 388 | - rust.ENVIRONMENT_DIR, | ||
| 389 | - 'system', | ||
| 390 | - ) | ||
| 391 | - binaries = os.listdir(os.path.join(envdir, 'bin')) | ||
| 392 | - # normalize for windows | ||
| 393 | - binaries = [os.path.splitext(binary)[0] for binary in binaries] | ||
| 394 | - assert 'shellharden' in binaries | ||
| 395 | - | ||
| 396 | - | ||
| 397 | - def test_additional_rust_lib_dependencies_installed( | ||
| 398 | - tempdir_factory, store, | ||
| 399 | - ): | ||
| 400 | - path = make_repo(tempdir_factory, 'rust_hooks_repo') | ||
| 401 | - config = make_config_from_repo(path) | ||
| 402 | - # A small rust package with no dependencies. | ||
| 403 | - deps = ['shellharden:3.1.0', 'git-version'] | ||
| 404 | - config['hooks'][0]['additional_dependencies'] = deps | ||
| 405 | - hook = _get_hook(config, store, 'rust-hook') | ||
| 406 | - envdir = helpers.environment_dir( | ||
| 407 | - hook.prefix, | ||
| 408 | - rust.ENVIRONMENT_DIR, | ||
| 409 | - 'system', | ||
| 410 | - ) | ||
| 411 | - binaries = os.listdir(os.path.join(envdir, 'bin')) | ||
| 412 | - # normalize for windows | ||
| 413 | - binaries = [os.path.splitext(binary)[0] for binary in binaries] | ||
| 414 | - assert 'rust-hello-world' in binaries | ||
| 415 | - assert 'shellharden' not in binaries | ||
| 416 | - | ||
| 417 | - | ||
| 418 | 369 | def test_missing_executable(tempdir_factory, store): | |
| 419 | 370 | _test_hook_repo( | |
| 420 | 371 | tempdir_factory, store, 'not_found_exe', | |
@@ -637,23 +588,6 @@ def test_local_golang_additional_dependencies(store): | |||
| 637 | 588 | assert _norm_out(out) == b'Hello, Go examples!\n' | |
| 638 | 589 | ||
| 639 | 590 | ||
| 640 | - def test_local_rust_additional_dependencies(store): | ||
| 641 | - config = { | ||
| 642 | - 'repo': 'local', | ||
| 643 | - 'hooks': [{ | ||
| 644 | - 'id': 'hello', | ||
| 645 | - 'name': 'hello', | ||
| 646 | - 'entry': 'hello', | ||
| 647 | - 'language': 'rust', | ||
| 648 | - 'additional_dependencies': ['cli:hello-cli:0.2.2'], | ||
| 649 | - }], | ||
| 650 | - } | ||
| 651 | - hook = _get_hook(config, store, 'hello') | ||
| 652 | - ret, out = _hook_run(hook, (), color=False) | ||
| 653 | - assert ret == 0 | ||
| 654 | - assert _norm_out(out) == b'Hello World!\n' | ||
| 655 | - | ||
| 656 | - | ||
| 657 | 591 | def test_fail_hooks(store): | |
| 658 | 592 | config = { | |
| 659 | 593 | 'repo': 'local', | |
| Back | FazBrowse Home | New Git URL |
0 commit comments