def test_base_runner_log(patches): run = runner.BaseRunner("path1", "path2", "path3") patched = patches( "logging.getLogger", "LogFilter", "coloredlogs", "verboselogs", ("BaseRunner.log_level", dict(new_callable=PropertyMock)), ("BaseRunner.log_level_styles", dict(new_callable=PropertyMock)), ("BaseRunner.log_field_styles", dict(new_callable=PropertyMock)), ("BaseRunner.log_fmt", dict(new_callable=PropertyMock)), ("BaseRunner.name", dict(new_callable=PropertyMock)), prefix="tools.base.runner") with patched as patchy: (m_logger, m_filter, m_color, m_verb, m_level, m_lstyle, m_fstyle, m_fmt, m_name) = patchy assert run.log == m_logger.return_value assert (list(m_verb.install.call_args) == [(), {}]) assert (list(m_logger.return_value.setLevel.call_args) == [ (m_level.return_value, ), {} ]) assert (list(m_logger.return_value.setLevel.call_args) == [ (m_level.return_value, ), {} ]) assert (list(m_color.install.call_args) == [(), { 'fmt': m_fmt.return_value, 'isatty': True, 'field_styles': m_fstyle.return_value, 'level': 'DEBUG', 'level_styles': m_lstyle.return_value, 'logger': m_logger.return_value }]) assert "log" in run.__dict__
def test_base_runner_stdout(patches): run = runner.BaseRunner("path1", "path2", "path3") patched = patches( "logging", ("BaseRunner.log_level", dict(new_callable=PropertyMock)), prefix="tools.base.runner") with patched as (m_log, m_level): assert run.stdout == m_log.getLogger.return_value assert ( list(m_log.getLogger.call_args) == [('stdout',), {}]) assert ( list(m_log.getLogger.return_value.setLevel.call_args) == [(m_level.return_value,), {}]) assert ( list(m_log.StreamHandler.call_args) == [(sys.stdout,), {}]) assert ( list(m_log.Formatter.call_args) == [('%(message)s',), {}]) assert ( list(m_log.StreamHandler.return_value.setFormatter.call_args) == [(m_log.Formatter.return_value,), {}]) assert ( list(m_log.getLogger.return_value.addHandler.call_args) == [(m_log.StreamHandler.return_value,), {}])
def test_base_runner_path(patches): run = runner.BaseRunner("path1", "path2", "path3") patched = patches("pathlib", prefix="tools.base.runner") with patched as (m_plib, ): assert run.path == m_plib.Path.return_value assert (list(m_plib.Path.call_args) == [(".", ), {}])
def test_base_runner_parser(patches): run = runner.BaseRunner("path1", "path2", "path3") patched = patches("argparse.ArgumentParser", "BaseRunner.add_arguments", prefix="tools.base.runner") with patched as (m_parser, m_add_args): assert run.parser == m_parser.return_value assert (list(m_parser.call_args) == [(), {"allow_abbrev": False}]) assert (list(m_add_args.call_args) == [(m_parser.return_value, ), {}]) assert "parser" in run.__dict__
def test_base_runner_add_arguments(): run = runner.BaseRunner("path1", "path2", "path3") parser = MagicMock() assert run.add_arguments(parser) is None assert ( list(list(c) for c in parser.add_argument.call_args_list) == [[('--log-level', '-l'), {'choices': ['debug', 'info', 'warn', 'error'], 'default': 'info', 'help': 'Log level to display'}], ])
def test_base_runner_log_level(patches): run = runner.BaseRunner("path1", "path2", "path3") patched = patches("dict", ("BaseRunner.args", dict(new_callable=PropertyMock)), prefix="tools.base.runner") with patched as (m_dict, m_args): assert run.log_level == m_dict.return_value.__getitem__.return_value assert (list(m_dict.call_args) == [(runner.LOG_LEVELS, ), {}]) assert (list(m_dict.return_value.__getitem__.call_args) == [ (m_args.return_value.log_level, ), {} ]) assert "log_level" in run.__dict__
def test_base_runner_extra_args(): run = runner.BaseRunner("path1", "path2", "path3") parser_mock = patch("tools.base.runner.BaseRunner.parser", new_callable=PropertyMock) with parser_mock as m_parser: assert run.extra_args == m_parser.return_value.parse_known_args.return_value.__getitem__.return_value assert (list(m_parser.return_value.parse_known_args.call_args) == [ (('path1', 'path2', 'path3'), ), {} ]) assert (list(m_parser.return_value.parse_known_args.return_value. __getitem__.call_args) == [(1, ), {}]) assert "extra_args" in run.__dict__
def test_base_runner__cleanup_tempdir(patches, cached): run = runner.BaseRunner() patched = patches(("BaseRunner.tempdir", dict(new_callable=PropertyMock)), prefix="tools.base.runner") if cached: run.__dict__["tempdir"] = "TEMPDIR" with patched as (m_temp, ): assert not run._cleanup_tempdir() if cached: assert (list(m_temp.return_value.cleanup.call_args) == [(), {}]) else: assert not m_temp.called assert "tempdir" not in run.__dict__
def _runner_factory(): if not has_fun: return runner.BaseRunner() class _Wrap: if cleansup: __cleansup__ = True class _Wrapper: if is_wrapped: __wrapped__ = _Wrap() class DummyRunner(runner.BaseRunner): run = _Wrapper() return DummyRunner()
def test_base_runner_tempdir(patches, missing): run = runner.BaseRunner() patched = patches( "tempfile", ("BaseRunner.log", dict(new_callable=PropertyMock)), ("BaseRunner._missing_cleanup", dict(new_callable=PropertyMock)), prefix="tools.base.runner") with patched as (m_tmp, m_log, m_missing): m_missing.return_value = missing assert run.tempdir == m_tmp.TemporaryDirectory.return_value if missing: assert (list(m_log.return_value.warning.call_args) == [( "Tempdir created but instance has a `run` method which is not decorated with `@runner.cleansup`", ), {}]) else: assert not m_log.called assert (list(m_tmp.TemporaryDirectory.call_args) == [(), {}]) assert "tempdir" in run.__dict__
def test_base_runner_constructor(): run = runner.BaseRunner("path1", "path2", "path3") assert run._args == ("path1", "path2", "path3") assert run.log_field_styles == runner.LOG_FIELD_STYLES assert run.log_level_styles == runner.LOG_LEVEL_STYLES assert run.log_fmt == runner.LOG_FMT