Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: input.stream is null when copied for namespaced commands #135

Merged
merged 4 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions cleo/application.py
Expand Up @@ -416,7 +416,9 @@ def _run(self, io: IO) -> int:
if index is not None:
del argv[index + 1 : index + 1 + (len(name.split(" ")) - 1)]

stream = io.input.stream
io.set_input(ArgvInput(argv))
io.input.set_stream(stream)

exit_code = self._run_command(command, io)
self._running_command = None
Expand Down
17 changes: 17 additions & 0 deletions tests/fixtures/foo3_command.py
@@ -0,0 +1,17 @@
from __future__ import annotations

from cleo.commands.command import Command


class Foo3Command(Command):

name = "foo3"

description = "The foo3 bar command"

aliases = ["foo3"]

def handle(self) -> int:
question = self.ask("echo:")
self.line(question)
return 0
17 changes: 17 additions & 0 deletions tests/fixtures/foo_sub_namespaced3_command.py
@@ -0,0 +1,17 @@
from __future__ import annotations

from cleo.commands.command import Command


class FooSubNamespaced3Command(Command):

name = "foo bar"

description = "The foo bar command"

aliases = ["foobar"]

def handle(self) -> int:
question = self.ask("")
self.line(question)
return 0
26 changes: 26 additions & 0 deletions tests/test_application.py
Expand Up @@ -16,9 +16,11 @@
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")
Expand Down Expand Up @@ -345,3 +347,27 @@ def test_run_with_help(tester: ApplicationTester):
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run5.txt").read_text()
)


def test_run_with_input():
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():
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"