3
0
Fork 0

Add --stdout and --stderr args for base Command class

also refactor its `run()` method to allow more customizing ability
This commit is contained in:
Lance Edgar 2023-11-22 09:11:36 -06:00
parent 13472a5ab5
commit 5c4dcb09f3
2 changed files with 135 additions and 38 deletions

View file

@ -7,9 +7,10 @@ from unittest.mock import MagicMock, patch
from wuttjamaican.commands import base
from wuttjamaican.commands.setup import Setup
from wuttjamaican.testing import FileConfigTestCase
class TestCommand(TestCase):
class TestCommand(FileConfigTestCase):
def test_base(self):
# base command is for 'wutta' and has a 'setup' subcommand
@ -133,6 +134,37 @@ class TestCommand(TestCase):
cmd.run('hello', '--foo')
run_with.assert_called_once_with(foo=True)
def test_run_uses_stdout_stderr_params(self):
myout = self.write_file('my.out', '')
myerr = self.write_file('my.err', '')
class Hello(base.Subcommand):
name = 'hello'
def run(self, args):
self.stdout.write("hello world")
self.stderr.write("error text")
with patch('wuttjamaican.commands.base.sys') as sys:
cmd = base.Command(subcommands={'hello': Hello})
# sys.stdout and sys.stderr should be used by default
cmd.run('hello')
sys.exit.assert_not_called()
sys.stdout.write.assert_called_once_with('hello world')
sys.stderr.write.assert_called_once_with('error text')
# but our files may be used instead if specified
sys.reset_mock()
cmd.run('hello', '--stdout', myout, '--stderr', myerr)
sys.exit.assert_not_called()
sys.stdout.write.assert_not_called()
sys.stderr.write.assert_not_called()
with open(myout, 'rt') as f:
self.assertEqual(f.read(), 'hello world')
with open(myerr, 'rt') as f:
self.assertEqual(f.read(), 'error text')
class TestCommandArgumentParser(TestCase):