summaryrefslogtreecommitdiffstats
path: root/tests/languages
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2020-11-03 06:17:33 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2020-11-03 06:17:33 +0000
commit22b6d3abcec9046e294d08c741aa7b6338984e84 (patch)
treea0a9c823a7d82771ffb8a8b39f7143d5ba2f6b30 /tests/languages
parentAdding upstream version 2.7.1. (diff)
downloadpre-commit-22b6d3abcec9046e294d08c741aa7b6338984e84.tar.xz
pre-commit-22b6d3abcec9046e294d08c741aa7b6338984e84.zip
Adding upstream version 2.8.2.upstream/2.8.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--tests/languages/dotnet_test.py0
-rw-r--r--tests/languages/helpers_test.py51
-rw-r--r--tests/languages/node_test.py65
-rw-r--r--tests/languages/pygrep_test.py72
-rw-r--r--tests/languages/python_test.py3
-rw-r--r--tests/languages/ruby_test.py40
6 files changed, 212 insertions, 19 deletions
diff --git a/tests/languages/dotnet_test.py b/tests/languages/dotnet_test.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/languages/dotnet_test.py
diff --git a/tests/languages/helpers_test.py b/tests/languages/helpers_test.py
index fa493cc..669cd33 100644
--- a/tests/languages/helpers_test.py
+++ b/tests/languages/helpers_test.py
@@ -1,17 +1,66 @@
import multiprocessing
-import os
+import os.path
import sys
from unittest import mock
import pytest
import pre_commit.constants as C
+from pre_commit import parse_shebang
from pre_commit.languages import helpers
from pre_commit.prefix import Prefix
from pre_commit.util import CalledProcessError
from testing.auto_namedtuple import auto_namedtuple
+@pytest.fixture
+def find_exe_mck():
+ with mock.patch.object(parse_shebang, 'find_executable') as mck:
+ yield mck
+
+
+@pytest.fixture
+def homedir_mck():
+ def fake_expanduser(pth):
+ assert pth == '~'
+ return os.path.normpath('/home/me')
+
+ with mock.patch.object(os.path, 'expanduser', fake_expanduser):
+ yield
+
+
+def test_exe_exists_does_not_exist(find_exe_mck, homedir_mck):
+ find_exe_mck.return_value = None
+ assert helpers.exe_exists('ruby') is False
+
+
+def test_exe_exists_exists(find_exe_mck, homedir_mck):
+ find_exe_mck.return_value = os.path.normpath('/usr/bin/ruby')
+ assert helpers.exe_exists('ruby') is True
+
+
+def test_exe_exists_false_if_shim(find_exe_mck, homedir_mck):
+ find_exe_mck.return_value = os.path.normpath('/foo/shims/ruby')
+ assert helpers.exe_exists('ruby') is False
+
+
+def test_exe_exists_false_if_homedir(find_exe_mck, homedir_mck):
+ find_exe_mck.return_value = os.path.normpath('/home/me/somedir/ruby')
+ assert helpers.exe_exists('ruby') is False
+
+
+def test_exe_exists_commonpath_raises_ValueError(find_exe_mck, homedir_mck):
+ find_exe_mck.return_value = os.path.normpath('/usr/bin/ruby')
+ with mock.patch.object(os.path, 'commonpath', side_effect=ValueError):
+ assert helpers.exe_exists('ruby') is True
+
+
+def test_exe_exists_true_when_homedir_is_slash(find_exe_mck):
+ find_exe_mck.return_value = os.path.normpath('/usr/bin/ruby')
+ with mock.patch.object(os.path, 'expanduser', return_value=os.sep):
+ assert helpers.exe_exists('ruby') is True
+
+
def test_basic_get_default_version():
assert helpers.basic_get_default_version() == C.DEFAULT
diff --git a/tests/languages/node_test.py b/tests/languages/node_test.py
index fd30046..8e52268 100644
--- a/tests/languages/node_test.py
+++ b/tests/languages/node_test.py
@@ -1,14 +1,21 @@
+import json
+import os
+import shutil
import sys
from unittest import mock
import pytest
import pre_commit.constants as C
+from pre_commit import envcontext
from pre_commit import parse_shebang
-from pre_commit.languages.node import get_default_version
+from pre_commit.languages import node
+from pre_commit.prefix import Prefix
+from pre_commit.util import cmd_output
+from testing.util import xfailif_windows
-ACTUAL_GET_DEFAULT_VERSION = get_default_version.__wrapped__
+ACTUAL_GET_DEFAULT_VERSION = node.get_default_version.__wrapped__
@pytest.fixture
@@ -45,3 +52,57 @@ def test_uses_default_when_node_and_npm_are_not_available(find_exe_mck):
def test_sets_default_on_windows(find_exe_mck):
find_exe_mck.return_value = '/path/to/exe'
assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT
+
+
+@xfailif_windows # pragma: win32 no cover
+def test_healthy_system_node(tmpdir):
+ tmpdir.join('package.json').write('{"name": "t", "version": "1.0.0"}')
+
+ prefix = Prefix(str(tmpdir))
+ node.install_environment(prefix, 'system', ())
+ assert node.healthy(prefix, 'system')
+
+
+@xfailif_windows # pragma: win32 no cover
+def test_unhealthy_if_system_node_goes_missing(tmpdir):
+ bin_dir = tmpdir.join('bin').ensure_dir()
+ node_bin = bin_dir.join('node')
+ node_bin.mksymlinkto(shutil.which('node'))
+
+ prefix_dir = tmpdir.join('prefix').ensure_dir()
+ prefix_dir.join('package.json').write('{"name": "t", "version": "1.0.0"}')
+
+ path = ('PATH', (str(bin_dir), os.pathsep, envcontext.Var('PATH')))
+ with envcontext.envcontext((path,)):
+ prefix = Prefix(str(prefix_dir))
+ node.install_environment(prefix, 'system', ())
+ assert node.healthy(prefix, 'system')
+
+ node_bin.remove()
+ assert not node.healthy(prefix, 'system')
+
+
+@xfailif_windows # pragma: win32 no cover
+def test_installs_without_links_outside_env(tmpdir):
+ tmpdir.join('bin/main.js').ensure().write(
+ '#!/usr/bin/env node\n'
+ '_ = require("lodash"); console.log("success!")\n',
+ )
+ tmpdir.join('package.json').write(
+ json.dumps({
+ 'name': 'foo',
+ 'version': '0.0.1',
+ 'bin': {'foo': './bin/main.js'},
+ 'dependencies': {'lodash': '*'},
+ }),
+ )
+
+ prefix = Prefix(str(tmpdir))
+ node.install_environment(prefix, 'system', ())
+ assert node.healthy(prefix, 'system')
+
+ # this directory shouldn't exist, make sure we succeed without it existing
+ cmd_output('rm', '-rf', str(tmpdir.join('node_modules')))
+
+ with node.in_env(prefix, 'system'):
+ assert cmd_output('foo')[1] == 'success!\n'
diff --git a/tests/languages/pygrep_test.py b/tests/languages/pygrep_test.py
index cabea22..d8bacc4 100644
--- a/tests/languages/pygrep_test.py
+++ b/tests/languages/pygrep_test.py
@@ -8,6 +8,9 @@ def some_files(tmpdir):
tmpdir.join('f1').write_binary(b'foo\nbar\n')
tmpdir.join('f2').write_binary(b'[INFO] hi\n')
tmpdir.join('f3').write_binary(b"with'quotes\n")
+ tmpdir.join('f4').write_binary(b'foo\npattern\nbar\n')
+ tmpdir.join('f5').write_binary(b'[INFO] hi\npattern\nbar')
+ tmpdir.join('f6').write_binary(b"pattern\nbarwith'foo\n")
with tmpdir.as_cwd():
yield
@@ -23,42 +26,99 @@ def some_files(tmpdir):
("h'q", 1, "f3:1:with'quotes\n"),
),
)
-def test_main(some_files, cap_out, pattern, expected_retcode, expected_out):
+def test_main(cap_out, pattern, expected_retcode, expected_out):
ret = pygrep.main((pattern, 'f1', 'f2', 'f3'))
out = cap_out.get()
assert ret == expected_retcode
assert out == expected_out
-def test_ignore_case(some_files, cap_out):
+@pytest.mark.usefixtures('some_files')
+def test_negate_by_line_no_match(cap_out):
+ ret = pygrep.main(('pattern\nbar', 'f4', 'f5', 'f6', '--negate'))
+ out = cap_out.get()
+ assert ret == 1
+ assert out == 'f4\nf5\nf6\n'
+
+
+@pytest.mark.usefixtures('some_files')
+def test_negate_by_line_two_match(cap_out):
+ ret = pygrep.main(('foo', 'f4', 'f5', 'f6', '--negate'))
+ out = cap_out.get()
+ assert ret == 1
+ assert out == 'f5\n'
+
+
+@pytest.mark.usefixtures('some_files')
+def test_negate_by_line_all_match(cap_out):
+ ret = pygrep.main(('pattern', 'f4', 'f5', 'f6', '--negate'))
+ out = cap_out.get()
+ assert ret == 0
+ assert out == ''
+
+
+@pytest.mark.usefixtures('some_files')
+def test_negate_by_file_no_match(cap_out):
+ ret = pygrep.main(('baz', 'f4', 'f5', 'f6', '--negate', '--multiline'))
+ out = cap_out.get()
+ assert ret == 1
+ assert out == 'f4\nf5\nf6\n'
+
+
+@pytest.mark.usefixtures('some_files')
+def test_negate_by_file_one_match(cap_out):
+ ret = pygrep.main(
+ ('foo\npattern', 'f4', 'f5', 'f6', '--negate', '--multiline'),
+ )
+ out = cap_out.get()
+ assert ret == 1
+ assert out == 'f5\nf6\n'
+
+
+@pytest.mark.usefixtures('some_files')
+def test_negate_by_file_all_match(cap_out):
+ ret = pygrep.main(
+ ('pattern\nbar', 'f4', 'f5', 'f6', '--negate', '--multiline'),
+ )
+ out = cap_out.get()
+ assert ret == 0
+ assert out == ''
+
+
+@pytest.mark.usefixtures('some_files')
+def test_ignore_case(cap_out):
ret = pygrep.main(('--ignore-case', 'info', 'f1', 'f2', 'f3'))
out = cap_out.get()
assert ret == 1
assert out == 'f2:1:[INFO] hi\n'
-def test_multiline(some_files, cap_out):
+@pytest.mark.usefixtures('some_files')
+def test_multiline(cap_out):
ret = pygrep.main(('--multiline', r'foo\nbar', 'f1', 'f2', 'f3'))
out = cap_out.get()
assert ret == 1
assert out == 'f1:1:foo\nbar\n'
-def test_multiline_line_number(some_files, cap_out):
+@pytest.mark.usefixtures('some_files')
+def test_multiline_line_number(cap_out):
ret = pygrep.main(('--multiline', r'ar', 'f1', 'f2', 'f3'))
out = cap_out.get()
assert ret == 1
assert out == 'f1:2:bar\n'
-def test_multiline_dotall_flag_is_enabled(some_files, cap_out):
+@pytest.mark.usefixtures('some_files')
+def test_multiline_dotall_flag_is_enabled(cap_out):
ret = pygrep.main(('--multiline', r'o.*bar', 'f1', 'f2', 'f3'))
out = cap_out.get()
assert ret == 1
assert out == 'f1:1:foo\nbar\n'
-def test_multiline_multiline_flag_is_enabled(some_files, cap_out):
+@pytest.mark.usefixtures('some_files')
+def test_multiline_multiline_flag_is_enabled(cap_out):
ret = pygrep.main(('--multiline', r'foo$.*bar', 'f1', 'f2', 'f3'))
out = cap_out.get()
assert ret == 1
diff --git a/tests/languages/python_test.py b/tests/languages/python_test.py
index 29c5a9b..cfe1483 100644
--- a/tests/languages/python_test.py
+++ b/tests/languages/python_test.py
@@ -36,13 +36,14 @@ def test_norm_version_expanduser():
def test_norm_version_of_default_is_sys_executable():
- assert python.norm_version('default') == os.path.realpath(sys.executable)
+ assert python.norm_version('default') is None
@pytest.mark.parametrize('v', ('python3.6', 'python3', 'python'))
def test_sys_executable_matches(v):
with mock.patch.object(sys, 'version_info', (3, 6, 7)):
assert python._sys_executable_matches(v)
+ assert python.norm_version(v) is None
@pytest.mark.parametrize('v', ('notpython', 'python3.x'))
diff --git a/tests/languages/ruby_test.py b/tests/languages/ruby_test.py
index 853bb73..6c0c9e5 100644
--- a/tests/languages/ruby_test.py
+++ b/tests/languages/ruby_test.py
@@ -30,23 +30,45 @@ 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):
+ gemspec = '''\
+Gem::Specification.new do |s|
+ s.name = 'pre_commit_dummy_package'
+ s.version = '0.0.0'
+ s.summary = 'dummy gem for pre-commit hooks'
+ s.authors = ['Anthony Sottile']
+end
+'''
+ tmpdir.join('dummy_gem.gemspec').write(gemspec)
+ yield Prefix(tmpdir)
+
+
+@xfailif_windows # pragma: win32 no cover
+def test_install_ruby_system(fake_gem_prefix):
+ ruby.install_environment(fake_gem_prefix, 'system', ())
+
+ # 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_dummy_package' in out
+
+
@xfailif_windows # pragma: win32 no cover
-def test_install_rbenv(tempdir_factory):
- prefix = Prefix(tempdir_factory.get())
- ruby._install_rbenv(prefix, C.DEFAULT)
+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(prefix.path('rbenv-default'))
+ assert os.path.exists(fake_gem_prefix.path('rbenv-default'))
# Should be able to activate using our script and access rbenv
- with ruby.in_env(prefix, 'default'):
+ with ruby.in_env(fake_gem_prefix, 'default'):
cmd_output('rbenv', '--help')
@xfailif_windows # pragma: win32 no cover
-def test_install_rbenv_with_version(tempdir_factory):
- prefix = Prefix(tempdir_factory.get())
- ruby._install_rbenv(prefix, version='1.9.3p547')
+def test_install_ruby_with_version(fake_gem_prefix):
+ ruby.install_environment(fake_gem_prefix, '2.7.2', ())
# Should be able to activate and use rbenv install
- with ruby.in_env(prefix, '1.9.3p547'):
+ with ruby.in_env(fake_gem_prefix, '2.7.2'):
cmd_output('rbenv', 'install', '--help')