summaryrefslogtreecommitdiffstats
path: root/tests/languages
diff options
context:
space:
mode:
Diffstat (limited to 'tests/languages')
-rw-r--r--tests/languages/golang_test.py4
-rw-r--r--tests/languages/node_test.py41
-rw-r--r--tests/languages/ruby_test.py126
-rw-r--r--tests/languages/rust_test.py101
4 files changed, 188 insertions, 84 deletions
diff --git a/tests/languages/golang_test.py b/tests/languages/golang_test.py
index 0219261..7c04255 100644
--- a/tests/languages/golang_test.py
+++ b/tests/languages/golang_test.py
@@ -1,9 +1,9 @@
from __future__ import annotations
-import re
from unittest import mock
import pytest
+import re_assert
import pre_commit.constants as C
from pre_commit.languages import golang
@@ -40,4 +40,4 @@ def test_golang_infer_go_version_default():
version = ACTUAL_INFER_GO_VERSION(C.DEFAULT)
assert version != C.DEFAULT
- assert re.match(r'^\d+\.\d+\.\d+$', version)
+ re_assert.Matches(r'^\d+\.\d+(?:\.\d+)?$').assert_matches(version)
diff --git a/tests/languages/node_test.py b/tests/languages/node_test.py
index b69adfa..cba0228 100644
--- a/tests/languages/node_test.py
+++ b/tests/languages/node_test.py
@@ -13,7 +13,9 @@ from pre_commit import envcontext
from pre_commit import parse_shebang
from pre_commit.languages import node
from pre_commit.prefix import Prefix
+from pre_commit.store import _make_local_repo
from pre_commit.util import cmd_output
+from testing.language_helpers import run_language
from testing.util import xfailif_windows
@@ -109,3 +111,42 @@ def test_installs_without_links_outside_env(tmpdir):
with node.in_env(prefix, 'system'):
assert cmd_output('foo')[1] == 'success!\n'
+
+
+def _make_hello_world(tmp_path):
+ package_json = '''\
+{"name": "t", "version": "0.0.1", "bin": {"node-hello": "./bin/main.js"}}
+'''
+ tmp_path.joinpath('package.json').write_text(package_json)
+ bin_dir = tmp_path.joinpath('bin')
+ bin_dir.mkdir()
+ bin_dir.joinpath('main.js').write_text(
+ '#!/usr/bin/env node\n'
+ 'console.log("Hello World");\n',
+ )
+
+
+def test_node_hook_system(tmp_path):
+ _make_hello_world(tmp_path)
+ ret = run_language(tmp_path, node, 'node-hello')
+ assert ret == (0, b'Hello World\n')
+
+
+def test_node_with_user_config_set(tmp_path):
+ cfg = tmp_path.joinpath('cfg')
+ cfg.write_text('cache=/dne\n')
+ with envcontext.envcontext((('NPM_CONFIG_USERCONFIG', str(cfg)),)):
+ test_node_hook_system(tmp_path)
+
+
+@pytest.mark.parametrize('version', (C.DEFAULT, '18.13.0'))
+def test_node_hook_versions(tmp_path, version):
+ _make_hello_world(tmp_path)
+ ret = run_language(tmp_path, node, 'node-hello', version=version)
+ assert ret == (0, b'Hello World\n')
+
+
+def test_node_additional_deps(tmp_path):
+ _make_local_repo(str(tmp_path))
+ ret, out = run_language(tmp_path, node, 'npm ls -g', deps=('lodash',))
+ assert b' lodash@' in out
diff --git a/tests/languages/ruby_test.py b/tests/languages/ruby_test.py
index 63a16eb..9cfaad5 100644
--- a/tests/languages/ruby_test.py
+++ b/tests/languages/ruby_test.py
@@ -1,6 +1,5 @@
from __future__ import annotations
-import os.path
import tarfile
from unittest import mock
@@ -8,10 +7,12 @@ import pytest
import pre_commit.constants as C
from pre_commit import parse_shebang
+from pre_commit.envcontext import envcontext
from pre_commit.languages import ruby
-from pre_commit.prefix import Prefix
-from pre_commit.util import cmd_output
+from pre_commit.store import _make_local_repo
from pre_commit.util import resource_bytesio
+from testing.language_helpers import run_language
+from testing.util import cwd
from testing.util import xfailif_windows
@@ -34,56 +35,105 @@ def test_uses_system_if_both_gem_and_ruby_are_available(find_exe_mck):
assert ACTUAL_GET_DEFAULT_VERSION() == 'system'
-@pytest.fixture
-def fake_gem_prefix(tmpdir):
+@pytest.mark.parametrize(
+ 'filename',
+ ('rbenv.tar.gz', 'ruby-build.tar.gz', 'ruby-download.tar.gz'),
+)
+def test_archive_root_stat(filename):
+ with resource_bytesio(filename) as f:
+ with tarfile.open(fileobj=f) as tarf:
+ root, _, _ = filename.partition('.')
+ assert oct(tarf.getmember(root).mode) == '0o755'
+
+
+def _setup_hello_world(tmp_path):
+ bin_dir = tmp_path.joinpath('bin')
+ bin_dir.mkdir()
+ bin_dir.joinpath('ruby_hook').write_text(
+ '#!/usr/bin/env ruby\n'
+ "puts 'Hello world from a ruby hook'\n",
+ )
gemspec = '''\
Gem::Specification.new do |s|
- s.name = 'pre_commit_placeholder_package'
- s.version = '0.0.0'
- s.summary = 'placeholder gem for pre-commit hooks'
+ s.name = 'ruby_hook'
+ s.version = '0.1.0'
s.authors = ['Anthony Sottile']
+ s.summary = 'A ruby hook!'
+ s.description = 'A ruby hook!'
+ s.files = ['bin/ruby_hook']
+ s.executables = ['ruby_hook']
end
'''
- tmpdir.join('placeholder_gem.gemspec').write(gemspec)
- yield Prefix(tmpdir)
+ tmp_path.joinpath('ruby_hook.gemspec').write_text(gemspec)
-@xfailif_windows # pragma: win32 no cover
-def test_install_ruby_system(fake_gem_prefix):
- ruby.install_environment(fake_gem_prefix, 'system', ())
+def test_ruby_hook_system(tmp_path):
+ assert ruby.get_default_version() == 'system'
+
+ _setup_hello_world(tmp_path)
+
+ ret = run_language(tmp_path, ruby, 'ruby_hook')
+ assert ret == (0, b'Hello world from a ruby hook\n')
- # Should be able to activate and use rbenv install
- with ruby.in_env(fake_gem_prefix, 'system'):
- _, out, _ = cmd_output('gem', 'list')
- assert 'pre_commit_placeholder_package' in out
+
+def test_ruby_with_user_install_set(tmp_path):
+ gemrc = tmp_path.joinpath('gemrc')
+ gemrc.write_text('gem: --user-install\n')
+
+ with envcontext((('GEMRC', str(gemrc)),)):
+ test_ruby_hook_system(tmp_path)
+
+
+def test_ruby_additional_deps(tmp_path):
+ _make_local_repo(tmp_path)
+
+ ret = run_language(
+ tmp_path,
+ ruby,
+ 'ruby -e',
+ args=('require "tins"',),
+ deps=('tins',),
+ )
+ assert ret == (0, b'')
@xfailif_windows # pragma: win32 no cover
-def test_install_ruby_default(fake_gem_prefix):
- ruby.install_environment(fake_gem_prefix, C.DEFAULT, ())
- # Should have created rbenv directory
- assert os.path.exists(fake_gem_prefix.path('rbenv-default'))
+def test_ruby_hook_default(tmp_path):
+ _setup_hello_world(tmp_path)
- # Should be able to activate using our script and access rbenv
- with ruby.in_env(fake_gem_prefix, 'default'):
- cmd_output('rbenv', '--help')
+ out, ret = run_language(tmp_path, ruby, 'rbenv --help', version='default')
+ assert out == 0
+ assert ret.startswith(b'Usage: rbenv ')
@xfailif_windows # pragma: win32 no cover
-def test_install_ruby_with_version(fake_gem_prefix):
- ruby.install_environment(fake_gem_prefix, '3.2.0', ())
+def test_ruby_hook_language_version(tmp_path):
+ _setup_hello_world(tmp_path)
+ tmp_path.joinpath('bin', 'ruby_hook').write_text(
+ '#!/usr/bin/env ruby\n'
+ 'puts RUBY_VERSION\n'
+ "puts 'Hello world from a ruby hook'\n",
+ )
- # Should be able to activate and use rbenv install
- with ruby.in_env(fake_gem_prefix, '3.2.0'):
- cmd_output('rbenv', 'install', '--help')
+ ret = run_language(tmp_path, ruby, 'ruby_hook', version='3.2.0')
+ assert ret == (0, b'3.2.0\nHello world from a ruby hook\n')
-@pytest.mark.parametrize(
- 'filename',
- ('rbenv.tar.gz', 'ruby-build.tar.gz', 'ruby-download.tar.gz'),
-)
-def test_archive_root_stat(filename):
- with resource_bytesio(filename) as f:
- with tarfile.open(fileobj=f) as tarf:
- root, _, _ = filename.partition('.')
- assert oct(tarf.getmember(root).mode) == '0o755'
+@xfailif_windows # pragma: win32 no cover
+def test_ruby_with_bundle_disable_shared_gems(tmp_path):
+ workdir = tmp_path.joinpath('workdir')
+ workdir.mkdir()
+ # this needs a `source` or there's a deprecation warning
+ # silencing this with `BUNDLE_GEMFILE` breaks some tools (#2739)
+ workdir.joinpath('Gemfile').write_text('source ""\ngem "lol_hai"\n')
+ # this bundle config causes things to be written elsewhere
+ bundle = workdir.joinpath('.bundle')
+ bundle.mkdir()
+ bundle.joinpath('config').write_text(
+ 'BUNDLE_DISABLE_SHARED_GEMS: true\n'
+ 'BUNDLE_PATH: vendor/gem\n',
+ )
+
+ with cwd(workdir):
+ # `3.2.0` has new enough `gem` reading `.bundle`
+ test_ruby_hook_language_version(tmp_path)
diff --git a/tests/languages/rust_test.py b/tests/languages/rust_test.py
index b8167a9..5c17f5b 100644
--- a/tests/languages/rust_test.py
+++ b/tests/languages/rust_test.py
@@ -1,6 +1,5 @@
from __future__ import annotations
-from typing import Mapping
from unittest import mock
import pytest
@@ -8,8 +7,8 @@ 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__
@@ -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()