def test_default_values(self): """Test :class:`codechecker.checker.task.CheckResult` default values.""" # pylint: disable=no-self-use checker_result = CheckResult('taskname') expected_checker_result = CheckResult( 'taskname', status=CheckResult.SUCCESS, summary=None, message=None ) assert_checkresult_equal(expected_checker_result, checker_result)
def test_task_returns_CheckResult(self): """Checker task should return :class:`codechecker.checker.task.CheckResult`""" # pylint: disable=no-self-use task = CheckerTask('taskname', 'command') result = task() expected_result = CheckResult('taskname') assert_checkresult_equal(expected_result, result)
def test_CheckResult_has_error_status_if_command_fails(self): """Task should fail if command exits with non zero status""" task = CheckerTask('taskname', 'command') errmsg = 'error message' self.patch_shellcommand_result(stdout=errmsg, returncode=1) result = task() expected_result = CheckResult('taskname', CheckResult.ERROR, message=errmsg) assert_checkresult_equal(expected_result, result)
def test_pass_if_code_rate_is_10(self): """Test if result is determined by function assigned to result_creator attribute.""" shell_output = create_pylint_output(10) self.patch_shellcommand_result(stdout=shell_output) taskname = 'pylint' task = create_pylint_task(taskname=taskname) task.result_creator = create_pylint_result result = task() expected_result = CheckResult(taskname) assert_checkresult_equal(expected_result, result)
def create_pylint_result(task, _, shell_output) -> CheckResult: """Create check result for pylint checker. .. list-table:: Result status :header-rows: 1 * - Status - Description * - SUCCESS - If computed code rate is 10 * - WARNING - If computed code rate is greater or equal than accepted code rate or pylint has not returned code rate * - ERROR - If computed code rate is less than accepted code rate """ messages = '\n'.join(_RE_PYLINT_MESSAGE.findall(shell_output)) try: actual_code_rate = float(_RE_PYLINT_CODE_RATE.findall(shell_output)[0]) except IndexError: # if pylint has not returned code rate status = CheckResult.WARNING summary = 'Code Rate UNKNOWN' return CheckResult(task.taskname, status, summary, messages) if actual_code_rate == 10: return CheckResult(task.taskname) accepted_code_rate = task.config['accepted-code-rate'] if actual_code_rate >= accepted_code_rate: status = CheckResult.WARNING summary = 'Code Rate {0:.2f}/10'.format(actual_code_rate) else: status = CheckResult.ERROR summary = 'Failed: Code Rate {0:.2f}/10'.format(actual_code_rate) rate_change_match = _RE_PYLINT_RATE_CHANGE.findall(shell_output) if rate_change_match: summary = ' '.join((summary, rate_change_match[0])) return CheckResult(task.taskname, status, summary, messages)
def test_unittest_errors(self): dummy_taskname = 'unittest' lines = ('ignored line', 'FAILED (errors=2)') shell_output = '\n'.join(lines) self.patch_shellcommand_result(stdout=shell_output, returncode=1) task = Task(dummy_taskname, 'dummy-command') task.result_creator = create_pyunittest_result result = task() expected_result = CheckResult(dummy_taskname, CheckResult.ERROR, 'FAILED (errors=2)', shell_output) assert_checkresult_equal(expected_result, result)
def test_result_is_warning_if_code_rate_is_not_returned_by_pylint(self): dummy_taskname = 'pylint' messages = ('filename.py:1: first warning', 'filename.py:10: other warning') shell_output = '\n'.join(messages) self.patch_shellcommand_result(stdout=shell_output) task = create_pylint_task(taskname=dummy_taskname) task.result_creator = create_pylint_result result = task() expected_result = CheckResult(dummy_taskname, CheckResult.WARNING, 'Code Rate UNKNOWN', '\n'.join(messages)) assert_checkresult_equal(expected_result, result)
def test_pass_if_code_rate_is_10(self): """Test if result is determined by function assigned to result_creator attribute.""" expected_summary = 'expected summary' def result_creator(task, *_): return CheckResult(task.taskname, summary=expected_summary) self.patch_shellcommand_result(stdout='dummy') taskname = 'dummy' task = CheckerTask(taskname, 'dummy-command') task.result_creator = result_creator result = task() expected_result = CheckResult(taskname, summary=expected_summary) assert_checkresult_equal(expected_result, result)
def test_unittest_skipped_tests(self): dummy_taskname = 'unittest' lines = ('ignored line', 'Ran 26 tests in 0.263s', 'OK (skipped=1)') shell_output = '\n'.join(lines) self.patch_shellcommand_result(stdout=shell_output) task = Task(dummy_taskname, 'dummy-command') task.result_creator = create_pyunittest_result result = task() expected_result = CheckResult( dummy_taskname, CheckResult.WARNING, 'Ran 26 tests in 0.263s - OK (skipped=1)') assert_checkresult_equal(expected_result, result)
def test_php_fatalerror(self): dummy_taskname = 'phpunit' lines = ('dummy', 'PHP Fatal error: Error description ..') stdout = '\n'.join(lines) self.patch_shellcommand_result(stdout=stdout, returncode=1) task = Task(dummy_taskname, 'dummy-command') task.result_creator = create_phpunit_result result = task() expected_result = CheckResult(dummy_taskname, CheckResult.ERROR, 'FAILED', message=stdout) assert_checkresult_equal(expected_result, result)
def test_ok(self): dummy_taskname = 'phpunit' resource_summary = 'Time: 60 ms, Memory: 3.75Mb' ran_tests_summary = 'OK (40 tests, 57 assertions)' lines = ('dummy', resource_summary, ran_tests_summary) stdout = '\n'.join(lines) self.patch_shellcommand_result(stdout=stdout) task = Task(dummy_taskname, 'dummy-command') task.result_creator = create_phpunit_result result = task() expected_result = CheckResult( dummy_taskname, CheckResult.SUCCESS, 'OK (40 tests, 57 assertions) - Time: 60 ms, Memory: 3.75Mb') assert_checkresult_equal(expected_result, result)
def test_result_is_error_if_code_rate_is_below_accepted(self): dummy_taskname = 'pylint' messages = ('filename.py:1: first warning', 'filename.py:10: other warning') code_rate = 8 shell_output = create_pylint_output(code_rate, messages) self.patch_shellcommand_result(stdout=shell_output) task = create_pylint_task(taskname=dummy_taskname) task.result_creator = create_pylint_result result = task() expected_result = CheckResult(dummy_taskname, CheckResult.ERROR, 'Failed: Code Rate 8.00/10', '\n'.join(messages)) assert_checkresult_equal(expected_result, result)
def test_result_is_warning_if_code_rate_is_between_accepted_and_10(self): """Test if config is accessible by result_creator function.""" dummy_taskname = 'pylint' messages = ('filename.py:1: first warning', 'filename.py:10: other warning') code_rate = 8.5 shell_output = create_pylint_output(code_rate, messages) self.patch_shellcommand_result(stdout=shell_output) config = {'accepted-code-rate': 8} task = create_pylint_task(taskname=dummy_taskname, config=config) task.result_creator = create_pylint_result result = task() expected_result = CheckResult(dummy_taskname, CheckResult.WARNING, 'Code Rate 8.50/10', '\n'.join(messages)) assert_checkresult_equal(expected_result, result)
def test_skipped_tests(self): dummy_taskname = 'phpunit' resource_summary = 'Time: 60 ms, Memory: 3.75Mb' ran_tests_summary = 'OK, but incomplete, skipped, or risky tests!' lines = ('dummy', resource_summary, ran_tests_summary) stdout = '\n'.join(lines) self.patch_shellcommand_result(stdout=stdout) task = Task(dummy_taskname, 'dummy-command') task.result_creator = create_phpunit_result result = task() expected_result = CheckResult( dummy_taskname, CheckResult.WARNING, 'OK, but incomplete, skipped, or risky tests!' \ ' - Time: 60 ms, Memory: 3.75Mb' ) assert_checkresult_equal(expected_result, result)
def test_failure(self): dummy_taskname = 'phpunit' resource_summary = 'Time: 60 ms, Memory: 3.75Mb' ran_tests_summary = 'Tests: 40, Assertions: 55, ' \ 'Failures: 1, Incomplete: 1.' lines = ('dummy', resource_summary, ran_tests_summary) stdout = '\n'.join(lines) self.patch_shellcommand_result(stdout=stdout, returncode=1) task = Task(dummy_taskname, 'dummy-command') task.result_creator = create_phpunit_result result = task() expected_result = CheckResult(dummy_taskname, CheckResult.ERROR, ran_tests_summary + ' - Time: 60 ms, Memory: 3.75Mb', message=stdout) assert_checkresult_equal(expected_result, result)
def create_phpunit_result(task, returncode, stdout) -> CheckResult: """Create python unittest checker result. .. list-table:: Result status :header-rows: 1 * - Status - Description * - SUCCESS - If phpunit exit status is 0 (all tests passes) * - WARNING - If phpunit exit status is 0 and skipped or incomplete test are found * - ERROR - If phpunit exit status is not 0 (some tests fail) Also additional informations are displayed in summary (ran tests, skipped tests, errors, failures, resources) """ summary_match = _RE_PHPUNIT_SUMMARY.findall(stdout) skipped_tests_match = _RE_PHPUNIT_SKIPPED_TESTS.findall(stdout) resources_match = _RE_PHPUNIT_RESOURCES.findall(stdout) if not summary_match: summary_match = skipped_tests_match message = None if returncode != 0: status = CheckResult.ERROR message = stdout elif skipped_tests_match: status = CheckResult.WARNING else: status = CheckResult.SUCCESS summary_parts = [] if summary_match: summary_parts.append(summary_match[0]) if resources_match: summary_parts.append(resources_match[0]) if not summary_parts and status is CheckResult.ERROR: summary_parts.append('FAILED') summary = ' - '.join(summary_parts) return CheckResult(task.taskname, status, summary, message)
def create_pyunittest_result(task, returncode, shell_output) -> CheckResult: """Create python unittest checker result. .. list-table:: Result status :header-rows: 1 * - Status - Description * - SUCCESS - If unittest exit status is 0 (all tests passes) * - WARNING - If unittest exit status is 0 and skipped test are found * - ERROR - If unittest exit status is not 0 (some tests fail) Also additional informations are displayed in summary (ran tests, skipped tests, errors, failures) """ summary_match = _RE_UNITTEST_SKIPPED_TESTS.findall(shell_output) if not summary_match: summary_match = _RE_UNITTEST_ERRORS.findall(shell_output) ran_tests_match = _RE_UNITTEST_TEST_NUMBER.findall(shell_output) test_number_summary = \ ran_tests_match[0] + ' - ' if ran_tests_match else '' message = None if returncode != 0: status = CheckResult.ERROR summary = test_number_summary + (summary_match[0] if summary_match else 'Failed') message = shell_output elif summary_match: status = CheckResult.WARNING summary = test_number_summary + summary_match[0] else: status = CheckResult.SUCCESS summary = test_number_summary + 'OK' return CheckResult(task.taskname, status, summary, message)
def result_creator(task, *_): return CheckResult(task.taskname, summary=expected_summary)