From 248930f6dcdaba91561bca59ea4b31ee27273ee7 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 23 Nov 2015 20:10:08 -0800 Subject: [PATCH] Fix appveyor and windows. Resolves #293 --- appveyor.yml | 5 +++-- pre_commit/languages/python.py | 19 +++++++++++-------- pre_commit/output.py | 15 ++++++++------- tests/commands/install_uninstall_test.py | 15 +++++++++++++-- tests/languages/python_test.py | 18 ++++++++++++++++++ tests/repository_test.py | 20 ++++++-------------- tox.ini | 2 +- 7 files changed, 60 insertions(+), 34 deletions(-) create mode 100644 tests/languages/python_test.py diff --git a/appveyor.yml b/appveyor.yml index d9e3b8b6..3506ea1b 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -7,6 +7,8 @@ install: - "SET PATH=C:\\Python34;C:\\Python34\\Scripts;%PATH%" - pip install tox - pip install virtualenv --upgrade + - "mkdir -p C:\\Temp" + - "SET TMPDIR=C:\\Temp" # Not a C# project build: false @@ -15,5 +17,4 @@ before_test: - git config --global user.name "AppVeyor CI" - git config --global user.email "user@example.com" -# Workaround for http://help.appveyor.com/discussions/problems/1531-having-issues-with-configured-git-bash -test_script: bash -c tox +test_script: tox diff --git a/pre_commit/languages/python.py b/pre_commit/languages/python.py index 7bcaaf4e..0ee0c04b 100644 --- a/pre_commit/languages/python.py +++ b/pre_commit/languages/python.py @@ -31,15 +31,18 @@ def in_env(repo_cmd_runner, language_version): def norm_version(version): - version = os.path.expanduser(version) if os.name == 'nt': # pragma: no cover (windows) - if not distutils.spawn.find_executable(version): - # expanduser introduces a leading slash - version = version.strip('\\') - # The default place for python on windows is: - # C:\PythonXX\python.exe - version = r'C:\{0}\python.exe'.format(version.replace('.', '')) - return version + # Try looking up by name + if distutils.spawn.find_executable(version): + return version + + # If it is in the form pythonx.x search in the default + # place on windows + if version.startswith('python'): + return r'C:\{0}\python.exe'.format(version.replace('.', '')) + + # Otherwise assume it is a path + return os.path.expanduser(version) def install_environment( diff --git a/pre_commit/output.py b/pre_commit/output.py index 0cb9d489..84697ec0 100644 --- a/pre_commit/output.py +++ b/pre_commit/output.py @@ -12,13 +12,14 @@ from pre_commit import five try: if not os.environ.get('TERM'): # pragma: no cover (dumb terminal) raise OSError('Cannot determine width without TERM') - COLS = int( - subprocess.Popen( - ('tput', 'cols'), stdout=subprocess.PIPE, - ).communicate()[0] or - # Default in the case of no terminal - 80 - ) + else: # pragma no cover (windows) + COLS = int( + subprocess.Popen( + ('tput', 'cols'), stdout=subprocess.PIPE, + ).communicate()[0] or + # Default in the case of no terminal + 80 + ) except OSError: # pragma: no cover (windows) COLS = 80 diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py index b596df77..84c4606a 100644 --- a/tests/commands/install_uninstall_test.py +++ b/tests/commands/install_uninstall_test.py @@ -90,7 +90,9 @@ def test_install_hooks_directory_not_present(tempdir_factory): @xfailif_no_symlink -def test_install_hooks_dead_symlink(tempdir_factory): +def test_install_hooks_dead_symlink( + tempdir_factory, +): # pragma: no cover (non-windows) path = git_dir(tempdir_factory) os.symlink('/fake/baz', os.path.join(path, '.git', 'hooks', 'pre-commit')) runner = Runner(path) @@ -175,6 +177,14 @@ def test_install_idempotent(tempdir_factory): assert NORMAL_PRE_COMMIT_RUN.match(output) +def _path_without_us(): + # Choose a path which *probably* doesn't include us + return os.pathsep.join([ + x for x in os.environ['PATH'].split(os.pathsep) + if x.lower() != os.path.dirname(sys.executable).lower() + ]) + + def test_environment_not_sourced(tempdir_factory): path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') with cwd(path): @@ -193,7 +203,7 @@ def test_environment_not_sourced(tempdir_factory): ) ret, stdout, stderr = cmd_output( 'git', 'commit', '--allow-empty', '-m', 'foo', - env={'HOME': homedir}, + env={'HOME': homedir, 'PATH': _path_without_us()}, retcode=None, ) assert ret == 1 @@ -422,6 +432,7 @@ def test_installed_from_venv(tempdir_factory): tempdir_factory, env_base={ 'HOME': os.path.expanduser('~'), + 'PATH': _path_without_us(), 'TERM': os.environ.get('TERM', ''), # Windows needs this to import `random` 'SYSTEMROOT': os.environ.get('SYSTEMROOT', ''), diff --git a/tests/languages/python_test.py b/tests/languages/python_test.py new file mode 100644 index 00000000..8715b690 --- /dev/null +++ b/tests/languages/python_test.py @@ -0,0 +1,18 @@ +from __future__ import absolute_import +from __future__ import unicode_literals + +import os.path + +from pre_commit.languages import python + + +def test_norm_version_expanduser(): + home = os.path.expanduser('~') + if os.name == 'nt': # pragma: no cover (nt) + path = r'~\python343' + expected_path = r'{0}\python343'.format(home) + else: # pragma: no cover (non-nt) + path = '~/.pyenv/versions/3.4.3/bin/python' + expected_path = home + '/.pyenv/versions/3.4.3/bin/python' + result = python.norm_version(path) + assert result == expected_path diff --git a/tests/repository_test.py b/tests/repository_test.py index 6163194b..c6aa1d7b 100644 --- a/tests/repository_test.py +++ b/tests/repository_test.py @@ -342,7 +342,9 @@ def test_additional_python_dependencies_installed(tempdir_factory, store): @xfailif_windows_no_ruby @pytest.mark.integration -def test_additional_ruby_dependencies_installed(tempdir_factory, store): +def test_additional_ruby_dependencies_installed( + tempdir_factory, store, +): # pragma: no cover (non-windows) path = make_repo(tempdir_factory, 'ruby_hooks_repo') config = make_config_from_repo(path) config['hooks'][0]['additional_dependencies'] = ['thread_safe'] @@ -355,7 +357,9 @@ def test_additional_ruby_dependencies_installed(tempdir_factory, store): @xfailif_windows_no_node @pytest.mark.integration -def test_additional_node_dependencies_installed(tempdir_factory, store): +def test_additional_node_dependencies_installed( + tempdir_factory, store, +): # pragma: no cover (non-windows) 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 @@ -481,15 +485,3 @@ def test_local_repository(): with pytest.raises(NotImplementedError): local_repo.manifest assert len(local_repo.hooks) == 1 - - -def test_norm_version_expanduser(): # pragma: no cover - home = os.path.expanduser('~') - if os.name == 'nt': - path = r'~\python343' - expected_path = r'C:{0}\python343\python.exe'.format(home) - else: - path = '~/.pyenv/versions/3.4.3/bin/python' - expected_path = home + '/.pyenv/versions/3.4.3/bin/python' - result = python.norm_version(path) - assert result == expected_path diff --git a/tox.ini b/tox.ini index 33620826..b5e89146 100644 --- a/tox.ini +++ b/tox.ini @@ -5,7 +5,7 @@ envlist = py26,py27,py33,py34,pypy [testenv] deps = -rrequirements-dev.txt -passenv = HOME HOMEPATH TERM +passenv = HOME HOMEPATH PROGRAMDATA TERM commands = coverage erase coverage run -m pytest {posargs:tests}