Skip to content

Commit 77b4ea3

Browse files
authored
Merge pull request pre-commit#2729 from pre-commit/rust-tests
test rust directly
2 parents 6abb05a + 2adca78 commit 77b4ea3

File tree

7 files changed

+59
-126
lines changed

7 files changed

+59
-126
lines changed

testing/language_helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import os
44
from typing import Sequence
55

6-
import pre_commit.constants as C
76
from pre_commit.languages.all import Language
87
from pre_commit.prefix import Prefix
98

@@ -14,11 +13,12 @@ def run_language(
1413
exe: str,
1514
args: Sequence[str] = (),
1615
file_args: Sequence[str] = (),
17-
version: str = C.DEFAULT,
16+
version: str | None = None,
1817
deps: Sequence[str] = (),
1918
is_local: bool = False,
2019
) -> tuple[int, bytes]:
2120
prefix = Prefix(str(path))
21+
version = version or language.get_default_version()
2222

2323
language.install_environment(prefix, version, deps)
2424
with language.in_env(prefix, version):

testing/resources/rust_hooks_repo/.pre-commit-hooks.yaml

Lines changed: 0 additions & 5 deletions
This file was deleted.

testing/resources/rust_hooks_repo/Cargo.lock

Lines changed: 0 additions & 3 deletions
This file was deleted.

testing/resources/rust_hooks_repo/Cargo.toml

Lines changed: 0 additions & 3 deletions
This file was deleted.

testing/resources/rust_hooks_repo/src/main.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/languages/rust_test.py

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
from __future__ import annotations
22

3-
from typing import Mapping
43
from unittest import mock
54

65
import pytest
76

87
import pre_commit.constants as C
98
from pre_commit import parse_shebang
109
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
1312

1413
ACTUAL_GET_DEFAULT_VERSION = rust.get_default_version.__wrapped__
1514

@@ -30,64 +29,78 @@ def test_uses_default_when_rust_is_not_available(cmd_output_b_mck):
3029
assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT
3130

3231

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(
3636
'fn main() {\n'
3737
' println!("Hello, world!");\n'
3838
'}\n',
3939
)
40-
tmpdir.join('Cargo.toml').ensure().write(
40+
tmp_path.joinpath('Cargo.toml').write_text(
4141
'[package]\n'
4242
'name = "hello_world"\n'
4343
'version = "0.1.0"\n'
4444
'edition = "2021"\n',
4545
)
46-
prefix = Prefix(str(tmpdir))
4746

48-
find_executable_exes = []
4947

50-
original_find_executable = parse_shebang.find_executable
48+
def test_installs_rust_missing_rustup(tmp_path):
49+
_make_hello_world(tmp_path)
5150

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
5954

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'
6459
return None
65-
return original_find_executable(exe, env=env)
60+
return orig(exe, env=env)
6661

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')
7166

72-
with rust.in_env(prefix, language_version):
73-
assert cmd_output('hello_world')[1] == 'Hello, world!\n'
7467

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
7571

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),),
8792
)
88-
prefix = Prefix(str(tmpdir))
93+
assert ret == (0, b'echo "$hi"\n')
8994

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()

tests/repository_test.py

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from pre_commit.languages import node
2121
from pre_commit.languages import python
2222
from pre_commit.languages import ruby
23-
from pre_commit.languages import rust
2423
from pre_commit.languages.all import languages
2524
from pre_commit.prefix import Prefix
2625
from pre_commit.repository import _hook_installed
@@ -367,54 +366,6 @@ def test_golang_with_recursive_submodule(tmpdir, tempdir_factory, store):
367366
assert _norm_out(out) == b'hello hello world\n'
368367

369368

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-
418369
def test_missing_executable(tempdir_factory, store):
419370
_test_hook_repo(
420371
tempdir_factory, store, 'not_found_exe',
@@ -637,23 +588,6 @@ def test_local_golang_additional_dependencies(store):
637588
assert _norm_out(out) == b'Hello, Go examples!\n'
638589

639590

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-
657591
def test_fail_hooks(store):
658592
config = {
659593
'repo': 'local',

0 commit comments

Comments
 (0)