def test_configuration_can_contain_additional_command_options(self): rcfile = 'tests/pylintrc' precommit_yaml_contents = yaml.dump({ 'file-checkers': { 'tests/*.py': [{'pylint': {'rcfile': rcfile}}] } }) staged_files = ['tests/module.py'] self.patch_git_repository(precommit_yaml_contents, staged_files) self.patch_file_checker( 'pylint', taskname='Pylint ${file_relpath}', command='pylint -f parseable ${file_abspath} ${rcfile}', defaultconfig={ 'rcfile': None, 'accepted-code-rate': 9 }, command_options={'rcfile': '--rcfile=${value}'} ) runner.main() expected_checker = Task( 'Pylint tests/module.py', 'pylint -f parseable {abspath} --rcfile={rcfile}' \ .format(abspath=git.abspath('tests/module.py'), rcfile=rcfile), config={ 'rcfile': rcfile, 'accepted-code-rate': 9 } ) expected_checker.rcfile = 'tests/pylintrc' self.worker.execute_checkers.assert_called_once_with( UnOrderedCollectionMatcher([expected_checker]) )
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_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_empty_string_is_valid_command_option(self): command_pattern = 'command ${options}' config = { 'config': '' } task = CheckerTask('dummy', command_pattern, config) task.command_options = { 'config': '--config ${value}' } task() expected_command = "command --config ''" self.assert_shell_command_executed(expected_command)
def test_option_is_not_passed_to_command_if_its_config_option_is_none2(self): command_pattern = 'command ${options}' config = { 'opt': None } task = CheckerTask('dummy', command_pattern, config) task.command_options = { 'opt': '--opt1 ${value}' } task() expected_command = 'command' self.assert_shell_command_executed(expected_command)
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_replace_options_placeholder_with_checker_options2(self): command_pattern = 'jshint ${options} /path/to/file.js' config = { 'config': '.jshintrc' } task = CheckerTask('dummy-taskname', command_pattern, config) task.command_options = { 'config': '--config ${value}' } task() expected_command = 'jshint --config .jshintrc /path/to/file.js' self.assert_shell_command_executed(expected_command)
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_config_options_should_be_properly_quoted(self): command_pattern = 'jshint ${options} /path/to/file.js' config = { 'config': '.jshint rc' } task = CheckerTask('dummy-taskname', command_pattern, config) task.command_options = { 'config': '--config ${value}' } task() expected_command = "jshint --config '.jshint rc' /path/to/file.js" self.assert_shell_command_executed(expected_command)
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_command_options_can_be_passed_directly_to_command_pattern(self): command_pattern = 'command ${opt1} ${opt2}' config = { 'opt1': 'val1', 'opt2': 'val2' } task = CheckerTask('dummy', command_pattern, config) task.command_options = { 'opt1': '--opt1 ${value}', 'opt2': '${value}' } task() expected_command = 'command --opt1 val1 val2' self.assert_shell_command_executed(expected_command)
def test_replace_options_placeholder_with_checker_options(self): """Pylint accepts pylintrc option.""" command_pattern = 'pylint -f parseable /path/to/module.py ${options}' config = { 'rcfile': 'pylintrc' } task = CheckerTask('dummy-taskname', command_pattern, config) task.command_options = { 'rcfile': '--rcfile=${value}' } task() expected_command = 'pylint -f parseable /path/to/module.py ' \ '--rcfile=pylintrc' self.assert_shell_command_executed(expected_command)
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 create(self, relpath=None, config=None): """Create Task for specified file.""" config = self._mix_config(config) if relpath: abspath = git.abspath(relpath) command = self._command.safe_substitute(file_abspath=abspath) taskname = self._taskname.substitute(file_relpath=relpath) else: taskname = self._taskname.template command = self._command.template task = Task(taskname, command, config) if self._command_options: task.command_options = self._command_options if self._result_creator: task.result_creator = self._result_creator return task
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 create_pylint_task(taskname='dummy', command='dummy', config=None): """Create Task. Default config argument contains accepted-code-rate option. """ if config is None: config = {'accepted-code-rate': 9} return Task(taskname, command, config)
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_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 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 test_checker_can_be_defined_with_custom_result_creator(self): """Checker can have own result creator. Result creator builds CheckResult based on stdout, returncode and config. """ accepted_code_rate = 9 pylint_config = { 'rcfile': None, 'accepted-code-rate': accepted_code_rate } precommit_yaml_contents = yaml.dump({ 'file-checkers': {'*.py': ['pylint']} }) staged_files = ['module.py'] self.patch_git_repository(precommit_yaml_contents, staged_files) self.patch_file_checker( 'pylint', taskname='Pylint ${file_relpath}', command='pylint -f parseable ${file_abspath} ${options}', defaultconfig=pylint_config, command_options={'rcfile': '--rcfile=${value}'}, result_creator=create_pylint_result ) runner.main() expected_task = Task( 'Pylint module.py', 'pylint -f parseable {}'.format(git.abspath('module.py')), pylint_config ) expected_task.result_creator = create_pylint_result self.worker.execute_checkers.assert_called_once_with( UnOrderedCollectionMatcher([expected_task]) )