diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-01-25 13:26:08 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-01-25 13:26:08 +0000 |
commit | 3313b4f9c3c5d6a579588e77068ca3ae3edffe2b (patch) | |
tree | 81fccb2a4db79b0de9b0d64701581add3a9a1d21 /gitlint/tests/git | |
parent | Adding upstream version 0.14.0. (diff) | |
download | gitlint-3313b4f9c3c5d6a579588e77068ca3ae3edffe2b.tar.xz gitlint-3313b4f9c3c5d6a579588e77068ca3ae3edffe2b.zip |
Adding upstream version 0.15.0.upstream/0.15.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gitlint/tests/git')
-rw-r--r-- | gitlint/tests/git/test_git.py | 45 | ||||
-rw-r--r-- | gitlint/tests/git/test_git_commit.py | 230 | ||||
-rw-r--r-- | gitlint/tests/git/test_git_context.py | 55 |
3 files changed, 156 insertions, 174 deletions
diff --git a/gitlint/tests/git/test_git.py b/gitlint/tests/git/test_git.py index 1830119..7b9b7c6 100644 --- a/gitlint/tests/git/test_git.py +++ b/gitlint/tests/git/test_git.py @@ -1,12 +1,7 @@ # -*- coding: utf-8 -*- import os -try: - # python 2.x - from mock import patch -except ImportError: - # python 3.x - from unittest.mock import patch # pylint: disable=no-name-in-module, import-error +from unittest.mock import patch from gitlint.shell import ErrorReturnCode, CommandNotFound @@ -19,7 +14,7 @@ class GitTests(BaseTestCase): # Expected special_args passed to 'sh' expected_sh_special_args = { '_tty_out': False, - '_cwd': u"fåke/path" + '_cwd': "fåke/path" } @patch('gitlint.git.sh') @@ -28,7 +23,7 @@ class GitTests(BaseTestCase): expected_msg = "'git' command not found. You need to install git to use gitlint on a local repository. " + \ "See https://git-scm.com/book/en/v2/Getting-Started-Installing-Git on how to install git." with self.assertRaisesMessage(GitNotInstalledError, expected_msg): - GitContext.from_local_repository(u"fåke/path") + GitContext.from_local_repository("fåke/path") # assert that commit message was read using git command sh.git.assert_called_once_with("log", "-1", "--pretty=%H", **self.expected_sh_special_args) @@ -39,8 +34,8 @@ class GitTests(BaseTestCase): err = b"fatal: Not a git repository (or any of the parent directories): .git" sh.git.side_effect = ErrorReturnCode("git log -1 --pretty=%H", b"", err) - with self.assertRaisesMessage(GitContextError, u"fåke/path is not a git repository."): - GitContext.from_local_repository(u"fåke/path") + with self.assertRaisesMessage(GitContextError, "fåke/path is not a git repository."): + GitContext.from_local_repository("fåke/path") # assert that commit message was read using git command sh.git.assert_called_once_with("log", "-1", "--pretty=%H", **self.expected_sh_special_args) @@ -49,9 +44,9 @@ class GitTests(BaseTestCase): err = b"fatal: Random git error" sh.git.side_effect = ErrorReturnCode("git log -1 --pretty=%H", b"", err) - expected_msg = u"An error occurred while executing 'git log -1 --pretty=%H': {0}".format(err) + expected_msg = f"An error occurred while executing 'git log -1 --pretty=%H': {err}" with self.assertRaisesMessage(GitContextError, expected_msg): - GitContext.from_local_repository(u"fåke/path") + GitContext.from_local_repository("fåke/path") # assert that commit message was read using git command sh.git.assert_called_once_with("log", "-1", "--pretty=%H", **self.expected_sh_special_args) @@ -63,9 +58,9 @@ class GitTests(BaseTestCase): sh.git.side_effect = ErrorReturnCode("git log -1 --pretty=%H", b"", err) - expected_msg = u"Current branch has no commits. Gitlint requires at least one commit to function." + expected_msg = "Current branch has no commits. Gitlint requires at least one commit to function." with self.assertRaisesMessage(GitContextError, expected_msg): - GitContext.from_local_repository(u"fåke/path") + GitContext.from_local_repository("fåke/path") # assert that commit message was read using git command sh.git.assert_called_once_with("log", "-1", "--pretty=%H", **self.expected_sh_special_args) @@ -78,12 +73,12 @@ class GitTests(BaseTestCase): b"'git <command> [<revision>...] -- [<file>...]'") sh.git.side_effect = [ - u"#\n", # git config --get core.commentchar + "#\n", # git config --get core.commentchar ErrorReturnCode("rev-parse --abbrev-ref HEAD", b"", err) ] with self.assertRaisesMessage(GitContextError, expected_msg): - context = GitContext.from_commit_msg(u"test") + context = GitContext.from_commit_msg("test") context.current_branch # assert that commit message was read using git command @@ -95,21 +90,19 @@ class GitTests(BaseTestCase): self.assertEqual(git_commentchar(), "#") git.return_value.exit_code = 0 - git.return_value.__str__ = lambda _: u"ä" - git.return_value.__unicode__ = lambda _: u"ä" - self.assertEqual(git_commentchar(), u"ä") + git.return_value = "ä" + self.assertEqual(git_commentchar(), "ä") git.return_value = ';\n' - self.assertEqual(git_commentchar(os.path.join(u"/föo", u"bar")), ';') + self.assertEqual(git_commentchar(os.path.join("/föo", "bar")), ';') git.assert_called_with("config", "--get", "core.commentchar", _ok_code=[0, 1], - _cwd=os.path.join(u"/föo", u"bar")) + _cwd=os.path.join("/föo", "bar")) @patch("gitlint.git._git") def test_git_hooks_dir(self, git): - hooks_dir = os.path.join(u"föo", ".git", "hooks") - git.return_value.__str__ = lambda _: hooks_dir + "\n" - git.return_value.__unicode__ = lambda _: hooks_dir + "\n" - self.assertEqual(git_hooks_dir(u"/blä"), os.path.abspath(os.path.join(u"/blä", hooks_dir))) + hooks_dir = os.path.join("föo", ".git", "hooks") + git.return_value = hooks_dir + "\n" + self.assertEqual(git_hooks_dir("/blä"), os.path.abspath(os.path.join("/blä", hooks_dir))) - git.assert_called_once_with("rev-parse", "--git-path", "hooks", _cwd=u"/blä") + git.assert_called_once_with("rev-parse", "--git-path", "hooks", _cwd="/blä") diff --git a/gitlint/tests/git/test_git_commit.py b/gitlint/tests/git/test_git_commit.py index 5f87a8e..6bb545a 100644 --- a/gitlint/tests/git/test_git_commit.py +++ b/gitlint/tests/git/test_git_commit.py @@ -6,17 +6,11 @@ import dateutil import arrow -try: - # python 2.x - from mock import patch, call -except ImportError: - # python 3.x - from unittest.mock import patch, call # pylint: disable=no-name-in-module, import-error +from unittest.mock import patch, call from gitlint.tests.base import BaseTestCase from gitlint.git import GitContext, GitCommit, GitContextError, LocalGitCommit, StagedLocalGitCommit, GitCommitMessage from gitlint.shell import ErrorReturnCode -from gitlint.utils import ustr class GitCommitTests(BaseTestCase): @@ -24,7 +18,7 @@ class GitCommitTests(BaseTestCase): # Expected special_args passed to 'sh' expected_sh_special_args = { '_tty_out': False, - '_cwd': u"fåke/path" + '_cwd': "fåke/path" } @patch('gitlint.git.sh') @@ -33,14 +27,14 @@ class GitCommitTests(BaseTestCase): sh.git.side_effect = [ sample_sha, - u"test åuthor\x00test-emåil@foo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" - u"cömmit-title\n\ncömmit-body", - u"#", # git config --get core.commentchar - u"file1.txt\npåth/to/file2.txt\n", - u"foöbar\n* hürdur\n" + "test åuthor\x00test-emåil@foo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" + "cömmit-title\n\ncömmit-body", + "#", # git config --get core.commentchar + "file1.txt\npåth/to/file2.txt\n", + "foöbar\n* hürdur\n" ] - context = GitContext.from_local_repository(u"fåke/path") + context = GitContext.from_local_repository("fåke/path") # assert that commit info was read using git command expected_calls = [ call("log", "-1", "--pretty=%H", **self.expected_sh_special_args), @@ -57,13 +51,13 @@ class GitCommitTests(BaseTestCase): last_commit = context.commits[-1] self.assertIsInstance(last_commit, LocalGitCommit) self.assertEqual(last_commit.sha, sample_sha) - self.assertEqual(last_commit.message.title, u"cömmit-title") - self.assertEqual(last_commit.message.body, ["", u"cömmit-body"]) - self.assertEqual(last_commit.author_name, u"test åuthor") - self.assertEqual(last_commit.author_email, u"test-emåil@foo.com") + self.assertEqual(last_commit.message.title, "cömmit-title") + self.assertEqual(last_commit.message.body, ["", "cömmit-body"]) + self.assertEqual(last_commit.author_name, "test åuthor") + self.assertEqual(last_commit.author_email, "test-emåil@foo.com") self.assertEqual(last_commit.date, datetime.datetime(2016, 12, 3, 15, 28, 15, tzinfo=dateutil.tz.tzoffset("+0100", 3600))) - self.assertListEqual(last_commit.parents, [u"åbc"]) + self.assertListEqual(last_commit.parents, ["åbc"]) self.assertFalse(last_commit.is_merge_commit) self.assertFalse(last_commit.is_fixup_commit) self.assertFalse(last_commit.is_squash_commit) @@ -72,11 +66,11 @@ class GitCommitTests(BaseTestCase): # First 2 'git log' calls should've happened at this point self.assertListEqual(sh.git.mock_calls, expected_calls[:3]) - self.assertListEqual(last_commit.changed_files, ["file1.txt", u"påth/to/file2.txt"]) + self.assertListEqual(last_commit.changed_files, ["file1.txt", "påth/to/file2.txt"]) # 'git diff-tree' should have happened at this point self.assertListEqual(sh.git.mock_calls, expected_calls[:4]) - self.assertListEqual(last_commit.branches, [u"foöbar", u"hürdur"]) + self.assertListEqual(last_commit.branches, ["foöbar", "hürdur"]) # All expected calls should've happened at this point self.assertListEqual(sh.git.mock_calls, expected_calls) @@ -86,14 +80,14 @@ class GitCommitTests(BaseTestCase): sh.git.side_effect = [ sample_sha, - u"test åuthor\x00test-emåil@foo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" - u"cömmit-title\n\ncömmit-body", - u"#", # git config --get core.commentchar - u"file1.txt\npåth/to/file2.txt\n", - u"foöbar\n* hürdur\n" + "test åuthor\x00test-emåil@foo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" + "cömmit-title\n\ncömmit-body", + "#", # git config --get core.commentchar + "file1.txt\npåth/to/file2.txt\n", + "foöbar\n* hürdur\n" ] - context = GitContext.from_local_repository(u"fåke/path", sample_sha) + context = GitContext.from_local_repository("fåke/path", sample_sha) # assert that commit info was read using git command expected_calls = [ call("rev-list", sample_sha, **self.expected_sh_special_args), @@ -110,13 +104,13 @@ class GitCommitTests(BaseTestCase): last_commit = context.commits[-1] self.assertIsInstance(last_commit, LocalGitCommit) self.assertEqual(last_commit.sha, sample_sha) - self.assertEqual(last_commit.message.title, u"cömmit-title") - self.assertEqual(last_commit.message.body, ["", u"cömmit-body"]) - self.assertEqual(last_commit.author_name, u"test åuthor") - self.assertEqual(last_commit.author_email, u"test-emåil@foo.com") + self.assertEqual(last_commit.message.title, "cömmit-title") + self.assertEqual(last_commit.message.body, ["", "cömmit-body"]) + self.assertEqual(last_commit.author_name, "test åuthor") + self.assertEqual(last_commit.author_email, "test-emåil@foo.com") self.assertEqual(last_commit.date, datetime.datetime(2016, 12, 3, 15, 28, 15, tzinfo=dateutil.tz.tzoffset("+0100", 3600))) - self.assertListEqual(last_commit.parents, [u"åbc"]) + self.assertListEqual(last_commit.parents, ["åbc"]) self.assertFalse(last_commit.is_merge_commit) self.assertFalse(last_commit.is_fixup_commit) self.assertFalse(last_commit.is_squash_commit) @@ -125,11 +119,11 @@ class GitCommitTests(BaseTestCase): # First 2 'git log' calls should've happened at this point self.assertListEqual(sh.git.mock_calls, expected_calls[:3]) - self.assertListEqual(last_commit.changed_files, ["file1.txt", u"påth/to/file2.txt"]) + self.assertListEqual(last_commit.changed_files, ["file1.txt", "påth/to/file2.txt"]) # 'git diff-tree' should have happened at this point self.assertListEqual(sh.git.mock_calls, expected_calls[:4]) - self.assertListEqual(last_commit.branches, [u"foöbar", u"hürdur"]) + self.assertListEqual(last_commit.branches, ["foöbar", "hürdur"]) # All expected calls should've happened at this point self.assertListEqual(sh.git.mock_calls, expected_calls) @@ -139,14 +133,14 @@ class GitCommitTests(BaseTestCase): sh.git.side_effect = [ sample_sha, - u"test åuthor\x00test-emåil@foo.com\x002016-12-03 15:28:15 +0100\x00åbc def\n" - u"Merge \"foo bår commit\"", - u"#", # git config --get core.commentchar - u"file1.txt\npåth/to/file2.txt\n", - u"foöbar\n* hürdur\n" + "test åuthor\x00test-emåil@foo.com\x002016-12-03 15:28:15 +0100\x00åbc def\n" + "Merge \"foo bår commit\"", + "#", # git config --get core.commentchar + "file1.txt\npåth/to/file2.txt\n", + "foöbar\n* hürdur\n" ] - context = GitContext.from_local_repository(u"fåke/path") + context = GitContext.from_local_repository("fåke/path") # assert that commit info was read using git command expected_calls = [ call("log", "-1", "--pretty=%H", **self.expected_sh_special_args), @@ -163,13 +157,13 @@ class GitCommitTests(BaseTestCase): last_commit = context.commits[-1] self.assertIsInstance(last_commit, LocalGitCommit) self.assertEqual(last_commit.sha, sample_sha) - self.assertEqual(last_commit.message.title, u"Merge \"foo bår commit\"") + self.assertEqual(last_commit.message.title, "Merge \"foo bår commit\"") self.assertEqual(last_commit.message.body, []) - self.assertEqual(last_commit.author_name, u"test åuthor") - self.assertEqual(last_commit.author_email, u"test-emåil@foo.com") + self.assertEqual(last_commit.author_name, "test åuthor") + self.assertEqual(last_commit.author_email, "test-emåil@foo.com") self.assertEqual(last_commit.date, datetime.datetime(2016, 12, 3, 15, 28, 15, tzinfo=dateutil.tz.tzoffset("+0100", 3600))) - self.assertListEqual(last_commit.parents, [u"åbc", "def"]) + self.assertListEqual(last_commit.parents, ["åbc", "def"]) self.assertTrue(last_commit.is_merge_commit) self.assertFalse(last_commit.is_fixup_commit) self.assertFalse(last_commit.is_squash_commit) @@ -178,11 +172,11 @@ class GitCommitTests(BaseTestCase): # First 2 'git log' calls should've happened at this point self.assertListEqual(sh.git.mock_calls, expected_calls[:3]) - self.assertListEqual(last_commit.changed_files, ["file1.txt", u"påth/to/file2.txt"]) + self.assertListEqual(last_commit.changed_files, ["file1.txt", "påth/to/file2.txt"]) # 'git diff-tree' should have happened at this point self.assertListEqual(sh.git.mock_calls, expected_calls[:4]) - self.assertListEqual(last_commit.branches, [u"foöbar", u"hürdur"]) + self.assertListEqual(last_commit.branches, ["foöbar", "hürdur"]) # All expected calls should've happened at this point self.assertListEqual(sh.git.mock_calls, expected_calls) @@ -194,14 +188,14 @@ class GitCommitTests(BaseTestCase): sh.git.side_effect = [ sample_sha, - u"test åuthor\x00test-emåil@foo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" - u"{0}! \"foo bår commit\"".format(commit_type), - u"#", # git config --get core.commentchar - u"file1.txt\npåth/to/file2.txt\n", - u"foöbar\n* hürdur\n" + "test åuthor\x00test-emåil@foo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" + f"{commit_type}! \"foo bår commit\"", + "#", # git config --get core.commentchar + "file1.txt\npåth/to/file2.txt\n", + "foöbar\n* hürdur\n" ] - context = GitContext.from_local_repository(u"fåke/path") + context = GitContext.from_local_repository("fåke/path") # assert that commit info was read using git command expected_calls = [ call("log", "-1", "--pretty=%H", **self.expected_sh_special_args), @@ -218,13 +212,13 @@ class GitCommitTests(BaseTestCase): last_commit = context.commits[-1] self.assertIsInstance(last_commit, LocalGitCommit) self.assertEqual(last_commit.sha, sample_sha) - self.assertEqual(last_commit.message.title, u"{0}! \"foo bår commit\"".format(commit_type)) + self.assertEqual(last_commit.message.title, f"{commit_type}! \"foo bår commit\"") self.assertEqual(last_commit.message.body, []) - self.assertEqual(last_commit.author_name, u"test åuthor") - self.assertEqual(last_commit.author_email, u"test-emåil@foo.com") + self.assertEqual(last_commit.author_name, "test åuthor") + self.assertEqual(last_commit.author_email, "test-emåil@foo.com") self.assertEqual(last_commit.date, datetime.datetime(2016, 12, 3, 15, 28, 15, tzinfo=dateutil.tz.tzoffset("+0100", 3600))) - self.assertListEqual(last_commit.parents, [u"åbc"]) + self.assertListEqual(last_commit.parents, ["åbc"]) # First 2 'git log' calls should've happened at this point self.assertEqual(sh.git.mock_calls, expected_calls[:3]) @@ -236,13 +230,13 @@ class GitCommitTests(BaseTestCase): self.assertFalse(last_commit.is_merge_commit) self.assertFalse(last_commit.is_revert_commit) - self.assertListEqual(last_commit.changed_files, ["file1.txt", u"påth/to/file2.txt"]) + self.assertListEqual(last_commit.changed_files, ["file1.txt", "påth/to/file2.txt"]) - self.assertListEqual(last_commit.changed_files, ["file1.txt", u"påth/to/file2.txt"]) + self.assertListEqual(last_commit.changed_files, ["file1.txt", "påth/to/file2.txt"]) # 'git diff-tree' should have happened at this point self.assertListEqual(sh.git.mock_calls, expected_calls[:4]) - self.assertListEqual(last_commit.branches, [u"foöbar", u"hürdur"]) + self.assertListEqual(last_commit.branches, ["foöbar", "hürdur"]) # All expected calls should've happened at this point self.assertListEqual(sh.git.mock_calls, expected_calls) @@ -250,27 +244,27 @@ class GitCommitTests(BaseTestCase): @patch("gitlint.git.git_commentchar") def test_from_commit_msg_full(self, commentchar): - commentchar.return_value = u"#" + commentchar.return_value = "#" gitcontext = GitContext.from_commit_msg(self.get_sample("commit_message/sample1")) - expected_title = u"Commit title contåining 'WIP', as well as trailing punctuation." + expected_title = "Commit title contåining '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.", - u"This line has a tråiling space. ", + "This line has a tråiling space. ", "This line has a trailing tab.\t"] expected_full = expected_title + "\n" + "\n".join(expected_body) expected_original = expected_full + ( - u"\n# This is a cömmented line\n" - u"# ------------------------ >8 ------------------------\n" - u"# Anything after this line should be cleaned up\n" - u"# this line appears on `git commit -v` command\n" - u"diff --git a/gitlint/tests/samples/commit_message/sample1 " - u"b/gitlint/tests/samples/commit_message/sample1\n" - u"index 82dbe7f..ae71a14 100644\n" - u"--- a/gitlint/tests/samples/commit_message/sample1\n" - u"+++ b/gitlint/tests/samples/commit_message/sample1\n" - u"@@ -1 +1 @@\n" + "\n# This is a cömmented line\n" + "# ------------------------ >8 ------------------------\n" + "# Anything after this line should be cleaned up\n" + "# this line appears on `git commit -v` command\n" + "diff --git a/gitlint/tests/samples/commit_message/sample1 " + "b/gitlint/tests/samples/commit_message/sample1\n" + "index 82dbe7f..ae71a14 100644\n" + "--- a/gitlint/tests/samples/commit_message/sample1\n" + "+++ b/gitlint/tests/samples/commit_message/sample1\n" + "@@ -1 +1 @@\n" ) commit = gitcontext.commits[-1] @@ -297,10 +291,10 @@ class GitCommitTests(BaseTestCase): self.assertIsInstance(commit, GitCommit) self.assertFalse(isinstance(commit, LocalGitCommit)) - self.assertEqual(commit.message.title, u"Just a title contåining WIP") + self.assertEqual(commit.message.title, "Just a title contåining WIP") self.assertEqual(commit.message.body, []) - self.assertEqual(commit.message.full, u"Just a title contåining WIP") - self.assertEqual(commit.message.original, u"Just a title contåining WIP") + self.assertEqual(commit.message.full, "Just a title contåining WIP") + self.assertEqual(commit.message.original, "Just a title contåining WIP") self.assertEqual(commit.author_name, None) self.assertEqual(commit.author_email, None) self.assertListEqual(commit.parents, []) @@ -334,16 +328,16 @@ class GitCommitTests(BaseTestCase): @patch("gitlint.git.git_commentchar") def test_from_commit_msg_comment(self, commentchar): - commentchar.return_value = u"#" - gitcontext = GitContext.from_commit_msg(u"Tïtle\n\nBödy 1\n#Cömment\nBody 2") + commentchar.return_value = "#" + gitcontext = GitContext.from_commit_msg("Tïtle\n\nBödy 1\n#Cömment\nBody 2") commit = gitcontext.commits[-1] self.assertIsInstance(commit, GitCommit) self.assertFalse(isinstance(commit, LocalGitCommit)) - self.assertEqual(commit.message.title, u"Tïtle") - self.assertEqual(commit.message.body, ["", u"Bödy 1", "Body 2"]) - self.assertEqual(commit.message.full, u"Tïtle\n\nBödy 1\nBody 2") - self.assertEqual(commit.message.original, u"Tïtle\n\nBödy 1\n#Cömment\nBody 2") + self.assertEqual(commit.message.title, "Tïtle") + self.assertEqual(commit.message.body, ["", "Bödy 1", "Body 2"]) + self.assertEqual(commit.message.full, "Tïtle\n\nBödy 1\nBody 2") + self.assertEqual(commit.message.original, "Tïtle\n\nBödy 1\n#Cömment\nBody 2") self.assertEqual(commit.author_name, None) self.assertEqual(commit.author_email, None) self.assertEqual(commit.date, None) @@ -402,7 +396,7 @@ class GitCommitTests(BaseTestCase): def test_from_commit_msg_fixup_squash_commit(self): commit_types = ["fixup", "squash"] for commit_type in commit_types: - commit_msg = "{0}! Test message".format(commit_type) + commit_msg = f"{commit_type}! Test message" gitcontext = GitContext.from_commit_msg(commit_msg) commit = gitcontext.commits[-1] @@ -431,16 +425,16 @@ class GitCommitTests(BaseTestCase): # StagedLocalGitCommit() sh.git.side_effect = [ - u"#", # git config --get core.commentchar - u"test åuthor\n", # git config --get user.name - u"test-emåil@foo.com\n", # git config --get user.email - u"my-brånch\n", # git rev-parse --abbrev-ref HEAD - u"file1.txt\npåth/to/file2.txt\n", + "#", # git config --get core.commentchar + "test åuthor\n", # git config --get user.name + "test-emåil@foo.com\n", # git config --get user.email + "my-brånch\n", # git rev-parse --abbrev-ref HEAD + "file1.txt\npåth/to/file2.txt\n", ] now.side_effect = [arrow.get("2020-02-19T12:18:46.675182+01:00")] # We use a fixup commit, just to test a non-default path - context = GitContext.from_staged_commit(u"fixup! Foōbar 123\n\ncömmit-body\n", u"fåke/path") + context = GitContext.from_staged_commit("fixup! Foōbar 123\n\ncömmit-body\n", "fåke/path") # git calls we're expexting expected_calls = [ @@ -454,15 +448,15 @@ class GitCommitTests(BaseTestCase): last_commit = context.commits[-1] self.assertIsInstance(last_commit, StagedLocalGitCommit) self.assertIsNone(last_commit.sha, None) - self.assertEqual(last_commit.message.title, u"fixup! Foōbar 123") - self.assertEqual(last_commit.message.body, ["", u"cömmit-body"]) + self.assertEqual(last_commit.message.title, "fixup! Foōbar 123") + self.assertEqual(last_commit.message.body, ["", "cömmit-body"]) # Only `git config --get core.commentchar` should've happened up until this point self.assertListEqual(sh.git.mock_calls, expected_calls[0:1]) - self.assertEqual(last_commit.author_name, u"test åuthor") + self.assertEqual(last_commit.author_name, "test åuthor") self.assertListEqual(sh.git.mock_calls, expected_calls[0:2]) - self.assertEqual(last_commit.author_email, u"test-emåil@foo.com") + self.assertEqual(last_commit.author_email, "test-emåil@foo.com") self.assertListEqual(sh.git.mock_calls, expected_calls[0:3]) self.assertEqual(last_commit.date, datetime.datetime(2020, 2, 19, 12, 18, 46, @@ -475,10 +469,10 @@ class GitCommitTests(BaseTestCase): self.assertFalse(last_commit.is_squash_commit) self.assertFalse(last_commit.is_revert_commit) - self.assertListEqual(last_commit.branches, [u"my-brånch"]) + self.assertListEqual(last_commit.branches, ["my-brånch"]) self.assertListEqual(sh.git.mock_calls, expected_calls[0:4]) - self.assertListEqual(last_commit.changed_files, ["file1.txt", u"påth/to/file2.txt"]) + self.assertListEqual(last_commit.changed_files, ["file1.txt", "påth/to/file2.txt"]) self.assertListEqual(sh.git.mock_calls, expected_calls[0:5]) @patch('gitlint.git.sh') @@ -486,32 +480,32 @@ class GitCommitTests(BaseTestCase): # StagedLocalGitCommit() sh.git.side_effect = [ - u"#", # git config --get core.commentchar + "#", # git config --get core.commentchar ErrorReturnCode('git config --get user.name', b"", b""), ] expected_msg = "Missing git configuration: please set user.name" with self.assertRaisesMessage(GitContextError, expected_msg): - ctx = GitContext.from_staged_commit(u"Foōbar 123\n\ncömmit-body\n", u"fåke/path") - [ustr(commit) for commit in ctx.commits] + ctx = GitContext.from_staged_commit("Foōbar 123\n\ncömmit-body\n", "fåke/path") + [str(commit) for commit in ctx.commits] @patch('gitlint.git.sh') def test_staged_commit_with_missing_email(self, sh): # StagedLocalGitCommit() sh.git.side_effect = [ - u"#", # git config --get core.commentchar - u"test åuthor\n", # git config --get user.name + "#", # git config --get core.commentchar + "test åuthor\n", # git config --get user.name ErrorReturnCode('git config --get user.name', b"", b""), ] expected_msg = "Missing git configuration: please set user.email" with self.assertRaisesMessage(GitContextError, expected_msg): - ctx = GitContext.from_staged_commit(u"Foōbar 123\n\ncömmit-body\n", u"fåke/path") - [ustr(commit) for commit in ctx.commits] + ctx = GitContext.from_staged_commit("Foōbar 123\n\ncömmit-body\n", "fåke/path") + [str(commit) for commit in ctx.commits] def test_gitcommitmessage_equality(self): - commit_message1 = GitCommitMessage(GitContext(), u"tëst\n\nfoo", u"tëst\n\nfoo", u"tēst", ["", u"föo"]) + commit_message1 = GitCommitMessage(GitContext(), "tëst\n\nfoo", "tëst\n\nfoo", "tēst", ["", "föo"]) attrs = ['original', 'full', 'title', 'body'] self.object_equality_test(commit_message1, attrs, {"context": commit_message1.context}) @@ -519,20 +513,20 @@ class GitCommitTests(BaseTestCase): 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 = u"foöbar" + git.return_value = "foöbar" # Test simple equality case now = datetime.datetime.utcnow() context1 = GitContext() - commit_message1 = GitCommitMessage(context1, u"tëst\n\nfoo", u"tëst\n\nfoo", u"tēst", ["", u"föo"]) - commit1 = GitCommit(context1, commit_message1, u"shä", now, u"Jöhn Smith", u"jöhn.smith@test.com", None, - [u"föo/bar"], [u"brånch1", u"brånch2"]) + 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öhn.smith@test.com", None, + ["föo/bar"], ["brånch1", "brånch2"]) context1.commits = [commit1] context2 = GitContext() - commit_message2 = GitCommitMessage(context2, u"tëst\n\nfoo", u"tëst\n\nfoo", u"tēst", ["", u"föo"]) - commit2 = GitCommit(context2, commit_message1, u"shä", now, u"Jöhn Smith", u"jöhn.smith@test.com", None, - [u"föo/bar"], [u"brånch1", u"brånch2"]) + 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öhn.smith@test.com", None, + ["föo/bar"], ["brånch1", "brånch2"]) context2.commits = [commit2] self.assertEqual(context1, context2) @@ -547,8 +541,8 @@ class GitCommitTests(BaseTestCase): 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': u"Merge: foöbar", 'is_fixup_commit': u"fixup! foöbar", - 'is_squash_commit': u"squash! foöbar", 'is_revert_commit': u"Revert: foöbar"} + 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) @@ -556,16 +550,16 @@ class GitCommitTests(BaseTestCase): self.assertTrue(getattr(clone1, key)) clone2 = GitCommit(context=commit1.context, **kwargs_copy) - clone2.message = GitCommitMessage.from_full_message(context1, u"foöbar") + clone2.message = GitCommitMessage.from_full_message(context1, "foöbar") self.assertNotEqual(clone1, clone2) @patch("gitlint.git.git_commentchar") def test_commit_msg_custom_commentchar(self, patched): - patched.return_value = u"ä" + patched.return_value = "ä" context = GitContext() - message = GitCommitMessage.from_full_message(context, u"Tïtle\n\nBödy 1\näCömment\nBody 2") + message = GitCommitMessage.from_full_message(context, "Tïtle\n\nBödy 1\näCömment\nBody 2") - self.assertEqual(message.title, u"Tïtle") - self.assertEqual(message.body, ["", u"Bödy 1", "Body 2"]) - self.assertEqual(message.full, u"Tïtle\n\nBödy 1\nBody 2") - self.assertEqual(message.original, u"Tïtle\n\nBödy 1\näCömment\nBody 2") + self.assertEqual(message.title, "Tïtle") + self.assertEqual(message.body, ["", "Bödy 1", "Body 2"]) + self.assertEqual(message.full, "Tïtle\n\nBödy 1\nBody 2") + self.assertEqual(message.original, "Tïtle\n\nBödy 1\näCömment\nBody 2") diff --git a/gitlint/tests/git/test_git_context.py b/gitlint/tests/git/test_git_context.py index b243d5e..bb05236 100644 --- a/gitlint/tests/git/test_git_context.py +++ b/gitlint/tests/git/test_git_context.py @@ -1,11 +1,6 @@ # -*- coding: utf-8 -*- -try: - # python 2.x - from mock import patch, call -except ImportError: - # python 3.x - from unittest.mock import patch, call # pylint: disable=no-name-in-module, import-error +from unittest.mock import patch, call from gitlint.tests.base import BaseTestCase from gitlint.git import GitContext @@ -16,15 +11,15 @@ class GitContextTests(BaseTestCase): # Expected special_args passed to 'sh' expected_sh_special_args = { '_tty_out': False, - '_cwd': u"fåke/path" + '_cwd': "fåke/path" } @patch('gitlint.git.sh') def test_gitcontext(self, sh): sh.git.side_effect = [ - u"#", # git config --get core.commentchar - u"\nfoöbar\n" + "#", # git config --get core.commentchar + "\nfoöbar\n" ] expected_calls = [ @@ -32,58 +27,58 @@ class GitContextTests(BaseTestCase): call("rev-parse", "--abbrev-ref", "HEAD", **self.expected_sh_special_args) ] - context = GitContext(u"fåke/path") + context = GitContext("fåke/path") self.assertEqual(sh.git.mock_calls, []) # gitcontext.comment_branch - self.assertEqual(context.commentchar, u"#") + self.assertEqual(context.commentchar, "#") self.assertEqual(sh.git.mock_calls, expected_calls[0:1]) # gitcontext.current_branch - self.assertEqual(context.current_branch, u"foöbar") + self.assertEqual(context.current_branch, "foöbar") self.assertEqual(sh.git.mock_calls, expected_calls) @patch('gitlint.git.sh') def test_gitcontext_equality(self, sh): sh.git.side_effect = [ - u"û\n", # context1: git config --get core.commentchar - u"û\n", # context2: git config --get core.commentchar - u"my-brånch\n", # context1: git rev-parse --abbrev-ref HEAD - u"my-brånch\n", # context2: git rev-parse --abbrev-ref HEAD + "û\n", # context1: git config --get core.commentchar + "û\n", # context2: git config --get core.commentchar + "my-brånch\n", # context1: git rev-parse --abbrev-ref HEAD + "my-brånch\n", # context2: git rev-parse --abbrev-ref HEAD ] - context1 = GitContext(u"fåke/path") - context1.commits = [u"fōo", u"bår"] # we don't need real commits to check for equality + context1 = GitContext("fåke/path") + context1.commits = ["fōo", "bår"] # we don't need real commits to check for equality - context2 = GitContext(u"fåke/path") - context2.commits = [u"fōo", u"bår"] + context2 = GitContext("fåke/path") + context2.commits = ["fōo", "bår"] self.assertEqual(context1, context2) # INEQUALITY # Different commits - context2.commits = [u"hür", u"dür"] + context2.commits = ["hür", "dür"] self.assertNotEqual(context1, context2) # Different repository_path context2.commits = context1.commits - context2.repository_path = u"ōther/path" + context2.repository_path = "ōther/path" self.assertNotEqual(context1, context2) # Different comment_char - context3 = GitContext(u"fåke/path") - context3.commits = [u"fōo", u"bår"] + context3 = GitContext("fåke/path") + context3.commits = ["fōo", "bår"] sh.git.side_effect = ([ - u"ç\n", # context3: git config --get core.commentchar - u"my-brånch\n" # context3: git rev-parse --abbrev-ref HEAD + "ç\n", # context3: git config --get core.commentchar + "my-brånch\n" # context3: git rev-parse --abbrev-ref HEAD ]) self.assertNotEqual(context1, context3) # Different current_branch - context4 = GitContext(u"fåke/path") - context4.commits = [u"fōo", u"bår"] + context4 = GitContext("fåke/path") + context4.commits = ["fōo", "bår"] sh.git.side_effect = ([ - u"û\n", # context4: git config --get core.commentchar - u"different-brånch\n" # context4: git rev-parse --abbrev-ref HEAD + "û\n", # context4: git config --get core.commentchar + "different-brånch\n" # context4: git rev-parse --abbrev-ref HEAD ]) self.assertNotEqual(context1, context4) |