Skip to content

Commit 8c3fb5b

Browse files
authored
fix: input.stream is null when copied for namespaced commands (#135)
* 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 <[email protected]>
1 parent 5cb93da commit 8c3fb5b

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

cleo/application.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,9 @@ def _run(self, io: IO) -> int:
416416
if index is not None:
417417
del argv[index + 1 : index + 1 + (len(name.split(" ")) - 1)]
418418

419+
stream = io.input.stream
419420
io.set_input(ArgvInput(argv))
421+
io.input.set_stream(stream)
420422

421423
exit_code = self._run_command(command, io)
422424
self._running_command = None

tests/fixtures/foo3_command.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from __future__ import annotations
2+
3+
from cleo.commands.command import Command
4+
5+
6+
class Foo3Command(Command):
7+
8+
name = "foo3"
9+
10+
description = "The foo3 bar command"
11+
12+
aliases = ["foo3"]
13+
14+
def handle(self) -> int:
15+
question = self.ask("echo:")
16+
self.line(question)
17+
return 0
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from __future__ import annotations
2+
3+
from cleo.commands.command import Command
4+
5+
6+
class FooSubNamespaced3Command(Command):
7+
8+
name = "foo bar"
9+
10+
description = "The foo bar command"
11+
12+
aliases = ["foobar"]
13+
14+
def handle(self) -> int:
15+
question = self.ask("")
16+
self.line(question)
17+
return 0

tests/test_application.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
from cleo.testers.application_tester import ApplicationTester
1717
from tests.fixtures.foo1_command import Foo1Command
1818
from tests.fixtures.foo2_command import Foo2Command
19+
from tests.fixtures.foo3_command import Foo3Command
1920
from tests.fixtures.foo_command import FooCommand
2021
from tests.fixtures.foo_sub_namespaced1_command import FooSubNamespaced1Command
2122
from tests.fixtures.foo_sub_namespaced2_command import FooSubNamespaced2Command
23+
from tests.fixtures.foo_sub_namespaced3_command import FooSubNamespaced3Command
2224

2325

2426
FIXTURES_PATH = Path(__file__).parent.joinpath("fixtures")
@@ -345,3 +347,27 @@ def test_run_with_help(tester: ApplicationTester):
345347
tester.io.fetch_output()
346348
== FIXTURES_PATH.joinpath("application_run5.txt").read_text()
347349
)
350+
351+
352+
def test_run_with_input():
353+
app = Application()
354+
command = Foo3Command()
355+
app.add(command)
356+
357+
tester = ApplicationTester(app)
358+
status_code = tester.execute("foo3", inputs="Hello world!")
359+
360+
assert status_code == 0
361+
assert tester.io.fetch_output() == "Hello world!\n"
362+
363+
364+
def test_run_namespaced_with_input():
365+
app = Application()
366+
command = FooSubNamespaced3Command()
367+
app.add(command)
368+
369+
tester = ApplicationTester(app)
370+
status_code = tester.execute("foo bar", inputs="Hello world!")
371+
372+
assert status_code == 0
373+
assert tester.io.fetch_output() == "Hello world!\n"

0 commit comments

Comments
 (0)