From f88e007f52ca985fbaa63320a21557e20ee12eeb Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 21 May 2018 21:38:36 -0700 Subject: [PATCH] Fix test since pip 10 changed output --- .../.pre-commit-hooks.yaml | 6 ----- .../resources/not_installable_repo/setup.py | 17 ------------- tests/commands/run_test.py | 25 ------------------- tests/languages/helpers_test.py | 18 +++++++++++++ 4 files changed, 18 insertions(+), 48 deletions(-) delete mode 100644 testing/resources/not_installable_repo/.pre-commit-hooks.yaml delete mode 100644 testing/resources/not_installable_repo/setup.py diff --git a/testing/resources/not_installable_repo/.pre-commit-hooks.yaml b/testing/resources/not_installable_repo/.pre-commit-hooks.yaml deleted file mode 100644 index 48c1f9ef..00000000 --- a/testing/resources/not_installable_repo/.pre-commit-hooks.yaml +++ /dev/null @@ -1,6 +0,0 @@ -- id: foo - name: Foo - entry: foo - language: python - language_version: python2.7 - files: \.py$ diff --git a/testing/resources/not_installable_repo/setup.py b/testing/resources/not_installable_repo/setup.py deleted file mode 100644 index ae5f6338..00000000 --- a/testing/resources/not_installable_repo/setup.py +++ /dev/null @@ -1,17 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import print_function -from __future__ import unicode_literals - -import sys - - -def main(): - # Intentionally write mixed encoding to the output. This should not crash - # pre-commit and should write bytes to the output. - sys.stderr.write('☃'.encode('UTF-8') + '²'.encode('latin1') + b'\n') - # Return 1 to indicate failures - return 1 - - -if __name__ == '__main__': - exit(main()) diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index d664e801..cd32c5f6 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -479,31 +479,6 @@ def test_stdout_write_bug_py26( assert 'UnicodeDecodeError' not in stdout -def test_hook_install_failure(mock_out_store_directory, tempdir_factory): - git_path = make_consuming_repo(tempdir_factory, 'not_installable_repo') - with cwd(git_path): - install(Runner(git_path, C.CONFIG_FILE)) - - _, stdout, _ = cmd_output_mocked_pre_commit_home( - 'git', 'commit', '-m', 'Commit!', - # git commit puts pre-commit to stderr - stderr=subprocess.STDOUT, - retcode=None, - encoding=None, - tempdir_factory=tempdir_factory, - ) - assert b'UnicodeDecodeError' not in stdout - # Doesn't actually happen, but a reasonable assertion - assert b'UnicodeEncodeError' not in stdout - - # Sanity check our output - assert ( - b'An unexpected error has occurred: CalledProcessError: ' in - stdout - ) - assert '☃'.encode('UTF-8') + '²'.encode('latin1') in stdout - - def test_lots_of_files(mock_out_store_directory, tempdir_factory): # windows xargs seems to have a bug, here's a regression test for # our workaround diff --git a/tests/languages/helpers_test.py b/tests/languages/helpers_test.py index 7019e260..ada2095b 100644 --- a/tests/languages/helpers_test.py +++ b/tests/languages/helpers_test.py @@ -1,7 +1,13 @@ from __future__ import absolute_import from __future__ import unicode_literals +import sys + +import pytest + from pre_commit.languages import helpers +from pre_commit.prefix import Prefix +from pre_commit.util import CalledProcessError def test_basic_get_default_version(): @@ -10,3 +16,15 @@ def test_basic_get_default_version(): def test_basic_healthy(): assert helpers.basic_healthy(None, None) is True + + +def test_failed_setup_command_does_not_unicode_error(): + script = ( + 'import sys\n' + "getattr(sys.stderr, 'buffer', sys.stderr).write(b'\\x81\\xfe')\n" + 'exit(1)\n' + ) + + # an assertion that this does not raise `UnicodeError` + with pytest.raises(CalledProcessError): + helpers.run_setup_cmd(Prefix('.'), (sys.executable, '-c', script))