def test_lint_sample3(self): linter = GitLinter(LintConfig()) gitcontext = GitContext() gitcontext.set_commit_msg(self.get_sample("commit_message/sample3")) violations = linter.lint(gitcontext) title = " Commit title containing 'WIP', \tleading and trailing whitespace and longer than 72 characters." expected = [ RuleViolation("T1", "Title exceeds max length (95>72)", title, 1), RuleViolation("T3", "Title has trailing punctuation (.)", title, 1), RuleViolation("T4", "Title contains hard tab characters (\\t)", title, 1), RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", title, 1), RuleViolation("T6", "Title has leading whitespace", title, 1), RuleViolation("B4", "Second line is not empty", "This line should be empty", 2), RuleViolation( "B1", "Line exceeds max length (101>80)", "This is the first line is meant to test a line that exceeds the maximum line " + "length of 80 characters.", 3), RuleViolation("B2", "Line has trailing whitespace", "This line has a trailing space. ", 4), RuleViolation("B2", "Line has trailing whitespace", "This line has a trailing tab.\t", 5), RuleViolation("B3", "Line contains hard tab characters (\\t)", "This line has a trailing tab.\t", 5), ] self.assertListEqual(violations, expected)
def cli(config, c, ignore, verbose, silent): """ Git lint tool, checks your git commit messages for styling issues """ try: # Config precedence: # First, load default config or config from configfile lint_config = get_lint_config(config) # Then process any commandline configuration flags try: lint_config.apply_config_options(c) except LintConfigError as e: click.echo("Config Error: {}".format(e.message)) exit(CONFIG_ERROR_CODE) # 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 except LintConfigError as e: click.echo("Config Error: {0}".format(e.message)) exit(CONFIG_ERROR_CODE) # return 10000 on config error if sys.stdin.isatty(): gitcontext = GitContext.from_environment() else: gitcontext = GitContext() gitcontext.set_commit_msg(sys.stdin.read()) linter = GitLinter(lint_config) violations = linter.lint(gitcontext) linter.print_violations(violations) exit(len(violations))
def test_lint_sample1(self): linter = GitLinter(LintConfig()) gitcontext = GitContext() gitcontext.set_commit_msg(self.get_sample("commit_message/sample1")) violations = linter.lint(gitcontext) expected_errors = [ RuleViolation( "T3", "Title has trailing punctuation (.)", "Commit title containing 'WIP', as well as trailing punctuation.", 1), RuleViolation( "T5", "Title contains the word 'WIP' (case-insensitive)", "Commit title containing 'WIP', as well as trailing punctuation.", 1), RuleViolation("B4", "Second line is not empty", "This line should be empty", 2), RuleViolation( "B1", "Line exceeds max length (135>80)", "This is the first line of the commit message body and it is meant to test " + "a line that exceeds the maximum line length of 80 characters.", 3), RuleViolation("B2", "Line has trailing whitespace", "This line has a trailing space. ", 4), RuleViolation("B2", "Line has trailing whitespace", "This line has a trailing tab.\t", 5), RuleViolation("B3", "Line contains hard tab characters (\\t)", "This line has a trailing tab.\t", 5), ] self.assertListEqual(violations, expected_errors)
def test_lint_sample3(self): linter = GitLinter(LintConfig()) gitcontext = GitContext() gitcontext.set_commit_msg(self.get_sample("commit_message/sample3")) violations = linter.lint(gitcontext) title = " Commit title containing 'WIP', \tleading and trailing whitespace and longer than 72 characters." expected = [ RuleViolation("T1", "Title exceeds max length (95>72)", title, 1), RuleViolation("T3", "Title has trailing punctuation (.)", title, 1), RuleViolation("T4", "Title contains hard tab characters (\\t)", title, 1), RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", title, 1), RuleViolation("T6", "Title has leading whitespace", title, 1), RuleViolation("B4", "Second line is not empty", "This line should be empty", 2), RuleViolation( "B1", "Line exceeds max length (101>80)", "This is the first line is meant to test a line that exceeds the maximum line " + "length of 80 characters.", 3, ), RuleViolation("B2", "Line has trailing whitespace", "This line has a trailing space. ", 4), RuleViolation("B2", "Line has trailing whitespace", "This line has a trailing tab.\t", 5), RuleViolation("B3", "Line contains hard tab characters (\\t)", "This line has a trailing tab.\t", 5), ] self.assertListEqual(violations, expected)
def cli(config, c, ignore, verbose, silent): """ Git lint tool, checks your git commit messages for styling issues """ try: # Config precedence: # First, load default config or config from configfile lint_config = get_lint_config(config) # Then process any commandline configuration flags try: lint_config.apply_config_options(c) except LintConfigError as e: click.echo("Config Error: {}".format(e.message)) exit(CONFIG_ERROR_CODE) # 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 except LintConfigError as e: click.echo("Config Error: {0}".format(e.message)) exit(CONFIG_ERROR_CODE) # return 10000 on config error if sys.stdin.isatty(): gitcontext = GitContext.from_environment() else: gitcontext = GitContext() gitcontext.set_commit_msg(sys.stdin.read()) linter = GitLinter(lint_config) violations = linter.lint(gitcontext) linter.print_violations(violations) exit(len(violations))
def test_lint_sample1(self): linter = GitLinter(LintConfig()) gitcontext = GitContext() gitcontext.set_commit_msg(self.get_sample("commit_message/sample1")) violations = linter.lint(gitcontext) expected_errors = [ RuleViolation( "T3", "Title has trailing punctuation (.)", "Commit title containing 'WIP', as well as trailing punctuation.", 1, ), RuleViolation( "T5", "Title contains the word 'WIP' (case-insensitive)", "Commit title containing 'WIP', as well as trailing punctuation.", 1, ), RuleViolation("B4", "Second line is not empty", "This line should be empty", 2), RuleViolation( "B1", "Line exceeds max length (135>80)", "This is the first line of the commit message body and it is meant to test " + "a line that exceeds the maximum line length of 80 characters.", 3, ), RuleViolation("B2", "Line has trailing whitespace", "This line has a trailing space. ", 4), RuleViolation("B2", "Line has trailing whitespace", "This line has a trailing tab.\t", 5), RuleViolation("B3", "Line contains hard tab characters (\\t)", "This line has a trailing tab.\t", 5), ] self.assertListEqual(violations, expected_errors)
def test_gitcontext_ignore_specific(self): # ignore specific rules config = LintConfig() context = GitContext() context.set_commit_msg("test\ngitlint-ignore: T1, body-hard-tab") config.apply_config_from_gitcontext(context) expected_rules = [rule for rule in config.rules if rule.id not in ["T1", "body-hard-tab"]] self.assertEqual(config.rules, expected_rules)
def test_set_commit_msg_just_title(self): gitcontext = GitContext() gitcontext.set_commit_msg(self.get_sample("commit_message/sample2")) self.assertEqual(gitcontext.commit_msg.title, "Just a title containing WIP") self.assertEqual(gitcontext.commit_msg.body, []) self.assertEqual(gitcontext.commit_msg.full, "Just a title containing WIP") self.assertEqual(gitcontext.commit_msg.original, "Just a title containing WIP")
def test_lint_sample4(self): gitcontext = GitContext() gitcontext.set_commit_msg(self.get_sample("commit_message/sample4")) lintconfig = LintConfig() lintconfig.apply_config_from_gitcontext(gitcontext) linter = GitLinter(lintconfig) violations = linter.lint(gitcontext) # expect no violations because sample4 has a 'gitlint: disable line' expected = [] self.assertListEqual(violations, expected)
def gitcontext(commit_msg_str, changed_files=None): """ Utility method to easily create gitcontext objects based on a given commit msg string and set of changed files""" gitcontext = GitContext() gitcontext.set_commit_msg(commit_msg_str) if changed_files: gitcontext.changed_files = changed_files else: gitcontext.changed_files = [] return gitcontext
def test_lint_sample2(self): linter = GitLinter(LintConfig()) gitcontext = GitContext() gitcontext.set_commit_msg(self.get_sample("commit_message/sample2")) violations = linter.lint(gitcontext) expected = [RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", "Just a title containing WIP", 1), RuleViolation("B6", "Body message is missing", None, 3)] self.assertListEqual(violations, expected)
def gitcontext(commit_msg_str, changed_files=None): """ Utility method to easily create gitcontext objects based on a given commit msg string and set of changed files""" gitcontext = GitContext() gitcontext.set_commit_msg(commit_msg_str) if changed_files: gitcontext.changed_files = changed_files else: gitcontext.changed_files = [] return gitcontext
def test_set_commit_msg_just_title(self): gitcontext = GitContext() gitcontext.set_commit_msg(self.get_sample("commit_message/sample2")) self.assertEqual(gitcontext.commit_msg.title, "Just a title containing WIP") self.assertEqual(gitcontext.commit_msg.body, []) self.assertEqual(gitcontext.commit_msg.full, "Just a title containing WIP") self.assertEqual(gitcontext.commit_msg.original, "Just a title containing WIP")
def test_lint_sample2(self): linter = GitLinter(LintConfig()) gitcontext = GitContext() gitcontext.set_commit_msg(self.get_sample("commit_message/sample2")) violations = linter.lint(gitcontext) expected = [ RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", "Just a title containing WIP", 1), RuleViolation("B6", "Body message is missing", None, 3) ] self.assertListEqual(violations, expected)
def test_gitcontext_ignore_all(self): config = LintConfig() original_rules = config.rules # nothing gitlint context = GitContext() context.set_commit_msg("test\ngitlint\nfoo") config.apply_config_from_gitcontext(context) self.assertListEqual(config.rules, original_rules) # ignore all rules context = GitContext() context.set_commit_msg("test\ngitlint-ignore: all\nfoo") config.apply_config_from_gitcontext(context) self.assertEqual(config.rules, []) # ignore all rules, no space config = LintConfig() context.set_commit_msg("test\ngitlint-ignore:all\nfoo") config.apply_config_from_gitcontext(context) self.assertEqual(config.rules, []) # ignore all rules, more spacing config = LintConfig() context.set_commit_msg("test\ngitlint-ignore: \t all\nfoo") config.apply_config_from_gitcontext(context) self.assertEqual(config.rules, [])
def test_set_commit_msg_full(self): gitcontext = GitContext() gitcontext.set_commit_msg(self.get_sample("commit_message/sample1")) expected_title = "Commit title containing 'WIP', as well as trailing punctuation." expected_body = ["This line should be empty", "This is the first line of the commit message body and it is meant to test a " + "line that exceeds the maximum line length of 80 characters.", "This line has a trailing space. ", "This line has a trailing tab.\t", ""] expected_full = expected_title + "\n" + "\n".join(expected_body) expected_original = expected_full + "# This is a commented line\n" self.assertEqual(gitcontext.commit_msg.title, expected_title) self.assertEqual(gitcontext.commit_msg.body, expected_body) self.assertEqual(gitcontext.commit_msg.full, expected_full) self.assertEqual(gitcontext.commit_msg.original, expected_original)
def test_set_commit_msg_full(self): gitcontext = GitContext() gitcontext.set_commit_msg(self.get_sample("commit_message/sample1")) expected_title = "Commit title containing 'WIP', as well as trailing punctuation." expected_body = [ "This line should be empty", "This is the first line of the commit message body and it is meant to test a " + "line that exceeds the maximum line length of 80 characters.", "This line has a trailing space. ", "This line has a trailing tab.\t", "" ] expected_full = expected_title + "\n" + "\n".join(expected_body) expected_original = expected_full + "# This is a commented line\n" self.assertEqual(gitcontext.commit_msg.title, expected_title) self.assertEqual(gitcontext.commit_msg.body, expected_body) self.assertEqual(gitcontext.commit_msg.full, expected_full) self.assertEqual(gitcontext.commit_msg.original, expected_original)
def test_lint_sample5(self): gitcontext = GitContext() gitcontext.set_commit_msg(self.get_sample("commit_message/sample5")) lintconfig = LintConfig() lintconfig.apply_config_from_gitcontext(gitcontext) linter = GitLinter(lintconfig) violations = linter.lint(gitcontext) title = " Commit title containing 'WIP', \tleading and trailing whitespace and longer than 72 characters." # expect only certain violations because sample5 has a 'gitlint: T3,' expected = [RuleViolation("T1", "Title exceeds max length (95>72)", title, 1), RuleViolation("T4", "Title contains hard tab characters (\\t)", title, 1), RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", title, 1), RuleViolation("B4", "Second line is not empty", "This line should be empty", 2), RuleViolation("B2", "Line has trailing whitespace", "This line has a trailing space. ", 4), RuleViolation("B2", "Line has trailing whitespace", "This line has a trailing tab.\t", 5), RuleViolation("B3", "Line contains hard tab characters (\\t)", "This line has a trailing tab.\t", 5), ] self.assertListEqual(violations, expected)