def test_checker_warning_count(): checker = Checker("path1", "path2", "path3") checker.warnings = dict(foo=["warn"] * 3, bar=["warn"] * 5, baz=["warn"] * 7) assert checker.warning_count == 15 assert "warning_count" not in checker.__dict__
def test_checker_warned(): checker = Checker("path1", "path2", "path3") checker.warnings = dict(foo=["check"] * 3, bar=["check"] * 5, baz=["check"] * 7) assert (checker.warned == dict(foo=3, bar=5, baz=7)) assert "warned" not in checker.__dict__
def test_checker_error(log, log_type, errors, newerrors): checker = Checker("path1", "path2", "path3") log_mock = patch("tools.base.checker.Checker.log", new_callable=PropertyMock) checker.errors = errors.copy() result = 1 if newerrors else 0 with log_mock as m_log: if log_type: assert checker.error("mycheck", newerrors, log, log_type=log_type) == result else: assert checker.error("mycheck", newerrors, log) == result if not newerrors: assert not m_log.called assert "mycheck" not in checker.errors return assert checker.errors["mycheck"] == errors.get("mycheck", []) + newerrors for k, v in errors.items(): if k != "mycheck": assert checker.errors[k] == v if log: assert (list( list(c) for c in getattr(m_log.return_value, log_type or "error").call_args_list) == [[ (f'[mycheck] err{i}', ), {} ] for i in range(1, 4)]) else: assert not getattr(m_log.return_value, log_type or "error").called
def test_checker_succeeded(): checker = Checker("path1", "path2", "path3") checker.success = dict(foo=["check"] * 3, bar=["check"] * 5, baz=["check"] * 7) assert (checker.succeeded == dict(foo=3, bar=5, baz=7)) assert "succeeded" not in checker.__dict__
def test_checker_exit(patches): checker = Checker("path1", "path2", "path3") patched = patches( "Checker.error", ("Checker.log", dict(new_callable=PropertyMock)), ("Checker.stdout", dict(new_callable=PropertyMock)), prefix="tools.base.checker") with patched as (m_error, m_log, m_stdout): assert checker.exit() == m_error.return_value assert ( list(m_log.return_value.handlers.__getitem__.call_args) == [(0,), {}]) assert ( list(m_log.return_value.handlers.__getitem__.return_value.setLevel.call_args) == [(logging.FATAL,), {}]) assert ( list(m_stdout.return_value.handlers.__getitem__.call_args) == [(0,), {}]) assert ( list(m_stdout.return_value.handlers.__getitem__.return_value.setLevel.call_args) == [(logging.FATAL,), {}]) assert ( list(m_error.call_args) == [('exiting', ['Keyboard exit']), {'log_type': 'fatal'}])
def test_checker_on_check_begin(patches): checker = Checker("path1", "path2", "path3") patched = patches(("Checker.log", dict(new_callable=PropertyMock)), prefix="tools.base.checker") with patched as (m_log, ): assert not checker.on_check_begin("checkname") assert checker.active_check == "checkname" assert (list(m_log.return_value.notice.call_args) == [ ('[checkname] Running check', ), {} ])
def test_checker_get_checks(checks): checker = Checker("path1", "path2", "path3") checker.checks = ("check1", "check2", "check3") args_mock = patch("tools.base.checker.Checker.args", new_callable=PropertyMock) with args_mock as m_args: m_args.return_value.check = checks if checks: assert (checker.get_checks() == [ check for check in checker.checks if check in checks or [] ]) else: assert checker.get_checks() == checker.checks
def test_checker_path(patches, path, paths, isdir): class DummyError(Exception): pass checker = Checker("path1", "path2", "path3") patched = patches("pathlib", ("Checker.args", dict(new_callable=PropertyMock)), ("Checker.parser", dict(new_callable=PropertyMock)), prefix="tools.base.checker") with patched as (m_plib, m_args, m_parser): m_parser.return_value.error = DummyError m_args.return_value.path = path m_args.return_value.paths = paths m_plib.Path.return_value.is_dir.return_value = isdir if not path and not paths: with pytest.raises(DummyError) as e: checker.path assert (e.value.args == ( 'Missing path: `path` must be set either as an arg or with --path', )) elif not isdir: with pytest.raises(DummyError) as e: checker.path assert (e.value.args == ( 'Incorrect path: `path` must be a directory, set either as first arg or with --path', )) else: assert checker.path == m_plib.Path.return_value assert (list(m_plib.Path.call_args) == [(path or paths[0], ), {}]) assert "path" in checker.__dict__ if path or paths: assert (list(m_plib.Path.return_value.is_dir.call_args) == [(), {}])
def test_checker_constructor(): super_mock = patch("tools.base.checker.runner.Runner.__init__") with super_mock as m_super: checker = Checker("path1", "path2", "path3") assert (list(m_super.call_args) == [('path1', 'path2', 'path3'), {}]) assert checker.summary_class == CheckerSummary
def test_checker_fix(): checker = Checker("path1", "path2", "path3") args_mock = patch("tools.base.checker.Checker.args", new_callable=PropertyMock) with args_mock as m_args: assert checker.fix == m_args.return_value.fix assert "fix" not in checker.__dict__
def test_checker_on_checks_complete(patches, failed, show_summary): checker = Checker("path1", "path2", "path3") patched = patches( ("Checker.has_failed", dict(new_callable=PropertyMock)), ("Checker.show_summary", dict(new_callable=PropertyMock)), ("Checker.summary", dict(new_callable=PropertyMock)), prefix="tools.base.checker") with patched as (m_failed, m_show_summary, m_summary): m_failed.return_value = failed m_show_summary.return_value = show_summary assert checker.on_checks_complete() is (1 if failed else 0) if show_summary: assert (list(m_summary.return_value.print_summary.call_args) == [(), {}]) else: assert not m_summary.return_value.print_summary.called
def test_checker_summary(): checker = Checker("path1", "path2", "path3") summary_mock = patch("tools.base.checker.Checker.summary_class", new_callable=PropertyMock) with summary_mock as m_summary: assert checker.summary == m_summary.return_value.return_value assert (list(m_summary.return_value.call_args) == [(checker, ), {}]) assert "summary" in checker.__dict__
def test_checker_error(patches, log, errors): checker = Checker("path1", "path2", "path3") log_mock = patch("tools.base.checker.Checker.log", new_callable=PropertyMock) checker.errors = errors.copy() with log_mock as m_log: assert checker.error("mycheck", ["err1", "err2", "err3"], log) == 1 assert checker.errors["mycheck"] == errors.get( "mycheck", []) + ["err1", "err2", "err3"] for k, v in errors.items(): if k != "mycheck": assert checker.errors[k] == v if log: assert (list(m_log.return_value.error.call_args) == [ ('err1\nerr2\nerr3', ), {} ]) else: assert not m_log.return_value.error.called
def test_checker_warn(patches, log, warns): checker = Checker("path1", "path2", "path3") log_mock = patch("tools.base.checker.Checker.log", new_callable=PropertyMock) checker.warnings = warns.copy() with log_mock as m_log: checker.warn("mycheck", ["warn1", "warn2", "warn3"], log) assert checker.warnings["mycheck"] == warns.get( "mycheck", []) + ["warn1", "warn2", "warn3"] for k, v in warns.items(): if k != "mycheck": assert checker.warnings[k] == v if log: assert (list(m_log.return_value.warning.call_args) == [ ('warn1\nwarn2\nwarn3', ), {} ]) else: assert not m_log.return_value.warn.called
def test_checker_succeed(patches, log, success): checker = Checker("path1", "path2", "path3") log_mock = patch("tools.base.checker.Checker.log", new_callable=PropertyMock) checker.success = success.copy() with log_mock as m_log: checker.succeed("mycheck", ["success1", "success2", "success3"], log) assert checker.success["mycheck"] == success.get( "mycheck", []) + ["success1", "success2", "success3"] for k, v in success.items(): if k != "mycheck": assert checker.success[k] == v if log: assert (list(m_log.return_value.info.call_args) == [ ('success1\nsuccess2\nsuccess3', ), {} ]) else: assert not m_log.return_value.info.called
def test_checker_paths(patches, paths): checker = Checker("path1", "path2", "path3") patched = patches(("Checker.args", dict(new_callable=PropertyMock)), ("Checker.path", dict(new_callable=PropertyMock)), prefix="tools.base.checker") with patched as (m_args, m_path): m_args.return_value.paths = paths result = checker.paths if paths: assert result == paths else: assert result == [m_path.return_value] assert "paths" not in checker.__dict__
def test_checker_has_failed(patches, failed, warned): checker = Checker("path1", "path2", "path3") patched = patches(("Checker.failed", dict(new_callable=PropertyMock)), ("Checker.warned", dict(new_callable=PropertyMock)), prefix="tools.base.checker") with patched as (m_failed, m_warned): m_failed.return_value = failed m_warned.return_value = warned result = checker.has_failed if failed or warned: assert result is True else: assert result is False assert "has_failed" not in checker.__dict__
def test_checker_show_summary(patches, summary, error_count, warning_count): checker = Checker("path1", "path2", "path3") patched = patches( ("Checker.args", dict(new_callable=PropertyMock)), ("Checker.error_count", dict(new_callable=PropertyMock)), ("Checker.warning_count", dict(new_callable=PropertyMock)), prefix="tools.base.checker") with patched as (m_args, m_errors, m_warnings): m_args.return_value.summary = summary m_errors.return_value = error_count m_warnings.return_value = warning_count result = checker.show_summary if summary or error_count or warning_count: assert result is True else: assert result is False assert "show_summary" not in checker.__dict__
def test_checker_status(patches): checker = Checker("path1", "path2", "path3") patched = patches( ("Checker.success_count", dict(new_callable=PropertyMock)), ("Checker.error_count", dict(new_callable=PropertyMock)), ("Checker.warning_count", dict(new_callable=PropertyMock)), ("Checker.failed", dict(new_callable=PropertyMock)), ("Checker.warned", dict(new_callable=PropertyMock)), ("Checker.succeeded", dict(new_callable=PropertyMock)), prefix="tools.base.checker") with patched as args: (m_success_count, m_error_count, m_warning_count, m_failed, m_warned, m_succeeded) = args assert (checker.status == dict(success=m_success_count.return_value, errors=m_error_count.return_value, warnings=m_warning_count.return_value, failed=m_failed.return_value, warned=m_warned.return_value, succeeded=m_succeeded.return_value)) assert "status" not in checker.__dict__
def test_checker_on_check_run(patches, errors, warnings, exiting): checker = Checker("path1", "path2", "path3") patched = patches( ("Checker.exiting", dict(new_callable=PropertyMock)), ("Checker.log", dict(new_callable=PropertyMock)), prefix="tools.base.checker") check = "CHECK1" checker.errors = errors checker.warnings = warnings checker._active_check = check with patched as (m_exit, m_log): m_exit.return_value = exiting assert not checker.on_check_run(check) assert checker.active_check is None if exiting: assert not m_log.called return if check in errors: assert ( list(m_log.return_value.error.call_args) == [('[CHECK1] Check failed',), {}]) assert not m_log.return_value.warning.called assert not m_log.return_value.success.called return if check in warnings: assert ( list(m_log.return_value.warning.call_args) == [('[CHECK1] Check has warnings',), {}]) assert not m_log.return_value.error.called assert not m_log.return_value.info.called return assert ( list(m_log.return_value.success.call_args) == [(f'[{check}] Check completed successfully',), {}]) assert not m_log.return_value.warning.called assert not m_log.return_value.error.called
def test_checker_failed(): checker = Checker("path1", "path2", "path3") checker.errors = dict(foo=["err"] * 3, bar=["err"] * 5, baz=["err"] * 7) assert checker.failed == {'foo': 3, 'bar': 5, 'baz': 7} assert "failed" not in checker.__dict__
def test_checker_error_count(): checker = Checker("path1", "path2", "path3") checker.errors = dict(foo=["err"] * 3, bar=["err"] * 5, baz=["err"] * 7) assert checker.error_count == 15 assert "error_count" not in checker.__dict__
def test_checker_exiting(errors): checker = Checker("path1", "path2", "path3") checker.errors = errors assert checker.exiting == bool("exiting" in errors) assert "exiting" not in checker.__dict__
def test_checker_on_checks_begin(): checker = Checker("path1", "path2", "path3") assert checker.on_checks_begin() is None
def test_checker_on_check_run(): checker = Checker("path1", "path2", "path3") assert not checker.on_check_run("checkname")