Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions testing/language_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
from typing import Sequence

import pre_commit.constants as C
from pre_commit.languages.all import Language
from pre_commit.prefix import Prefix

Expand All @@ -14,10 +13,11 @@ def run_language(
exe: str,
args: Sequence[str] = (),
file_args: Sequence[str] = (),
version: str = C.DEFAULT,
version: str | None = None,
deps: Sequence[str] = (),
) -> tuple[int, bytes]:
prefix = Prefix(str(path))
version = version or language.get_default_version()

language.install_environment(prefix, version, deps)
with language.in_env(prefix, version):
Expand Down
5 changes: 0 additions & 5 deletions testing/resources/rust_hooks_repo/.pre-commit-hooks.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions testing/resources/rust_hooks_repo/Cargo.lock

This file was deleted.

3 changes: 0 additions & 3 deletions testing/resources/rust_hooks_repo/Cargo.toml

This file was deleted.

3 changes: 0 additions & 3 deletions testing/resources/rust_hooks_repo/src/main.rs

This file was deleted.

101 changes: 57 additions & 44 deletions tests/languages/rust_test.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from __future__ import annotations

from typing import Mapping
from unittest import mock

import pytest

import pre_commit.constants as C
from pre_commit import parse_shebang
from pre_commit.languages import rust
from pre_commit.prefix import Prefix
from pre_commit.util import cmd_output
from pre_commit.store import _make_local_repo
from testing.language_helpers import run_language

ACTUAL_GET_DEFAULT_VERSION = rust.get_default_version.__wrapped__

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


