def test_author_valid_email_rule(self): rule = AuthorValidEmail() # valid email addresses valid_email_addresses = [ u"fö[email protected]", u"Jö[email protected]", u"jö[email protected]", u"jöhn/[email protected]", u"jö[email protected]" ] for email in valid_email_addresses: commit = GitCommit(None, None, author_email=email) violations = rule.validate(commit) self.assertIsNone(violations) # No email address (=allowed for now, as gitlint also lints messages passed via stdin that don't have an # email address) commit = GitCommit(None, None) violations = rule.validate(commit) self.assertIsNone(violations) # Invalid email addresses: no TLD, no domain, no @, space anywhere (=valid but not allowed by gitlint) invalid_email_addresses = [ u"föo@bar", u"JöhnDoe", u"Jöhn Doe", u"Jöhn [email protected]", u" Jö[email protected]", u"JöhnDoe@ foo.com", u"JöhnDoe@foo. com", u"JöhnDoe@foo. com", u"@bår.com", u"fö[email protected]" ] for email in invalid_email_addresses: commit = GitCommit(None, None, author_email=email) violations = rule.validate(commit) self.assertListEqual(violations, [ RuleViolation("M1", "Author email for commit is invalid", email) ])
def test_gitcommit_equality(self): # Test simple equality case now = datetime.datetime.utcnow() context1 = GitContext() commit_message1 = GitCommitMessage(u"tëst\n\nfoo", u"tëst\n\nfoo", u"tēst", ["", u"föo"]) commit1 = GitCommit(context1, commit_message1, now, u"Jöhn Smith", u"jö[email protected]", None, True, [u"föo/bar"]) context1.commits = [commit1] context2 = GitContext() commit_message2 = GitCommitMessage(u"tëst\n\nfoo", u"tëst\n\nfoo", u"tēst", ["", u"föo"]) commit2 = GitCommit(context1, commit_message1, now, u"Jöhn Smith", u"jö[email protected]", None, True, [u"föo/bar"]) context2.commits = [commit2] self.assertEqual(context1, context2) self.assertEqual(commit_message1, commit_message2) self.assertEqual(commit1, commit2) # Check that objects are inequal when changing a single attribute for attr in [ 'message', 'author_name', 'author_email', 'parents', 'is_merge_commit', 'changed_files' ]: prev_val = getattr(commit1, attr) setattr(commit1, attr, u"föo") self.assertNotEqual(commit1, commit2) setattr(commit1, attr, prev_val) self.assertEqual(commit1, commit2)
def test_author_valid_email_rule_custom_regex(self): # Custom domain rule = AuthorValidEmail({'regex': u"[^@]+@bår.com"}) valid_email_addresses = [ u"föo@bår.com", u"Jöhn.Doe@bår.com", u"jöhn+doe@bår.com", u"jöhn/doe@bår.com" ] for email in valid_email_addresses: commit = GitCommit(None, None, author_email=email) violations = rule.validate(commit) self.assertIsNone(violations) # Invalid email addresses invalid_email_addresses = [u"fö[email protected]"] for email in invalid_email_addresses: commit = GitCommit(None, None, author_email=email) violations = rule.validate(commit) self.assertListEqual(violations, [ RuleViolation("M1", "Author email for commit is invalid", email) ])
def test_gitcommit_equality(self, git): # git will be called to setup the context (commentchar and current_branch), just return the same value # This only matters to test gitcontext equality, not gitcommit equality git.return_value = "foöbar" # Test simple equality case now = datetime.datetime.utcnow() context1 = GitContext() commit_message1 = GitCommitMessage(context1, "tëst\n\nfoo", "tëst\n\nfoo", "tēst", ["", "föo"]) commit1 = GitCommit(context1, commit_message1, "shä", now, "Jöhn Smith", "jö[email protected]", None, ["föo/bar"], ["brånch1", "brånch2"]) context1.commits = [commit1] context2 = GitContext() commit_message2 = GitCommitMessage(context2, "tëst\n\nfoo", "tëst\n\nfoo", "tēst", ["", "föo"]) commit2 = GitCommit(context2, commit_message1, "shä", now, "Jöhn Smith", "jö[email protected]", None, ["föo/bar"], ["brånch1", "brånch2"]) context2.commits = [commit2] self.assertEqual(context1, context2) self.assertEqual(commit_message1, commit_message2) self.assertEqual(commit1, commit2) # Check that objects are unequal when changing a single attribute kwargs = {'message': commit1.message, 'sha': commit1.sha, 'date': commit1.date, 'author_name': commit1.author_name, 'author_email': commit1.author_email, 'parents': commit1.parents, 'changed_files': commit1.changed_files, 'branches': commit1.branches} self.object_equality_test(commit1, kwargs.keys(), {"context": commit1.context}) # Check that the is_* attributes that are affected by the commit message affect equality special_messages = {'is_merge_commit': "Merge: foöbar", 'is_fixup_commit': "fixup! foöbar", 'is_squash_commit': "squash! foöbar", 'is_revert_commit': "Revert: foöbar"} for key in special_messages: kwargs_copy = copy.deepcopy(kwargs) clone1 = GitCommit(context=commit1.context, **kwargs_copy) clone1.message = GitCommitMessage.from_full_message(context1, special_messages[key]) self.assertTrue(getattr(clone1, key)) clone2 = GitCommit(context=commit1.context, **kwargs_copy) clone2.message = GitCommitMessage.from_full_message(context1, "foöbar") self.assertNotEqual(clone1, clone2)