| 1 |
import os |
| 2 |
import sys |
| 3 |
from pathlib import Path |
| 4 |
|
| 5 |
import pytest |
| 6 |
from _util import only_utf8 |
| 7 |
from pytest_relaxed import trap |
| 8 |
|
| 9 |
from invoke import __version__, run |
| 10 |
from invoke.terminals import WINDOWS |
| 11 |
|
| 12 |
|
| 13 |
def _output_eq(cmd: str, expected: str) -> None: |
| 14 |
assert run(cmd, hide=True).stdout.strip() == expected |
| 15 |
|
| 16 |
|
| 17 |
class Main: |
| 18 |
def setup_method(self) -> None: |
| 19 |
self.cwd = os.getcwd() |
| 20 |
# Enter integration/_support as all support files are in there now |
| 21 |
os.chdir(Path(__file__).parent / "_support") |
| 22 |
|
| 23 |
def teardown_method(self) -> None: |
| 24 |
os.chdir(self.cwd) |
| 25 |
|
| 26 |
class basics: |
| 27 |
@trap |
| 28 |
def basic_invocation(self) -> None: |
| 29 |
_output_eq("invoke print-foo", "foo") |
| 30 |
|
| 31 |
@trap |
| 32 |
def version_output(self) -> None: |
| 33 |
_output_eq("invoke --version", "Invoke {}".format(__version__)) |
| 34 |
|
| 35 |
@trap |
| 36 |
def help_output(self) -> None: |
| 37 |
assert "Usage: inv[oke] " in run("invoke --help").stdout |
| 38 |
|
| 39 |
@trap |
| 40 |
def per_task_help(self) -> None: |
| 41 |
assert "Frobazz" in run("invoke -c _explicit foo --help").stdout |
| 42 |
|
| 43 |
@trap |
| 44 |
def shorthand_binary_name(self) -> None: |
| 45 |
_output_eq("inv print-foo", "foo") |
| 46 |
|
| 47 |
@trap |
| 48 |
def explicit_task_module(self) -> None: |
| 49 |
_output_eq("inv --collection _explicit foo", "Yup") |
| 50 |
|
| 51 |
@trap |
| 52 |
def invocation_with_args(self) -> None: |
| 53 |
_output_eq("inv print-name --name whatevs", "whatevs") |
| 54 |
|
| 55 |
@trap |
| 56 |
def bad_collection_exits_nonzero(self) -> None: |
| 57 |
result = run("inv -c nope -l", warn=True) |
| 58 |
assert result.exited == 1 |
| 59 |
assert not result.stdout |
| 60 |
assert result.stderr |
| 61 |
|
| 62 |
@trap |
| 63 |
def package_style_collections_internally_importable(self) -> None: |
| 64 |
# After merging #919 blew this up and unit tests did not detect! |
| 65 |
result = run("cd package && inv -l") |
| 66 |
assert "mytask" in result.stdout |
| 67 |
|
| 68 |
def loads_real_user_config(self) -> None: |
| 69 |
path = os.path.expanduser("~/.invoke.yaml") |
| 70 |
try: |
| 71 |
with open(path, "w") as fd: |
| 72 |
fd.write("foo: bar") |
| 73 |
_output_eq("inv print-config", "bar") |
| 74 |
finally: |
| 75 |
try: |
| 76 |
os.unlink(path) |
| 77 |
except OSError: |
| 78 |
pass |
| 79 |
|
| 80 |
@trap |
| 81 |
def invocable_via_python_dash_m(self) -> None: |
| 82 |
_output_eq( |
| 83 |
"python -m invoke print-name --name mainline", "mainline" |
| 84 |
) |
| 85 |
|
| 86 |
class funky_characters_in_stdout: |
| 87 |
@only_utf8 |
| 88 |
def basic_nonstandard_characters(self) -> None: |
| 89 |
# Crummy "doesn't explode with decode errors" test |
| 90 |
cmd = ("type" if WINDOWS else "cat") + " tree.out" |
| 91 |
run(cmd, hide="stderr") |
| 92 |
|
| 93 |
@only_utf8 |
| 94 |
def nonprinting_bytes(self) -> None: |
| 95 |
# Seriously non-printing characters (i.e. non UTF8) also don't |
| 96 |
# asplode (they would print as escapes normally, but still) |
| 97 |
run("echo '\xff'", hide="stderr") |
| 98 |
|
| 99 |
@only_utf8 |
| 100 |
def nonprinting_bytes_pty(self) -> None: |
| 101 |
if WINDOWS: |
| 102 |
return |
| 103 |
# PTY use adds another utf-8 decode spot which can also fail. |
| 104 |
run("echo '\xff'", pty=True, hide="stderr") |
| 105 |
|
| 106 |
class ptys: |
| 107 |
def complex_nesting_under_ptys_doesnt_break(self) -> None: |
| 108 |
if WINDOWS: # Not sure how to make this work on Windows |
| 109 |
return |
| 110 |
# GH issue 191 |
| 111 |
substr = " hello\t\t\nworld with spaces" |
| 112 |
cmd = """ eval 'echo "{}" ' """.format(substr) |
| 113 |
expected = " hello\t\t\r\nworld with spaces\r\n" |
| 114 |
assert run(cmd, pty=True, hide="both").stdout == expected |
| 115 |
|
| 116 |
def pty_puts_both_streams_in_stdout(self) -> None: |
| 117 |
if WINDOWS: |
| 118 |
return |
| 119 |
err_echo = "{} err.py".format(sys.executable) |
| 120 |
command = "echo foo && {} bar".format(err_echo) |
| 121 |
r = run(command, hide="both", pty=True) |
| 122 |
assert r.stdout == "foo\r\nbar\r\n" |
| 123 |
assert r.stderr == "" |
| 124 |
|
| 125 |
def simple_command_with_pty(self) -> None: |
| 126 |
""" |
| 127 |
Run command under PTY |
| 128 |
""" |
| 129 |
# Most Unix systems should have stty, which asplodes when not run |
| 130 |
# under a pty, and prints useful info otherwise |
| 131 |
result = run("stty -a", hide=True, pty=True) |
| 132 |
# PTYs use \r\n, not \n, line separation |
| 133 |
assert "\r\n" in result.stdout |
| 134 |
assert result.pty is True |
| 135 |
|
| 136 |
@pytest.mark.skip(reason="CircleCI env actually does have 0x0 stty") |
| 137 |
def pty_size_is_realistic(self) -> None: |
| 138 |
# When we don't explicitly set pty size, 'stty size' sees it as |
| 139 |
# 0x0. |
| 140 |
# When we do set it, it should be some non 0x0, non 80x24 (the |
| 141 |
# default) value. (yes, this means it fails if you really do have |
| 142 |
# an 80x24 terminal. but who does that?) |
| 143 |
size = run("stty size", hide=True, pty=True).stdout.strip() |
| 144 |
assert size != "" |
| 145 |
assert size != "0 0" |
| 146 |
assert size != "24 80" |
| 147 |
|
| 148 |
class parsing: |
| 149 |
def false_as_optional_arg_default_value_works_okay(self) -> None: |
| 150 |
# (Dis)proves #416. When bug present, parser gets very confused, |
| 151 |
# asks "what the hell is 'whee'?". See also a unit test for |
| 152 |
# Task.get_arguments. |
| 153 |
for argstr, expected in ( |
| 154 |
("", "False"), |
| 155 |
("--meh", "True"), |
| 156 |
("--meh=whee", "whee"), |
| 157 |
): |
| 158 |
_output_eq("inv -c parsing foo {}".format(argstr), expected) |
| 159 |
|
| 160 |
@pytest.mark.parametrize( |
| 161 |
"argstr, expected", |
| 162 |
[ |
| 163 |
# No 'optional' bool flag |
| 164 |
( |
| 165 |
"val -- extra1 --extra2 -e3", |
| 166 |
"known='val', optional=False, remainder='extra1 --extra2 -e3'", # noqa |
| 167 |
), |
| 168 |
# Bool flag |
| 169 |
( |
| 170 |
"val --optional -- extra1 --extra2 -e3", |
| 171 |
"known='val', optional=True, remainder='extra1 --extra2 -e3'", # noqa |
| 172 |
), |
| 173 |
# Explicit inverse bool flag, also tweak 'extra' order for fun |
| 174 |
( |
| 175 |
"val -- --extra2 extra1 -e3", |
| 176 |
"known='val', optional=False, remainder='--extra2 extra1 -e3'", # noqa |
| 177 |
), |
| 178 |
], |
| 179 |
) |
| 180 |
def tasks_can_access_the_parser_remainder_value( |
| 181 |
self, argstr: str, expected: str |
| 182 |
) -> None: |
| 183 |
# I.e., GH issue #250. |
| 184 |
_output_eq(f"inv -c parsing use-remainder {argstr}", expected) |