From 8666f4bbca1e5a6159ab222a38cdf67a190b06f4 Mon Sep 17 00:00:00 2001 From: Chad Crawford Date: Mon, 30 May 2022 21:34:01 -0400 Subject: [PATCH 1/4] Fix issue where input copies did not include stream, leading to null errors in some cases. --- cleo/application.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cleo/application.py b/cleo/application.py index 77fc8dae..7ec8345b 100644 --- a/cleo/application.py +++ b/cleo/application.py @@ -373,7 +373,9 @@ def _run(self, io: IO) -> int: if io.input.has_parameter_option(["--help", "-h"], True): if not name: name = "help" + stream = io.input.stream io.set_input(ArgvInput(["console", "help", self._default_command])) + io.input.set_stream(stream) else: self._want_helps = True @@ -416,7 +418,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 From 364e8c381ab3192872910d1a9c2d5766ac65ff38 Mon Sep 17 00:00:00 2001 From: Chad Crawford Date: Tue, 24 May 2022 08:51:15 -0700 Subject: [PATCH 2/4] Add appropriate tests. --- cleo/application.py | 2 -- tests/commands/test_command.py | 13 ++++++++++ tests/fixtures/foo3_command.py | 15 +++++++++++ tests/fixtures/foo_sub_namespaced3_command.py | 16 ++++++++++++ tests/test_application.py | 26 +++++++++++++++++++ 5 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/foo3_command.py create mode 100644 tests/fixtures/foo_sub_namespaced3_command.py diff --git a/cleo/application.py b/cleo/application.py index 7ec8345b..82180799 100644 --- a/cleo/application.py +++ b/cleo/application.py @@ -373,9 +373,7 @@ def _run(self, io: IO) -> int: if io.input.has_parameter_option(["--help", "-h"], True): if not name: name = "help" - stream = io.input.stream io.set_input(ArgvInput(["console", "help", self._default_command])) - io.input.set_stream(stream) else: self._want_helps = True diff --git a/tests/commands/test_command.py b/tests/commands/test_command.py index c2616261..95bed21e 100644 --- a/tests/commands/test_command.py +++ b/tests/commands/test_command.py @@ -39,6 +39,19 @@ def handle(self): self.line(",".join(foos)) +class MyNamespacedCommand(Command): + name = "test three" + description = "Command testing" + + arguments = [argument("foo", "Bar", multiple=True)] + def handle(self): + foos = self.argument("foo") + + repeat = self.ask("Simon says:") + self.line(",".join(foos)) + self.line(repeat) + + def test_set_application(): application = Application() command = Command() diff --git a/tests/fixtures/foo3_command.py b/tests/fixtures/foo3_command.py new file mode 100644 index 00000000..5cf6f378 --- /dev/null +++ b/tests/fixtures/foo3_command.py @@ -0,0 +1,15 @@ +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..5f87b96a --- /dev/null +++ b/tests/fixtures/foo_sub_namespaced3_command.py @@ -0,0 +1,16 @@ +from cleo.commands.command import Command +from cleo.io.io import IO + + +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..5645c452 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 "Hello world!\n" == tester.io.fetch_output() + + +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 "Hello world!\n" == tester.io.fetch_output() From fda5f6f4483b09f4a6a553eb6bbc9b481874ea36 Mon Sep 17 00:00:00 2001 From: Chad Crawford Date: Mon, 30 May 2022 21:41:33 -0400 Subject: [PATCH 3/4] Fix to meet pre-commit requirements. --- tests/commands/test_command.py | 1 + tests/fixtures/foo3_command.py | 2 ++ tests/fixtures/foo_sub_namespaced3_command.py | 3 ++- tests/test_application.py | 4 ++-- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/commands/test_command.py b/tests/commands/test_command.py index 95bed21e..7475ace1 100644 --- a/tests/commands/test_command.py +++ b/tests/commands/test_command.py @@ -44,6 +44,7 @@ class MyNamespacedCommand(Command): description = "Command testing" arguments = [argument("foo", "Bar", multiple=True)] + def handle(self): foos = self.argument("foo") diff --git a/tests/fixtures/foo3_command.py b/tests/fixtures/foo3_command.py index 5cf6f378..cfd20292 100644 --- a/tests/fixtures/foo3_command.py +++ b/tests/fixtures/foo3_command.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from cleo.commands.command import Command diff --git a/tests/fixtures/foo_sub_namespaced3_command.py b/tests/fixtures/foo_sub_namespaced3_command.py index 5f87b96a..741c48b7 100644 --- a/tests/fixtures/foo_sub_namespaced3_command.py +++ b/tests/fixtures/foo_sub_namespaced3_command.py @@ -1,5 +1,6 @@ +from __future__ import annotations + from cleo.commands.command import Command -from cleo.io.io import IO class FooSubNamespaced3Command(Command): diff --git a/tests/test_application.py b/tests/test_application.py index 5645c452..3a53b486 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -358,7 +358,7 @@ def test_run_with_input(): status_code = tester.execute("foo3", inputs="Hello world!") assert status_code == 0 - assert "Hello world!\n" == tester.io.fetch_output() + assert tester.io.fetch_output() == "Hello world!\n" def test_run_namespaced_with_input(): @@ -370,4 +370,4 @@ def test_run_namespaced_with_input(): status_code = tester.execute("foo bar", inputs="Hello world!") assert status_code == 0 - assert "Hello world!\n" == tester.io.fetch_output() + assert tester.io.fetch_output() == "Hello world!\n" From 07128cc01ba3dada709f7e0e89db9ba6d8a54579 Mon Sep 17 00:00:00 2001 From: Chad Crawford Date: Mon, 30 May 2022 21:43:28 -0400 Subject: [PATCH 4/4] Remove unneeded test in test_command.py --- tests/commands/test_command.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/tests/commands/test_command.py b/tests/commands/test_command.py index 7475ace1..c2616261 100644 --- a/tests/commands/test_command.py +++ b/tests/commands/test_command.py @@ -39,20 +39,6 @@ def handle(self): self.line(",".join(foos)) -class MyNamespacedCommand(Command): - name = "test three" - description = "Command testing" - - arguments = [argument("foo", "Bar", multiple=True)] - - def handle(self): - foos = self.argument("foo") - - repeat = self.ask("Simon says:") - self.line(",".join(foos)) - self.line(repeat) - - def test_set_application(): application = Application() command = Command()