def checker(hook, repo_path, txn_name, profile_name, halt_on_exception): """ Function to singularize the repoguard in precommit or postcommit mode. :param hook: Execute the repoguard as pre- or postcommit. :type hook: string :param repo_path: The path to the repository. :type repo_path: string :param txn_name: The name of the current transaction. :type txn_name: string :param halt_on_exception: Flag which indicates whether we halt on unexpected exceptions or not. :type halt_on_exception: boolean """ logger = LoggerFactory().create('%s.tools.checker' % constants.NAME) try: hooks_path = os.path.abspath(os.path.join(repo_path, "hooks")) project_config = os.path.join(hooks_path, constants.CONFIG_FILENAME) os.chdir(hooks_path) logger.debug("RepoGuard initializing...") repoguard = RepoGuard(hook, repo_path) logger.debug("Loading transaction...") repoguard.load_transaction(txn_name) logger.debug("Loading configuration...") main_config = RepoGuardConfig(constants.CONFIG_PATH) repoguard.load_config(main_config.template_dirs, project_config) logger.debug("Validating configuration...") if main_config.validate: repoguard.validate() else: logger.warning("Validation skipped.") logger.debug("RepoGuard running...") if profile_name: result = repoguard.run_profile(profile_name) else: result = repoguard.run() logger.debug("RepoGuard finished with %s.", result) if result == constants.SUCCESS: return 0 else: return 1 except validate.ValidateError: logger.exception("The configuration is invalid!") return 1 except: # pylint: disable=W0702 logger.exception( "An unexpected error occurred during the RepoGuard run! Halt on exception is '%s'." % halt_on_exception) if not halt_on_exception: return 0 else: return 1
class TestRepoGuard(object): def setup_method(self, _): self._checker = RepoGuard(constants.PRECOMMIT, "/repo/dir") self._checker.checks = mock.Mock() self._checker.handlers = mock.Mock() self._checker.transaction = transaction.Transaction("/path/to/repository", "10") self._checker.transaction._execute_svn = mock.Mock(return_value=dict()) self._checker.load_config("/template/dir", _CONFIG_DEFAULT.splitlines()) def test_run_success(self): assert self._checker.run() == constants.SUCCESS def test_run_error(self): self._set_error_entry() self._set_transaction_changeset(["A Project/vendors/deli/"]) assert self._checker.run() == constants.ERROR def _set_error_entry(self): self._checker.checks.fetch().run.return_value = mock.Mock(result=constants.ERROR) self._checker.checks.fetch.reset_mock() def _set_transaction_changeset(self, changeset): # pylint: disable=W0212 # Access to non-public method is fine for testing. self._checker.transaction._execute_svn.return_value = changeset def test_match_default_profile(self): self._set_transaction_changeset(["A Project/vendors/deli/"]) self._checker.run() assert self._checker.checks.fetch.call_count == 1 assert self._checker.checks.fetch.call_args[0][0] == "Mantis" def test_match_projecta_profile(self): self._set_transaction_changeset(["A ProjectA/vendors/deli/"]) self._checker.run() assert self._checker.checks.fetch.call_count == 1 assert self._checker.checks.fetch.call_args[0][0] == "PyLint" def test_match_projectb_profile(self): self._set_transaction_changeset(["A ProjectB/vendors/deli/"]) self._checker.run() assert self._checker.checks.fetch.call_count == 1 assert self._checker.checks.fetch.call_args[0][0] == "Checkstyle" def test_match_all_profiles(self): self._set_transaction_changeset([ "A ProjectB/vendors/deli/", "A ProjectA/vendors/deli/", "A Project/vendors/deli/"]) self._checker.run() assert self._checker.checks.fetch.call_count == 3 assert self._checker.checks.fetch.call_args_list[0][0][0] == "Mantis" assert self._checker.checks.fetch.call_args_list[1][0][0] == "PyLint" assert self._checker.checks.fetch.call_args_list[2][0][0] == "Checkstyle" def test_large_default_profile_changeset(self): large_changset = list() for _ in range(10000): large_changset.append("A Project/vendors/deli%f" % random.random()) self._set_transaction_changeset(large_changset) self._checker.run() def test_run_profile_success(self): self._checker.run_profile("ProjectA") assert self._checker.checks.fetch.call_count == 1 assert self._checker.checks.fetch.call_args[0][0] == "PyLint" def test_run_missing_profile(self): self._checker.run_profile("UNDEFINED_PROFILE") assert self._checker.checks.fetch.call_count == 0
def checker(hook, repo_path, txn_name, profile_name, halt_on_exception): """ Function to singularize the repoguard in precommit or postcommit mode. :param hook: Execute the repoguard as pre- or postcommit. :type hook: string :param repo_path: The path to the repository. :type repo_path: string :param txn_name: The name of the current transaction. :type txn_name: string :param halt_on_exception: Flag which indicates whether we halt on unexpected exceptions or not. :type halt_on_exception: boolean """ logger = LoggerFactory().create("%s.tools.checker" % constants.NAME) try: hooks_path = os.path.abspath(os.path.join(repo_path, "hooks")) project_config = os.path.join(hooks_path, constants.CONFIG_FILENAME) os.chdir(hooks_path) logger.debug("RepoGuard initializing...") repoguard = RepoGuard(hook, repo_path) logger.debug("Loading transaction...") repoguard.load_transaction(txn_name) logger.debug("Loading configuration...") main_config = RepoGuardConfig(constants.CONFIG_PATH) repoguard.load_config(main_config.template_dirs, project_config) logger.debug("Validating configuration...") if main_config.validate: repoguard.validate() else: logger.warning("Validation skipped.") logger.debug("RepoGuard running...") if profile_name: result = repoguard.run_profile(profile_name) else: result = repoguard.run() logger.debug("RepoGuard finished with %s.", result) if result == constants.SUCCESS: return 0 else: return 1 except validate.ValidateError: logger.exception("The configuration is invalid!") return 1 except: # pylint: disable=W0702 logger.exception( "An unexpected error occurred during the RepoGuard run! Halt on exception is '%s'." % halt_on_exception ) if not halt_on_exception: return 0 else: return 1
class TestRepoGuard(object): def setup_method(self, _): self._checker = RepoGuard(constants.PRECOMMIT, "/repo/dir") self._checker.checks = mock.Mock() self._checker.handlers = mock.Mock() self._checker.transaction = transaction.Transaction( "/path/to/repository", "10") self._checker.transaction._execute_svn = mock.Mock(return_value=dict()) self._checker.load_config("/template/dir", _CONFIG_DEFAULT.splitlines()) def test_run_success(self): assert self._checker.run() == constants.SUCCESS def test_run_error(self): self._set_error_entry() self._set_transaction_changeset(["A Project/vendors/deli/"]) assert self._checker.run() == constants.ERROR def _set_error_entry(self): self._checker.checks.fetch().run.return_value = mock.Mock( result=constants.ERROR) self._checker.checks.fetch.reset_mock() def _set_transaction_changeset(self, changeset): # pylint: disable=W0212 # Access to non-public method is fine for testing. self._checker.transaction._execute_svn.return_value = changeset def test_match_default_profile(self): self._set_transaction_changeset(["A Project/vendors/deli/"]) self._checker.run() assert self._checker.checks.fetch.call_count == 1 assert self._checker.checks.fetch.call_args[0][0] == "Mantis" def test_match_projecta_profile(self): self._set_transaction_changeset(["A ProjectA/vendors/deli/"]) self._checker.run() assert self._checker.checks.fetch.call_count == 1 assert self._checker.checks.fetch.call_args[0][0] == "PyLint" def test_match_projectb_profile(self): self._set_transaction_changeset(["A ProjectB/vendors/deli/"]) self._checker.run() assert self._checker.checks.fetch.call_count == 1 assert self._checker.checks.fetch.call_args[0][0] == "Checkstyle" def test_match_all_profiles(self): self._set_transaction_changeset([ "A ProjectB/vendors/deli/", "A ProjectA/vendors/deli/", "A Project/vendors/deli/" ]) self._checker.run() assert self._checker.checks.fetch.call_count == 3 assert self._checker.checks.fetch.call_args_list[0][0][0] == "Mantis" assert self._checker.checks.fetch.call_args_list[1][0][0] == "PyLint" assert self._checker.checks.fetch.call_args_list[2][0][ 0] == "Checkstyle" def test_large_default_profile_changeset(self): large_changset = list() for _ in range(10000): large_changset.append("A Project/vendors/deli%f" % random.random()) self._set_transaction_changeset(large_changset) self._checker.run() def test_run_profile_success(self): self._checker.run_profile("ProjectA") assert self._checker.checks.fetch.call_count == 1 assert self._checker.checks.fetch.call_args[0][0] == "PyLint" def test_run_missing_profile(self): self._checker.run_profile("UNDEFINED_PROFILE") assert self._checker.checks.fetch.call_count == 0