Merge pull request #225 from untitaker/test_with_colors
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add color parameter to testrunner
authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents a9b0cbc, 770e1e9 · Browse files · 2 files·+49 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+49 added−3 removed
- tests:+27 added−0 removed
- other:+22 added−3 removed
Provenance
- Commit
4a7fe69- Tree
3f02c92- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent 1
a9b0cbc- Parent 2
770e1e9
2 files changed+49 added−3 removed
modified:click/testing.py+22 added-3 removed
133 unmodified lines134 rv.update(overrides)135 return rv136 137 @contextlib.contextmanager138- def isolation(self, input=None, env=None):138+ def isolation(self, input=None, env=None, color=False):139 """A context manager that sets up the isolation for invoking of a140 command line tool. This sets up stdin with the given input data141 and `os.environ` with the overrides from the given dictionary.142 This also rebinds some internals in Click to be mocked (like the143 prompt functionality).144 145 This is automatically done in the :meth:`invoke` method.146 147+ .. versionadded:: 4.0148+ The ``color`` parameter was added.149+150 :param input: the input stream to put into sys.stdin.151 :param env: the environment overrides as dictionary.152+ :param color: whether the output should contain color codes. The153+ application can still override this explicitly.154 """155 input = make_input_stream(input, self.charset)156 157 old_stdin = sys.stdin34 unmodified lines192 sys.stdout.write(char)193 sys.stdout.flush()194 return char195 196+ default_color = color197+ def should_strip_ansi(stream=None, color=None):198+ if color is None:199+ return not default_color200+ return not color201+202 old_visible_prompt_func = clickpkg.termui.visible_prompt_func203 old_hidden_prompt_func = clickpkg.termui.hidden_prompt_func204 old__getchar_func = clickpkg.termui._getchar205+ old_should_strip_ansi = clickpkg.utils.should_strip_ansi206 clickpkg.termui.visible_prompt_func = visible_input207 clickpkg.termui.hidden_prompt_func = hidden_input208 clickpkg.termui._getchar = _getchar209+ clickpkg.utils.should_strip_ansi = should_strip_ansi210 211 old_env = {}212 try:213 for key, value in iteritems(env):20 unmodified lines234 sys.stdin = old_stdin235 clickpkg.termui.visible_prompt_func = old_visible_prompt_func236 clickpkg.termui.hidden_prompt_func = old_hidden_prompt_func237 clickpkg.termui._getchar = old__getchar_func238+ clickpkg.utils.should_strip_ansi = old_should_strip_ansi239 240 def invoke(self, cli, args=None, input=None, env=None,227- catch_exceptions=True, **extra):241+ catch_exceptions=True, color=False, **extra):242 """Invokes a command in an isolated environment. The arguments are243 forwarded directly to the command line script, the `extra` keyword244 arguments are passed to the :meth:`~clickpkg.Command.main` function of245 the command.6 unmodified lines252 .. versionchanged:: 3.0253 The result object now has an `exc_info` attribute with the254 traceback if available.255 256+ .. versionadded:: 4.0257+ The ``color`` parameter was added.258+259 :param cli: the command to invoke260 :param args: the arguments to invoke261 :param input: the input data for `sys.stdin`.262 :param env: the environment overrides.263 :param catch_exceptions: Whether to catch any other exceptions than264 ``SystemExit``.265 :param extra: the keyword arguments to pass to :meth:`main`.266+ :param color: whether the output should contain color codes. The267+ application can still override this explicitly.268 """269 exc_info = None251- with self.isolation(input=input, env=env) as out:270+ with self.isolation(input=input, env=env, color=color) as out:271 exception = None272 exit_code = 0273 274 try:39 unmodified linesmodified:tests/test_testing.py+27 added
111 unmodified lines112 CustomError = SystemExit113 114 result = runner.invoke(cli)115 assert result.exit_code == 1116+117+118+def test_with_color():119+ @click.command()120+ def cli():121+ click.secho('hello world', fg='blue')122+123+ runner = CliRunner()124+125+ result = runner.invoke(cli)126+ assert result.output == 'hello world\n'127+ assert not result.exception128+129+ result = runner.invoke(cli, color=True)130+ assert result.output == click.style('hello world', fg='blue') + '\n'131+ assert not result.exception132+133+134+def test_with_color_but_pause_not_blocking():135+ @click.command()136+ def cli():137+ click.pause()138+139+ runner = CliRunner()140+ result = runner.invoke(cli, color=True)141+ assert not result.exception142+ assert result.output == ''