Skip to content
Myr

conftest.py

This file

Path

tests/conftest.py

158 lines · 4.4 KiB · Python

Last changed by

Added support for default overrides. This fixes #42Armin Ronacherbefore adoptione77783d

History of this file

Take it

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 1–3why, one step back
1import os
2import sys3import click

Added some tests for some optionsArmin Ronacherbefore adoption7360097

lines 4–5why, one step back
4import shutil
5import tempfile

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 6–9why, one step back
6import contextlib
78import pytest9

Made tests pass on 3.xArmin Ronacherbefore adoption14c9b1a

lines 10–16why, one step back
10PY2 = sys.version_info[0] == 2
11if PY2:12 from cStringIO import StringIO13 iteritems = lambda x: x.iteritems()14else:15 import io16 iteritems = lambda x: iter(x.items())

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

line 17why, one step back
17

Made tests pass on 3.xArmin Ronacherbefore adoption14c9b1a

line 18why, one step back
18

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 19–62why, one step back
19class EchoingStdin(object):
2021 def __init__(self, input, output):22 self._input = input23 self._output = output2425 def __getattr__(self, x):26 return getattr(self._input, x)2728 def _echo(self, rv):29 mark = False30 if rv.endswith('\xff'):31 rv = rv[:-1]32 mark = True33 self._output.write(rv)34 if mark:35 self._output.write('^D\n')36 return rv3738 def read(self, n=-1):39 return self._echo(self._input.read(n))4041 def readline(self, n=-1):42 return self._echo(self._input.readline(n))4344 def readlines(self):45 return [self._echo(x) for x in self._input.readlines()]4647 def __iter__(self):48 return iter(self._echo(x) for x in self._input)495051class Result(object):5253 def __init__(self, output, exit_code, exception):54 self.output = output55 self.exit_code = exit_code56 self.exception = exception575859class CliRunner(object):6061 @contextlib.contextmanager62 def isolation(self, input=None, env=None):

Made tests pass on 3.xArmin Ronacherbefore adoption14c9b1a

line 63why, one step back
63 if input is not None and not isinstance(input, bytes):

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 64–65why, one step back
64 input = input.encode('utf-8')
65

Made tests pass on 3.xArmin Ronacherbefore adoption14c9b1a

lines 66–82why, one step back
66 old_stdout = sys.stdout
67 old_stderr = sys.stderr6869 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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 83–94why, one step back
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')87 sys.stdout.flush()88 return val8990 def hidden_input(prompt=None):91 sys.stdout.write((prompt or '') + '\n')92 sys.stdout.flush()93 return input.readline().rstrip('\r\n')94

Added lazy file openingArmin Ronacherbefore adoptionf270dd2

lines 95–98why, one step back
95 old_visible_prompt_func = click.termui.visible_prompt_func
96 old_hidden_prompt_func = click.termui.hidden_prompt_func97 click.termui.visible_prompt_func = visible_input98 click.termui.hidden_prompt_func = hidden_input

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 99–102why, one step back
99
100 old_env = {}101 try:102 if env:

Made tests pass on 3.xArmin Ronacherbefore adoption14c9b1a

line 103why, one step back
103 for key, value in iteritems(env):

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 104–107why, one step back
104 old_env[key] = os.environ.get(value)
105 os.environ[key] = value106 yield output107 finally:

Made tests pass on 3.xArmin Ronacherbefore adoption14c9b1a

line 108why, one step back
108 for key, value in iteritems(old_env):

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 109–117why, one step back
109 if value is None:
110 try:111 del os.environ[key]112 except Exception:113 pass114 else:115 os.environ[key] = value116 sys.stdout = old_stdout117 sys.stderr = old_stderr

Added lazy file openingArmin Ronacherbefore adoptionf270dd2

lines 118–119why, one step back
118 click.termui.visible_prompt_func = old_visible_prompt_func
119 click.termui.hidden_prompt_func = old_hidden_prompt_func

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

line 120why, one step back
120

Added support for default overrides. This fixes #42Armin Ronacherbefore adoptione77783d

line 121why, one step back
121 def invoke(self, cli, args, **extra):

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 122–126why, one step back
122 with self.isolation() as out:
123 exception = None124 exit_code = 0125126 try:

Added support for default overrides. This fixes #42Armin Ronacherbefore adoptione77783d

lines 127–128why, one step back
127 cli.main(args=args, prog_name=cli.name or 'root',
128 **extra)

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 129–135why, one step back
129 except SystemExit as e:
130 if e.code != 0:131 exception = e132 exit_code = e.code133 except Exception as e:134 exception = e135 exit_code = -1

Added some tests for some optionsArmin Ronacherbefore adoption7360097

line 136why, one step back
136 output = out.getvalue().decode('utf-8').replace('\r\n', '\n')

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 137–139why, one step back
137
138 return Result(output=output, exit_code=exit_code,139 exception=exception)

Added some tests for some optionsArmin Ronacherbefore adoption7360097

lines 140–153why, one step back
140
141 @contextlib.contextmanager142 def isolated_filesystem(self):143 cwd = os.getcwd()144 t = tempfile.mkdtemp()145 os.chdir(t)146 try:147 yield148 finally:149 os.chdir(cwd)150 try:151 shutil.rmtree(t)152 except (OSError, IOError):153 pass

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 154–158why, one step back
154
155156@pytest.fixture(scope='function')157def runner(request):158 return CliRunner()