1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
from __future__ import annotations
import os
import sys
from pathlib import Path
import pytest
from cleo.application import Application
from cleo.commands.command import Command
from cleo.exceptions import CleoCommandNotFoundError
from cleo.exceptions import CleoNamespaceNotFoundError
from cleo.io.io import IO
from cleo.io.outputs.stream_output import StreamOutput
from cleo.testers.application_tester import ApplicationTester
from tests.fixtures.foo1_command import Foo1Command
from tests.fixtures.foo2_command import Foo2Command
from tests.fixtures.foo3_command import Foo3Command
from tests.fixtures.foo_command import FooCommand
from tests.fixtures.foo_sub_namespaced1_command import FooSubNamespaced1Command
from tests.fixtures.foo_sub_namespaced2_command import FooSubNamespaced2Command
from tests.fixtures.foo_sub_namespaced3_command import FooSubNamespaced3Command
FIXTURES_PATH = Path(__file__).parent.joinpath("fixtures")
@pytest.fixture()
def app() -> Application:
return Application()
@pytest.fixture()
def tester(app: Application) -> ApplicationTester:
app.catch_exceptions(False)
return ApplicationTester(app)
def test_name_version_getters() -> None:
app = Application("foo", "bar")
assert app.name == "foo"
assert app.display_name == "Foo"
assert app.version == "bar"
def test_name_version_setter() -> None:
app = Application("foo", "bar")
app.set_name("bar")
app.set_version("foo")
assert app.name == "bar"
assert app.display_name == "Bar"
assert app.version == "foo"
app.set_display_name("Baz")
assert app.display_name == "Baz"
def test_long_version() -> None:
app = Application("foo", "bar")
assert app.long_version == "<b>Foo</b> (version <c1>bar</c1>)"
def test_help(app: Application) -> None:
assert app.help == FIXTURES_PATH.joinpath("application_help.txt").read_text()
def test_all(app: Application) -> None:
commands = app.all()
assert isinstance(commands["help"], Command)
app.add(FooCommand())
assert len(app.all("foo")) == 1
def test_add(app: Application) -> None:
foo = FooCommand()
app.add(foo)
commands = app.all()
assert [commands["foo bar"]] == [foo]
foo1 = Foo1Command()
app.add(foo1)
commands = app.all()
assert [commands["foo bar"], commands["foo bar1"]] == [foo, foo1]
def test_has_get(app: Application) -> None:
assert app.has("list")
assert not app.has("afoobar")
foo = FooCommand()
app.add(foo)
assert app.has("foo bar")
assert app.has("afoobar")
assert app.get("foo bar") == foo
assert app.get("afoobar") == foo
def test_silent_help(app: Application) -> None:
app.catch_exceptions(False)
tester = ApplicationTester(app)
tester.execute("-h -q", decorated=False)
assert tester.io.fetch_output() == ""
def test_get_namespaces(app: Application) -> None:
app.add(FooCommand())
app.add(Foo1Command())
assert app.get_namespaces() == ["foo"]
def test_find_namespace(app: Application) -> None:
app.add(FooCommand())
assert app.find_namespace("foo") == "foo"
def test_find_namespace_with_sub_namespaces(app: Application) -> None:
app.add(FooSubNamespaced1Command())
app.add(FooSubNamespaced2Command())
assert app.find_namespace("foo") == "foo"
def test_find_ambiguous_namespace(app: Application) -> None:
app.add(FooCommand())
app.add(Foo2Command())
with pytest.raises(
CleoNamespaceNotFoundError,
match=(
r'There are no commands in the "f" namespace\.\n\n'
r"Did you mean this\?\n foo"
),
):
app.find_namespace("f")
def test_find_invalid_namespace(app: Application) -> None:
app.add(FooCommand())
app.add(Foo2Command())
with pytest.raises(
CleoNamespaceNotFoundError,
match=r'There are no commands in the "bar" namespace\.',
):
app.find_namespace("bar")
def test_find_unique_name_but_namespace_name(app: Application) -> None:
app.add(FooCommand())
app.add(Foo1Command())
app.add(Foo2Command())
with pytest.raises(
CleoCommandNotFoundError,
match=r'The command "foo1" does not exist\.',
):
app.find("foo1")
def test_find(app: Application) -> None:
app.add(FooCommand())
assert isinstance(app.find("foo bar"), FooCommand)
assert isinstance(app.find("afoobar"), FooCommand)
def test_find_ambiguous_command(app: Application) -> None:
app.add(FooCommand())
with pytest.raises(
CleoCommandNotFoundError,
match=(
r'The command "foo b" does not exist\.\n\nDid you mean this\?\n foo bar'
),
):
app.find("foo b")
def test_find_ambiguous_command_hidden(app: Application) -> None:
foo = FooCommand()
foo.hidden = True
app.add(foo)
with pytest.raises(
CleoCommandNotFoundError,
match=r'The command "foo b" does not exist\.$',
):
app.find("foo b")
def test_set_catch_exceptions(app: Application, environ: dict[str, str]) -> None:
app.auto_exits(False)
os.environ["COLUMNS"] = "120"
tester = ApplicationTester(app)
app.catch_exceptions(True)
assert app.are_exceptions_caught()
tester.execute("foo", decorated=False)
assert tester.io.fetch_output() == ""
assert (
tester.io.fetch_error()
== FIXTURES_PATH.joinpath("application_exception1.txt").read_text()
)
app.catch_exceptions(False)
with pytest.raises(CleoCommandNotFoundError):
tester.execute("foo", decorated=False)
def test_auto_exit(app: Application) -> None:
app.auto_exits(False)
assert not app.is_auto_exit_enabled()
app.auto_exits()
assert app.is_auto_exit_enabled()
def test_run(app: Application, argv: list[str]) -> None:
app.catch_exceptions(False)
app.auto_exits(False)
command = Foo1Command()
app.add(command)
sys.argv = ["console", "foo bar1"]
app.run()
assert isinstance(command.io, IO)
assert isinstance(command.io.output, StreamOutput)
assert isinstance(command.io.error_output, StreamOutput)
assert command.io.output.stream == sys.stdout
assert command.io.error_output.stream == sys.stderr
def test_run_runs_the_list_command_without_arguments(tester: ApplicationTester) -> None:
tester.execute("", decorated=False)
assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run1.txt").read_text()
)
def test_run_runs_help_command_if_required(tester: ApplicationTester) -> None:
tester.execute("--help", decorated=False)
assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run2.txt").read_text()
)
tester.execute("-h", decorated=False)
assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run2.txt").read_text()
)
def test_run_runs_help_command_with_command(tester: ApplicationTester) -> None:
tester.execute("--help list", decorated=False)
assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run3.txt").read_text()
)
tester.execute("list -h", decorated=False)
assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run3.txt").read_text()
)
def test_run_removes_all_output_if_quiet(tester: ApplicationTester) -> None:
tester.execute("list --quiet")
assert tester.io.fetch_output() == ""
tester.execute("list -q")
assert tester.io.fetch_output() == ""
def test_run_with_verbosity(tester: ApplicationTester) -> None:
tester.execute("list --verbose")
assert tester.io.is_verbose()
tester.execute("list -v")
assert tester.io.is_verbose()
tester.execute("list -vv")
assert tester.io.is_very_verbose()
tester.execute("list -vvv")
assert tester.io.is_debug()
def test_run_with_version(tester: ApplicationTester) -> None:
tester.execute("--version")
assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run4.txt").read_text()
)
tester.execute("-V")
assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run4.txt").read_text()
)
def test_run_with_help(tester: ApplicationTester) -> None:
tester.execute("help --help")
assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run5.txt").read_text()
)
tester.execute("-h help")
assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run5.txt").read_text()
)
def test_run_with_input() -> None:
app = Application()
command = Foo3Command()
app.add(command)
tester = ApplicationTester(app)
status_code = tester.execute("foo3", inputs="Hello world!")
assert status_code == 0
assert tester.io.fetch_output() == "Hello world!\n"
def test_run_namespaced_with_input() -> None:
app = Application()
command = FooSubNamespaced3Command()
app.add(command)
tester = ApplicationTester(app)
status_code = tester.execute("foo bar", inputs="Hello world!")
assert status_code == 0
assert tester.io.fetch_output() == "Hello world!\n"
@pytest.mark.parametrize("cmd", (Foo3Command(), FooSubNamespaced3Command()))
def test_run_with_input_and_non_interactive(cmd: Command) -> None:
app = Application()
app.add(cmd)
tester = ApplicationTester(app)
status_code = tester.execute(f"--no-interaction {cmd.name}", inputs="Hello world!")
assert status_code == 0
assert tester.io.fetch_output() == "default input\n"
|