def test_error_reporting(self): with fetch_std_streams() as (stdout, stderr): plugin._report_plugin_error_msg('XXX') self.assertEqual(stderr.getvalue(), 'error: XXX\n') with fetch_std_streams() as (stdout, stderr): plugin._report_plugin_exception('YYY') self.assertEqual(stderr.getvalue(), 'error: YYY\n')
def assert_main(self, args: Union[None, List[str]], expected_status: int = 0, expected_stdout: Union[None, str, List[str]] = None, expected_stderr: Union[None, str, List[str]] = '') -> None: with fetch_std_streams() as (stdout, stderr): actual_status = main.main(args=args) self.assertEqual( actual_status, expected_status, msg='args = %s\n' 'status = %s\n' 'stdout = [%s]\n' 'stderr = [%s]' % (args, actual_status, stdout.getvalue(), stderr.getvalue())) print(stdout.getvalue()) if isinstance(expected_stdout, str): self.assertEqual(expected_stdout, stdout.getvalue()) elif expected_stdout: for item in expected_stdout: self.assertIn(item, stdout.getvalue()) if isinstance(expected_stderr, str): self.assertEqual(expected_stderr, stderr.getvalue()) elif expected_stderr: for item in expected_stderr: self.assertIn(item, stderr.getvalue())
def test_cancel(self): m = ConsoleMonitor() with fetch_std_streams(): m.start('task A', total_work=10) self.assertFalse(m.is_cancelled()) m.cancel() self.assertTrue(m.is_cancelled())
def test_console_monitor_with_progress_bar(self): m = ConsoleMonitor(progress_bar_size=10) with fetch_std_streams() as (stdout, stderr): m.start('task A', total_work=10) m.progress(work=1) m.progress(work=5, msg='phase 1') m.progress(msg='phase 2') m.progress(work=4) m.progress() m.done() actual_stdout = stdout.getvalue() expected_stdout = 'task A: started\n' + \ 'task A: [#---------] 10% \n' + \ 'task A: [######----] 60% phase 1\n' + \ 'task A: phase 2\n' + \ 'task A: [##########] 100% \n' + \ 'task A: in progress...\n' + \ 'task A: done\n' self.assertEqual(actual_stdout, expected_stdout) self.assertEqual(stderr.getvalue(), '')
def test_console_monitor(self): m = ConsoleMonitor() with fetch_std_streams() as (stdout, stderr): m.start('task A', total_work=10) m.progress(work=1) m.progress(work=5, msg='phase 1') m.progress(msg='phase 2') m.progress(work=4) m.progress() m.done() actual_stdout = stdout.getvalue() expected_stdout = 'task A: started\n' + \ 'task A: 10%\n' + \ 'task A: 60% phase 1\n' + \ 'task A: phase 2\n' + \ 'task A: 100%\n' + \ 'task A: progress\n' + \ 'task A: done\n' self.assertEqual(actual_stdout, expected_stdout) self.assertEqual(stderr.getvalue(), '') self.assertTrue(True)
def assert_main(self, args: Union[None, List[str]], expected_status: int = 0, expected_stdout: Union[None, str, List[str]] = None, expected_stderr: Union[None, str, List[str]] = '') -> None: with fetch_std_streams() as (stdout, stderr): actual_status = main.main(args=args) self.assertEqual(actual_status, expected_status, msg='args = %s\n' 'status = %s\n' 'stdout = [%s]\n' 'stderr = [%s]' % (args, actual_status, stdout.getvalue(), stderr.getvalue())) print(stdout.getvalue()) if isinstance(expected_stdout, str): self.assertEqual(expected_stdout, stdout.getvalue()) elif expected_stdout: for item in expected_stdout: self.assertIn(item, stdout.getvalue()) if isinstance(expected_stderr, str): self.assertEqual(expected_stderr, stderr.getvalue()) elif expected_stderr: for item in expected_stderr: self.assertIn(item, stderr.getvalue())
def test_label_required(self): monitor = ConsoleMonitor() with fetch_std_streams(): with self.assertRaises(ValueError): # "ValueError: label must be given" monitor.start('', total_work=10)