Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 097bba6

Browse files
authored
Merge pull request #674 from pre-commit/simpler_cross_version_tests
Simplify cross version tests
2 parents c5030c8 + 753979d commit 097bba6

7 files changed

Lines changed: 27 additions & 11 deletions

File tree

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ matrix:
1616
install: pip install coveralls tox
1717
script: tox
1818
before_install:
19-
# work around https://github.com/travis-ci/travis-ci/issues/8363
20-
- which python3.5 || (pyenv install 3.5.4 && pyenv global system 3.5.4)
2119
- git --version
2220
- |
2321
if [ "$LATEST_GIT" = "1" ]; then

CONTRIBUTING.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
- The complete test suite depends on having at least the following installed (possibly not
66
a complete list)
77
- git (A sufficiently newer version is required to run pre-push tests)
8-
- python
9-
- python3.4 (Required by a test which checks different python versions)
10-
- python3.5 (Required by a test which checks different python versions)
8+
- python2 (Required by a test which checks different python versions)
9+
- python3 (Required by a test which checks different python versions)
1110
- tox (or virtualenv)
1211
- ruby + gem
1312
- docker

pre_commit/languages/python.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pre_commit.envcontext import Var
1010
from pre_commit.languages import helpers
1111
from pre_commit.parse_shebang import find_executable
12+
from pre_commit.util import CalledProcessError
1213
from pre_commit.util import clean_path_on_failure
1314
from pre_commit.util import cmd_output
1415
from pre_commit.xargs import xargs
@@ -40,6 +41,17 @@ def in_env(repo_cmd_runner, language_version):
4041
yield
4142

4243

44+
def _find_by_py_launcher(version): # pragma: no cover (windows only)
45+
if version.startswith('python'):
46+
try:
47+
return cmd_output(
48+
'py', '-{}'.format(version[len('python'):]),
49+
'-c', 'import sys; print(sys.executable)',
50+
)[1].strip()
51+
except CalledProcessError:
52+
pass
53+
54+
4355
def _get_default_version(): # pragma: no cover (platform dependent)
4456
def _norm(path):
4557
_, exe = os.path.split(path.lower())
@@ -66,6 +78,9 @@ def _norm(path):
6678
if find_executable(exe):
6779
return exe
6880

81+
if _find_by_py_launcher(exe):
82+
return exe
83+
6984
# Give a best-effort try for windows
7085
if os.path.exists(r'C:\{}\python.exe'.format(exe.replace('.', ''))):
7186
return exe
@@ -99,6 +114,10 @@ def norm_version(version):
99114
if version_exec and version_exec != version:
100115
return version_exec
101116

117+
version_exec = _find_by_py_launcher(version)
118+
if version_exec:
119+
return version_exec
120+
102121
# If it is in the form pythonx.x search in the default
103122
# place on windows
104123
if version.startswith('python'):

testing/resources/arbitrary_bytes_repo/.pre-commit-hooks.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
name: Python 3 Hook
33
entry: python3-hook
44
language: python
5-
language_version: python3.5
5+
language_version: python3
66
files: \.py$

testing/resources/python3_hooks_repo/.pre-commit-hooks.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
name: Python 3 Hook
33
entry: python3-hook
44
language: python
5-
language_version: python3.5
5+
language_version: python3
66
files: \.py$

testing/resources/python3_hooks_repo/python3_hook/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
def func():
7-
print('{}.{}'.format(*sys.version_info[:2]))
7+
print(sys.version_info[0])
88
print(repr(sys.argv[1:]))
99
print('Hello World')
1010
return 0

tests/repository_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ def run_on_version(version, expected_output):
130130
assert ret[0] == 0
131131
assert _norm_out(ret[1]) == expected_output
132132

133-
run_on_version('python3.4', b'3.4\n[]\nHello World\n')
134-
run_on_version('python3.5', b'3.5\n[]\nHello World\n')
133+
run_on_version('python2', b'2\n[]\nHello World\n')
134+
run_on_version('python3', b'3\n[]\nHello World\n')
135135

136136

137137
@pytest.mark.integration
@@ -140,7 +140,7 @@ def test_versioned_python_hook(tempdir_factory, store):
140140
tempdir_factory, store, 'python3_hooks_repo',
141141
'python3-hook',
142142
[os.devnull],
143-
b"3.5\n['" + five.to_bytes(os.devnull) + b"']\nHello World\n",
143+
b"3\n['" + five.to_bytes(os.devnull) + b"']\nHello World\n",
144144
)
145145

146146

0 commit comments

Comments
 (0)