|
1 | 1 | import multiprocessing |
2 | | -import os |
| 2 | +import os.path |
3 | 3 | import sys |
4 | 4 | from unittest import mock |
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 |
|
8 | 8 | import pre_commit.constants as C |
| 9 | +from pre_commit import parse_shebang |
9 | 10 | from pre_commit.languages import helpers |
10 | 11 | from pre_commit.prefix import Prefix |
11 | 12 | from pre_commit.util import CalledProcessError |
12 | 13 | from testing.auto_namedtuple import auto_namedtuple |
13 | 14 |
|
14 | 15 |
|
| 16 | +@pytest.fixture |
| 17 | +def find_exe_mck(): |
| 18 | + with mock.patch.object(parse_shebang, 'find_executable') as mck: |
| 19 | + yield mck |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def homedir_mck(): |
| 24 | + def fake_expanduser(pth): |
| 25 | + assert pth == '~' |
| 26 | + return os.path.normpath('/home/me') |
| 27 | + |
| 28 | + with mock.patch.object(os.path, 'expanduser', fake_expanduser): |
| 29 | + yield |
| 30 | + |
| 31 | + |
| 32 | +def test_exe_exists_does_not_exist(find_exe_mck, homedir_mck): |
| 33 | + find_exe_mck.return_value = None |
| 34 | + assert helpers.exe_exists('ruby') is False |
| 35 | + |
| 36 | + |
| 37 | +def test_exe_exists_exists(find_exe_mck, homedir_mck): |
| 38 | + find_exe_mck.return_value = os.path.normpath('/usr/bin/ruby') |
| 39 | + assert helpers.exe_exists('ruby') is True |
| 40 | + |
| 41 | + |
| 42 | +def test_exe_exists_false_if_shim(find_exe_mck, homedir_mck): |
| 43 | + find_exe_mck.return_value = os.path.normpath('/foo/shims/ruby') |
| 44 | + assert helpers.exe_exists('ruby') is False |
| 45 | + |
| 46 | + |
| 47 | +def test_exe_exists_false_if_homedir(find_exe_mck, homedir_mck): |
| 48 | + find_exe_mck.return_value = os.path.normpath('/home/me/somedir/ruby') |
| 49 | + assert helpers.exe_exists('ruby') is False |
| 50 | + |
| 51 | + |
| 52 | +def test_exe_exists_commonpath_raises_ValueError(find_exe_mck, homedir_mck): |
| 53 | + find_exe_mck.return_value = os.path.normpath('/usr/bin/ruby') |
| 54 | + with mock.patch.object(os.path, 'commonpath', side_effect=ValueError): |
| 55 | + assert helpers.exe_exists('ruby') is True |
| 56 | + |
| 57 | + |
15 | 58 | def test_basic_get_default_version(): |
16 | 59 | assert helpers.basic_get_default_version() == C.DEFAULT |
17 | 60 |
|
|
0 commit comments