diff --git a/cleo/application.py b/cleo/application.py index 77fc8dae..82180799 100644 --- a/cleo/application.py +++ b/cleo/application.py @@ -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 diff --git a/tests/fixtures/foo3_command.py b/tests/fixtures/foo3_command.py new file mode 100644 index 00000000..cfd20292 --- /dev/null +++ b/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 diff --git a/tests/fixtures/foo_sub_namespaced3_command.py b/tests/fixtures/foo_sub_namespaced3_command.py new file mode 100644 index 00000000..741c48b7 --- /dev/null +++ b/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 diff --git a/tests/test_application.py b/tests/test_application.py index eb93bdee..3a53b486 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -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") @@ -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"