from __future__ import absolute_import from __future__ import unicode_literals import collections import os.path import re import shutil import mock import pytest import pre_commit.constants as C from pre_commit import five from pre_commit import parse_shebang from pre_commit.clientlib import load_manifest from pre_commit.languages import golang from pre_commit.languages import helpers from pre_commit.languages import node from pre_commit.languages import pcre from pre_commit.languages import python from pre_commit.languages import ruby from pre_commit.languages import rust from pre_commit.repository import Repository from pre_commit.util import cmd_output from testing.fixtures import config_with_local_hooks from testing.fixtures import make_config_from_repo from testing.fixtures import make_repo from testing.fixtures import modify_manifest from testing.util import cwd from testing.util import get_resource_path from testing.util import skipif_cant_run_docker from testing.util import skipif_cant_run_swift from testing.util import xfailif_broken_deep_listdir from testing.util import xfailif_no_pcre_support from testing.util import xfailif_no_venv from testing.util import xfailif_windows_no_ruby def _norm_out(b): return b.replace(b'\r\n', b'\n') def _test_hook_repo( tempdir_factory, store, repo_path, hook_id, args, expected, expected_return_code=0, config_kwargs=None, ): path = make_repo(tempdir_factory, repo_path) config = make_config_from_repo(path, **(config_kwargs or {})) repo = Repository.create(config, store) hook_dict, = [ hook for repo_hook_id, hook in repo.hooks if repo_hook_id == hook_id ] ret = repo.run_hook(hook_dict, args) assert ret[0] == expected_return_code assert _norm_out(ret[1]) == expected def test_python_hook(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'python_hooks_repo', 'foo', [os.devnull], b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n", ) def test_python_hook_default_version(tempdir_factory, store): # make sure that this continues to work for platforms where default # language detection does not work with mock.patch.object( python, 'get_default_version', return_value='default', ): test_python_hook(tempdir_factory, store) def test_python_hook_args_with_spaces(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'python_hooks_repo', 'foo', [], b"['i have spaces', 'and\"\\'quotes', '$and !this']\n" b'Hello World\n', config_kwargs={ 'hooks': [{ 'id': 'foo', 'args': ['i have spaces', 'and"\'quotes', '$and !this'], }], }, ) def test_python_hook_weird_setup_cfg(in_git_dir, tempdir_factory, store): in_git_dir.join('setup.cfg').write('[install]\ninstall_scripts=/usr/sbin') _test_hook_repo( tempdir_factory, store, 'python_hooks_repo', 'foo', [os.devnull], b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n", ) @xfailif_no_venv def test_python_venv(tempdir_factory, store): # pragma: no cover (no venv) _test_hook_repo( tempdir_factory, store, 'python_venv_hooks_repo', 'foo', [os.devnull], b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n", ) def test_switch_language_versions_doesnt_clobber(tempdir_factory, store): # We're using the python3 repo because it prints the python version path = make_repo(tempdir_factory, 'python3_hooks_repo') def run_on_version(version, expected_output): config = make_config_from_repo( path, hooks=[{'id': 'python3-hook', 'language_version': version}], ) repo = Repository.create(config, store) hook_dict, = [ hook for repo_hook_id, hook in repo.hooks if repo_hook_id == 'python3-hook' ] ret = repo.run_hook(hook_dict, []) assert ret[0] == 0 assert _norm_out(ret[1]) == expected_output run_on_version('python2', b'2\n[]\nHello World\n') run_on_version('python3', b'3\n[]\nHello World\n') def test_versioned_python_hook(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'python3_hooks_repo', 'python3-hook', [os.devnull], b"3\n['" + five.to_bytes(os.devnull) + b"']\nHello World\n", ) @skipif_cant_run_docker def test_run_a_docker_hook(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'docker_hooks_repo', 'docker-hook', ['Hello World from docker'], b'Hello World from docker\n', ) @skipif_cant_run_docker def test_run_a_docker_hook_with_entry_args(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'docker_hooks_repo', 'docker-hook-arg', ['Hello World from docker'], b'Hello World from docker', ) @skipif_cant_run_docker def test_run_a_failing_docker_hook(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'docker_hooks_repo', 'docker-hook-failing', ['Hello World from docker'], b'', expected_return_code=1, ) @skipif_cant_run_docker @pytest.mark.parametrize('hook_id', ('echo-entrypoint', 'echo-cmd')) def test_run_a_docker_image_hook(tempdir_factory, store, hook_id): _test_hook_repo( tempdir_factory, store, 'docker_image_hooks_repo', hook_id, ['Hello World from docker'], b'Hello World from docker\n', ) @xfailif_broken_deep_listdir def test_run_a_node_hook(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'node_hooks_repo', 'foo', [os.devnull], b'Hello World\n', ) @xfailif_broken_deep_listdir def test_run_versioned_node_hook(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'node_versioned_hooks_repo', 'versioned-node-hook', [os.devnull], b'v9.3.0\nHello World\n', ) @xfailif_windows_no_ruby def test_run_a_ruby_hook(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'ruby_hooks_repo', 'ruby_hook', [os.devnull], b'Hello world from a ruby hook\n', ) @xfailif_windows_no_ruby def test_run_versioned_ruby_hook(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'ruby_versioned_hooks_repo', 'ruby_hook', [os.devnull], b'2.1.5\nHello world from a ruby hook\n', ) @xfailif_windows_no_ruby def test_run_ruby_hook_with_disable_shared_gems( tempdir_factory, store, tmpdir, ): """Make sure a Gemfile in the project doesn't interfere.""" tmpdir.join('Gemfile').write('gem "lol_hai"') tmpdir.join('.bundle').mkdir() tmpdir.join('.bundle', 'config').write( 'BUNDLE_DISABLE_SHARED_GEMS: true\n' 'BUNDLE_PATH: vendor/gem\n', ) with cwd(tmpdir.strpath): _test_hook_repo( tempdir_factory, store, 'ruby_versioned_hooks_repo', 'ruby_hook', [os.devnull], b'2.1.5\nHello world from a ruby hook\n', ) def test_system_hook_with_spaces(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'system_hook_with_spaces_repo', 'system-hook-with-spaces', [os.devnull], b'Hello World\n', ) @skipif_cant_run_swift def test_swift_hook(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'swift_hooks_repo', 'swift-hooks-repo', [], b'Hello, world!\n', ) def test_golang_hook(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'golang_hooks_repo', 'golang-hook', [], b'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] repo = Repository.create(config, store) repo.require_installed() (prefix, _, _, _), = repo._venvs() binaries = os.listdir(prefix.path( helpers.environment_dir(rust.ENVIRONMENT_DIR, 'default'), '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'] config['hooks'][0]['additional_dependencies'] = deps repo = Repository.create(config, store) repo.require_installed() (prefix, _, _, _), = repo._venvs() binaries = os.listdir(prefix.path( helpers.environment_dir(rust.ENVIRONMENT_DIR, 'default'), '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', 'not-found-exe', [os.devnull], b'Executable `i-dont-exist-lol` not found', expected_return_code=1, ) def test_run_a_script_hook(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'script_hooks_repo', 'bash_hook', ['bar'], b'bar\nHello World\n', ) def test_run_hook_with_spaced_args(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'arg_per_line_hooks_repo', 'arg-per-line', ['foo bar', 'baz'], b'arg: hello\narg: world\narg: foo bar\narg: baz\n', ) def test_run_hook_with_curly_braced_arguments(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'arg_per_line_hooks_repo', 'arg-per-line', [], b"arg: hi {1}\narg: I'm {a} problem\n", config_kwargs={ 'hooks': [{ 'id': 'arg-per-line', 'args': ['hi {1}', "I'm {a} problem"], }], }, ) def _make_grep_repo(language, entry, store, args=()): config = collections.OrderedDict(( ('repo', 'local'), ( 'hooks', [ collections.OrderedDict(( ('id', 'grep-hook'), ('name', 'grep-hook'), ('language', language), ('entry', entry), ('args', args), ('types', ['text']), )), ], ), )) repo = Repository.create(config, store) (_, hook), = repo.hooks return repo, hook @pytest.fixture def greppable_files(tmpdir): with tmpdir.as_cwd(): cmd_output('git', 'init', '.') tmpdir.join('f1').write_binary(b"hello'hi\nworld\n") tmpdir.join('f2').write_binary(b'foo\nbar\nbaz\n') tmpdir.join('f3').write_binary(b'[WARN] hi\n') yield tmpdir class TestPygrep(object): language = 'pygrep' def test_grep_hook_matching(self, greppable_files, store): repo, hook = _make_grep_repo(self.language, 'ello', store) ret, out, _ = repo.run_hook(hook, ('f1', 'f2', 'f3')) assert ret == 1 assert _norm_out(out) == b"f1:1:hello'hi\n" def test_grep_hook_case_insensitive(self, greppable_files, store): repo, hook = _make_grep_repo(self.language, 'ELLO', store, args=['-i']) ret, out, _ = repo.run_hook(hook, ('f1', 'f2', 'f3')) assert ret == 1 assert _norm_out(out) == b"f1:1:hello'hi\n" @pytest.mark.parametrize('regex', ('nope', "foo'bar", r'^\[INFO\]')) def test_grep_hook_not_matching(self, regex, greppable_files, store): repo, hook = _make_grep_repo(self.language, regex, store) ret, out, _ = repo.run_hook(hook, ('f1', 'f2', 'f3')) assert (ret, out) == (0, b'') @xfailif_no_pcre_support class TestPCRE(TestPygrep): """organized as a class for xfailing pcre""" language = 'pcre' def test_pcre_hook_many_files(self, greppable_files, store): # This is intended to simulate lots of passing files and one failing # file to make sure it still fails. This is not the case when naively # using a system hook with `grep -H -n '...'` repo, hook = _make_grep_repo('pcre', 'ello', store) ret, out, _ = repo.run_hook(hook, (os.devnull,) * 15000 + ('f1',)) assert ret == 1 assert _norm_out(out) == b"f1:1:hello'hi\n" def test_missing_pcre_support(self, greppable_files, store): orig_find_executable = parse_shebang.find_executable def no_grep(exe, **kwargs): if exe == pcre.GREP: return None else: return orig_find_executable(exe, **kwargs) with mock.patch.object(parse_shebang, 'find_executable', no_grep): repo, hook = _make_grep_repo('pcre', 'ello', store) ret, out, _ = repo.run_hook(hook, ('f1', 'f2', 'f3')) assert ret == 1 expected = 'Executable `{}` not found'.format(pcre.GREP).encode() assert out == expected def _norm_pwd(path): # Under windows bash's temp and windows temp is different. # This normalizes to the bash /tmp return cmd_output( 'bash', '-c', "cd '{}' && pwd".format(path), encoding=None, )[1].strip() def test_cwd_of_hook(in_git_dir, tempdir_factory, store): # Note: this doubles as a test for `system` hooks _test_hook_repo( tempdir_factory, store, 'prints_cwd_repo', 'prints_cwd', ['-L'], _norm_pwd(in_git_dir.strpath) + b'\n', ) def test_lots_of_files(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'script_hooks_repo', 'bash_hook', [os.devnull] * 15000, mock.ANY, ) def test_venvs(tempdir_factory, store): path = make_repo(tempdir_factory, 'python_hooks_repo') config = make_config_from_repo(path) repo = Repository.create(config, store) venv, = repo._venvs() assert venv == (mock.ANY, 'python', python.get_default_version(), ()) def test_additional_dependencies(tempdir_factory, store): path = make_repo(tempdir_factory, 'python_hooks_repo') config = make_config_from_repo(path) config['hooks'][0]['additional_dependencies'] = ['pep8'] repo = Repository.create(config, store) env, = repo._venvs() assert env == (mock.ANY, 'python', python.get_default_version(), ('pep8',)) def test_additional_dependencies_roll_forward(tempdir_factory, store): path = make_repo(tempdir_factory, 'python_hooks_repo') config1 = make_config_from_repo(path) repo1 = Repository.create(config1, store) repo1.require_installed() (prefix1, _, version1, _), = repo1._venvs() with python.in_env(prefix1, version1): assert 'mccabe' not in cmd_output('pip', 'freeze', '-l')[1] # Make another repo with additional dependencies config2 = make_config_from_repo(path) config2['hooks'][0]['additional_dependencies'] = ['mccabe'] repo2 = Repository.create(config2, store) repo2.require_installed() (prefix2, _, version2, _), = repo2._venvs() with python.in_env(prefix2, version2): assert 'mccabe' in cmd_output('pip', 'freeze', '-l')[1] # should not have affected original with python.in_env(prefix1, version1): assert 'mccabe' not in cmd_output('pip', 'freeze', '-l')[1] @xfailif_windows_no_ruby # pragma: windows no cover def test_additional_ruby_dependencies_installed(tempdir_factory, store): path = make_repo(tempdir_factory, 'ruby_hooks_repo') config = make_config_from_repo(path) config['hooks'][0]['additional_dependencies'] = ['thread_safe', 'tins'] repo = Repository.create(config, store) repo.require_installed() (prefix, _, version, _), = repo._venvs() with ruby.in_env(prefix, version): output = cmd_output('gem', 'list', '--local')[1] assert 'thread_safe' in output assert 'tins' in output @xfailif_broken_deep_listdir # pragma: windows no cover def test_additional_node_dependencies_installed(tempdir_factory, store): path = make_repo(tempdir_factory, 'node_hooks_repo') config = make_config_from_repo(path) # Careful to choose a small package that's not depped by npm config['hooks'][0]['additional_dependencies'] = ['lodash'] repo = Repository.create(config, store) repo.require_installed() (prefix, _, version, _), = repo._venvs() with node.in_env(prefix, version): output = cmd_output('npm', 'ls', '-g')[1] assert 'lodash' in output def test_additional_golang_dependencies_installed( tempdir_factory, store, ): path = make_repo(tempdir_factory, 'golang_hooks_repo') config = make_config_from_repo(path) # A small go package deps = ['github.com/golang/example/hello'] config['hooks'][0]['additional_dependencies'] = deps repo = Repository.create(config, store) repo.require_installed() (prefix, _, _, _), = repo._venvs() binaries = os.listdir(prefix.path( helpers.environment_dir(golang.ENVIRONMENT_DIR, 'default'), 'bin', )) # normalize for windows binaries = [os.path.splitext(binary)[0] for binary in binaries] assert 'hello' in binaries def test_local_golang_additional_dependencies(store): config = { 'repo': 'local', 'hooks': [{ 'id': 'hello', 'name': 'hello', 'entry': 'hello', 'language': 'golang', 'additional_dependencies': ['github.com/golang/example/hello'], }], } repo = Repository.create(config, store) (_, hook), = repo.hooks ret = repo.run_hook(hook, ('filename',)) assert ret[0] == 0 assert _norm_out(ret[1]) == 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'], }], } repo = Repository.create(config, store) (_, hook), = repo.hooks ret = repo.run_hook(hook, ()) assert ret[0] == 0 assert _norm_out(ret[1]) == b"Hello World!\n" def test_fail_hooks(store): config = { 'repo': 'local', 'hooks': [{ 'id': 'fail', 'name': 'fail', 'language': 'fail', 'entry': 'make sure to name changelogs as .rst!', 'files': r'changelog/.*(?