summaryrefslogtreecommitdiffstats
path: root/tests/languages
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-01-04 07:22:18 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-01-04 07:22:18 +0000
commit58a809aa43ae21566be4613eb42ac56faf54f901 (patch)
tree371b9cc8a265cbabea4b12e5508b8bde6edf0b89 /tests/languages
parentAdding upstream version 2.20.0. (diff)
downloadpre-commit-58a809aa43ae21566be4613eb42ac56faf54f901.tar.xz
pre-commit-58a809aa43ae21566be4613eb42ac56faf54f901.zip
Adding upstream version 2.21.0.upstream/2.21.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/languages')
-rw-r--r--tests/languages/docker_test.py2
-rw-r--r--tests/languages/r_test.py3
-rw-r--r--tests/languages/ruby_test.py4
-rw-r--r--tests/languages/rust_test.py90
4 files changed, 95 insertions, 4 deletions
diff --git a/tests/languages/docker_test.py b/tests/languages/docker_test.py
index 5838761..5f7c85e 100644
--- a/tests/languages/docker_test.py
+++ b/tests/languages/docker_test.py
@@ -178,6 +178,6 @@ def test_get_docker_path_in_docker_windows(in_docker):
def test_get_docker_path_in_docker_docker_in_docker(in_docker):
# won't be able to discover "self" container in true docker-in-docker
- err = CalledProcessError(1, (), 0, b'', b'')
+ err = CalledProcessError(1, (), b'', b'')
with mock.patch.object(docker, 'cmd_output_b', side_effect=err):
assert docker._get_docker_path('/project') == '/project'
diff --git a/tests/languages/r_test.py b/tests/languages/r_test.py
index 5bc63b2..c52d5ac 100644
--- a/tests/languages/r_test.py
+++ b/tests/languages/r_test.py
@@ -6,6 +6,7 @@ import pytest
from pre_commit import envcontext
from pre_commit.languages import r
+from pre_commit.util import win_exe
from testing.fixtures import make_config_from_repo
from testing.fixtures import make_repo
from tests.repository_test import _get_hook_no_install
@@ -133,7 +134,7 @@ def test_r_parsing_file_local(tempdir_factory, store):
def test_rscript_exec_relative_to_r_home():
- expected = os.path.join('r_home_dir', 'bin', 'Rscript')
+ expected = os.path.join('r_home_dir', 'bin', win_exe('Rscript'))
with envcontext.envcontext((('R_HOME', 'r_home_dir'),)):
assert r._rscript_exec() == expected
diff --git a/tests/languages/ruby_test.py b/tests/languages/ruby_test.py
index dc55456..29f3c80 100644
--- a/tests/languages/ruby_test.py
+++ b/tests/languages/ruby_test.py
@@ -71,10 +71,10 @@ def test_install_ruby_default(fake_gem_prefix):
@xfailif_windows # pragma: win32 no cover
def test_install_ruby_with_version(fake_gem_prefix):
- ruby.install_environment(fake_gem_prefix, '2.7.2', ())
+ ruby.install_environment(fake_gem_prefix, '3.1.0', ())
# Should be able to activate and use rbenv install
- with ruby.in_env(fake_gem_prefix, '2.7.2'):
+ with ruby.in_env(fake_gem_prefix, '3.1.0'):
cmd_output('rbenv', 'install', '--help')
diff --git a/tests/languages/rust_test.py b/tests/languages/rust_test.py
new file mode 100644
index 0000000..f011e71
--- /dev/null
+++ b/tests/languages/rust_test.py
@@ -0,0 +1,90 @@
+from __future__ import annotations
+
+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
+
+ACTUAL_GET_DEFAULT_VERSION = rust.get_default_version.__wrapped__
+
+
+@pytest.fixture
+def cmd_output_b_mck():
+ with mock.patch.object(rust, 'cmd_output_b') as mck:
+ yield mck
+
+
+def test_sets_system_when_rust_is_available(cmd_output_b_mck):
+ cmd_output_b_mck.return_value = (0, b'', b'')
+ assert ACTUAL_GET_DEFAULT_VERSION() == 'system'
+
+
+def test_uses_default_when_rust_is_not_available(cmd_output_b_mck):
+ cmd_output_b_mck.return_value = (127, b'', b'error: not found')
+ 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(
+ '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',
+ )
+ prefix = Prefix(str(tmpdir))
+
+ find_executable_exes = []
+
+ original_find_executable = parse_shebang.find_executable
+
+ def mocked_find_executable(exe: str) -> 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.
+
+ Also log the arguments to ensure that everything works as expected.
+ """
+ find_executable_exes.append(exe)
+ if len(find_executable_exes) == 1:
+ return None
+ return original_find_executable(exe)
+
+ 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 rust.in_env(prefix, language_version):
+ assert cmd_output('hello_world')[1] == 'Hello, world!\n'
+
+
+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',
+ )
+ prefix = Prefix(str(tmpdir))
+
+ 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'