|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import unicode_literals |
| 3 | + |
| 4 | +import contextlib |
| 5 | +import distutils.spawn |
| 6 | +import io |
| 7 | +import os |
| 8 | +import sys |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from pre_commit import parse_shebang |
| 13 | +from pre_commit.envcontext import envcontext |
| 14 | +from pre_commit.envcontext import Var |
| 15 | +from pre_commit.util import make_executable |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.parametrize( |
| 19 | + ('s', 'expected'), |
| 20 | + ( |
| 21 | + (b'', ()), |
| 22 | + (b'#!/usr/bin/python', ('/usr/bin/python',)), |
| 23 | + (b'#!/usr/bin/env python', ('python',)), |
| 24 | + (b'#! /usr/bin/python', ('/usr/bin/python',)), |
| 25 | + (b'#!/usr/bin/foo python', ('/usr/bin/foo', 'python')), |
| 26 | + (b'\xf9\x93\x01\x42\xcd', ()), |
| 27 | + (b'#!\xf9\x93\x01\x42\xcd', ()), |
| 28 | + (b'#!\x00\x00\x00\x00', ()), |
| 29 | + ), |
| 30 | +) |
| 31 | +def test_parse_bytesio(s, expected): |
| 32 | + assert parse_shebang.parse_bytesio(io.BytesIO(s)) == expected |
| 33 | + |
| 34 | + |
| 35 | +def test_file_doesnt_exist(): |
| 36 | + assert parse_shebang.parse_filename('herp derp derp') == () |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.xfail( |
| 40 | + sys.platform == 'win32', reason='Windows says everything is X_OK', |
| 41 | +) |
| 42 | +def test_file_not_executable(tmpdir): |
| 43 | + x = tmpdir.join('f') |
| 44 | + x.write_text('#!/usr/bin/env python', encoding='UTF-8') |
| 45 | + assert parse_shebang.parse_filename(x.strpath) == () |
| 46 | + |
| 47 | + |
| 48 | +def test_simple_case(tmpdir): |
| 49 | + x = tmpdir.join('f') |
| 50 | + x.write_text('#!/usr/bin/env python', encoding='UTF-8') |
| 51 | + make_executable(x.strpath) |
| 52 | + assert parse_shebang.parse_filename(x.strpath) == ('python',) |
| 53 | + |
| 54 | + |
| 55 | +def test_find_executable_full_path(): |
| 56 | + assert parse_shebang.find_executable(sys.executable) == sys.executable |
| 57 | + |
| 58 | + |
| 59 | +def test_find_executable_on_path(): |
| 60 | + expected = distutils.spawn.find_executable('echo') |
| 61 | + assert parse_shebang.find_executable('echo') == expected |
| 62 | + |
| 63 | + |
| 64 | +def test_find_executable_not_found_none(): |
| 65 | + assert parse_shebang.find_executable('not-a-real-executable') is None |
| 66 | + |
| 67 | + |
| 68 | +def write_executable(shebang, filename='run'): |
| 69 | + os.mkdir('bin') |
| 70 | + path = os.path.join('bin', filename) |
| 71 | + with io.open(path, 'w') as f: |
| 72 | + f.write('#!{0}'.format(shebang)) |
| 73 | + make_executable(path) |
| 74 | + return path |
| 75 | + |
| 76 | + |
| 77 | +@contextlib.contextmanager |
| 78 | +def bin_on_path(): |
| 79 | + bindir = os.path.join(os.getcwd(), 'bin') |
| 80 | + with envcontext((('PATH', (bindir, os.pathsep, Var('PATH'))),)): |
| 81 | + yield |
| 82 | + |
| 83 | + |
| 84 | +def test_find_executable_path_added(in_tmpdir): |
| 85 | + path = os.path.abspath(write_executable('/usr/bin/env sh')) |
| 86 | + assert parse_shebang.find_executable('run') is None |
| 87 | + with bin_on_path(): |
| 88 | + assert parse_shebang.find_executable('run') == path |
| 89 | + |
| 90 | + |
| 91 | +def test_find_executable_path_ext(in_tmpdir): |
| 92 | + """Windows exports PATHEXT as a list of extensions to automatically add |
| 93 | + to executables when doing PATH searching. |
| 94 | + """ |
| 95 | + exe_path = os.path.abspath(write_executable( |
| 96 | + '/usr/bin/env sh', filename='run.myext', |
| 97 | + )) |
| 98 | + env_path = {'PATH': os.path.dirname(exe_path)} |
| 99 | + env_path_ext = dict(env_path, PATHEXT=os.pathsep.join(('.exe', '.myext'))) |
| 100 | + assert parse_shebang.find_executable('run') is None |
| 101 | + assert parse_shebang.find_executable('run', _environ=env_path) is None |
| 102 | + ret = parse_shebang.find_executable('run.myext', _environ=env_path) |
| 103 | + assert ret == exe_path |
| 104 | + ret = parse_shebang.find_executable('run', _environ=env_path_ext) |
| 105 | + assert ret == exe_path |
| 106 | + |
| 107 | + |
| 108 | +def test_normexe_does_not_exist(): |
| 109 | + with pytest.raises(OSError) as excinfo: |
| 110 | + parse_shebang.normexe('i-dont-exist-lol') |
| 111 | + assert excinfo.value.args == ('Executable i-dont-exist-lol not found',) |
| 112 | + |
| 113 | + |
| 114 | +def test_normexe_already_full_path(): |
| 115 | + assert parse_shebang.normexe(sys.executable) == sys.executable |
| 116 | + |
| 117 | + |
| 118 | +def test_normexe_gives_full_path(): |
| 119 | + expected = distutils.spawn.find_executable('echo') |
| 120 | + assert parse_shebang.normexe('echo') == expected |
| 121 | + assert os.sep in expected |
| 122 | + |
| 123 | + |
| 124 | +def test_normalize_cmd_trivial(): |
| 125 | + cmd = (distutils.spawn.find_executable('echo'), 'hi') |
| 126 | + assert parse_shebang.normalize_cmd(cmd) == cmd |
| 127 | + |
| 128 | + |
| 129 | +def test_normalize_cmd_PATH(): |
| 130 | + cmd = ('python', '--version') |
| 131 | + expected = (distutils.spawn.find_executable('python'), '--version') |
| 132 | + assert parse_shebang.normalize_cmd(cmd) == expected |
| 133 | + |
| 134 | + |
| 135 | +def test_normalize_cmd_shebang(in_tmpdir): |
| 136 | + python = distutils.spawn.find_executable('python') |
| 137 | + path = write_executable(python.replace(os.sep, '/')) |
| 138 | + assert parse_shebang.normalize_cmd((path,)) == (python, path) |
| 139 | + |
| 140 | + |
| 141 | +def test_normalize_cmd_PATH_shebang_full_path(in_tmpdir): |
| 142 | + python = distutils.spawn.find_executable('python') |
| 143 | + path = write_executable(python.replace(os.sep, '/')) |
| 144 | + with bin_on_path(): |
| 145 | + ret = parse_shebang.normalize_cmd(('run',)) |
| 146 | + assert ret == (python, os.path.abspath(path)) |
| 147 | + |
| 148 | + |
| 149 | +def test_normalize_cmd_PATH_shebang_PATH(in_tmpdir): |
| 150 | + python = distutils.spawn.find_executable('python') |
| 151 | + path = write_executable('/usr/bin/env python') |
| 152 | + with bin_on_path(): |
| 153 | + ret = parse_shebang.normalize_cmd(('run',)) |
| 154 | + assert ret == (python, os.path.abspath(path)) |
0 commit comments