Made tests pass on 3.x
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 82d9b0c · Browse files · 3 files·+37 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+37 added−15 removed
- tests:+30 added−15 removed
- other:+7 added−0 removed
Provenance
- Commit
14c9b1a- Tree
9b1eb80- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
82d9b0c
3 files changed+37 added−15 removed
modified:tests/conftest.py+28 added-13 removed
3 unmodified lines4 import shutil5 import tempfile6 import contextlib7 8-from cStringIO import StringIO9-8 import pytest9 10+PY2 = sys.version_info[0] == 211+if PY2:12+ from cStringIO import StringIO13+ iteritems = lambda x: x.iteritems()14+else:15+ import io16+ iteritems = lambda x: iter(x.items())17 18+19 class EchoingStdin(object):20 21 def __init__(self, input, output):22 self._input = input36 unmodified lines59 class CliRunner(object):60 61 @contextlib.contextmanager62 def isolation(self, input=None, env=None):57- if isinstance(input, unicode):63+ if input is not None and not isinstance(input, bytes):64 input = input.encode('utf-8')59- input = StringIO(input or '')60- output = StringIO()61- sys.stdin = EchoingStdin(input, output)62- sys.stdin.encoding = 'utf-8'65 66+ old_stdout = sys.stdout67+ old_stderr = sys.stderr68+69+ if PY2:70+ input = StringIO(input or '')71+ output = StringIO()72+ sys.stdin = EchoingStdin(input, output)73+ sys.stdin.encoding = 'utf-8'74+ sys.stdout = sys.stderr = output75+ else:76+ real_input = io.BytesIO(input)77+ output = io.BytesIO()78+ input = io.TextIOWrapper(real_input, encoding='utf-8')79+ sys.stdin = EchoingStdin(real_input, output)80+ sys.stdout = sys.stderr = io.TextIOWrapper(output,81+ encoding='utf-8')82+83 def visible_input(prompt=None):84 sys.stdout.write(prompt or '')85 val = input.readline().rstrip('\r\n')86 sys.stdout.write(val + '\n')4 unmodified lines91 sys.stdout.write((prompt or '') + '\n')92 sys.stdout.flush()93 return input.readline().rstrip('\r\n')94 76- old_stdout = sys.stdout77- sys.stdout = output78- old_stderr = sys.stderr79- sys.stderr = output95 old_visible_prompt_func = click.helpers.visible_prompt_func96 old_hidden_prompt_func = click.helpers.hidden_prompt_func97 click.helpers.visible_prompt_func = visible_input98 click.helpers.hidden_prompt_func = hidden_input99 100 old_env = {}101 try:102 if env:88- for key, value in env.iteritems():103+ for key, value in iteritems(env):104 old_env[key] = os.environ.get(value)105 os.environ[key] = value106 yield output107 finally:93- for key, value in old_env.iteritems():108+ for key, value in iteritems(old_env):109 if value is None:110 try:111 del os.environ[key]112 except Exception:45 unmodified linesmodified:tests/test_basic.py+2 added-2 removed
145 unmodified lines146 147 148 def test_file_option(runner):149 @click.command()150- @click.option('--file', type=click.File('wb'))150+ @click.option('--file', type=click.File('w'))151 def input(file):152 file.write('Hello World!\n')153 154 @click.command()155- @click.option('--file', type=click.File('rb'))155+ @click.option('--file', type=click.File('r'))156 def output(file):157 click.echo(file.read())158 159 with runner.isolated_filesystem():7 unmodified linesmodified:LICENSE+7 added
1 Copyright (c) 2014 by Armin Ronacher.2 3+Click uses parts of optparse written by Gregory P. Ward and maintained by the4+Python software foundation. This is limited to code in the _optparse.py5+module:6+7+Copyright (c) 2001-2006 Gregory P. Ward. All rights reserved.8+Copyright (c) 2002-2006 Python Software Foundation. All rights reserved.9+10 Some rights reserved.11 12 Redistribution and use in source and binary forms, with or without13 modification, are permitted provided that the following conditions are25 unmodified lines