| 1 |
import json |
| 2 |
import os |
| 3 |
import sys |
| 4 |
from io import BytesIO |
| 5 |
from pathlib import Path |
| 6 |
from unittest.mock import ANY, Mock, patch |
| 7 |
|
| 8 |
import pytest |
| 9 |
from _util import ( |
| 10 |
ROOT, |
| 11 |
expect, |
| 12 |
load, |
| 13 |
run, |
| 14 |
skip_if_windows, |
| 15 |
support, |
| 16 |
support_file, |
| 17 |
support_path, |
| 18 |
) |
| 19 |
from pytest import skip |
| 20 |
from pytest_relaxed import trap |
| 21 |
|
| 22 |
from invoke import ( |
| 23 |
Argument, |
| 24 |
Collection, |
| 25 |
Config, |
| 26 |
Executor, |
| 27 |
Exit, |
| 28 |
FilesystemLoader, |
| 29 |
ParserContext, |
| 30 |
ParseResult, |
| 31 |
Program, |
| 32 |
Result, |
| 33 |
Task, |
| 34 |
UnexpectedExit, |
| 35 |
main, |
| 36 |
) |
| 37 |
from invoke.config import merge_dicts |
| 38 |
from invoke.util import Lexicon, cd |
| 39 |
|
| 40 |
pytestmark = pytest.mark.usefixtures("integration") |
| 41 |
|
| 42 |
|
| 43 |
class Program_: |
| 44 |
class init: |
| 45 |
"__init__" |
| 46 |
|
| 47 |
def may_specify_version(self): |
| 48 |
assert Program(version="1.2.3").version == "1.2.3" |
| 49 |
|
| 50 |
def default_version_is_unknown(self): |
| 51 |
assert Program().version == "unknown" |
| 52 |
|
| 53 |
def may_specify_namespace(self): |
| 54 |
foo = load("foo") |
| 55 |
assert Program(namespace=foo).namespace is foo |
| 56 |
|
| 57 |
def may_specify_name(self): |
| 58 |
assert Program(name="Myapp").name == "Myapp" |
| 59 |
|
| 60 |
def may_specify_binary(self): |
| 61 |
assert Program(binary="myapp").binary == "myapp" |
| 62 |
|
| 63 |
def loader_class_defaults_to_FilesystemLoader(self): |
| 64 |
assert Program().loader_class is FilesystemLoader |
| 65 |
|
| 66 |
def may_specify_loader_class(self): |
| 67 |
klass = object() |
| 68 |
assert Program(loader_class=klass).loader_class == klass |
| 69 |
|
| 70 |
def executor_class_defaults_to_Executor(self): |
| 71 |
assert Program().executor_class is Executor |
| 72 |
|
| 73 |
def may_specify_executor_class(self): |
| 74 |
klass = object() |
| 75 |
assert Program(executor_class=klass).executor_class == klass |
| 76 |
|
| 77 |
def config_class_defaults_to_Config(self): |
| 78 |
assert Program().config_class is Config |
| 79 |
|
| 80 |
def may_specify_config_class(self): |
| 81 |
klass = object() |
| 82 |
assert Program(config_class=klass).config_class == klass |
| 83 |
|
| 84 |
class miscellaneous: |
| 85 |
"miscellaneous behaviors" |
| 86 |
|
| 87 |
def debug_flag_activates_logging(self): |
| 88 |
# Have to patch our logger to get in before logcapture kicks in. |
| 89 |
with patch("invoke.util.debug") as debug: |
| 90 |
Program().run("invoke -d -c debugging foo") |
| 91 |
debug.assert_called_with("my-sentinel") |
| 92 |
|
| 93 |
def debug_honored_as_env_var_too(self, reset_environ): |
| 94 |
os.environ["INVOKE_DEBUG"] = "1" |
| 95 |
with patch("invoke.util.debug") as debug: |
| 96 |
# NOTE: no use of -d/--debug |
| 97 |
Program().run("invoke -c debugging foo") |
| 98 |
debug.assert_called_with("my-sentinel") |
| 99 |
|
| 100 |
def bytecode_skipped_by_default(self): |
| 101 |
expect("-c foo mytask") |
| 102 |
assert sys.dont_write_bytecode |
| 103 |
|
| 104 |
def write_pyc_explicitly_enables_bytecode_writing(self): |
| 105 |
expect("--write-pyc -c foo mytask") |
| 106 |
assert not sys.dont_write_bytecode |
| 107 |
|
| 108 |
class normalize_argv: |
| 109 |
@patch("invoke.program.sys") |
| 110 |
def defaults_to_sys_argv(self, mock_sys): |
| 111 |
argv = ["inv", "--version"] |
| 112 |
mock_sys.argv = argv |
| 113 |
p = Program() |
| 114 |
p.print_version = Mock() |
| 115 |
p.run(exit=False) |
| 116 |
p.print_version.assert_called() |
| 117 |
|
| 118 |
def uses_a_list_unaltered(self): |
| 119 |
p = Program() |
| 120 |
p.print_version = Mock() |
| 121 |
p.run(["inv", "--version"], exit=False) |
| 122 |
p.print_version.assert_called() |
| 123 |
|
| 124 |
def splits_a_string(self): |
| 125 |
p = Program() |
| 126 |
p.print_version = Mock() |
| 127 |
p.run("inv --version", exit=False) |
| 128 |
p.print_version.assert_called() |
| 129 |
|
| 130 |
class name: |
| 131 |
def defaults_to_capitalized_binary_when_None(self): |
| 132 |
expect("myapp --version", out="Myapp unknown\n", invoke=False) |
| 133 |
|
| 134 |
def benefits_from_binary_absolute_behavior(self): |
| 135 |
"benefits from binary()'s absolute path behavior" |
| 136 |
expect( |
| 137 |
"/usr/local/bin/myapp --version", |
| 138 |
out="Myapp unknown\n", |
| 139 |
invoke=False, |
| 140 |
) |
| 141 |
|
| 142 |
def uses_overridden_value_when_given(self): |
| 143 |
p = Program(name="NotInvoke") |
| 144 |
expect("--version", out="NotInvoke unknown\n", program=p) |
| 145 |
|
| 146 |
class binary: |
| 147 |
def defaults_to_argv_when_None(self): |
| 148 |
stdout, _ = run("myapp --help", invoke=False) |
| 149 |
assert "myapp [--core-opts]" in stdout |
| 150 |
|
| 151 |
def uses_overridden_value_when_given(self): |
| 152 |
stdout, _ = run( |
| 153 |
"myapp --help", invoke=False, program=Program(binary="nope") |
| 154 |
) |
| 155 |
assert "nope [--core-opts]" in stdout |
| 156 |
|
| 157 |
@trap |
| 158 |
def use_binary_basename_when_invoked_absolutely(self): |
| 159 |
Program().run("/usr/local/bin/myapp --help", exit=False) |
| 160 |
stdout = sys.stdout.getvalue() |
| 161 |
assert "myapp [--core-opts]" in stdout |
| 162 |
assert "/usr/local/bin" not in stdout |
| 163 |
|
| 164 |
class called_as: |
| 165 |
# NOTE: these tests are meh due to Program's lifecycle design |
| 166 |
# (attributes get modified during run(), such as things based on |
| 167 |
# observed argv). It's not great, but, whatever. |
| 168 |
@trap |
| 169 |
def is_the_whole_deal_when_just_a_name(self): |
| 170 |
p = Program() |
| 171 |
p.run("whatever --help", exit=False) |
| 172 |
assert p.called_as == "whatever" |
| 173 |
|
| 174 |
@trap |
| 175 |
def is_basename_when_given_a_path(self): |
| 176 |
p = Program() |
| 177 |
p.run("/usr/local/bin/whatever --help", exit=False) |
| 178 |
assert p.called_as == "whatever" |
| 179 |
|
| 180 |
class binary_names: |
| 181 |
# NOTE: this is currently only used for completion stuff, so we use |
| 182 |
# that to test. TODO: maybe make this more unit-y... |
| 183 |
def defaults_to_argv_when_None(self): |
| 184 |
stdout, _ = run("foo --print-completion-script zsh", invoke=False) |
| 185 |
assert " foo" in stdout |
| 186 |
|
| 187 |
def can_be_given_directly(self): |
| 188 |
program = Program(binary_names=["foo", "bar"]) |
| 189 |
stdout, _ = run( |
| 190 |
"foo --print-completion-script zsh", |
| 191 |
invoke=False, |
| 192 |
program=program, |
| 193 |
) |
| 194 |
assert " foo bar" in stdout |
| 195 |
|
| 196 |
class print_version: |
| 197 |
def displays_name_and_version(self): |
| 198 |
expect( |
| 199 |
"--version", |
| 200 |
program=Program(name="MyProgram", version="0.1.0"), |
| 201 |
out="MyProgram 0.1.0\n", |
| 202 |
) |
| 203 |
|
| 204 |
class initial_context: |
| 205 |
def contains_truly_core_arguments_regardless_of_namespace_value(self): |
| 206 |
# Spot check. See integration-style --help tests for full argument |
| 207 |
# checkup. |
| 208 |
for program in (Program(), Program(namespace=Collection())): |
| 209 |
for arg in ("--complete", "--debug", "--warn-only", "--list"): |
| 210 |
stdout, _ = run("--help", program=program) |
| 211 |
assert arg in stdout |
| 212 |
|
| 213 |
def null_namespace_triggers_task_related_args(self): |
| 214 |
program = Program(namespace=None) |
| 215 |
for arg in program.task_args(): |
| 216 |
stdout, _ = run("--help", program=program) |
| 217 |
assert arg.name in stdout |
| 218 |
|
| 219 |
def non_null_namespace_does_not_trigger_task_related_args(self): |
| 220 |
for arg in Program().task_args(): |
| 221 |
program = Program(namespace=Collection(mytask=Task(Mock()))) |
| 222 |
stdout, _ = run("--help", program=program) |
| 223 |
assert arg.name not in stdout |
| 224 |
|
| 225 |
class load_collection: |
| 226 |
def complains_when_default_collection_not_found(self): |
| 227 |
# NOTE: assumes system under test has no tasks.py in root. Meh. |
| 228 |
with cd(ROOT): |
| 229 |
expect("-l", err="Can't find any collection named 'tasks'!\n") |
| 230 |
|
| 231 |
def complains_when_explicit_collection_not_found(self): |
| 232 |
expect( |
| 233 |
"-c huhwhat -l", |
| 234 |
err="Can't find any collection named 'huhwhat'!\n", |
| 235 |
) |
| 236 |
|
| 237 |
@trap |
| 238 |
def uses_loader_class_given(self): |
| 239 |
klass = Mock(side_effect=FilesystemLoader) |
| 240 |
Program(loader_class=klass).run("myapp --help foo", exit=False) |
| 241 |
klass.assert_called_with(start=ANY, config=ANY) |
| 242 |
|
| 243 |
def config_location_correct_for_package_type_task_trees(self): |
| 244 |
with cd(Path(support) / "configs" / "package"): |
| 245 |
expect("mytask") # will assert if config not loaded right |
| 246 |
|
| 247 |
class execute: |
| 248 |
def uses_executor_class_given(self): |
| 249 |
klass = Mock() |
| 250 |
Program(executor_class=klass).run("myapp foo", exit=False) |
| 251 |
klass.assert_called_with(ANY, ANY, ANY) |
| 252 |
klass.return_value.execute.assert_called_with(ANY) |
| 253 |
|
| 254 |
def executor_class_may_be_overridden_via_configured_string(self): |
| 255 |
class ExecutorOverridingConfig(Config): |
| 256 |
@staticmethod |
| 257 |
def global_defaults(): |
| 258 |
defaults = Config.global_defaults() |
| 259 |
path = "custom_executor.CustomExecutor" |
| 260 |
merge_dicts(defaults, {"tasks": {"executor_class": path}}) |
| 261 |
return defaults |
| 262 |
|
| 263 |
mock = load("custom_executor").CustomExecutor |
| 264 |
p = Program(config_class=ExecutorOverridingConfig) |
| 265 |
p.run("myapp noop", exit=False) |
| 266 |
assert mock.assert_called |
| 267 |
assert mock.return_value.execute.called |
| 268 |
|
| 269 |
def executor_is_given_access_to_core_args_and_remainder(self): |
| 270 |
klass = Mock() |
| 271 |
cmd = "myapp -e foo -- myremainder" |
| 272 |
Program(executor_class=klass).run(cmd, exit=False) |
| 273 |
core = klass.call_args[0][2] |
| 274 |
assert core[0].args["echo"].value |
| 275 |
assert core.remainder == "myremainder" |
| 276 |
|
| 277 |
class core_args: |
| 278 |
def returns_core_args_list(self): |
| 279 |
# Mostly so we encode explicity doc'd public API member in tests. |
| 280 |
# Spot checks good enough, --help tests include the full deal. |
| 281 |
core_args = Program().core_args() |
| 282 |
core_arg_names = [x.names[0] for x in core_args] |
| 283 |
for name in ("complete", "help", "pty", "version"): |
| 284 |
assert name in core_arg_names |
| 285 |
# Also make sure it's a list for easier tweaking/appending |
| 286 |
assert isinstance(core_args, list) |
| 287 |
|
| 288 |
class args_property: |
| 289 |
def shorthand_for_self_core_args(self): |
| 290 |
"is shorthand for self.core[0].args" |
| 291 |
p = Program() |
| 292 |
p.run("myapp -e noop", exit=False) |
| 293 |
args = p.args |
| 294 |
assert isinstance(args, Lexicon) |
| 295 |
assert args.echo.value is True |
| 296 |
|
| 297 |
class core_args_from_task_contexts: |
| 298 |
# NOTE: many of these use Program.args in lieu of Program.core[0], for |
| 299 |
# convenience, tho also because initially the behavior was _in_ .args |
| 300 |
def core_context_gets_updated_with_core_flags_from_tasks(self): |
| 301 |
# Part of #466. |
| 302 |
p = Program() |
| 303 |
p.run("myapp -e noop --hide both", exit=False) |
| 304 |
# Was given in core |
| 305 |
assert p.args.echo.value is True |
| 306 |
# Was given in per-task |
| 307 |
assert p.args.hide.value == "both" |
| 308 |
|
| 309 |
def copying_from_task_context_does_not_set_empty_list_values(self): |
| 310 |
# Less of an issue for scalars, but for list-type args, doing |
| 311 |
# .value = <default value> actually ends up creating a |
| 312 |
# list-of-lists. |
| 313 |
p = Program() |
| 314 |
|
| 315 |
# Set up core-args parser context with an iterable arg that hasn't |
| 316 |
# seen any value yet |
| 317 |
def filename_args(): |
| 318 |
return [Argument("filename", kind=list)] |
| 319 |
|
| 320 |
p.core = ParseResult([ParserContext(args=filename_args())]) |
| 321 |
# And a core-via-tasks context with a copy of that same arg, which |
| 322 |
# also hasn't seen any value yet |
| 323 |
p.core_via_tasks = ParserContext(args=filename_args()) |
| 324 |
# Now the behavior of .args can be tested as desired |
| 325 |
assert p.args["filename"].value == [] # Not [[]]! |
| 326 |
|
| 327 |
def copying_from_task_context_does_not_overwrite_good_values(self): |
| 328 |
# Another subcase, also mostly applying to list types: core context |
| 329 |
# got a useful value, nothing was found in the per-task context; |
| 330 |
# when a naive 'is not None' check is used, this overwrites the |
| 331 |
# good value with an empty list. |
| 332 |
# (Other types tend to not have this problem because their ._value |
| 333 |
# is always None when not set. TODO: maybe this should be |
| 334 |
# considered incorrect behavior for list type args?) |
| 335 |
def make_arg(): |
| 336 |
return Argument("filename", kind=list) |
| 337 |
|
| 338 |
p = Program() |
| 339 |
# Core arg, which got a value |
| 340 |
arg = make_arg() |
| 341 |
arg.value = "some-file" # appends to list |
| 342 |
p.core = ParseResult([ParserContext(args=[arg])]) |
| 343 |
# Set core-via-tasks version to vanilla/blank/empty-list version |
| 344 |
p.core_via_tasks = ParserContext(args=[make_arg()]) |
| 345 |
# Call .args, expect that the initial value was not overwritten |
| 346 |
assert p.args.filename.value == ["some-file"] |
| 347 |
|
| 348 |
class run: |
| 349 |
# NOTE: some of these are integration-style tests, but they are still |
| 350 |
# fast tests (so not needing to go into the integration suite) and |
| 351 |
# touch on transformations to the command line that occur above, or |
| 352 |
# around, the actual parser classes/methods (thus not being suitable |
| 353 |
# for the parser's own unit tests). |
| 354 |
|
| 355 |
def seeks_and_loads_tasks_module_by_default(self): |
| 356 |
expect("foo", out="Hm\n") |
| 357 |
|
| 358 |
def does_not_seek_tasks_module_if_namespace_was_given(self): |
| 359 |
expect( |
| 360 |
"foo", |
| 361 |
err="No idea what 'foo' is!\n", |
| 362 |
program=Program(namespace=Collection("blank")), |
| 363 |
) |
| 364 |
|
| 365 |
def explicit_namespace_works_correctly(self): |
| 366 |
# Regression-ish test re #288 |
| 367 |
ns = Collection.from_module(load("integration")) |
| 368 |
expect("print-foo", out="foo\n", program=Program(namespace=ns)) |
| 369 |
|
| 370 |
def allows_explicit_task_module_specification(self): |
| 371 |
expect("-c integration print-foo", out="foo\n") |
| 372 |
|
| 373 |
def handles_task_arguments(self): |
| 374 |
expect("-c integration print-name --name inigo", out="inigo\n") |
| 375 |
|
| 376 |
def can_change_collection_search_root(self): |
| 377 |
for flag in ("-r", "--search-root"): |
| 378 |
expect( |
| 379 |
"{} branch/ alt-root".format(flag), |
| 380 |
out="Down with the alt-root!\n", |
| 381 |
) |
| 382 |
|
| 383 |
def can_change_collection_search_root_with_explicit_module_name(self): |
| 384 |
for flag in ("-r", "--search-root"): |
| 385 |
expect( |
| 386 |
"{} branch/ -c explicit lyrics".format(flag), |
| 387 |
out="Don't swear!\n", |
| 388 |
) |
| 389 |
|
| 390 |
@trap |
| 391 |
@patch("invoke.program.sys.exit") |
| 392 |
def ParseErrors_display_message_and_exit_1(self, mock_exit): |
| 393 |
p = Program() |
| 394 |
# Run with a definitely-parser-angering incorrect input; the fact |
| 395 |
# that this line doesn't raise an exception and thus fail the |
| 396 |
# test, is what we're testing... |
| 397 |
nah = "nopenotvalidsorry" |
| 398 |
p.run("myapp {}".format(nah)) |
| 399 |
# Expect that we did print the core body of the ParseError (e.g. |
| 400 |
# "no idea what foo is!") and exit 1. (Intent is to display that |
| 401 |
# info w/o a full traceback, basically.) |
| 402 |
stderr = sys.stderr.getvalue() |
| 403 |
assert stderr == "No idea what '{}' is!\n".format(nah) |
| 404 |
mock_exit.assert_called_with(1) |
| 405 |
|
| 406 |
@trap |
| 407 |
@patch("invoke.program.sys.exit") |
| 408 |
def UnexpectedExit_exits_with_code_when_no_hiding(self, mock_exit): |
| 409 |
p = Program() |
| 410 |
oops = UnexpectedExit( |
| 411 |
Result(command="meh", exited=17, hide=tuple()) |
| 412 |
) |
| 413 |
p.execute = Mock(side_effect=oops) |
| 414 |
p.run("myapp foo") |
| 415 |
# Expect NO repr printed, because stdout/err were not hidden, so we |
| 416 |
# don't want to add extra annoying verbosity - we want to be more |
| 417 |
# Make-like here. |
| 418 |
assert sys.stderr.getvalue() == "" |
| 419 |
# But we still exit with expected code (vs e.g. 1 or 0) |
| 420 |
mock_exit.assert_called_with(17) |
| 421 |
|
| 422 |
@trap |
| 423 |
@patch("invoke.program.sys.exit") |
| 424 |
def shows_UnexpectedExit_str_when_streams_hidden(self, mock_exit): |
| 425 |
p = Program() |
| 426 |
oops = UnexpectedExit( |
| 427 |
Result( |
| 428 |
command="meh", |
| 429 |
exited=54, |
| 430 |
stdout="things!", |
| 431 |
stderr="ohnoz!", |
| 432 |
encoding="utf-8", |
| 433 |
hide=("stdout", "stderr"), |
| 434 |
) |
| 435 |
) |
| 436 |
p.execute = Mock(side_effect=oops) |
| 437 |
p.run("myapp foo") |
| 438 |
# Expect repr() of exception prints to stderr |
| 439 |
# NOTE: this partially duplicates a test in runners.py; whatever. |
| 440 |
stderr = sys.stderr.getvalue() |
| 441 |
expected = """Encountered a bad command exit code! |
| 442 |
|
| 443 |
Command: 'meh' |
| 444 |
|
| 445 |
Exit code: 54 |
| 446 |
|
| 447 |
Stdout: |
| 448 |
|
| 449 |
things! |
| 450 |
|
| 451 |
Stderr: |
| 452 |
|
| 453 |
ohnoz! |
| 454 |
|
| 455 |
""" |
| 456 |
assert stderr == expected |
| 457 |
# And exit with expected code (vs e.g. 1 or 0) |
| 458 |
mock_exit.assert_called_with(54) |
| 459 |
|
| 460 |
@trap |
| 461 |
@patch("invoke.program.sys.exit") |
| 462 |
def UnexpectedExit_str_encodes_stdout_and_err(self, mock_exit): |
| 463 |
p = Program() |
| 464 |
oops = UnexpectedExit( |
| 465 |
Result( |
| 466 |
command="meh", |
| 467 |
exited=54, |
| 468 |
stdout="this is not ascii: \u1234", |
| 469 |
stderr="this is also not ascii: \u4321", |
| 470 |
encoding="utf-8", |
| 471 |
hide=("stdout", "stderr"), |
| 472 |
) |
| 473 |
) |
| 474 |
p.execute = Mock(side_effect=oops) |
| 475 |
p.run("myapp foo") |
| 476 |
# NOTE: using explicit binary ASCII here, & accessing raw |
| 477 |
# getvalue() of the faked sys.stderr (spec.trap auto-decodes it |
| 478 |
# normally) to have a not-quite-tautological test. otherwise we'd |
| 479 |
# just be comparing unicode to unicode. shrug? |
| 480 |
expected = b"""Encountered a bad command exit code! |
| 481 |
|
| 482 |
Command: 'meh' |
| 483 |
|
| 484 |
Exit code: 54 |
| 485 |
|
| 486 |
Stdout: |
| 487 |
|
| 488 |
this is not ascii: \xe1\x88\xb4 |
| 489 |
|
| 490 |
Stderr: |
| 491 |
|
| 492 |
this is also not ascii: \xe4\x8c\xa1 |
| 493 |
|
| 494 |
""" |
| 495 |
got = BytesIO.getvalue(sys.stderr) |
| 496 |
assert got == expected |
| 497 |
|
| 498 |
class Exit_: |
| 499 |
@patch("invoke.program.sys.exit") |
| 500 |
def defaults_to_exiting_0(self, mock_exit): |
| 501 |
p = Program() |
| 502 |
p.execute = Mock(side_effect=Exit()) |
| 503 |
p.run("myapp foo") |
| 504 |
mock_exit.assert_called_once_with(0) |
| 505 |
|
| 506 |
@trap |
| 507 |
@patch("invoke.program.sys.exit") |
| 508 |
def prints_message_exiting_1_if_message_given(self, mock_exit): |
| 509 |
p = Program() |
| 510 |
p.execute = Mock(side_effect=Exit("onoz")) |
| 511 |
p.run("myapp foo") |
| 512 |
mock_exit.assert_called_once_with(1) |
| 513 |
assert sys.stderr.getvalue() == "onoz\n" |
| 514 |
|
| 515 |
@trap |
| 516 |
@patch("invoke.program.sys.exit") |
| 517 |
def may_explicitly_supply_code_with_message(self, mock_exit): |
| 518 |
p = Program() |
| 519 |
p.execute = Mock(side_effect=Exit("onoz", code=17)) |
| 520 |
p.run("myapp foo") |
| 521 |
mock_exit.assert_called_once_with(17) |
| 522 |
assert sys.stderr.getvalue() == "onoz\n" |
| 523 |
|
| 524 |
@trap |
| 525 |
@patch("invoke.program.sys.exit") |
| 526 |
def may_explicitly_supply_code_without_message(self, mock_exit): |
| 527 |
p = Program() |
| 528 |
p.execute = Mock(side_effect=Exit(code=17)) |
| 529 |
p.run("myapp foo") |
| 530 |
mock_exit.assert_called_once_with(17) |
| 531 |
assert sys.stderr.getvalue() == "" |
| 532 |
|
| 533 |
def should_show_core_usage_on_core_parse_failures(self): |
| 534 |
skip() |
| 535 |
|
| 536 |
def should_show_context_usage_on_context_parse_failures(self): |
| 537 |
skip() |
| 538 |
|
| 539 |
@trap |
| 540 |
@patch("invoke.program.sys.exit") |
| 541 |
def turns_KeyboardInterrupt_into_exit_code_1(self, mock_exit): |
| 542 |
p = Program() |
| 543 |
p.execute = Mock(side_effect=KeyboardInterrupt) |
| 544 |
p.run("myapp -c foo mytask") |
| 545 |
mock_exit.assert_called_with(1) |
| 546 |
|
| 547 |
class help_: |
| 548 |
"--help" |
| 549 |
|
| 550 |
class core: |
| 551 |
def empty_invocation_with_no_default_task_prints_help(self): |
| 552 |
stdout, _ = run("-c foo") |
| 553 |
assert "Core options:" in stdout |
| 554 |
|
| 555 |
# TODO: On Windows, we don't get a pty, so we don't get a |
| 556 |
# guaranteed terminal size of 80x24. Skip for now, but maybe |
| 557 |
# a suitable fix would be to just strip all whitespace from the |
| 558 |
# returned and expected values before testing. Then terminal |
| 559 |
# size is ignored. |
| 560 |
@skip_if_windows |
| 561 |
def core_help_option_prints_core_help(self): |
| 562 |
# TODO: change dynamically based on parser contents? |
| 563 |
# e.g. no core args == no [--core-opts], |
| 564 |
# no tasks == no task stuff? |
| 565 |
# NOTE: test will trigger default pty size of 80x24, so the |
| 566 |
# below string is formatted appropriately. |
| 567 |
# TODO: add more unit-y tests for specific behaviors: |
| 568 |
# * fill terminal w/ columns + spacing |
| 569 |
# * line-wrap help text in its own column |
| 570 |
expected = """ |
| 571 |
Usage: inv[oke] [--core-opts] task1 [--task1-opts] ... taskN [--taskN-opts] |
| 572 |
|
| 573 |
Core options: |
| 574 |
|
| 575 |
--complete Print tab-completion candidates for given |
| 576 |
parse remainder. |
| 577 |
--hide=STRING Set default value of run()'s 'hide' kwarg. |
| 578 |
--no-dedupe Disable task deduplication. |
| 579 |
--print-completion-script=STRING Print the tab-completion script for your |
| 580 |
preferred shell (bash|zsh|fish). |
| 581 |
--prompt-for-sudo-password Prompt user at start of session for the |
| 582 |
sudo.password config value. |
| 583 |
--write-pyc Enable creation of .pyc files. |
| 584 |
-c STRING, --collection=STRING Specify collection name to load. |
| 585 |
-d, --debug Enable debug output. |
| 586 |
-D INT, --list-depth=INT When listing tasks, only show the first |
| 587 |
INT levels. |
| 588 |
-e, --echo Echo executed commands before running. |
| 589 |
-f STRING, --config=STRING Runtime configuration file to use. |
| 590 |
-F STRING, --list-format=STRING Change the display format used when |
| 591 |
listing tasks. Should be one of: flat |
| 592 |
(default), nested, json. |
| 593 |
-h [STRING], --help[=STRING] Show core or per-task help and exit. |
| 594 |
-l [STRING], --list[=STRING] List available tasks, optionally limited |
| 595 |
to a namespace. |
| 596 |
-p, --pty Use a pty when executing shell commands. |
| 597 |
-r STRING, --search-root=STRING Change root directory used for finding |
| 598 |
task modules. |
| 599 |
-R, --dry Echo commands instead of running. |
| 600 |
-T INT, --command-timeout=INT Specify a global command execution |
| 601 |
timeout, in seconds. |
| 602 |
-V, --version Show version and exit. |
| 603 |
-w, --warn-only Warn, instead of failing, when shell |
| 604 |
commands fail. |
| 605 |
|
| 606 |
""".lstrip() |
| 607 |
for flag in ["-h", "--help"]: |
| 608 |
expect(flag, out=expected, program=main.program) |
| 609 |
|
| 610 |
def bundled_namespace_help_includes_subcommand_listing(self): |
| 611 |
t1, t2 = Task(Mock()), Task(Mock()) |
| 612 |
coll = Collection(task1=t1, task2=t2) |
| 613 |
p = Program(namespace=coll) |
| 614 |
# Spot checks for expected bits, so we don't have to change |
| 615 |
# this every time core args change. |
| 616 |
for expected in ( |
| 617 |
# Usage line changes somewhat |
| 618 |
"Usage: myapp [--core-opts] <subcommand> [--subcommand-opts] ...\n", # noqa |
| 619 |
# Core options are still present |
| 620 |
"Core options:\n", |
| 621 |
"--echo", |
| 622 |
# Subcommands are listed |
| 623 |
"Subcommands:\n", |
| 624 |
" task1", |
| 625 |
" task2", |
| 626 |
): |
| 627 |
stdout, _ = run("myapp --help", program=p, invoke=False) |
| 628 |
assert expected in stdout |
| 629 |
|
| 630 |
def core_help_doesnt_get_mad_if_loading_fails(self): |
| 631 |
# Expects no tasks.py in root of FS |
| 632 |
with cd(ROOT): |
| 633 |
stdout, _ = run("--help") |
| 634 |
assert "Usage: " in stdout |
| 635 |
|
| 636 |
class per_task: |
| 637 |
"per-task" |
| 638 |
|
| 639 |
def prints_help_for_task_only(self): |
| 640 |
expected = """ |
| 641 |
Usage: invoke [--core-opts] punch [--options] [other tasks here ...] |
| 642 |
|
| 643 |
Docstring: |
| 644 |
none |
| 645 |
|
| 646 |
Options: |
| 647 |
-h STRING, --why=STRING Motive |
| 648 |
-w STRING, --who=STRING Who to punch |
| 649 |
|
| 650 |
""".lstrip() |
| 651 |
for flag in ["-h", "--help"]: |
| 652 |
expect("-c decorators {} punch".format(flag), out=expected) |
| 653 |
|
| 654 |
def works_for_unparameterized_tasks(self): |
| 655 |
expected = """ |
| 656 |
Usage: invoke [--core-opts] biz [other tasks here ...] |
| 657 |
|
| 658 |
Docstring: |
| 659 |
none |
| 660 |
|
| 661 |
Options: |
| 662 |
none |
| 663 |
|
| 664 |
""".lstrip() |
| 665 |
expect("-c decorators -h biz", out=expected) |
| 666 |
|
| 667 |
def honors_program_binary(self): |
| 668 |
stdout, _ = run( |
| 669 |
"-c decorators -h biz", program=Program(binary="notinvoke") |
| 670 |
) |
| 671 |
assert "Usage: notinvoke" in stdout |
| 672 |
|
| 673 |
def displays_docstrings_if_given(self): |
| 674 |
expected = """ |
| 675 |
Usage: invoke [--core-opts] foo [other tasks here ...] |
| 676 |
|
| 677 |
Docstring: |
| 678 |
Foo the bar. |
| 679 |
|
| 680 |
Options: |
| 681 |
none |
| 682 |
|
| 683 |
""".lstrip() |
| 684 |
expect("-c decorators -h foo", out=expected) |
| 685 |
|
| 686 |
def dedents_correctly(self): |
| 687 |
expected = """ |
| 688 |
Usage: invoke [--core-opts] foo2 [other tasks here ...] |
| 689 |
|
| 690 |
Docstring: |
| 691 |
Foo the bar: |
| 692 |
|
| 693 |
example code |
| 694 |
|
| 695 |
Added in 1.0 |
| 696 |
|
| 697 |
Options: |
| 698 |
none |
| 699 |
|
| 700 |
""".lstrip() |
| 701 |
expect("-c decorators -h foo2", out=expected) |
| 702 |
|
| 703 |
def dedents_correctly_for_alt_docstring_style(self): |
| 704 |
expected = """ |
| 705 |
Usage: invoke [--core-opts] foo3 [other tasks here ...] |
| 706 |
|
| 707 |
Docstring: |
| 708 |
Foo the other bar: |
| 709 |
|
| 710 |
example code |
| 711 |
|
| 712 |
Added in 1.1 |
| 713 |
|
| 714 |
Options: |
| 715 |
none |
| 716 |
|
| 717 |
""".lstrip() |
| 718 |
expect("-c decorators -h foo3", out=expected) |
| 719 |
|
| 720 |
def exits_after_printing(self): |
| 721 |
# TODO: find & test the other variants of this error case, such |
| 722 |
# as core --help not exiting, --list not exiting, etc |
| 723 |
expected = """ |
| 724 |
Usage: invoke [--core-opts] punch [--options] [other tasks here ...] |
| 725 |
|
| 726 |
Docstring: |
| 727 |
none |
| 728 |
|
| 729 |
Options: |
| 730 |
-h STRING, --why=STRING Motive |
| 731 |
-w STRING, --who=STRING Who to punch |
| 732 |
|
| 733 |
""".lstrip() |
| 734 |
expect("-c decorators -h punch --list", out=expected) |
| 735 |
|
| 736 |
def complains_if_given_invalid_task_name(self): |
| 737 |
expect("-h this", err="No idea what 'this' is!\n") |
| 738 |
|
| 739 |
class task_list: |
| 740 |
"--list" |
| 741 |
|
| 742 |
def _listing(self, lines): |
| 743 |
joined = "\n".join(" " + x for x in lines) |
| 744 |
return f""" |
| 745 |
Available tasks: |
| 746 |
|
| 747 |
{joined} |
| 748 |
|
| 749 |
""".lstrip() |
| 750 |
|
| 751 |
def _list_eq(self, collection, listing): |
| 752 |
cmd = "-c {} --list".format(collection) |
| 753 |
expect(cmd, out=self._listing(listing)) |
| 754 |
|
| 755 |
def simple_output(self): |
| 756 |
expected = self._listing( |
| 757 |
( |
| 758 |
"bar", |
| 759 |
"biz", |
| 760 |
"boz", |
| 761 |
"foo", |
| 762 |
"post1", |
| 763 |
"post2", |
| 764 |
"print-foo", |
| 765 |
"print-name", |
| 766 |
"print-underscored-arg", |
| 767 |
) |
| 768 |
) |
| 769 |
for flag in ("-l", "--list"): |
| 770 |
expect("-c integration {}".format(flag), out=expected) |
| 771 |
|
| 772 |
def namespacing(self): |
| 773 |
self._list_eq("namespacing", ("toplevel", "module.mytask")) |
| 774 |
|
| 775 |
def top_level_tasks_listed_first(self): |
| 776 |
self._list_eq("simple_ns_list", ("z-toplevel", "a.b.subtask")) |
| 777 |
|
| 778 |
def aliases_sorted_alphabetically(self): |
| 779 |
self._list_eq("alias_sorting", ("toplevel (a, z)",)) |
| 780 |
|
| 781 |
def default_tasks(self): |
| 782 |
# sub-ns default task display as "real.name (collection name)" |
| 783 |
self._list_eq( |
| 784 |
"explicit_root", |
| 785 |
( |
| 786 |
"top-level (other-top)", |
| 787 |
"sub-level.sub-task (sub-level, sub-level.other-sub)", |
| 788 |
), |
| 789 |
) |
| 790 |
|
| 791 |
def docstrings_shown_alongside(self): |
| 792 |
self._list_eq( |
| 793 |
"docstrings", |
| 794 |
( |
| 795 |
"leading-whitespace foo", |
| 796 |
"no-docstring", |
| 797 |
"one-line foo", |
| 798 |
"two-lines foo", |
| 799 |
"with-aliases (a, b) foo", |
| 800 |
), |
| 801 |
) |
| 802 |
|
| 803 |
def docstrings_are_wrapped_to_terminal_width(self): |
| 804 |
self._list_eq( |
| 805 |
"nontrivial_docstrings", |
| 806 |
( |
| 807 |
"no-docstring", |
| 808 |
"task-one Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n Nullam id dictum", # noqa |
| 809 |
"task-two Nulla eget ultrices ante. Curabitur sagittis commodo posuere.\n Duis dapibus", # noqa |
| 810 |
), |
| 811 |
) |
| 812 |
|
| 813 |
def empty_collections_say_no_tasks(self): |
| 814 |
expect( |
| 815 |
"-c empty -l", err="No tasks found in collection 'empty'!\n" |
| 816 |
) |
| 817 |
|
| 818 |
def nontrivial_trees_are_sorted_by_namespace_and_depth(self): |
| 819 |
# By using a larger sample, we can guard against unintuitive |
| 820 |
# behaviors arising from the above simple unit style tests. E.g. |
| 821 |
# earlier implementations 'broke up' collections that had more than |
| 822 |
# 2 levels of depth, because they displayed all 2nd-level tasks |
| 823 |
# before any 3rd-level ones. |
| 824 |
# The code must square that concern against "show shallow tasks |
| 825 |
# before deep ones" (vs straight up alpha sorting) |
| 826 |
expected = """Available tasks: |
| 827 |
|
| 828 |
shell (ipython) Load a REPL with project state already |
| 829 |
set up. |
| 830 |
test (run-tests) Run the test suite with baked-in args. |
| 831 |
build.all (build, build.everything) Build all necessary artifacts. |
| 832 |
build.c-ext (build.ext) Build our internal C extension. |
| 833 |
build.zap A silly way to clean. |
| 834 |
build.docs.all (build.docs) Build all doc formats. |
| 835 |
build.docs.html Build HTML output only. |
| 836 |
build.docs.pdf Build PDF output only. |
| 837 |
build.python.all (build.python) Build all Python packages. |
| 838 |
build.python.sdist Build classic style tar.gz. |
| 839 |
build.python.wheel Build a wheel. |
| 840 |
deploy.db (deploy.db-servers) Deploy to our database servers. |
| 841 |
deploy.everywhere (deploy) Deploy to all targets. |
| 842 |
deploy.web Update and bounce the webservers. |
| 843 |
provision.db Stand up one or more DB servers. |
| 844 |
provision.web Stand up a Web server. |
| 845 |
|
| 846 |
Default task: test |
| 847 |
|
| 848 |
""" |
| 849 |
stdout, _ = run("-c tree --list") |
| 850 |
assert expected == stdout |
| 851 |
|
| 852 |
class namespace_limiting: |
| 853 |
def argument_limits_display_to_given_namespace(self): |
| 854 |
stdout, _ = run("-c tree --list build") |
| 855 |
expected = """Available 'build' tasks: |
| 856 |
|
| 857 |
.all (.everything) Build all necessary artifacts. |
| 858 |
.c-ext (.ext) Build our internal C extension. |
| 859 |
.zap A silly way to clean. |
| 860 |
.docs.all (.docs) Build all doc formats. |
| 861 |
.docs.html Build HTML output only. |
| 862 |
.docs.pdf Build PDF output only. |
| 863 |
.python.all (.python) Build all Python packages. |
| 864 |
.python.sdist Build classic style tar.gz. |
| 865 |
.python.wheel Build a wheel. |
| 866 |
|
| 867 |
Default 'build' task: .all |
| 868 |
|
| 869 |
""" |
| 870 |
assert expected == stdout |
| 871 |
|
| 872 |
def argument_may_be_a_nested_namespace(self): |
| 873 |
stdout, _ = run("-c tree --list build.docs") |
| 874 |
expected = """Available 'build.docs' tasks: |
| 875 |
|
| 876 |
.all Build all doc formats. |
| 877 |
.html Build HTML output only. |
| 878 |
.pdf Build PDF output only. |
| 879 |
|
| 880 |
Default 'build.docs' task: .all |
| 881 |
|
| 882 |
""" |
| 883 |
assert expected == stdout |
| 884 |
|
| 885 |
def empty_namespaces_say_no_tasks_in_namespace(self): |
| 886 |
# In other words, outer namespace may not be empty, but the |
| 887 |
# inner one is - this should act just like when there is no |
| 888 |
# namespace explicitly requested and there's no tasks. |
| 889 |
# TODO: should the name in the error message be the fully |
| 890 |
# qualified one instead? |
| 891 |
expect( |
| 892 |
"-c empty_subcollection -l subcollection", |
| 893 |
err="No tasks found in collection 'subcollection'!\n", # noqa |
| 894 |
) |
| 895 |
|
| 896 |
def invalid_namespaces_exit_with_message(self): |
| 897 |
expect( |
| 898 |
"-c empty -l nope", |
| 899 |
err="Sub-collection 'nope' not found!\n", |
| 900 |
) |
| 901 |
|
| 902 |
class depth_limiting: |
| 903 |
def limits_display_to_given_depth(self): |
| 904 |
# Base case: depth=1 aka "show me the namespaces" |
| 905 |
expected = """Available tasks (depth=1): |
| 906 |
|
| 907 |
shell (ipython) Load a REPL with project state already set |
| 908 |
up. |
| 909 |
test (run-tests) Run the test suite with baked-in args. |
| 910 |
build [3 tasks, 2 collections] Tasks for compiling static code and assets. |
| 911 |
deploy [3 tasks] How to deploy our code and configs. |
| 912 |
provision [2 tasks] System setup code. |
| 913 |
|
| 914 |
Default task: test |
| 915 |
|
| 916 |
""" |
| 917 |
stdout, _ = run("-c tree --list -F flat --list-depth 1") |
| 918 |
assert expected == stdout |
| 919 |
|
| 920 |
def non_base_case(self): |
| 921 |
# Middle case: depth=2 |
| 922 |
expected = """Available tasks (depth=2): |
| 923 |
|
| 924 |
shell (ipython) Load a REPL with project state already |
| 925 |
set up. |
| 926 |
test (run-tests) Run the test suite with baked-in args. |
| 927 |
build.all (build, build.everything) Build all necessary artifacts. |
| 928 |
build.c-ext (build.ext) Build our internal C extension. |
| 929 |
build.zap A silly way to clean. |
| 930 |
build.docs [3 tasks] Tasks for managing Sphinx docs. |
| 931 |
build.python [3 tasks] PyPI/etc distribution artifacts. |
| 932 |
deploy.db (deploy.db-servers) Deploy to our database servers. |
| 933 |
deploy.everywhere (deploy) Deploy to all targets. |
| 934 |
deploy.web Update and bounce the webservers. |
| 935 |
provision.db Stand up one or more DB servers. |
| 936 |
provision.web Stand up a Web server. |
| 937 |
|
| 938 |
Default task: test |
| 939 |
|
| 940 |
""" |
| 941 |
stdout, _ = run("-c tree --list --list-depth=2") |
| 942 |
assert expected == stdout |
| 943 |
|
| 944 |
def depth_can_be_deeper_than_real_depth(self): |
| 945 |
# Edge case: depth > actual depth = same as no depth arg |
| 946 |
expected = """Available tasks (depth=5): |
| 947 |
|
| 948 |
shell (ipython) Load a REPL with project state already |
| 949 |
set up. |
| 950 |
test (run-tests) Run the test suite with baked-in args. |
| 951 |
build.all (build, build.everything) Build all necessary artifacts. |
| 952 |
build.c-ext (build.ext) Build our internal C extension. |
| 953 |
build.zap A silly way to clean. |
| 954 |
build.docs.all (build.docs) Build all doc formats. |
| 955 |
build.docs.html Build HTML output only. |
| 956 |
build.docs.pdf Build PDF output only. |
| 957 |
build.python.all (build.python) Build all Python packages. |
| 958 |
build.python.sdist Build classic style tar.gz. |
| 959 |
build.python.wheel Build a wheel. |
| 960 |
deploy.db (deploy.db-servers) Deploy to our database servers. |
| 961 |
deploy.everywhere (deploy) Deploy to all targets. |
| 962 |
deploy.web Update and bounce the webservers. |
| 963 |
provision.db Stand up one or more DB servers. |
| 964 |
provision.web Stand up a Web server. |
| 965 |
|
| 966 |
Default task: test |
| 967 |
|
| 968 |
""" |
| 969 |
stdout, _ = run("-c tree --list --list-depth=5") |
| 970 |
assert expected == stdout |
| 971 |
|
| 972 |
def works_with_explicit_namespace(self): |
| 973 |
expected = """Available 'build' tasks (depth=1): |
| 974 |
|
| 975 |
.all (.everything) Build all necessary artifacts. |
| 976 |
.c-ext (.ext) Build our internal C extension. |
| 977 |
.zap A silly way to clean. |
| 978 |
.docs [3 tasks] Tasks for managing Sphinx docs. |
| 979 |
.python [3 tasks] PyPI/etc distribution artifacts. |
| 980 |
|
| 981 |
Default 'build' task: .all |
| 982 |
|
| 983 |
""" |
| 984 |
stdout, _ = run("-c tree --list build --list-depth=1") |
| 985 |
assert expected == stdout |
| 986 |
|
| 987 |
def short_flag_is_D(self): |
| 988 |
expected = """Available tasks (depth=1): |
| 989 |
|
| 990 |
shell (ipython) Load a REPL with project state already set |
| 991 |
up. |
| 992 |
test (run-tests) Run the test suite with baked-in args. |
| 993 |
build [3 tasks, 2 collections] Tasks for compiling static code and assets. |
| 994 |
deploy [3 tasks] How to deploy our code and configs. |
| 995 |
provision [2 tasks] System setup code. |
| 996 |
|
| 997 |
Default task: test |
| 998 |
|
| 999 |
""" |
| 1000 |
stdout, _ = run("-c tree --list --list-format=flat -D 1") |
| 1001 |
assert expected == stdout |
| 1002 |
|
| 1003 |
def depth_of_zero_is_same_as_max_depth(self): |
| 1004 |
expected = """Available tasks: |
| 1005 |
|
| 1006 |
shell (ipython) Load a REPL with project state already |
| 1007 |
set up. |
| 1008 |
test (run-tests) Run the test suite with baked-in args. |
| 1009 |
build.all (build, build.everything) Build all necessary artifacts. |
| 1010 |
build.c-ext (build.ext) Build our internal C extension. |
| 1011 |
build.zap A silly way to clean. |
| 1012 |
build.docs.all (build.docs) Build all doc formats. |
| 1013 |
build.docs.html Build HTML output only. |
| 1014 |
build.docs.pdf Build PDF output only. |
| 1015 |
build.python.all (build.python) Build all Python packages. |
| 1016 |
build.python.sdist Build classic style tar.gz. |
| 1017 |
build.python.wheel Build a wheel. |
| 1018 |
deploy.db (deploy.db-servers) Deploy to our database servers. |
| 1019 |
deploy.everywhere (deploy) Deploy to all targets. |
| 1020 |
deploy.web Update and bounce the webservers. |
| 1021 |
provision.db Stand up one or more DB servers. |
| 1022 |
provision.web Stand up a Web server. |
| 1023 |
|
| 1024 |
Default task: test |
| 1025 |
|
| 1026 |
""" |
| 1027 |
stdout, _ = run("-c tree --list --list-format=flat -D 0") |
| 1028 |
assert expected == stdout |
| 1029 |
|
| 1030 |
class format: |
| 1031 |
def flat_is_legacy_default_format(self): |
| 1032 |
# Sanity test that --list --list-format=flat is the same as the |
| 1033 |
# old "just --list". |
| 1034 |
expected = """Available tasks: |
| 1035 |
|
| 1036 |
shell (ipython) Load a REPL with project state already |
| 1037 |
set up. |
| 1038 |
test (run-tests) Run the test suite with baked-in args. |
| 1039 |
build.all (build, build.everything) Build all necessary artifacts. |
| 1040 |
build.c-ext (build.ext) Build our internal C extension. |
| 1041 |
build.zap A silly way to clean. |
| 1042 |
build.docs.all (build.docs) Build all doc formats. |
| 1043 |
build.docs.html Build HTML output only. |
| 1044 |
build.docs.pdf Build PDF output only. |
| 1045 |
build.python.all (build.python) Build all Python packages. |
| 1046 |
build.python.sdist Build classic style tar.gz. |
| 1047 |
build.python.wheel Build a wheel. |
| 1048 |
deploy.db (deploy.db-servers) Deploy to our database servers. |
| 1049 |
deploy.everywhere (deploy) Deploy to all targets. |
| 1050 |
deploy.web Update and bounce the webservers. |
| 1051 |
provision.db Stand up one or more DB servers. |
| 1052 |
provision.web Stand up a Web server. |
| 1053 |
|
| 1054 |
Default task: test |
| 1055 |
|
| 1056 |
""" |
| 1057 |
stdout, _ = run("-c tree --list --list-format=flat") |
| 1058 |
assert expected == stdout |
| 1059 |
|
| 1060 |
class nested: |
| 1061 |
def base_case(self): |
| 1062 |
expected = """Available tasks ('*' denotes collection defaults): |
| 1063 |
|
| 1064 |
shell (ipython) Load a REPL with project state already set up. |
| 1065 |
test* (run-tests) Run the test suite with baked-in args. |
| 1066 |
build Tasks for compiling static code and assets. |
| 1067 |
.all* (.everything) Build all necessary artifacts. |
| 1068 |
.c-ext (.ext) Build our internal C extension. |
| 1069 |
.zap A silly way to clean. |
| 1070 |
.docs Tasks for managing Sphinx docs. |
| 1071 |
.all* Build all doc formats. |
| 1072 |
.html Build HTML output only. |
| 1073 |
.pdf Build PDF output only. |
| 1074 |
.python PyPI/etc distribution artifacts. |
| 1075 |
.all* Build all Python packages. |
| 1076 |
.sdist Build classic style tar.gz. |
| 1077 |
.wheel Build a wheel. |
| 1078 |
deploy How to deploy our code and configs. |
| 1079 |
.db (.db-servers) Deploy to our database servers. |
| 1080 |
.everywhere* Deploy to all targets. |
| 1081 |
.web Update and bounce the webservers. |
| 1082 |
provision System setup code. |
| 1083 |
.db Stand up one or more DB servers. |
| 1084 |
.web Stand up a Web server. |
| 1085 |
|
| 1086 |
Default task: test |
| 1087 |
|
| 1088 |
""" # noqa |
| 1089 |
stdout, _ = run("-c tree -l -F nested") |
| 1090 |
assert expected == stdout |
| 1091 |
|
| 1092 |
def honors_namespace_arg_to_list(self): |
| 1093 |
stdout, _ = run("-c tree --list build -F nested") |
| 1094 |
expected = """Available 'build' tasks ('*' denotes collection defaults): |
| 1095 |
|
| 1096 |
.all* (.everything) Build all necessary artifacts. |
| 1097 |
.c-ext (.ext) Build our internal C extension. |
| 1098 |
.zap A silly way to clean. |
| 1099 |
.docs Tasks for managing Sphinx docs. |
| 1100 |
.all* Build all doc formats. |
| 1101 |
.html Build HTML output only. |
| 1102 |
.pdf Build PDF output only. |
| 1103 |
.python PyPI/etc distribution artifacts. |
| 1104 |
.all* Build all Python packages. |
| 1105 |
.sdist Build classic style tar.gz. |
| 1106 |
.wheel Build a wheel. |
| 1107 |
|
| 1108 |
Default 'build' task: .all |
| 1109 |
|
| 1110 |
""" # noqa |
| 1111 |
assert expected == stdout |
| 1112 |
|
| 1113 |
def honors_depth_arg(self): |
| 1114 |
expected = """Available tasks (depth=2; '*' denotes collection defaults): |
| 1115 |
|
| 1116 |
shell (ipython) Load a REPL with project state already set up. |
| 1117 |
test* (run-tests) Run the test suite with baked-in args. |
| 1118 |
build Tasks for compiling static code and assets. |
| 1119 |
.all* (.everything) Build all necessary artifacts. |
| 1120 |
.c-ext (.ext) Build our internal C extension. |
| 1121 |
.zap A silly way to clean. |
| 1122 |
.docs [3 tasks] Tasks for managing Sphinx docs. |
| 1123 |
.python [3 tasks] PyPI/etc distribution artifacts. |
| 1124 |
deploy How to deploy our code and configs. |
| 1125 |
.db (.db-servers) Deploy to our database servers. |
| 1126 |
.everywhere* Deploy to all targets. |
| 1127 |
.web Update and bounce the webservers. |
| 1128 |
provision System setup code. |
| 1129 |
.db Stand up one or more DB servers. |
| 1130 |
.web Stand up a Web server. |
| 1131 |
|
| 1132 |
Default task: test |
| 1133 |
|
| 1134 |
""" # noqa |
| 1135 |
stdout, _ = run("-c tree -l -F nested --list-depth 2") |
| 1136 |
assert expected == stdout |
| 1137 |
|
| 1138 |
def depth_arg_deeper_than_real_depth(self): |
| 1139 |
expected = """Available tasks (depth=5; '*' denotes collection defaults): |
| 1140 |
|
| 1141 |
shell (ipython) Load a REPL with project state already set up. |
| 1142 |
test* (run-tests) Run the test suite with baked-in args. |
| 1143 |
build Tasks for compiling static code and assets. |
| 1144 |
.all* (.everything) Build all necessary artifacts. |
| 1145 |
.c-ext (.ext) Build our internal C extension. |
| 1146 |
.zap A silly way to clean. |
| 1147 |
.docs Tasks for managing Sphinx docs. |
| 1148 |
.all* Build all doc formats. |
| 1149 |
.html Build HTML output only. |
| 1150 |
.pdf Build PDF output only. |
| 1151 |
.python PyPI/etc distribution artifacts. |
| 1152 |
.all* Build all Python packages. |
| 1153 |
.sdist Build classic style tar.gz. |
| 1154 |
.wheel Build a wheel. |
| 1155 |
deploy How to deploy our code and configs. |
| 1156 |
.db (.db-servers) Deploy to our database servers. |
| 1157 |
.everywhere* Deploy to all targets. |
| 1158 |
.web Update and bounce the webservers. |
| 1159 |
provision System setup code. |
| 1160 |
.db Stand up one or more DB servers. |
| 1161 |
.web Stand up a Web server. |
| 1162 |
|
| 1163 |
Default task: test |
| 1164 |
|
| 1165 |
""" # noqa |
| 1166 |
stdout, _ = run("-c tree -l -F nested --list-depth 5") |
| 1167 |
assert expected == stdout |
| 1168 |
|
| 1169 |
def all_possible_options(self): |
| 1170 |
expected = """Available 'build' tasks (depth=1; '*' denotes collection defaults): |
| 1171 |
|
| 1172 |
.all* (.everything) Build all necessary artifacts. |
| 1173 |
.c-ext (.ext) Build our internal C extension. |
| 1174 |
.zap A silly way to clean. |
| 1175 |
.docs [3 tasks] Tasks for managing Sphinx docs. |
| 1176 |
.python [3 tasks] PyPI/etc distribution artifacts. |
| 1177 |
|
| 1178 |
Default 'build' task: .all |
| 1179 |
|
| 1180 |
""" # noqa |
| 1181 |
stdout, _ = run("-c tree -l build -F nested -D1") |
| 1182 |
assert expected == stdout |
| 1183 |
|
| 1184 |
# TODO: having these in each format smells like a POSSIBLY good |
| 1185 |
# use for parameterized tests... |
| 1186 |
def empty_namespaces_say_no_tasks_in_namespace(self): |
| 1187 |
expect( |
| 1188 |
"-c empty_subcollection -l subcollection -F nested", |
| 1189 |
err="No tasks found in collection 'subcollection'!\n", |
| 1190 |
) |
| 1191 |
|
| 1192 |
def invalid_namespaces_exit_with_message(self): |
| 1193 |
expect( |
| 1194 |
"-c empty -l nope -F nested", |
| 1195 |
err="Sub-collection 'nope' not found!\n", |
| 1196 |
) |
| 1197 |
|
| 1198 |
class json: |
| 1199 |
def setup_method(self): |
| 1200 |
# Stored expected data as an actual JSON file cuz it's big |
| 1201 |
# & looks like crap if inlined. Plus by round-tripping it |
| 1202 |
# we remove the pretty-printing. Win-win? |
| 1203 |
self.tree = json.loads(support_file("tree.json")) |
| 1204 |
self.by_name = { |
| 1205 |
x["name"]: x for x in self.tree["collections"] |
| 1206 |
} |
| 1207 |
|
| 1208 |
def base_case(self): |
| 1209 |
stdout, _ = run("-c tree --list --list-format=json") |
| 1210 |
assert self.tree == json.loads(stdout) |
| 1211 |
|
| 1212 |
def honors_namespace_arg_to_list(self): |
| 1213 |
stdout, _ = run("-c tree --list deploy --list-format=json") |
| 1214 |
expected = self.by_name["deploy"] |
| 1215 |
assert expected == json.loads(stdout) |
| 1216 |
|
| 1217 |
def does_not_honor_depth_arg(self): |
| 1218 |
_, stderr = run("-c tree -l --list-format json -D 2") |
| 1219 |
expected = "The --list-depth option is not supported with JSON format!\n" # noqa |
| 1220 |
assert expected == stderr |
| 1221 |
|
| 1222 |
def does_not_honor_depth_arg_even_with_namespace(self): |
| 1223 |
_, stderr = run("-c tree -l build -F json -D 2") |
| 1224 |
expected = "The --list-depth option is not supported with JSON format!\n" # noqa |
| 1225 |
assert expected == stderr |
| 1226 |
|
| 1227 |
# TODO: should an empty-but-valid namespace in JSON format |
| 1228 |
# actually just be an empty dict instead? Let's stay consistent |
| 1229 |
# with the other formats for now, but... |
| 1230 |
def empty_namespaces_say_no_tasks_in_namespace(self): |
| 1231 |
expect( |
| 1232 |
"-c empty_subcollection -l subcollection -F nested", |
| 1233 |
err="No tasks found in collection 'subcollection'!\n", # noqa |
| 1234 |
) |
| 1235 |
|
| 1236 |
# NOTE: this should probably still exit with a message even if |
| 1237 |
# the previous test re: valid-but-empty is determined to want a |
| 1238 |
# non-error behavior. |
| 1239 |
def invalid_namespaces_exit_with_message(self): |
| 1240 |
expect( |
| 1241 |
"-c empty -l nope -F nested", |
| 1242 |
err="Sub-collection 'nope' not found!\n", |
| 1243 |
) |
| 1244 |
|
| 1245 |
class run_options: |
| 1246 |
"run() related CLI flags affect 'run' config values" |
| 1247 |
|
| 1248 |
def _test_flag(self, flag, key, value=True): |
| 1249 |
p = Program() |
| 1250 |
p.execute = Mock() # neuter |
| 1251 |
p.run("inv {} foo".format(flag)) |
| 1252 |
assert p.config.run[key] == value |
| 1253 |
|
| 1254 |
def warn_only(self): |
| 1255 |
self._test_flag("-w", "warn") |
| 1256 |
|
| 1257 |
def pty(self): |
| 1258 |
self._test_flag("-p", "pty") |
| 1259 |
|
| 1260 |
def hide(self): |
| 1261 |
self._test_flag("--hide both", "hide", value="both") |
| 1262 |
|
| 1263 |
def echo(self): |
| 1264 |
self._test_flag("-e", "echo") |
| 1265 |
|
| 1266 |
def timeout(self): |
| 1267 |
for flag in ("-T", "--command-timeout"): |
| 1268 |
p = Program() |
| 1269 |
p.execute = Mock() # neuter |
| 1270 |
p.run("inv {} 5 foo".format(flag)) |
| 1271 |
assert p.config.timeouts.command == 5 |
| 1272 |
|
| 1273 |
class configuration: |
| 1274 |
"Configuration-related concerns" |
| 1275 |
|
| 1276 |
def _klass(self): |
| 1277 |
# Pauper's mock that can honor .tasks.collection_name (Loader |
| 1278 |
# looks in the config for this by default.) |
| 1279 |
instance_mock = Mock( |
| 1280 |
tasks=Mock(collection_name="whatever", search_root="meh") |
| 1281 |
) |
| 1282 |
return Mock(return_value=instance_mock) |
| 1283 |
|
| 1284 |
@trap |
| 1285 |
def config_class_init_kwarg_is_honored(self): |
| 1286 |
klass = self._klass() |
| 1287 |
Program(config_class=klass).run("myapp foo", exit=False) |
| 1288 |
# Don't care about actual args... |
| 1289 |
assert len(klass.call_args_list) == 1 |
| 1290 |
|
| 1291 |
@trap |
| 1292 |
def config_attribute_is_memoized(self): |
| 1293 |
klass = self._klass() |
| 1294 |
# Can't .config without .run (meh); .run calls .config once. |
| 1295 |
p = Program(config_class=klass) |
| 1296 |
p.run("myapp foo", exit=False) |
| 1297 |
assert klass.call_count == 1 |
| 1298 |
# Second access should use cached value |
| 1299 |
p.config |
| 1300 |
assert klass.call_count == 1 |
| 1301 |
|
| 1302 |
# NOTE: these tests all rely on the invoked tasks to perform the |
| 1303 |
# necessary asserts. |
| 1304 |
# TODO: can probably tighten these up to assert things about |
| 1305 |
# Program.config instead? |
| 1306 |
|
| 1307 |
def per_project_config_files_are_loaded_before_task_parsing(self): |
| 1308 |
# Relies on auto_dash_names being loaded at project-conf level; |
| 1309 |
# fixes #467; when bug present, project conf is loaded _after_ |
| 1310 |
# attempt to parse tasks, causing explosion when i_have_underscores |
| 1311 |
# is only sent to parser as i-have-underscores. |
| 1312 |
with cd(os.path.join("configs", "underscores")): |
| 1313 |
expect("i_have_underscores") |
| 1314 |
|
| 1315 |
def per_project_config_files_load_with_explicit_ns(self): |
| 1316 |
# Re: #234 |
| 1317 |
with cd(os.path.join("configs", "yaml")): |
| 1318 |
expect("-c explicit mytask") |
| 1319 |
|
| 1320 |
class runtime_config_file: |
| 1321 |
def can_be_set_via_cli_option(self): |
| 1322 |
with cd("configs"): |
| 1323 |
expect("-c runtime -f yaml/invoke.yaml mytask") |
| 1324 |
|
| 1325 |
def can_be_set_via_env(self, reset_environ): |
| 1326 |
os.environ["INVOKE_RUNTIME_CONFIG"] = "yaml/invoke.yaml" |
| 1327 |
with cd("configs"): |
| 1328 |
expect("-c runtime mytask") |
| 1329 |
|
| 1330 |
def cli_option_wins_over_env(self, reset_environ): |
| 1331 |
# Set env var to load the JSON config instead of the YAML one, |
| 1332 |
# which contains a "json" string internally. |
| 1333 |
os.environ["INVOKE_RUNTIME_CONFIG"] = "json/invoke.json" |
| 1334 |
with cd("configs"): |
| 1335 |
# But run the default test task, which expects a "yaml" |
| 1336 |
# string. If the env var won, this would explode. |
| 1337 |
expect("-c runtime -f yaml/invoke.yaml mytask") |
| 1338 |
|
| 1339 |
def tasks_dedupe_honors_configuration(self): |
| 1340 |
# Kinda-sorta duplicates some tests in executor.py, but eh. |
| 1341 |
with cd("configs"): |
| 1342 |
# Runtime conf file |
| 1343 |
expect( |
| 1344 |
"-c integration -f no-dedupe.yaml biz", |
| 1345 |
out=""" |
| 1346 |
foo |
| 1347 |
foo |
| 1348 |
bar |
| 1349 |
biz |
| 1350 |
post1 |
| 1351 |
post2 |
| 1352 |
post2 |
| 1353 |
""".lstrip(), |
| 1354 |
) |
| 1355 |
# Flag beats runtime |
| 1356 |
expect( |
| 1357 |
"-c integration -f dedupe.yaml --no-dedupe biz", |
| 1358 |
out=""" |
| 1359 |
foo |
| 1360 |
foo |
| 1361 |
bar |
| 1362 |
biz |
| 1363 |
post1 |
| 1364 |
post2 |
| 1365 |
post2 |
| 1366 |
""".lstrip(), |
| 1367 |
) |
| 1368 |
|
| 1369 |
# * debug (top level?) |
| 1370 |
# * hide (run.hide...lol) |
| 1371 |
# * pty (run.pty) |
| 1372 |
# * warn (run.warn) |
| 1373 |
|
| 1374 |
def env_vars_load_with_prefix(self, monkeypatch): |
| 1375 |
monkeypatch.setenv("INVOKE_RUN_ECHO", "1") |
| 1376 |
expect("-c contextualized check-echo") |
| 1377 |
|
| 1378 |
def env_var_prefix_can_be_overridden(self, monkeypatch): |
| 1379 |
monkeypatch.setenv("MYAPP_RUN_HIDE", "both") |
| 1380 |
|
| 1381 |
# This forces the execution stuff, including Executor, to run |
| 1382 |
# NOTE: it's not really possible to rework the impl so this test is |
| 1383 |
# cleaner - tasks require per-task/per-collection config, which can |
| 1384 |
# only be realized at the time a given task is to be executed. |
| 1385 |
# Unless we overhaul the Program/Executor relationship so Program |
| 1386 |
# does more of the heavy lifting re: task lookup/load/etc... |
| 1387 |
# NOTE: check-hide will kaboom if its context's run.hide is not set |
| 1388 |
# to True (default False). |
| 1389 |
class MyConf(Config): |
| 1390 |
env_prefix = "MYAPP" |
| 1391 |
|
| 1392 |
p = Program(config_class=MyConf) |
| 1393 |
p.run("inv -c contextualized check-hide") |
| 1394 |
|
| 1395 |
class other_behavior: |
| 1396 |
@patch("invoke.program.getpass.getpass") |
| 1397 |
def sudo_prompt_up_front(self, getpass): |
| 1398 |
getpass.return_value = "mypassword" |
| 1399 |
# Task under test makes expectations re: sudo config (doesn't |
| 1400 |
# actually even sudo, sudo's use of config is tested in Config |
| 1401 |
# tests) |
| 1402 |
with support_path(): |
| 1403 |
try: |
| 1404 |
Program().run( |
| 1405 |
"inv --prompt-for-sudo-password -c sudo_prompt expect-config" # noqa |
| 1406 |
) |
| 1407 |
except SystemExit as e: |
| 1408 |
# If inner call failed, we'll already have seen its output, |
| 1409 |
# and this will just ensure we ourselves are marked failed |
| 1410 |
assert e.code == 0 |
| 1411 |
# Sanity check that getpass spat out desired prompt |
| 1412 |
prompt = "Desired 'sudo.password' config value: " |
| 1413 |
getpass.assert_called_once_with(prompt) |