import contextlib import io import tempfile import unittest from lesana import command class Args: def __init__(self, args): self.args = args def __getattribute__(self, k): try: return super().__getattribute__(k) except AttributeError: try: return self.args[k] except KeyError as e: raise AttributeError(e) class testCommands(unittest.TestCase): def setUp(self): self.collection_dir = tempfile.TemporaryDirectory() def tearDown(self): pass def _edit_file(self, filepath): return True def _run_command(self, cmd, args): stream = { 'stdout': io.StringIO(), 'stderr': io.StringIO(), } cmd.edit_file_in_external_editor = self._edit_file cmd.args = Args(args) with contextlib.redirect_stdout(stream['stdout']): with contextlib.redirect_stderr(stream['stderr']): cmd.main() return stream def test_init(self): args = { 'collection': self.collection_dir.name, 'git': True, } streams = self._run_command(command.Init(), args) self.assertEqual(streams['stdout'].getvalue(), '') self.assertEqual(streams['stderr'].getvalue(), '') if __name__ == '__main__': unittest.main()