@pytest.mark.parametrize('language_version', (C.DEFAULT, '1.56.0'))
def test_installs_with_bootstrapped_rustup(tmpdir, language_version):
tmpdir.join('src', 'main.rs').ensure().write(
def _make_hello_world(tmp_path):
src_dir = tmp_path.joinpath('src')
src_dir.mkdir()
src_dir.joinpath('main.rs').write_text(
'fn main() {\n'
' println!("Hello, world!");\n'
'}\n',
)
tmpdir.join('Cargo.toml').ensure().write(
tmp_path.joinpath('Cargo.toml').write_text(
'[package]\n'
'name = "hello_world"\n'
'version = "0.1.0"\n'
'edition = "2021"\n',
)
prefix = Prefix(str(tmpdir))

find_executable_exes = []

original_find_executable = parse_shebang.find_executable
def test_installs_rust_missing_rustup(tmp_path):
_make_hello_world(tmp_path)

def mocked_find_executable(
exe: str, *, env: Mapping[str, str] | None = None,
) -> str | None:
"""
Return `None` the first time `find_executable` is called to ensure
that the bootstrapping code is executed, then just let the function
work as normal.
# pretend like `rustup` doesn't exist so it gets bootstrapped
calls = []
orig = parse_shebang.find_executable

Also log the arguments to ensure that everything works as expected.
"""
find_executable_exes.append(exe)
if len(find_executable_exes) == 1:
def mck(exe, env=None):
calls.append(exe)
if len(calls) == 1:
assert exe == 'rustup'
return None
return original_find_executable(exe, env=env)
return orig(exe, env=env)

with mock.patch.object(parse_shebang, 'find_executable') as find_exe_mck:
find_exe_mck.side_effect = mocked_find_executable
rust.install_environment(prefix, language_version, ())
assert find_executable_exes == ['rustup', 'rustup', 'cargo']
with mock.patch.object(parse_shebang, 'find_executable', side_effect=mck):
ret = run_language(tmp_path, rust, 'hello_world', version='1.56.0')
assert calls == ['rustup', 'rustup', 'cargo', 'hello_world']
assert ret == (0, b'Hello, world!\n')

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

@pytest.mark.parametrize('version', (C.DEFAULT, '1.56.0'))
def test_language_version_with_rustup(tmp_path, version):
assert parse_shebang.find_executable('rustup') is not None

def test_installs_with_existing_rustup(tmpdir):
tmpdir.join('src', 'main.rs').ensure().write(
'fn main() {\n'
' println!("Hello, world!");\n'
'}\n',
)
tmpdir.join('Cargo.toml').ensure().write(
'[package]\n'
'name = "hello_world"\n'
'version = "0.1.0"\n'
'edition = "2021"\n',
_make_hello_world(tmp_path)

ret = run_language(tmp_path, rust, 'hello_world', version=version)
assert ret == (0, b'Hello, world!\n')


@pytest.mark.parametrize('dep', ('cli:shellharden:4.2.0', 'cli:shellharden'))
def test_rust_cli_additional_dependencies(tmp_path, dep):
_make_local_repo(str(tmp_path))

t_sh = tmp_path.joinpath('t.sh')
t_sh.write_text('echo $hi\n')

assert rust.get_default_version() == 'system'
ret = run_language(
tmp_path,
rust,
'shellharden --transform',
deps=(dep,),
args=(str(t_sh),),
)
prefix = Prefix(str(tmpdir))
assert ret == (0, b'echo "$hi"\n')

assert parse_shebang.find_executable('rustup') is not None
rust.install_environment(prefix, '1.56.0', ())
with rust.in_env(prefix, '1.56.0'):
assert cmd_output('hello_world')[1] == 'Hello, world!\n'

def test_run_lib_additional_dependencies(tmp_path):
_make_hello_world(tmp_path)

deps = ('shellharden:4.2.0', 'git-version')
ret = run_language(tmp_path, rust, 'hello_world', deps=deps)
assert ret == (0, b'Hello, world!\n')

bin_dir = tmp_path.joinpath('rustenv-system', 'bin')
assert bin_dir.is_dir()
assert not bin_dir.joinpath('shellharden').exists()
assert not bin_dir.joinpath('shellharden.exe').exists()
66 changes: 0 additions & 66 deletions tests/repository_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from pre_commit.languages import node
from pre_commit.languages import python
from pre_commit.languages import ruby
from pre_commit.languages import rust
from pre_commit.languages.all import languages
from pre_commit.prefix import Prefix
from pre_commit.repository import _hook_installed
Expand Down Expand Up @@ -366,54 +365,6 @@ def test_golang_with_recursive_submodule(tmpdir, tempdir_factory, store):
assert _norm_out(out) == b'hello hello world\n'


def test_rust_hook(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'rust_hooks_repo',
'rust-hook', [], b'hello world\n',
)


@pytest.mark.parametrize('dep', ('cli:shellharden:3.1.0', 'cli:shellharden'))
def test_additional_rust_cli_dependencies_installed(
tempdir_factory, store, dep,
):
path = make_repo(tempdir_factory, 'rust_hooks_repo')
config = make_config_from_repo(path)
# A small rust package with no dependencies.
config['hooks'][0]['additional_dependencies'] = [dep]
hook = _get_hook(config, store, 'rust-hook')
envdir = helpers.environment_dir(
hook.prefix,
rust.ENVIRONMENT_DIR,
'system',
)
binaries = os.listdir(os.path.join(envdir, 'bin'))
# normalize for windows
binaries = [os.path.splitext(binary)[0] for binary in binaries]
assert 'shellharden' in binaries


def test_additional_rust_lib_dependencies_installed(
tempdir_factory, store,
):
path = make_repo(tempdir_factory, 'rust_hooks_repo')
config = make_config_from_repo(path)
# A small rust package with no dependencies.
deps = ['shellharden:3.1.0', 'git-version']
config['hooks'][0]['additional_dependencies'] = deps
hook = _get_hook(config, store, 'rust-hook')
envdir = helpers.environment_dir(
hook.prefix,
rust.ENVIRONMENT_DIR,
'system',
)
binaries = os.listdir(os.path.join(envdir, 'bin'))
# normalize for windows
binaries = [os.path.splitext(binary)[0] for binary in binaries]
assert 'rust-hello-world' in binaries
assert 'shellharden' not in binaries


def test_missing_executable(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'not_found_exe',
Expand Down Expand Up @@ -636,23 +587,6 @@ def test_local_golang_additional_dependencies(store):
assert _norm_out(out) == b'Hello, Go examples!\n'


def test_local_rust_additional_dependencies(store):
config = {
'repo': 'local',
'hooks': [{
'id': 'hello',
'name': 'hello',
'entry': 'hello',
'language': 'rust',
'additional_dependencies': ['cli:hello-cli:0.2.2'],
}],
}
hook = _get_hook(config, store, 'hello')
ret, out = _hook_run(hook, (), color=False)
assert ret == 0
assert _norm_out(out) == b'Hello World!\n'


def test_fail_hooks(store):
config = {
'repo': 'local',
Expand Down