def test_uninstall_commit_msg_hook_negative(self, isdir, path_exists, remove): lint_config = LintConfig() lint_config.target = self.SAMPLES_DIR # mock that the current directory is not a git repo isdir.return_value = False expected_msg = "{0} is not a git repository".format(self.SAMPLES_DIR) with self.assertRaisesRegex(GitHookInstallerError, expected_msg): GitHookInstaller.uninstall_commit_msg_hook(lint_config) isdir.assert_called_with(os.path.join(self.SAMPLES_DIR, '.git/hooks')) path_exists.assert_not_called() remove.assert_not_called() # mock that there is no commit hook present isdir.return_value = True path_exists.return_value = False expected_dst = os.path.join(self.SAMPLES_DIR, COMMIT_MSG_HOOK_DST_PATH) expected_msg = "There is no commit-msg hook present in {0}.".format(expected_dst) with self.assertRaisesRegex(GitHookInstallerError, expected_msg): GitHookInstaller.uninstall_commit_msg_hook(lint_config) isdir.assert_called_with(os.path.join(self.SAMPLES_DIR, '.git/hooks')) path_exists.assert_called_once_with(expected_dst) remove.assert_not_called() # mock that there is a different (=not gitlint) commit hook isdir.return_value = True path_exists.return_value = True read_data = "#!/bin/sh\nfoo" expected_dst = os.path.join(self.SAMPLES_DIR, COMMIT_MSG_HOOK_DST_PATH) expected_msg = "The commit-msg hook in {0} was not installed by gitlint ".format(expected_dst) + \ r"\(or it was modified\).\nUninstallation of 3th party or modified gitlint hooks " + \ "is not supported." with patch('gitlint.hooks.open', mock_open(read_data=read_data), create=True): with self.assertRaisesRegex(GitHookInstallerError, expected_msg): GitHookInstaller.uninstall_commit_msg_hook(lint_config) remove.assert_not_called()
def test_install_commit_msg_hook_negative(self, git_hooks_dir, isdir, path_exists, copy): lint_config = LintConfig() lint_config.target = os.path.join(u"/hür", u"dur") git_hooks_dir.return_value = os.path.join(u"/föo", u"bar", ".git", "hooks") # mock that current dir is not a git repo isdir.return_value = False expected_msg = u"{0} is not a git repository".format( lint_config.target) with self.assertRaisesRegex(GitHookInstallerError, expected_msg): GitHookInstaller.install_commit_msg_hook(lint_config) isdir.assert_called_with(git_hooks_dir.return_value) path_exists.assert_not_called() copy.assert_not_called() # mock that there is already a commit hook present isdir.return_value = True path_exists.return_value = True expected_dst = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH) expected_msg = u"There is already a commit-msg hook file present in {0}.\n".format(expected_dst) + \ "gitlint currently does not support appending to an existing commit-msg file." with self.assertRaisesRegex(GitHookInstallerError, expected_msg): GitHookInstaller.install_commit_msg_hook(lint_config)
def test_commit_msg_hook_path(self): lint_config = LintConfig() lint_config.target = self.SAMPLES_DIR expected_path = os.path.join(self.SAMPLES_DIR, COMMIT_MSG_HOOK_DST_PATH) path = GitHookInstaller.commit_msg_hook_path(lint_config) self.assertEqual(path, expected_path)
def get_config(ctx, target, config_path, c, ignore, verbose, silent): """ Creates a LintConfig object based on a set of commandline parameters. """ try: # Config precedence: # First, load default config or config from configfile lint_config = load_config_from_path(ctx, config_path) # default to default configuration when no config file was loaded if lint_config: click.echo("Using config from {0}".format(lint_config.config_path)) else: lint_config = LintConfig() # Then process any commandline configuration flags lint_config.apply_config_options(c) # Finally, overwrite with any convenience commandline flags lint_config.apply_on_csv_string(ignore, lint_config.disable_rule) if silent: lint_config.verbosity = 0 elif verbose > 0: lint_config.verbosity = verbose # Set target lint_config.target = target return lint_config except LintConfigError as e: click.echo("Config Error: {0}".format(str(e))) ctx.exit(CONFIG_ERROR_CODE) # return CONFIG_ERROR_CODE on config error
def test_uninstall_commit_msg_hook_negative(self, isdir, path_exists, remove): lint_config = LintConfig() lint_config.target = u"/foo/bår" # mock that the current directory is not a git repo isdir.return_value = False expected_msg = u"{0} is not a git repository".format(u"/foo/bår") with self.assertRaisesRegex(GitHookInstallerError, expected_msg): GitHookInstaller.uninstall_commit_msg_hook(lint_config) isdir.assert_called_with(os.path.join(u"/foo/bår", '.git/hooks')) path_exists.assert_not_called() remove.assert_not_called() # mock that there is no commit hook present isdir.return_value = True path_exists.return_value = False expected_dst = os.path.join(u"/foo/bår", COMMIT_MSG_HOOK_DST_PATH) expected_msg = u"There is no commit-msg hook present in {0}.".format(expected_dst) with self.assertRaisesRegex(GitHookInstallerError, expected_msg): GitHookInstaller.uninstall_commit_msg_hook(lint_config) isdir.assert_called_with(os.path.join(u"/foo/bår", '.git/hooks')) path_exists.assert_called_once_with(expected_dst) remove.assert_not_called() # mock that there is a different (=not gitlint) commit hook isdir.return_value = True path_exists.return_value = True read_data = "#!/bin/sh\nfoo" expected_dst = os.path.join(u"/foo/bår", COMMIT_MSG_HOOK_DST_PATH) expected_msg = u"The commit-msg hook in {0} was not installed by gitlint ".format(expected_dst) + \ r"\(or it was modified\).\nUninstallation of 3th party or modified gitlint hooks " + \ "is not supported." with patch('gitlint.hooks.open', mock_open(read_data=read_data), create=True): with self.assertRaisesRegex(GitHookInstallerError, expected_msg): GitHookInstaller.uninstall_commit_msg_hook(lint_config) remove.assert_not_called()
def test_set_general_option_negative(self): config = LintConfig() # Note that we should't test whether we can set unicode because python just doesn't allow unicode attributes with self.assertRaisesRegex(LintConfigError, "'foo' is not a valid gitlint option"): config.set_general_option("foo", u"bår") # try setting _config_path, this is a real attribute of LintConfig, but the code should prevent it from # being set with self.assertRaisesRegex( LintConfigError, "'_config_path' is not a valid gitlint option"): config.set_general_option("_config_path", u"bår") # invalid verbosity` incorrect_values = [-1, u"föo"] for value in incorrect_values: expected_msg = u"Option 'verbosity' must be a positive integer \(current value: '{0}'\)".format( value) with self.assertRaisesRegex(LintConfigError, expected_msg): config.verbosity = value incorrect_values = [4] for value in incorrect_values: with self.assertRaisesRegex( LintConfigError, "Option 'verbosity' must be set between 0 and 3"): config.verbosity = value # invalid ignore_merge_commits incorrect_values = [-1, 4, u"föo"] for value in incorrect_values: with self.assertRaisesRegex( LintConfigError, "Option 'ignore-merge-commits' must be either 'true' or 'false'" ): config.ignore_merge_commits = value # invalid ignore -> not here because ignore is a ListOption which converts everything to a string before # splitting which means it it will accept just about everything # invalid debug with self.assertRaisesRegex( LintConfigError, "Option 'debug' must be either 'true' or 'false'"): config.debug = u"föobar" # extra-path has its own negative test # invalid target with self.assertRaisesRegex( LintConfigError, u"Option target must be an existing directory \(current value: 'föo/bar'\)" ): config.target = u"föo/bar"
def test_install_commit_msg_hook(isdir, path_exists, copy, stat, chmod): lint_config = LintConfig() lint_config.target = u"/foo/bår" expected_dst = os.path.join(u"/foo/bår", COMMIT_MSG_HOOK_DST_PATH) GitHookInstaller.install_commit_msg_hook(lint_config) isdir.assert_called_with(u"/foo/bår" + '/.git/hooks') path_exists.assert_called_once_with(expected_dst) copy.assert_called_once_with(COMMIT_MSG_HOOK_SRC_PATH, expected_dst) stat.assert_called_once_with(expected_dst) chmod.assert_called_once_with(expected_dst, ANY)
def test_install_commit_msg_hook(self, isdir, path_exists, copy, stat, chmod): lint_config = LintConfig() lint_config.target = self.SAMPLES_DIR expected_dst = os.path.join(self.SAMPLES_DIR, COMMIT_MSG_HOOK_DST_PATH) GitHookInstaller.install_commit_msg_hook(lint_config) isdir.assert_called_with(self.SAMPLES_DIR + '/.git/hooks') path_exists.assert_called_once_with(expected_dst) copy.assert_called_once_with(COMMIT_MSG_HOOK_SRC_PATH, expected_dst) stat.assert_called_once_with(expected_dst) chmod.assert_called_once_with(expected_dst, ANY)
def test_commit_msg_hook_path(self, git_hooks_dir): git_hooks_dir.return_value = os.path.join(u"/föo", u"bar") lint_config = LintConfig() lint_config.target = self.SAMPLES_DIR expected_path = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH) path = GitHookInstaller.commit_msg_hook_path(lint_config) git_hooks_dir.assert_called_once_with(self.SAMPLES_DIR) self.assertEqual(path, expected_path)
def test_uninstall_commit_msg_hook(isdir, path_exists, remove): lint_config = LintConfig() lint_config.target = u"/foo/bår" read_data = "#!/bin/sh\n" + GITLINT_HOOK_IDENTIFIER with patch('gitlint.hooks.open', mock_open(read_data=read_data), create=True): GitHookInstaller.uninstall_commit_msg_hook(lint_config) expected_dst = os.path.join(u"/foo/bår", COMMIT_MSG_HOOK_DST_PATH) isdir.assert_called_with(os.path.join(u"/foo/bår", '.git/hooks')) path_exists.assert_called_once_with(expected_dst) remove.assert_called_with(expected_dst)
def test_uninstall_commit_msg_hook(self, isdir, path_exists, remove): lint_config = LintConfig() lint_config.target = self.SAMPLES_DIR read_data = "#!/bin/sh\n" + GITLINT_HOOK_IDENTIFIER with patch('gitlint.hooks.open', mock_open(read_data=read_data), create=True): GitHookInstaller.uninstall_commit_msg_hook(lint_config) expected_dst = os.path.join(self.SAMPLES_DIR, COMMIT_MSG_HOOK_DST_PATH) isdir.assert_called_with(os.path.join(self.SAMPLES_DIR, '.git/hooks')) path_exists.assert_called_once_with(expected_dst) remove.assert_called_with(expected_dst)
def test_set_general_option_negative(self): config = LintConfig() # Note that we shouldn't test whether we can set unicode because python just doesn't allow unicode attributes with self.assertRaisesRegex(LintConfigError, "'foo' is not a valid gitlint option"): config.set_general_option("foo", u"bår") # try setting _config_path, this is a real attribute of LintConfig, but the code should prevent it from # being set with self.assertRaisesRegex(LintConfigError, "'_config_path' is not a valid gitlint option"): config.set_general_option("_config_path", u"bår") # invalid verbosity incorrect_values = [-1, u"föo"] for value in incorrect_values: expected_msg = u"Option 'verbosity' must be a positive integer (current value: '{0}')".format(value) with self.assertRaisesRegex(LintConfigError, expected_msg): config.verbosity = value incorrect_values = [4] for value in incorrect_values: with self.assertRaisesRegex(LintConfigError, "Option 'verbosity' must be set between 0 and 3"): config.verbosity = value # invalid ignore_xxx_commits ignore_attributes = ["ignore_merge_commits", "ignore_fixup_commits", "ignore_squash_commits", "ignore_revert_commits"] incorrect_values = [-1, 4, u"föo"] for attribute in ignore_attributes: for value in incorrect_values: option_name = attribute.replace("_", "-") with self.assertRaisesRegex(LintConfigError, "Option '{0}' must be either 'true' or 'false'".format(option_name)): setattr(config, attribute, value) # invalid ignore -> not here because ignore is a ListOption which converts everything to a string before # splitting which means it it will accept just about everything # invalid boolean options for attribute in ['debug', 'staged', 'ignore_stdin']: option_name = attribute.replace("_", "-") with self.assertRaisesRegex(LintConfigError, "Option '{0}' must be either 'true' or 'false'".format(option_name)): setattr(config, attribute, u"föobar") # extra-path has its own negative test # invalid target with self.assertRaisesRegex(LintConfigError, u"Option target must be an existing directory (current value: 'föo/bar')"): config.target = u"föo/bar"
def test_install_commit_msg_hook(git_hooks_dir, isdir, path_exists, copy, stat, chmod): lint_config = LintConfig() lint_config.target = os.path.join(u"/hür", u"dur") git_hooks_dir.return_value = os.path.join(u"/föo", u"bar", ".git", "hooks") expected_dst = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH) GitHookInstaller.install_commit_msg_hook(lint_config) isdir.assert_called_with(git_hooks_dir.return_value) path_exists.assert_called_once_with(expected_dst) copy.assert_called_once_with(COMMIT_MSG_HOOK_SRC_PATH, expected_dst) stat.assert_called_once_with(expected_dst) chmod.assert_called_once_with(expected_dst, ANY) git_hooks_dir.assert_called_with(lint_config.target)
def test_set_general_option_negative(self): config = LintConfig() with self.assertRaisesRegex(LintConfigError, "'foo' is not a valid gitlint option"): config.set_general_option("foo", "bar") # try setting _config_path, this is a real attribute of LintConfig, but the code should prevent it from # being set with self.assertRaisesRegex(LintConfigError, "'_config_path' is not a valid gitlint option"): config.set_general_option("_config_path", "bar") # invalid verbosity` incorrect_values = [-1, "foo"] for value in incorrect_values: expected_msg = r"Option 'verbosity' must be a positive integer \(current value: '{0}'\)".format(value) with self.assertRaisesRegex(LintConfigError, expected_msg): config.verbosity = value incorrect_values = [4] for value in incorrect_values: with self.assertRaisesRegex(LintConfigError, "Option 'verbosity' must be set between 0 and 3"): config.verbosity = value # invalid ignore_merge_commits incorrect_values = [-1, 4, "foo"] for value in incorrect_values: with self.assertRaisesRegex(LintConfigError, r"Option 'ignore-merge-commits' must be either 'true' or 'false'"): config.ignore_merge_commits = value # invalid ignore -> not here because ignore is a ListOption which converts everything to a string before # splitting which means it it will accept just about everything # invalid debug with self.assertRaisesRegex(LintConfigError, r"Option 'debug' must be either 'true' or 'false'"): config.debug = "foobar" # invalid extra-path with self.assertRaisesRegex(LintConfigError, r"Option extra-path must be an existing directory \(current value: 'foo/bar'\)"): config.extra_path = "foo/bar" # invalid target with self.assertRaisesRegex(LintConfigError, r"Option target must be an existing directory \(current value: 'foo/bar'\)"): config.target = "foo/bar"
def test_uninstall_commit_msg_hook(git_hooks_dir, isdir, path_exists, remove): lint_config = LintConfig() git_hooks_dir.return_value = os.path.join(u"/föo", u"bar", ".git", "hooks") lint_config.target = os.path.join(u"/hür", u"dur") read_data = "#!/bin/sh\n" + GITLINT_HOOK_IDENTIFIER with patch('gitlint.hooks.io.open', mock_open(read_data=read_data), create=True): GitHookInstaller.uninstall_commit_msg_hook(lint_config) expected_dst = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH) isdir.assert_called_with(git_hooks_dir.return_value) path_exists.assert_called_once_with(expected_dst) remove.assert_called_with(expected_dst) git_hooks_dir.assert_called_with(lint_config.target)
def test_install_commit_msg_hook_negative(self, isdir, path_exists, copy): lint_config = LintConfig() lint_config.target = self.SAMPLES_DIR # mock that current dir is not a git repo isdir.return_value = False expected_msg = "{0} is not a git repository".format(self.SAMPLES_DIR) with self.assertRaisesRegex(GitHookInstallerError, expected_msg): GitHookInstaller.install_commit_msg_hook(lint_config) isdir.assert_called_with(os.path.join(self.SAMPLES_DIR, '.git/hooks')) path_exists.assert_not_called() copy.assert_not_called() # mock that there is already a commit hook present isdir.return_value = True path_exists.return_value = True expected_dst = os.path.join(self.SAMPLES_DIR, COMMIT_MSG_HOOK_DST_PATH) expected_msg = "There is already a commit-msg hook file present in {0}.\n".format(expected_dst) + \ "gitlint currently does not support appending to an existing commit-msg file." with self.assertRaisesRegex(GitHookInstallerError, expected_msg): GitHookInstaller.install_commit_msg_hook(lint_config)
def get_config(ctx, target, config_path, c, extra_path, ignore, verbose, silent, debug): """ Creates a LintConfig object based on a set of commandline parameters. """ try: # Config precedence: # First, load default config or config from configfile lint_config = load_config_from_path(ctx, config_path) # default to default configuration when no config file was loaded if lint_config: if debug: click.echo("Using config from {0}".format( lint_config.config_path)) else: lint_config = LintConfig() # Then process any commandline configuration flags lint_config.apply_config_options(c) # Finally, overwrite with any convenience commandline flags lint_config.apply_on_csv_string(ignore, lint_config.disable_rule) if silent: lint_config.verbosity = 0 elif verbose > 0: lint_config.verbosity = verbose if extra_path: lint_config.extra_path = extra_path if debug: lint_config.debug = True # Set target lint_config.target = target return lint_config except LintConfigError as e: click.echo("Config Error: {0}".format(str(e))) ctx.exit(CONFIG_ERROR_CODE) # return CONFIG_ERROR_CODE on config error
def test_commit_msg_hook_path(self): lint_config = LintConfig() lint_config.target = self.SAMPLES_DIR expected_path = os.path.join(self.SAMPLES_DIR, COMMIT_MSG_HOOK_DST_PATH) path = GitHookInstaller.commit_msg_hook_path(lint_config) self.assertEqual(path, expected_path)