Skip to content

Commit

Permalink
fix: input.stream is null when copied for namespaced commands (#135)
Browse files Browse the repository at this point in the history
* Fix issue where input copies did not include stream, leading to null errors in some cases.

* Add appropriate tests.

* Fix to meet pre-commit requirements.

* Remove unneeded test in test_command.py

Co-authored-by: Chad Crawford <chadcr@amazon.com>
  • Loading branch information
chadac and chadac committed May 31, 2022
1 parent 5cb93da commit 8c3fb5b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cleo/application.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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"

0 comments on commit 8c3fb5b

Please sign in to comment.