diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-01-25 13:26:11 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-01-25 13:26:35 +0000 |
commit | 4fee3e091a8d79a40f70ff9c1f87b29b9340049a (patch) | |
tree | 465ad9629a8ee56548bd6c51c3fc710907564911 /gitlint/tests | |
parent | Releasing debian version 0.14.0-1. (diff) | |
download | gitlint-4fee3e091a8d79a40f70ff9c1f87b29b9340049a.tar.xz gitlint-4fee3e091a8d79a40f70ff9c1f87b29b9340049a.zip |
Merging upstream version 0.15.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gitlint/tests')
30 files changed, 798 insertions, 885 deletions
diff --git a/gitlint/tests/base.py b/gitlint/tests/base.py index c8f68c4..9406240 100644 --- a/gitlint/tests/base.py +++ b/gitlint/tests/base.py @@ -9,33 +9,12 @@ import re import shutil import tempfile -try: - # python 2.x - import unittest2 as unittest -except ImportError: - # python 3.x - import unittest - -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 gitlint.git import GitContext -from gitlint.utils import ustr, IS_PY2, LOG_FORMAT, DEFAULT_ENCODING +import unittest +from unittest.mock import patch -# unittest2's assertRaisesRegex doesn't do unicode comparison. -# Let's monkeypatch the str() function to point to unicode() so that it does :) -# For reference, this is where this patch is required: -# https://hg.python.org/unittest2/file/tip/unittest2/case.py#l227 -try: - # python 2.x - unittest.case.str = unicode -except (AttributeError, NameError): - pass # python 3.x +from gitlint.git import GitContext +from gitlint.utils import LOG_FORMAT, DEFAULT_ENCODING class BaseTestCase(unittest.TestCase): @@ -72,24 +51,22 @@ class BaseTestCase(unittest.TestCase): def get_sample_path(filename=""): # Don't join up empty files names because this will add a trailing slash if filename == "": - return ustr(BaseTestCase.SAMPLES_DIR) + return BaseTestCase.SAMPLES_DIR - return ustr(os.path.join(BaseTestCase.SAMPLES_DIR, filename)) + return os.path.join(BaseTestCase.SAMPLES_DIR, filename) @staticmethod def get_sample(filename=""): """ Read and return the contents of a file in gitlint/tests/samples """ sample_path = BaseTestCase.get_sample_path(filename) with io.open(sample_path, encoding=DEFAULT_ENCODING) as content: - sample = ustr(content.read()) + sample = content.read() return sample @staticmethod def patch_input(side_effect): """ Patches the built-in input() with a provided side-effect """ module_path = "builtins.input" - if IS_PY2: - module_path = "__builtin__.raw_input" patched_module = patch(module_path, side_effect=side_effect) return patched_module @@ -99,7 +76,7 @@ class BaseTestCase(unittest.TestCase): Optionally replace template variables specified by variable_dict. """ expected_path = os.path.join(BaseTestCase.EXPECTED_DIR, filename) with io.open(expected_path, encoding=DEFAULT_ENCODING) as content: - expected = ustr(content.read()) + expected = content.read() if variable_dict: expected = expected.format(**variable_dict) @@ -114,7 +91,7 @@ class BaseTestCase(unittest.TestCase): """ Utility method to easily create gitcontext objects based on a given commit msg string and an optional set of changed files""" with patch("gitlint.git.git_commentchar") as comment_char: - comment_char.return_value = u"#" + comment_char.return_value = "#" gitcontext = GitContext.from_commit_msg(commit_msg_str) commit = gitcontext.commits[-1] if changed_files: @@ -147,8 +124,7 @@ class BaseTestCase(unittest.TestCase): """ Pass-through method to unittest.TestCase.assertRaisesRegex that applies re.escape() to the passed `expected_regex`. This is useful to automatically escape all file paths that might be present in the regex. """ - return super(BaseTestCase, self).assertRaisesRegex(expected_exception, re.escape(expected_regex), - *args, **kwargs) + return super().assertRaisesRegex(expected_exception, re.escape(expected_regex), *args, **kwargs) @contextlib.contextmanager def assertRaisesMessage(self, expected_exception, expected_msg): # pylint: disable=invalid-name @@ -156,17 +132,17 @@ class BaseTestCase(unittest.TestCase): try: yield except expected_exception as exc: - exception_msg = ustr(exc) + exception_msg = str(exc) if exception_msg != expected_msg: - error = u"Right exception, wrong message:\n got: {0}\n expected: {1}" - raise self.fail(error.format(exception_msg, expected_msg)) + error = f"Right exception, wrong message:\n got: {exception_msg}\n expected: {expected_msg}" + raise self.fail(error) # else: everything is fine, just return return except Exception as exc: - raise self.fail(u"Expected '{0}' got '{1}'".format(expected_exception.__name__, exc.__class__.__name__)) + raise self.fail(f"Expected '{expected_exception.__name__}' got '{exc.__class__.__name__}'") # No exception raised while we expected one - raise self.fail("Expected to raise {0}, didn't get an exception at all".format(expected_exception.__name__)) + raise self.fail(f"Expected to raise {expected_exception.__name__}, didn't get an exception at all") def object_equality_test(self, obj, attr_list, ctor_kwargs=None): """ Helper function to easily implement object equality tests. @@ -190,9 +166,9 @@ class BaseTestCase(unittest.TestCase): self.assertEqual(obj, clone) # Change attribute and assert objects are different (via both attribute set and ctor) - setattr(clone, attr, u"föo") + setattr(clone, attr, "föo") self.assertNotEqual(obj, clone) - attr_kwargs_copy[attr] = u"föo" + attr_kwargs_copy[attr] = "föo" self.assertNotEqual(obj, obj.__class__(**attr_kwargs_copy)) @@ -205,4 +181,4 @@ class LogCapture(logging.Handler): self.messages = [] def emit(self, record): - self.messages.append(ustr(self.format(record))) + self.messages.append(self.format(record)) diff --git a/gitlint/tests/cli/test_cli.py b/gitlint/tests/cli/test_cli.py index 88bcfb7..bf35e96 100644 --- a/gitlint/tests/cli/test_cli.py +++ b/gitlint/tests/cli/test_cli.py @@ -8,21 +8,11 @@ import platform import arrow -try: - # python 2.x - from StringIO import StringIO -except ImportError: - # python 3.x - from io import StringIO # pylint: disable=ungrouped-imports +from io import StringIO from click.testing import CliRunner -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 CommandNotFound @@ -59,7 +49,7 @@ class CLITests(BaseTestCase): def test_version(self): """ Test for --version option """ result = self.cli.invoke(cli.cli, ["--version"]) - self.assertEqual(result.output.split("\n")[0], "cli, version {0}".format(__version__)) + self.assertEqual(result.output.split("\n")[0], f"cli, version {__version__}") @patch('gitlint.cli.get_stdin_data', return_value=False) @patch('gitlint.git.sh') @@ -67,11 +57,11 @@ class CLITests(BaseTestCase): """ Test for basic simple linting functionality """ sh.git.side_effect = [ "6f29bf81a8322a04071bb794666e48c443a90360", - u"test åuthor\x00test-email@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" - u"commït-title\n\ncommït-body", - u"#", # git config --get core.commentchar - u"commit-1-branch-1\ncommit-1-branch-2\n", - u"file1.txt\npåth/to/file2.txt\n" + "test åuthor\x00test-email@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" + "commït-title\n\ncommït-body", + "#", # git config --get core.commentchar + "commit-1-branch-1\ncommit-1-branch-2\n", + "file1.txt\npåth/to/file2.txt\n" ] with patch('gitlint.display.stderr', new=StringIO()) as stderr: @@ -89,21 +79,21 @@ class CLITests(BaseTestCase): "25053ccec5e28e1bb8f7551fdbb5ab213ada2401\n" + "4da2656b0dadc76c7ee3fd0243a96cb64007f125\n", # git log --pretty <FORMAT> <SHA> - u"test åuthor1\x00test-email1@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" - u"commït-title1\n\ncommït-body1", - u"#", # git config --get core.commentchar - u"commit-1-branch-1\ncommit-1-branch-2\n", # git branch --contains <sha> - u"commit-1/file-1\ncommit-1/file-2\n", # git diff-tree + "test åuthor1\x00test-email1@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" + "commït-title1\n\ncommït-body1", + "#", # git config --get core.commentchar + "commit-1-branch-1\ncommit-1-branch-2\n", # git branch --contains <sha> + "commit-1/file-1\ncommit-1/file-2\n", # git diff-tree # git log --pretty <FORMAT> <SHA> - u"test åuthor2\x00test-email3@föo.com\x002016-12-04 15:28:15 +0100\x00åbc\n" - u"commït-title2\n\ncommït-body2", - u"commit-2-branch-1\ncommit-2-branch-2\n", # git branch --contains <sha> - u"commit-2/file-1\ncommit-2/file-2\n", # git diff-tree + "test åuthor2\x00test-email3@föo.com\x002016-12-04 15:28:15 +0100\x00åbc\n" + "commït-title2\n\ncommït-body2", + "commit-2-branch-1\ncommit-2-branch-2\n", # git branch --contains <sha> + "commit-2/file-1\ncommit-2/file-2\n", # git diff-tree # git log --pretty <FORMAT> <SHA> - u"test åuthor3\x00test-email3@föo.com\x002016-12-05 15:28:15 +0100\x00åbc\n" - u"commït-title3\n\ncommït-body3", - u"commit-3-branch-1\ncommit-3-branch-2\n", # git branch --contains <sha> - u"commit-3/file-1\ncommit-3/file-2\n", # git diff-tree + "test åuthor3\x00test-email3@föo.com\x002016-12-05 15:28:15 +0100\x00åbc\n" + "commït-title3\n\ncommït-body3", + "commit-3-branch-1\ncommit-3-branch-2\n", # git branch --contains <sha> + "commit-3/file-1\ncommit-3/file-2\n", # git diff-tree ] with patch('gitlint.display.stderr', new=StringIO()) as stderr: @@ -122,21 +112,21 @@ class CLITests(BaseTestCase): "25053ccec5e28e1bb8f7551fdbb5ab213ada2401\n" + "4da2656b0dadc76c7ee3fd0243a96cb64007f125\n", # git log --pretty <FORMAT> <SHA> - u"test åuthor1\x00test-email1@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" - u"commït-title1\n\ncommït-body1", - u"#", # git config --get core.commentchar - u"commit-1-branch-1\ncommit-1-branch-2\n", # git branch --contains <sha> - u"commit-1/file-1\ncommit-1/file-2\n", # git diff-tree + "test åuthor1\x00test-email1@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" + "commït-title1\n\ncommït-body1", + "#", # git config --get core.commentchar + "commit-1-branch-1\ncommit-1-branch-2\n", # git branch --contains <sha> + "commit-1/file-1\ncommit-1/file-2\n", # git diff-tree # git log --pretty <FORMAT> <SHA> - u"test åuthor2\x00test-email2@föo.com\x002016-12-04 15:28:15 +0100\x00åbc\n" - u"commït-title2.\n\ncommït-body2\ngitlint-ignore: T3\n", - u"commit-2-branch-1\ncommit-2-branch-2\n", # git branch --contains <sha> - u"commit-2/file-1\ncommit-2/file-2\n", # git diff-tree + "test åuthor2\x00test-email2@föo.com\x002016-12-04 15:28:15 +0100\x00åbc\n" + "commït-title2.\n\ncommït-body2\ngitlint-ignore: T3\n", + "commit-2-branch-1\ncommit-2-branch-2\n", # git branch --contains <sha> + "commit-2/file-1\ncommit-2/file-2\n", # git diff-tree # git log --pretty <FORMAT> <SHA> - u"test åuthor3\x00test-email3@föo.com\x002016-12-05 15:28:15 +0100\x00åbc\n" - u"commït-title3.\n\ncommït-body3", - u"commit-3-branch-1\ncommit-3-branch-2\n", # git branch --contains <sha> - u"commit-3/file-1\ncommit-3/file-2\n", # git diff-tree + "test åuthor3\x00test-email3@föo.com\x002016-12-05 15:28:15 +0100\x00åbc\n" + "commït-title3.\n\ncommït-body3", + "commit-3-branch-1\ncommit-3-branch-2\n", # git branch --contains <sha> + "commit-3/file-1\ncommit-3/file-2\n", # git diff-tree ] with patch('gitlint.display.stderr', new=StringIO()) as stderr: @@ -157,24 +147,24 @@ class CLITests(BaseTestCase): "25053ccec5e28e1bb8f7551fdbb5ab213ada2401\n" + "4da2656b0dadc76c7ee3fd0243a96cb64007f125\n", # git log --pretty <FORMAT> <SHA> - u"test åuthor1\x00test-email1@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" - u"commït-title1\n\ncommït-body1", - u"#", # git config --get core.commentchar - u"commit-1-branch-1\ncommit-1-branch-2\n", # git branch --contains <sha> - u"commit-1/file-1\ncommit-1/file-2\n", # git diff-tree + "test åuthor1\x00test-email1@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" + "commït-title1\n\ncommït-body1", + "#", # git config --get core.commentchar + "commit-1-branch-1\ncommit-1-branch-2\n", # git branch --contains <sha> + "commit-1/file-1\ncommit-1/file-2\n", # git diff-tree # git log --pretty <FORMAT> <SHA> - u"test åuthor2\x00test-email3@föo.com\x002016-12-04 15:28:15 +0100\x00åbc\n" + "test åuthor2\x00test-email3@föo.com\x002016-12-04 15:28:15 +0100\x00åbc\n" # Normally T3 violation (trailing punctuation), but this commit is ignored because of # config below - u"commït-title2.\n\ncommït-body2\n", - u"commit-2-branch-1\ncommit-2-branch-2\n", # git branch --contains <sha> - u"commit-2/file-1\ncommit-2/file-2\n", # git diff-tree + "commït-title2.\n\ncommït-body2\n", + "commit-2-branch-1\ncommit-2-branch-2\n", # git branch --contains <sha> + "commit-2/file-1\ncommit-2/file-2\n", # git diff-tree # git log --pretty <FORMAT> <SHA> - u"test åuthor3\x00test-email3@föo.com\x002016-12-05 15:28:15 +0100\x00åbc\n" + "test åuthor3\x00test-email3@föo.com\x002016-12-05 15:28:15 +0100\x00åbc\n" # Normally T1 and B5 violations, now only T1 because we're ignoring B5 in config below - u"commït-title3.\n\ncommït-body3 foo", - u"commit-3-branch-1\ncommit-3-branch-2\n", # git branch --contains <sha> - u"commit-3/file-1\ncommit-3/file-2\n", # git diff-tree + "commït-title3.\n\ncommït-body3 foo", + "commit-3-branch-1\ncommit-3-branch-2\n", # git branch --contains <sha> + "commit-3/file-1\ncommit-3/file-2\n", # git diff-tree ] with patch('gitlint.display.stderr', new=StringIO()) as stderr: @@ -183,9 +173,9 @@ class CLITests(BaseTestCase): # We expect that the second commit has no failures because of it matching against I1.regex # Because we do test for the 3th commit to return violations, this test also ensures that a unique # config object is passed to each commit lint call - expected = (u"Commit 6f29bf81a8:\n" + expected = ("Commit 6f29bf81a8:\n" u'3: B5 Body message is too short (12<20): "commït-body1"\n\n' - u"Commit 4da2656b0d:\n" + "Commit 4da2656b0d:\n" u'1: T3 Title has trailing punctuation (.): "commït-title3."\n') self.assertEqual(stderr.getvalue(), expected) self.assertEqual(result.exit_code, 2) @@ -218,11 +208,11 @@ class CLITests(BaseTestCase): """ Test for ignoring stdin when --ignore-stdin flag is enabled""" sh.git.side_effect = [ "6f29bf81a8322a04071bb794666e48c443a90360", - u"test åuthor\x00test-email@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" - u"commït-title\n\ncommït-body", - u"#", # git config --get core.commentchar - u"commit-1-branch-1\ncommit-1-branch-2\n", # git branch --contains <sha> - u"file1.txt\npåth/to/file2.txt\n" # git diff-tree + "test åuthor\x00test-email@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" + "commït-title\n\ncommït-body", + "#", # git config --get core.commentchar + "commit-1-branch-1\ncommit-1-branch-2\n", # git branch --contains <sha> + "file1.txt\npåth/to/file2.txt\n" # git diff-tree ] with patch('gitlint.display.stderr', new=StringIO()) as stderr: @@ -240,11 +230,11 @@ class CLITests(BaseTestCase): """ Test for ignoring stdin when --ignore-stdin flag is enabled""" sh.git.side_effect = [ - u"#", # git config --get core.commentchar - u"föo user\n", # git config --get user.name - u"föo@bar.com\n", # git config --get user.email - u"my-branch\n", # git rev-parse --abbrev-ref HEAD (=current branch) - u"commit-1/file-1\ncommit-1/file-2\n", # git diff-tree + "#", # git config --get core.commentchar + "föo user\n", # git config --get user.name + "föo@bar.com\n", # git config --get user.email + "my-branch\n", # git rev-parse --abbrev-ref HEAD (=current branch) + "commit-1/file-1\ncommit-1/file-2\n", # git diff-tree ] with patch('gitlint.display.stderr', new=StringIO()) as stderr: @@ -263,17 +253,17 @@ class CLITests(BaseTestCase): """ Test for ignoring stdin when --ignore-stdin flag is enabled""" sh.git.side_effect = [ - u"#", # git config --get core.commentchar - u"föo user\n", # git config --get user.name - u"föo@bar.com\n", # git config --get user.email - u"my-branch\n", # git rev-parse --abbrev-ref HEAD (=current branch) - u"commit-1/file-1\ncommit-1/file-2\n", # git diff-tree + "#", # git config --get core.commentchar + "föo user\n", # git config --get user.name + "föo@bar.com\n", # git config --get user.email + "my-branch\n", # git rev-parse --abbrev-ref HEAD (=current branch) + "commit-1/file-1\ncommit-1/file-2\n", # git diff-tree ] with self.tempdir() as tmpdir: msg_filename = os.path.join(tmpdir, "msg") with io.open(msg_filename, 'w', encoding=DEFAULT_ENCODING) as f: - f.write(u"WIP: msg-filename tïtle\n") + f.write("WIP: msg-filename tïtle\n") with patch('gitlint.display.stderr', new=StringIO()) as stderr: result = self.cli.invoke(cli.cli, ["--debug", "--staged", "--msg-filename", msg_filename]) @@ -289,17 +279,17 @@ class CLITests(BaseTestCase): def test_lint_staged_negative(self, _): result = self.cli.invoke(cli.cli, ["--staged"]) self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE) - self.assertEqual(result.output, (u"Error: The 'staged' option (--staged) can only be used when using " - u"'--msg-filename' or when piping data to gitlint via stdin.\n")) + self.assertEqual(result.output, ("Error: The 'staged' option (--staged) can only be used when using " + "'--msg-filename' or when piping data to gitlint via stdin.\n")) @patch('gitlint.cli.get_stdin_data', return_value=False) def test_msg_filename(self, _): - expected_output = u"3: B6 Body message is missing\n" + expected_output = "3: B6 Body message is missing\n" with self.tempdir() as tmpdir: msg_filename = os.path.join(tmpdir, "msg") with io.open(msg_filename, 'w', encoding=DEFAULT_ENCODING) as f: - f.write(u"Commït title\n") + f.write("Commït title\n") with patch('gitlint.display.stderr', new=StringIO()) as stderr: result = self.cli.invoke(cli.cli, ["--msg-filename", msg_filename]) @@ -307,7 +297,7 @@ class CLITests(BaseTestCase): self.assertEqual(result.exit_code, 1) self.assertEqual(result.output, "") - @patch('gitlint.cli.get_stdin_data', return_value=u"WIP: tïtle \n") + @patch('gitlint.cli.get_stdin_data', return_value="WIP: tïtle \n") def test_silent_mode(self, _): """ Test for --silent option """ with patch('gitlint.display.stderr', new=StringIO()) as stderr: @@ -316,7 +306,7 @@ class CLITests(BaseTestCase): self.assertEqual(result.exit_code, 3) self.assertEqual(result.output, "") - @patch('gitlint.cli.get_stdin_data', return_value=u"WIP: tïtle \n") + @patch('gitlint.cli.get_stdin_data', return_value="WIP: tïtle \n") def test_verbosity(self, _): """ Test for --verbosity option """ # We only test -v and -vv, more testing is really not required here @@ -333,7 +323,7 @@ class CLITests(BaseTestCase): "3: B6 Body message is missing\n" with patch('gitlint.display.stderr', new=StringIO()) as stderr: - result = self.cli.invoke(cli.cli, ["-vv"], input=u"WIP: tïtle \n") + result = self.cli.invoke(cli.cli, ["-vv"], input="WIP: tïtle \n") self.assertEqual(stderr.getvalue(), expected_output) self.assertEqual(result.exit_code, 3) self.assertEqual(result.output, "") @@ -355,19 +345,19 @@ class CLITests(BaseTestCase): "25053ccec5e28e1bb8f7551fdbb5ab213ada2401\n" "4da2656b0dadc76c7ee3fd0243a96cb64007f125\n", # git log --pretty <FORMAT> <SHA> - u"test åuthor1\x00test-email1@föo.com\x002016-12-03 15:28:15 +0100\x00abc\n" - u"commït-title1\n\ncommït-body1", - u"#", # git config --get core.commentchar - u"commit-1-branch-1\ncommit-1-branch-2\n", # git branch --contains <sha> - u"commit-1/file-1\ncommit-1/file-2\n", # git diff-tree - u"test åuthor2\x00test-email2@föo.com\x002016-12-04 15:28:15 +0100\x00abc\n" - u"commït-title2.\n\ncommït-body2", - u"commit-2-branch-1\ncommit-2-branch-2\n", # git branch --contains <sha> - u"commit-2/file-1\ncommit-2/file-2\n", # git diff-tree - u"test åuthor3\x00test-email3@föo.com\x002016-12-05 15:28:15 +0100\x00abc\n" - u"föobar\nbar", - u"commit-3-branch-1\ncommit-3-branch-2\n", # git branch --contains <sha> - u"commit-3/file-1\ncommit-3/file-2\n", # git diff-tree + "test åuthor1\x00test-email1@föo.com\x002016-12-03 15:28:15 +0100\x00abc\n" + "commït-title1\n\ncommït-body1", + "#", # git config --get core.commentchar + "commit-1-branch-1\ncommit-1-branch-2\n", # git branch --contains <sha> + "commit-1/file-1\ncommit-1/file-2\n", # git diff-tree + "test åuthor2\x00test-email2@föo.com\x002016-12-04 15:28:15 +0100\x00abc\n" + "commït-title2.\n\ncommït-body2", + "commit-2-branch-1\ncommit-2-branch-2\n", # git branch --contains <sha> + "commit-2/file-1\ncommit-2/file-2\n", # git diff-tree + "test åuthor3\x00test-email3@föo.com\x002016-12-05 15:28:15 +0100\x00abc\n" + "föobar\nbar", + "commit-3-branch-1\ncommit-3-branch-2\n", # git branch --contains <sha> + "commit-3/file-1\ncommit-3/file-2\n", # git diff-tree ] with patch('gitlint.display.stderr', new=StringIO()) as stderr: @@ -387,14 +377,14 @@ class CLITests(BaseTestCase): expected_logs = self.get_expected('cli/test_cli/test_debug_1', expected_kwargs) self.assert_logged(expected_logs) - @patch('gitlint.cli.get_stdin_data', return_value=u"Test tïtle\n") + @patch('gitlint.cli.get_stdin_data', return_value="Test tïtle\n") def test_extra_path(self, _): """ Test for --extra-path flag """ # Test extra-path pointing to a directory with patch('gitlint.display.stderr', new=StringIO()) as stderr: extra_path = self.get_sample_path("user_rules") result = self.cli.invoke(cli.cli, ["--extra-path", extra_path]) - expected_output = u"1: UC1 Commit violåtion 1: \"Contënt 1\"\n" + \ + expected_output = "1: UC1 Commit violåtion 1: \"Contënt 1\"\n" + \ "3: B6 Body message is missing\n" self.assertEqual(stderr.getvalue(), expected_output) self.assertEqual(result.exit_code, 2) @@ -403,12 +393,12 @@ class CLITests(BaseTestCase): with patch('gitlint.display.stderr', new=StringIO()) as stderr: extra_path = self.get_sample_path(os.path.join("user_rules", "my_commit_rules.py")) result = self.cli.invoke(cli.cli, ["--extra-path", extra_path]) - expected_output = u"1: UC1 Commit violåtion 1: \"Contënt 1\"\n" + \ + expected_output = "1: UC1 Commit violåtion 1: \"Contënt 1\"\n" + \ "3: B6 Body message is missing\n" self.assertEqual(stderr.getvalue(), expected_output) self.assertEqual(result.exit_code, 2) - @patch('gitlint.cli.get_stdin_data', return_value=u"Test tïtle\n\nMy body that is long enough") + @patch('gitlint.cli.get_stdin_data', return_value="Test tïtle\n\nMy body that is long enough") def test_contrib(self, _): # Test enabled contrib rules with patch('gitlint.display.stderr', new=StringIO()) as stderr: @@ -417,13 +407,13 @@ class CLITests(BaseTestCase): self.assertEqual(stderr.getvalue(), expected_output) self.assertEqual(result.exit_code, 3) - @patch('gitlint.cli.get_stdin_data', return_value=u"Test tïtle\n") + @patch('gitlint.cli.get_stdin_data', return_value="Test tïtle\n") def test_contrib_negative(self, _): - result = self.cli.invoke(cli.cli, ["--contrib", u"föobar,CC1"]) - self.assertEqual(result.output, u"Config Error: No contrib rule with id or name 'föobar' found.\n") + result = self.cli.invoke(cli.cli, ["--contrib", "föobar,CC1"]) + self.assertEqual(result.output, "Config Error: No contrib rule with id or name 'föobar' found.\n") self.assertEqual(result.exit_code, self.CONFIG_ERROR_CODE) - @patch('gitlint.cli.get_stdin_data', return_value=u"WIP: tëst") + @patch('gitlint.cli.get_stdin_data', return_value="WIP: tëst") def test_config_file(self, _): """ Test for --config option """ with patch('gitlint.display.stderr', new=StringIO()) as stderr: @@ -438,16 +428,14 @@ class CLITests(BaseTestCase): # Directory as config file config_path = self.get_sample_path("config") result = self.cli.invoke(cli.cli, ["--config", config_path]) - expected_string = u"Error: Invalid value for \"-C\" / \"--config\": File \"{0}\" is a directory.".format( - config_path) + expected_string = f"Error: Invalid value for '-C' / '--config': File '{config_path}' is a directory." self.assertEqual(result.output.split("\n")[3], expected_string) self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE) # Non existing file - config_path = self.get_sample_path(u"föo") + config_path = self.get_sample_path("föo") result = self.cli.invoke(cli.cli, ["--config", config_path]) - expected_string = u"Error: Invalid value for \"-C\" / \"--config\": File \"{0}\" does not exist.".format( - config_path) + expected_string = f"Error: Invalid value for '-C' / '--config': File '{config_path}' does not exist." self.assertEqual(result.output.split("\n")[3], expected_string) self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE) @@ -471,37 +459,37 @@ class CLITests(BaseTestCase): def test_target_negative(self): """ Negative test for the --target option """ # try setting a non-existing target - result = self.cli.invoke(cli.cli, ["--target", u"/föo/bar"]) + result = self.cli.invoke(cli.cli, ["--target", "/föo/bar"]) self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE) - expected_msg = u"Error: Invalid value for \"--target\": Directory \"/föo/bar\" does not exist." + expected_msg = "Error: Invalid value for '--target': Directory '/föo/bar' does not exist." self.assertEqual(result.output.split("\n")[3], expected_msg) # try setting a file as target target_path = self.get_sample_path(os.path.join("config", "gitlintconfig")) result = self.cli.invoke(cli.cli, ["--target", target_path]) self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE) - expected_msg = u"Error: Invalid value for \"--target\": Directory \"{0}\" is a file.".format(target_path) + expected_msg = f"Error: Invalid value for '--target': Directory '{target_path}' is a file." self.assertEqual(result.output.split("\n")[3], expected_msg) @patch('gitlint.config.LintConfigGenerator.generate_config') def test_generate_config(self, generate_config): """ Test for the generate-config subcommand """ - result = self.cli.invoke(cli.cli, ["generate-config"], input=u"tëstfile\n") + result = self.cli.invoke(cli.cli, ["generate-config"], input="tëstfile\n") self.assertEqual(result.exit_code, 0) - expected_msg = u"Please specify a location for the sample gitlint config file [.gitlint]: tëstfile\n" + \ - u"Successfully generated {0}\n".format(os.path.realpath(u"tëstfile")) + expected_msg = "Please specify a location for the sample gitlint config file [.gitlint]: tëstfile\n" + \ + f"Successfully generated {os.path.realpath('tëstfile')}\n" self.assertEqual(result.output, expected_msg) - generate_config.assert_called_once_with(os.path.realpath(u"tëstfile")) + generate_config.assert_called_once_with(os.path.realpath("tëstfile")) def test_generate_config_negative(self): """ Negative test for the generate-config subcommand """ # Non-existing directory - fake_dir = os.path.abspath(u"/föo") - fake_path = os.path.join(fake_dir, u"bar") + fake_dir = os.path.abspath("/föo") + fake_path = os.path.join(fake_dir, "bar") result = self.cli.invoke(cli.cli, ["generate-config"], input=fake_path) self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE) - expected_msg = (u"Please specify a location for the sample gitlint config file [.gitlint]: {0}\n" - + u"Error: Directory '{1}' does not exist.\n").format(fake_path, fake_dir) + expected_msg = f"Please specify a location for the sample gitlint config file [.gitlint]: {fake_path}\n" + \ + f"Error: Directory '{fake_dir}' does not exist.\n" self.assertEqual(result.output, expected_msg) # Existing file @@ -509,8 +497,8 @@ class CLITests(BaseTestCase): result = self.cli.invoke(cli.cli, ["generate-config"], input=sample_path) self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE) expected_msg = "Please specify a location for the sample gitlint " + \ - "config file [.gitlint]: {0}\n".format(sample_path) + \ - "Error: File \"{0}\" already exists.\n".format(sample_path) + f"config file [.gitlint]: {sample_path}\n" + \ + f"Error: File \"{sample_path}\" already exists.\n" self.assertEqual(result.output, expected_msg) @patch('gitlint.cli.get_stdin_data', return_value=False) @@ -528,10 +516,10 @@ class CLITests(BaseTestCase): sh.git.side_effect = lambda *_args, **_kwargs: "" result = self.cli.invoke(cli.cli, ["--commits", "master...HEAD"]) - self.assert_log_contains(u"DEBUG: gitlint.cli No commits in range \"master...HEAD\"") + self.assert_log_contains("DEBUG: gitlint.cli No commits in range \"master...HEAD\"") self.assertEqual(result.exit_code, 0) - @patch('gitlint.cli.get_stdin_data', return_value=u"WIP: tëst tïtle") + @patch('gitlint.cli.get_stdin_data', return_value="WIP: tëst tïtle") def test_named_rules(self, _): with patch('gitlint.display.stderr', new=StringIO()) as stderr: config_path = self.get_sample_path(os.path.join("config", "named-rules")) diff --git a/gitlint/tests/cli/test_cli_hooks.py b/gitlint/tests/cli/test_cli_hooks.py index b5e7fc4..825345f 100644 --- a/gitlint/tests/cli/test_cli_hooks.py +++ b/gitlint/tests/cli/test_cli_hooks.py @@ -1,28 +1,18 @@ # -*- coding: utf-8 -*- import io +from io import StringIO import os from click.testing import CliRunner -try: - # python 2.x - from StringIO import StringIO -except ImportError: - # python 3.x - from io import StringIO # pylint: disable=ungrouped-imports - -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.tests.base import BaseTestCase from gitlint import cli from gitlint import hooks from gitlint import config +from gitlint.shell import ErrorReturnCode from gitlint.utils import DEFAULT_ENCODING @@ -45,12 +35,12 @@ class CLIHookTests(BaseTestCase): self.git_version_path.stop() @patch('gitlint.hooks.GitHookInstaller.install_commit_msg_hook') - @patch('gitlint.hooks.git_hooks_dir', return_value=os.path.join(u"/hür", u"dur")) + @patch('gitlint.hooks.git_hooks_dir', return_value=os.path.join("/hür", "dur")) def test_install_hook(self, _, install_hook): """ Test for install-hook subcommand """ result = self.cli.invoke(cli.cli, ["install-hook"]) - expected_path = os.path.join(u"/hür", u"dur", hooks.COMMIT_MSG_HOOK_DST_PATH) - expected = u"Successfully installed gitlint commit-msg hook in {0}\n".format(expected_path) + expected_path = os.path.join("/hür", "dur", hooks.COMMIT_MSG_HOOK_DST_PATH) + expected = f"Successfully installed gitlint commit-msg hook in {expected_path}\n" self.assertEqual(result.output, expected) self.assertEqual(result.exit_code, 0) expected_config = config.LintConfig() @@ -58,12 +48,12 @@ class CLIHookTests(BaseTestCase): install_hook.assert_called_once_with(expected_config) @patch('gitlint.hooks.GitHookInstaller.install_commit_msg_hook') - @patch('gitlint.hooks.git_hooks_dir', return_value=os.path.join(u"/hür", u"dur")) + @patch('gitlint.hooks.git_hooks_dir', return_value=os.path.join("/hür", "dur")) def test_install_hook_target(self, _, install_hook): """ Test for install-hook subcommand with a specific --target option specified """ # Specified target result = self.cli.invoke(cli.cli, ["--target", self.SAMPLES_DIR, "install-hook"]) - expected_path = os.path.join(u"/hür", u"dur", hooks.COMMIT_MSG_HOOK_DST_PATH) + expected_path = os.path.join("/hür", "dur", hooks.COMMIT_MSG_HOOK_DST_PATH) expected = "Successfully installed gitlint commit-msg hook in %s\n" % expected_path self.assertEqual(result.exit_code, 0) self.assertEqual(result.output, expected) @@ -72,40 +62,40 @@ class CLIHookTests(BaseTestCase): expected_config.target = self.SAMPLES_DIR install_hook.assert_called_once_with(expected_config) - @patch('gitlint.hooks.GitHookInstaller.install_commit_msg_hook', side_effect=hooks.GitHookInstallerError(u"tëst")) + @patch('gitlint.hooks.GitHookInstaller.install_commit_msg_hook', side_effect=hooks.GitHookInstallerError("tëst")) def test_install_hook_negative(self, install_hook): """ Negative test for install-hook subcommand """ result = self.cli.invoke(cli.cli, ["install-hook"]) self.assertEqual(result.exit_code, self.GIT_CONTEXT_ERROR_CODE) - self.assertEqual(result.output, u"tëst\n") + self.assertEqual(result.output, "tëst\n") expected_config = config.LintConfig() expected_config.target = os.path.realpath(os.getcwd()) install_hook.assert_called_once_with(expected_config) @patch('gitlint.hooks.GitHookInstaller.uninstall_commit_msg_hook') - @patch('gitlint.hooks.git_hooks_dir', return_value=os.path.join(u"/hür", u"dur")) + @patch('gitlint.hooks.git_hooks_dir', return_value=os.path.join("/hür", "dur")) def test_uninstall_hook(self, _, uninstall_hook): """ Test for uninstall-hook subcommand """ result = self.cli.invoke(cli.cli, ["uninstall-hook"]) - expected_path = os.path.join(u"/hür", u"dur", hooks.COMMIT_MSG_HOOK_DST_PATH) - expected = u"Successfully uninstalled gitlint commit-msg hook from {0}\n".format(expected_path) + expected_path = os.path.join("/hür", "dur", hooks.COMMIT_MSG_HOOK_DST_PATH) + expected = f"Successfully uninstalled gitlint commit-msg hook from {expected_path}\n" self.assertEqual(result.exit_code, 0) self.assertEqual(result.output, expected) expected_config = config.LintConfig() expected_config.target = os.path.realpath(os.getcwd()) uninstall_hook.assert_called_once_with(expected_config) - @patch('gitlint.hooks.GitHookInstaller.uninstall_commit_msg_hook', side_effect=hooks.GitHookInstallerError(u"tëst")) + @patch('gitlint.hooks.GitHookInstaller.uninstall_commit_msg_hook', side_effect=hooks.GitHookInstallerError("tëst")) def test_uninstall_hook_negative(self, uninstall_hook): """ Negative test for uninstall-hook subcommand """ result = self.cli.invoke(cli.cli, ["uninstall-hook"]) self.assertEqual(result.exit_code, self.GIT_CONTEXT_ERROR_CODE) - self.assertEqual(result.output, u"tëst\n") + self.assertEqual(result.output, "tëst\n") expected_config = config.LintConfig() expected_config.target = os.path.realpath(os.getcwd()) uninstall_hook.assert_called_once_with(expected_config) - def test_hook_no_tty(self): + def test_run_hook_no_tty(self): """ Test for run-hook subcommand. When no TTY is available (like is the case for this test), the hook will abort after the first check. """ @@ -119,9 +109,9 @@ class CLIHookTests(BaseTestCase): # check the output which indirectly proves the same thing. with self.tempdir() as tmpdir: - msg_filename = os.path.join(tmpdir, u"hür") + msg_filename = os.path.join(tmpdir, "hür") with io.open(msg_filename, 'w', encoding=DEFAULT_ENCODING) as f: - f.write(u"WIP: tïtle\n") + f.write("WIP: tïtle\n") with patch('gitlint.display.stderr', new=StringIO()) as stderr: result = self.cli.invoke(cli.cli, ["--msg-filename", msg_filename, "run-hook"]) @@ -132,12 +122,12 @@ class CLIHookTests(BaseTestCase): self.assertEqual(result.exit_code, 1) @patch('gitlint.cli.shell') - def test_hook_edit(self, shell): + def test_run_hook_edit(self, shell): """ Test for run-hook subcommand, answering 'e(dit)' after commit-hook """ - set_editors = [None, u"myeditor"] - expected_editors = [u"vim -n", u"myeditor"] - commit_messages = [u"WIP: höok edit 1", u"WIP: höok edit 2"] + set_editors = [None, "myeditor"] + expected_editors = ["vim -n", "myeditor"] + commit_messages = ["WIP: höok edit 1", "WIP: höok edit 2"] for i in range(0, len(set_editors)): if set_editors[i]: @@ -145,7 +135,7 @@ class CLIHookTests(BaseTestCase): with self.patch_input(['e', 'e', 'n']): with self.tempdir() as tmpdir: - msg_filename = os.path.realpath(os.path.join(tmpdir, u"hür")) + msg_filename = os.path.realpath(os.path.join(tmpdir, "hür")) with io.open(msg_filename, 'w', encoding=DEFAULT_ENCODING) as f: f.write(commit_messages[i] + "\n") @@ -161,18 +151,17 @@ class CLIHookTests(BaseTestCase): self.assertEqual(result.exit_code, 2) shell.assert_called_with(expected_editors[i] + " " + msg_filename) - self.assert_log_contains(u"DEBUG: gitlint.cli run-hook: editing commit message") - self.assert_log_contains(u"DEBUG: gitlint.cli run-hook: {0} {1}".format(expected_editors[i], - msg_filename)) + self.assert_log_contains("DEBUG: gitlint.cli run-hook: editing commit message") + self.assert_log_contains(f"DEBUG: gitlint.cli run-hook: {expected_editors[i]} {msg_filename}") - def test_hook_no(self): + def test_run_hook_no(self): """ Test for run-hook subcommand, answering 'n(o)' after commit-hook """ with self.patch_input(['n']): with self.tempdir() as tmpdir: - msg_filename = os.path.join(tmpdir, u"hür") + msg_filename = os.path.join(tmpdir, "hür") with io.open(msg_filename, 'w', encoding=DEFAULT_ENCODING) as f: - f.write(u"WIP: höok no\n") + f.write("WIP: höok no\n") with patch('gitlint.display.stderr', new=StringIO()) as stderr: result = self.cli.invoke(cli.cli, ["--msg-filename", msg_filename, "run-hook"]) @@ -184,13 +173,13 @@ class CLIHookTests(BaseTestCase): self.assertEqual(result.exit_code, 2) self.assert_log_contains("DEBUG: gitlint.cli run-hook: commit message declined") - def test_hook_yes(self): + def test_run_hook_yes(self): """ Test for run-hook subcommand, answering 'y(es)' after commit-hook """ with self.patch_input(['y']): with self.tempdir() as tmpdir: - msg_filename = os.path.join(tmpdir, u"hür") + msg_filename = os.path.join(tmpdir, "hür") with io.open(msg_filename, 'w', encoding=DEFAULT_ENCODING) as f: - f.write(u"WIP: höok yes\n") + f.write("WIP: höok yes\n") with patch('gitlint.display.stderr', new=StringIO()) as stderr: result = self.cli.invoke(cli.cli, ["--msg-filename", msg_filename, "run-hook"]) @@ -202,8 +191,32 @@ class CLIHookTests(BaseTestCase): self.assertEqual(result.exit_code, 0) self.assert_log_contains("DEBUG: gitlint.cli run-hook: commit message accepted") - @patch('gitlint.cli.get_stdin_data', return_value=u"WIP: Test hook stdin tïtle\n") - def test_hook_stdin_violations(self, _): + @patch('gitlint.cli.get_stdin_data', return_value=False) + @patch('gitlint.git.sh') + def test_run_hook_negative(self, sh, _): + """ Negative test for the run-hook subcommand: testing whether exceptions are correctly handled when + running `gitlint run-hook`. + """ + # GIT_CONTEXT_ERROR_CODE: git error + error_msg = b"fatal: not a git repository (or any of the parent directories): .git" + sh.git.side_effect = ErrorReturnCode("full command", b"stdout", error_msg) + result = self.cli.invoke(cli.cli, ["run-hook"]) + expected = self.get_expected('cli/test_cli_hooks/test_run_hook_negative_1', {'git_repo': os.getcwd()}) + self.assertEqual(result.output, expected) + self.assertEqual(result.exit_code, self.GIT_CONTEXT_ERROR_CODE) + + # USAGE_ERROR_CODE: incorrect use of gitlint + result = self.cli.invoke(cli.cli, ["--staged", "run-hook"]) + self.assertEqual(result.output, self.get_expected('cli/test_cli_hooks/test_run_hook_negative_2')) + self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE) + + # CONFIG_ERROR_CODE: incorrect config. Note that this is handled before the hook even runs + result = self.cli.invoke(cli.cli, ["-c", "föo.bár=1", "run-hook"]) + self.assertEqual(result.output, "Config Error: No such rule 'föo'\n") + self.assertEqual(result.exit_code, self.CONFIG_ERROR_CODE) + + @patch('gitlint.cli.get_stdin_data', return_value="WIP: Test hook stdin tïtle\n") + def test_run_hook_stdin_violations(self, _): """ Test for passing stdin data to run-hook, expecting some violations. Equivalent of: $ echo "WIP: Test hook stdin tïtle" | gitlint run-hook """ @@ -216,8 +229,8 @@ class CLIHookTests(BaseTestCase): # Hook will auto-abort because we're using stdin. Abort = exit code 1 self.assertEqual(result.exit_code, 1) - @patch('gitlint.cli.get_stdin_data', return_value=u"Test tïtle\n\nTest bödy that is long enough") - def test_hook_stdin_no_violations(self, _): + @patch('gitlint.cli.get_stdin_data', return_value="Test tïtle\n\nTest bödy that is long enough") + def test_run_hook_stdin_no_violations(self, _): """ Test for passing stdin data to run-hook, expecting *NO* violations, Equivalent of: $ echo -e "Test tïtle\n\nTest bödy that is long enough" | gitlint run-hook """ @@ -229,8 +242,8 @@ class CLIHookTests(BaseTestCase): self.assertEqual(result.output, expected_stdout) self.assertEqual(result.exit_code, 0) - @patch('gitlint.cli.get_stdin_data', return_value=u"WIP: Test hook config tïtle\n") - def test_hook_config(self, _): + @patch('gitlint.cli.get_stdin_data', return_value="WIP: Test hook config tïtle\n") + def test_run_hook_config(self, _): """ Test that gitlint still respects config when running run-hook, equivalent of: $ echo "WIP: Test hook config tïtle" | gitlint -c title-max-length.line-length=5 --ignore B6 run-hook """ @@ -244,18 +257,18 @@ class CLIHookTests(BaseTestCase): @patch('gitlint.cli.get_stdin_data', return_value=False) @patch('gitlint.git.sh') - def test_hook_local_commit(self, sh, _): + def test_run_hook_local_commit(self, sh, _): """ Test running the hook on the last commit-msg from the local repo, equivalent of: $ gitlint run-hook and then choosing 'e' """ sh.git.side_effect = [ "6f29bf81a8322a04071bb794666e48c443a90360", - u"test åuthor\x00test-email@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" - u"WIP: commït-title\n\ncommït-body", - u"#", # git config --get core.commentchar - u"commit-1-branch-1\ncommit-1-branch-2\n", - u"file1.txt\npåth/to/file2.txt\n" + "test åuthor\x00test-email@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" + "WIP: commït-title\n\ncommït-body", + "#", # git config --get core.commentchar + "commit-1-branch-1\ncommit-1-branch-2\n", + "file1.txt\npåth/to/file2.txt\n" ] with self.patch_input(['e']): diff --git a/gitlint/tests/config/test_config.py b/gitlint/tests/config/test_config.py index b981a86..93e35de 100644 --- a/gitlint/tests/config/test_config.py +++ b/gitlint/tests/config/test_config.py @@ -1,16 +1,11 @@ # -*- coding: utf-8 -*- -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 import rules from gitlint.config import LintConfig, LintConfigError, LintConfigGenerator, GITLINT_CONFIG_TEMPLATE_SRC_PATH from gitlint import options -from gitlint.tests.base import BaseTestCase, ustr +from gitlint.tests.base import BaseTestCase class LintConfigTests(BaseTestCase): @@ -29,20 +24,20 @@ class LintConfigTests(BaseTestCase): config = LintConfig() # non-existing rule - expected_error_msg = u"No such rule 'föobar'" + expected_error_msg = "No such rule 'föobar'" with self.assertRaisesMessage(LintConfigError, expected_error_msg): config.set_rule_option(u'föobar', u'lïne-length', 60) # non-existing option - expected_error_msg = u"Rule 'title-max-length' has no option 'föobar'" + expected_error_msg = "Rule 'title-max-length' has no option 'föobar'" with self.assertRaisesMessage(LintConfigError, expected_error_msg): config.set_rule_option('title-max-length', u'föobar', 60) # invalid option value - expected_error_msg = u"'föo' is not a valid value for option 'title-max-length.line-length'. " + \ - u"Option 'line-length' must be a positive integer (current value: 'föo')." + expected_error_msg = "'föo' is not a valid value for option 'title-max-length.line-length'. " + \ + "Option 'line-length' must be a positive integer (current value: 'föo')." with self.assertRaisesMessage(LintConfigError, expected_error_msg): - config.set_rule_option('title-max-length', 'line-length', u"föo") + config.set_rule_option('title-max-length', 'line-length', "föo") def test_set_general_option(self): config = LintConfig() @@ -117,7 +112,7 @@ class LintConfigTests(BaseTestCase): actual_rule = config.rules.find_rule("contrib-title-conventional-commits") self.assertTrue(actual_rule.is_contrib) - self.assertEqual(ustr(type(actual_rule)), "<class 'conventional_commit.ConventionalCommit'>") + self.assertEqual(str(type(actual_rule)), "<class 'conventional_commit.ConventionalCommit'>") self.assertEqual(actual_rule.id, 'CT1') self.assertEqual(actual_rule.name, u'contrib-title-conventional-commits') self.assertEqual(actual_rule.target, rules.CommitMessageTitle) @@ -135,7 +130,7 @@ class LintConfigTests(BaseTestCase): actual_rule = config.rules.find_rule("contrib-body-requires-signed-off-by") self.assertTrue(actual_rule.is_contrib) - self.assertEqual(ustr(type(actual_rule)), "<class 'signedoff_by.SignedOffBy'>") + self.assertEqual(str(type(actual_rule)), "<class 'signedoff_by.SignedOffBy'>") self.assertEqual(actual_rule.id, 'CC1') self.assertEqual(actual_rule.name, u'contrib-body-requires-signed-off-by') @@ -151,15 +146,15 @@ class LintConfigTests(BaseTestCase): def test_contrib_negative(self): config = LintConfig() # non-existent contrib rule - with self.assertRaisesMessage(LintConfigError, u"No contrib rule with id or name 'föo' found."): - config.contrib = u"contrib-title-conventional-commits,föo" + with self.assertRaisesMessage(LintConfigError, "No contrib rule with id or name 'föo' found."): + config.contrib = "contrib-title-conventional-commits,föo" # UserRuleError, RuleOptionError should be re-raised as LintConfigErrors - side_effects = [rules.UserRuleError(u"üser-rule"), options.RuleOptionError(u"rüle-option")] + side_effects = [rules.UserRuleError("üser-rule"), options.RuleOptionError("rüle-option")] for side_effect in side_effects: with patch('gitlint.config.rule_finder.find_rule_classes', side_effect=side_effect): - with self.assertRaisesMessage(LintConfigError, ustr(side_effect)): - config.contrib = u"contrib-title-conventional-commits" + with self.assertRaisesMessage(LintConfigError, str(side_effect)): + config.contrib = "contrib-title-conventional-commits" def test_extra_path(self): config = LintConfig() @@ -168,11 +163,11 @@ class LintConfigTests(BaseTestCase): self.assertEqual(config.extra_path, self.get_user_rules_path()) actual_rule = config.rules.find_rule('UC1') self.assertTrue(actual_rule.is_user_defined) - self.assertEqual(ustr(type(actual_rule)), "<class 'my_commit_rules.MyUserCommitRule'>") + self.assertEqual(str(type(actual_rule)), "<class 'my_commit_rules.MyUserCommitRule'>") self.assertEqual(actual_rule.id, 'UC1') self.assertEqual(actual_rule.name, u'my-üser-commit-rule') self.assertEqual(actual_rule.target, None) - expected_rule_option = options.IntOption('violation-count', 1, u"Number of violåtions to return") + expected_rule_option = options.IntOption('violation-count', 1, "Number of violåtions to return") self.assertListEqual(actual_rule.options_spec, [expected_rule_option]) self.assertDictEqual(actual_rule.options, {'violation-count': expected_rule_option}) @@ -183,10 +178,10 @@ class LintConfigTests(BaseTestCase): def test_extra_path_negative(self): config = LintConfig() - regex = u"Option extra-path must be either an existing directory or file (current value: 'föo/bar')" + regex = "Option extra-path must be either an existing directory or file (current value: 'föo/bar')" # incorrect extra_path with self.assertRaisesMessage(LintConfigError, regex): - config.extra_path = u"föo/bar" + config.extra_path = "föo/bar" # extra path contains classes with errors with self.assertRaisesMessage(LintConfigError, @@ -198,17 +193,17 @@ class LintConfigTests(BaseTestCase): # Note that we shouldn't test whether we can set unicode because python just doesn't allow unicode attributes with self.assertRaisesMessage(LintConfigError, "'foo' is not a valid gitlint option"): - config.set_general_option("foo", u"bår") + config.set_general_option("foo", "bår") # try setting _config_path, this is a real attribute of LintConfig, but the code should prevent it from # being set with self.assertRaisesMessage(LintConfigError, "'_config_path' is not a valid gitlint option"): - config.set_general_option("_config_path", u"bår") + config.set_general_option("_config_path", "bår") # invalid verbosity - incorrect_values = [-1, u"föo"] + incorrect_values = [-1, "föo"] for value in incorrect_values: - expected_msg = u"Option 'verbosity' must be a positive integer (current value: '{0}')".format(value) + expected_msg = f"Option 'verbosity' must be a positive integer (current value: '{value}')" with self.assertRaisesMessage(LintConfigError, expected_msg): config.verbosity = value @@ -220,12 +215,12 @@ class LintConfigTests(BaseTestCase): # 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"] + incorrect_values = [-1, 4, "föo"] for attribute in ignore_attributes: for value in incorrect_values: option_name = attribute.replace("_", "-") with self.assertRaisesMessage(LintConfigError, - "Option '{0}' must be either 'true' or 'false'".format(option_name)): + f"Option '{option_name}' must be either 'true' or 'false'"): setattr(config, attribute, value) # invalid ignore -> not here because ignore is a ListOption which converts everything to a string before @@ -235,15 +230,15 @@ class LintConfigTests(BaseTestCase): for attribute in ['debug', 'staged', 'ignore_stdin']: option_name = attribute.replace("_", "-") with self.assertRaisesMessage(LintConfigError, - "Option '{0}' must be either 'true' or 'false'".format(option_name)): - setattr(config, attribute, u"föobar") + f"Option '{option_name}' must be either 'true' or 'false'"): + setattr(config, attribute, "föobar") # extra-path has its own negative test # invalid target with self.assertRaisesMessage(LintConfigError, - u"Option target must be an existing directory (current value: 'föo/bar')"): - config.target = u"föo/bar" + "Option target must be an existing directory (current value: 'föo/bar')"): + config.target = "föo/bar" def test_ignore_independent_from_rules(self): # Test that the lintconfig rules are not modified when setting config.ignore @@ -273,9 +268,9 @@ class LintConfigTests(BaseTestCase): # Other attributes don't matter config1 = LintConfig() config2 = LintConfig() - config1.foo = u"bår" + config1.foo = "bår" self.assertEqual(config1, config2) - config2.foo = u"dūr" + config2.foo = "dūr" self.assertEqual(config1, config2) @@ -283,5 +278,5 @@ class LintConfigGeneratorTests(BaseTestCase): @staticmethod @patch('gitlint.config.shutil.copyfile') def test_install_commit_msg_hook_negative(copy): - LintConfigGenerator.generate_config(u"föo/bar/test") - copy.assert_called_with(GITLINT_CONFIG_TEMPLATE_SRC_PATH, u"föo/bar/test") + LintConfigGenerator.generate_config("föo/bar/test") + copy.assert_called_with(GITLINT_CONFIG_TEMPLATE_SRC_PATH, "föo/bar/test") diff --git a/gitlint/tests/config/test_config_builder.py b/gitlint/tests/config/test_config_builder.py index 5a28c9f..e0d7f9b 100644 --- a/gitlint/tests/config/test_config_builder.py +++ b/gitlint/tests/config/test_config_builder.py @@ -42,30 +42,30 @@ class LintConfigBuilderTests(BaseTestCase): config_builder = LintConfigBuilder() # nothing gitlint - config_builder.set_config_from_commit(self.gitcommit(u"tëst\ngitlint\nfoo")) + config_builder.set_config_from_commit(self.gitcommit("tëst\ngitlint\nfoo")) config = config_builder.build() self.assertSequenceEqual(config.rules, original_rules) self.assertListEqual(config.ignore, []) # ignore all rules - config_builder.set_config_from_commit(self.gitcommit(u"tëst\ngitlint-ignore: all\nfoo")) + config_builder.set_config_from_commit(self.gitcommit("tëst\ngitlint-ignore: all\nfoo")) config = config_builder.build() self.assertEqual(config.ignore, original_rule_ids) # ignore all rules, no space - config_builder.set_config_from_commit(self.gitcommit(u"tëst\ngitlint-ignore:all\nfoo")) + config_builder.set_config_from_commit(self.gitcommit("tëst\ngitlint-ignore:all\nfoo")) config = config_builder.build() self.assertEqual(config.ignore, original_rule_ids) # ignore all rules, more spacing - config_builder.set_config_from_commit(self.gitcommit(u"tëst\ngitlint-ignore: \t all\nfoo")) + config_builder.set_config_from_commit(self.gitcommit("tëst\ngitlint-ignore: \t all\nfoo")) config = config_builder.build() self.assertEqual(config.ignore, original_rule_ids) def test_set_from_commit_ignore_specific(self): # ignore specific rules config_builder = LintConfigBuilder() - config_builder.set_config_from_commit(self.gitcommit(u"tëst\ngitlint-ignore: T1, body-hard-tab")) + config_builder.set_config_from_commit(self.gitcommit("tëst\ngitlint-ignore: T1, body-hard-tab")) config = config_builder.build() self.assertEqual(config.ignore, ["T1", "body-hard-tab"]) @@ -89,14 +89,14 @@ class LintConfigBuilderTests(BaseTestCase): config_builder = LintConfigBuilder() # bad config file load - foo_path = self.get_sample_path(u"föo") - expected_error_msg = u"Invalid file path: {0}".format(foo_path) + foo_path = self.get_sample_path("föo") + expected_error_msg = f"Invalid file path: {foo_path}" with self.assertRaisesMessage(LintConfigError, expected_error_msg): config_builder.set_from_config_file(foo_path) # error during file parsing path = self.get_sample_path("config/no-sections") - expected_error_msg = u"File contains no section headers." + expected_error_msg = "File contains no section headers." # We only match the start of the message here, since the exact message can vary depending on platform with self.assertRaisesRegex(LintConfigError, expected_error_msg): config_builder.set_from_config_file(path) @@ -105,7 +105,7 @@ class LintConfigBuilderTests(BaseTestCase): path = self.get_sample_path("config/nonexisting-rule") config_builder = LintConfigBuilder() config_builder.set_from_config_file(path) - expected_error_msg = u"No such rule 'föobar'" + expected_error_msg = "No such rule 'föobar'" with self.assertRaisesMessage(LintConfigError, expected_error_msg): config_builder.build() @@ -113,7 +113,7 @@ class LintConfigBuilderTests(BaseTestCase): path = self.get_sample_path("config/nonexisting-general-option") config_builder = LintConfigBuilder() config_builder.set_from_config_file(path) - expected_error_msg = u"'foo' is not a valid gitlint option" + expected_error_msg = "'foo' is not a valid gitlint option" with self.assertRaisesMessage(LintConfigError, expected_error_msg): config_builder.build() @@ -121,7 +121,7 @@ class LintConfigBuilderTests(BaseTestCase): path = self.get_sample_path("config/nonexisting-option") config_builder = LintConfigBuilder() config_builder.set_from_config_file(path) - expected_error_msg = u"Rule 'title-max-length' has no option 'föobar'" + expected_error_msg = "Rule 'title-max-length' has no option 'föobar'" with self.assertRaisesMessage(LintConfigError, expected_error_msg): config_builder.build() @@ -129,8 +129,8 @@ class LintConfigBuilderTests(BaseTestCase): path = self.get_sample_path("config/invalid-option-value") config_builder = LintConfigBuilder() config_builder.set_from_config_file(path) - expected_error_msg = u"'föo' is not a valid value for option 'title-max-length.line-length'. " + \ - u"Option 'line-length' must be a positive integer (current value: 'föo')." + expected_error_msg = "'föo' is not a valid value for option 'title-max-length.line-length'. " + \ + "Option 'line-length' must be a positive integer (current value: 'föo')." with self.assertRaisesMessage(LintConfigError, expected_error_msg): config_builder.build() @@ -141,39 +141,39 @@ class LintConfigBuilderTests(BaseTestCase): config_builder = LintConfigBuilder() config_builder.set_config_from_string_list(['general.verbosity=1', 'title-max-length.line-length=60', 'body-max-line-length.line-length=120', - u"title-must-not-contain-word.words=håha"]) + "title-must-not-contain-word.words=håha"]) config = config_builder.build() self.assertEqual(config.get_rule_option('title-max-length', 'line-length'), 60) self.assertEqual(config.get_rule_option('body-max-line-length', 'line-length'), 120) - self.assertListEqual(config.get_rule_option('title-must-not-contain-word', 'words'), [u"håha"]) + self.assertListEqual(config.get_rule_option('title-must-not-contain-word', 'words'), ["håha"]) self.assertEqual(config.verbosity, 1) def test_set_config_from_string_list_negative(self): config_builder = LintConfigBuilder() # assert error on incorrect rule - this happens at build time - config_builder.set_config_from_string_list([u"föo.bar=1"]) - with self.assertRaisesMessage(LintConfigError, u"No such rule 'föo'"): + config_builder.set_config_from_string_list(["föo.bar=1"]) + with self.assertRaisesMessage(LintConfigError, "No such rule 'föo'"): config_builder.build() # no equal sign - expected_msg = u"'föo.bar' is an invalid configuration option. Use '<rule>.<option>=<value>'" + expected_msg = "'föo.bar' is an invalid configuration option. Use '<rule>.<option>=<value>'" with self.assertRaisesMessage(LintConfigError, expected_msg): - config_builder.set_config_from_string_list([u"föo.bar"]) + config_builder.set_config_from_string_list(["föo.bar"]) # missing value - expected_msg = u"'föo.bar=' is an invalid configuration option. Use '<rule>.<option>=<value>'" + expected_msg = "'föo.bar=' is an invalid configuration option. Use '<rule>.<option>=<value>'" with self.assertRaisesMessage(LintConfigError, expected_msg): - config_builder.set_config_from_string_list([u"föo.bar="]) + config_builder.set_config_from_string_list(["föo.bar="]) # space instead of equal sign - expected_msg = u"'föo.bar 1' is an invalid configuration option. Use '<rule>.<option>=<value>'" + expected_msg = "'föo.bar 1' is an invalid configuration option. Use '<rule>.<option>=<value>'" with self.assertRaisesMessage(LintConfigError, expected_msg): - config_builder.set_config_from_string_list([u"föo.bar 1"]) + config_builder.set_config_from_string_list(["föo.bar 1"]) # no period between rule and option names - expected_msg = u"'föobar=1' is an invalid configuration option. Use '<rule>.<option>=<value>'" + expected_msg = "'föobar=1' is an invalid configuration option. Use '<rule>.<option>=<value>'" with self.assertRaisesMessage(LintConfigError, expected_msg): config_builder.set_config_from_string_list([u'föobar=1']) @@ -216,15 +216,15 @@ class LintConfigBuilderTests(BaseTestCase): # Add a named rule by setting an option in the config builder that follows the named rule pattern # Assert that whitespace in the rule name is stripped rule_qualifiers = [u'T7:my-extra-rüle', u' T7 : my-extra-rüle ', u'\tT7:\tmy-extra-rüle\t', - u'T7:\t\n \tmy-extra-rüle\t\n\n', u"title-match-regex:my-extra-rüle"] + u'T7:\t\n \tmy-extra-rüle\t\n\n', "title-match-regex:my-extra-rüle"] for rule_qualifier in rule_qualifiers: config_builder = LintConfigBuilder() - config_builder.set_option(rule_qualifier, 'regex', u"föo") + config_builder.set_option(rule_qualifier, 'regex', "föo") expected_rules = copy.deepcopy(default_rules) - my_rule = rules.TitleRegexMatches({'regex': u"föo"}) - my_rule.id = rules.TitleRegexMatches.id + u":my-extra-rüle" - my_rule.name = rules.TitleRegexMatches.name + u":my-extra-rüle" + my_rule = rules.TitleRegexMatches({'regex': "föo"}) + my_rule.id = rules.TitleRegexMatches.id + ":my-extra-rüle" + my_rule.name = rules.TitleRegexMatches.name + ":my-extra-rüle" expected_rules._rules[u'T7:my-extra-rüle'] = my_rule self.assertEqual(config_builder.build().rules, expected_rules) @@ -233,32 +233,32 @@ class LintConfigBuilderTests(BaseTestCase): # to the same rule for other_rule_qualifier in rule_qualifiers: cb = config_builder.clone() - cb.set_option(other_rule_qualifier, 'regex', other_rule_qualifier + u"bōr") + cb.set_option(other_rule_qualifier, 'regex', other_rule_qualifier + "bōr") # before setting the expected rule option value correctly, the RuleCollection should be different self.assertNotEqual(cb.build().rules, expected_rules) # after setting the option on the expected rule, it should be equal - my_rule.options['regex'].set(other_rule_qualifier + u"bōr") + my_rule.options['regex'].set(other_rule_qualifier + "bōr") self.assertEqual(cb.build().rules, expected_rules) - my_rule.options['regex'].set(u"wrong") + my_rule.options['regex'].set("wrong") def test_named_rules_negative(self): # T7 = title-match-regex # Invalid rule name - for invalid_name in ["", " ", " ", "\t", "\n", u"å b", u"å:b", u"åb:", u":åb"]: + for invalid_name in ["", " ", " ", "\t", "\n", "å b", "å:b", "åb:", ":åb"]: config_builder = LintConfigBuilder() - config_builder.set_option(u"T7:{0}".format(invalid_name), 'regex', u"tëst") - expected_msg = u"The rule-name part in 'T7:{0}' cannot contain whitespace, colons or be empty" - with self.assertRaisesMessage(LintConfigError, expected_msg.format(invalid_name)): + config_builder.set_option(f"T7:{invalid_name}", 'regex', "tëst") + expected_msg = f"The rule-name part in 'T7:{invalid_name}' cannot contain whitespace, colons or be empty" + with self.assertRaisesMessage(LintConfigError, expected_msg): config_builder.build() # Invalid parent rule name config_builder = LintConfigBuilder() - config_builder.set_option(u"Ž123:foöbar", u"fåke-option", u"fåke-value") - with self.assertRaisesMessage(LintConfigError, u"No such rule 'Ž123' (named rule: 'Ž123:foöbar')"): + config_builder.set_option("Ž123:foöbar", "fåke-option", "fåke-value") + with self.assertRaisesMessage(LintConfigError, "No such rule 'Ž123' (named rule: 'Ž123:foöbar')"): config_builder.build() # Invalid option name (this is the same as with regular rules) config_builder = LintConfigBuilder() - config_builder.set_option(u"T7:foöbar", u"blå", u"my-rëgex") - with self.assertRaisesMessage(LintConfigError, u"Rule 'T7:foöbar' has no option 'blå'"): + config_builder.set_option("T7:foöbar", "blå", "my-rëgex") + with self.assertRaisesMessage(LintConfigError, "Rule 'T7:foöbar' has no option 'blå'"): config_builder.build() diff --git a/gitlint/tests/config/test_config_precedence.py b/gitlint/tests/config/test_config_precedence.py index a0eeccd..aa4de88 100644 --- a/gitlint/tests/config/test_config_precedence.py +++ b/gitlint/tests/config/test_config_precedence.py @@ -1,20 +1,10 @@ # -*- coding: utf-8 -*- -try: - # python 2.x - from StringIO import StringIO -except ImportError: - # python 3.x - from io import StringIO +from io import StringIO from click.testing import CliRunner -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.tests.base import BaseTestCase from gitlint import cli @@ -25,7 +15,7 @@ class LintConfigPrecedenceTests(BaseTestCase): def setUp(self): self.cli = CliRunner() - @patch('gitlint.cli.get_stdin_data', return_value=u"WIP:fö\n\nThis is å test message\n") + @patch('gitlint.cli.get_stdin_data', return_value="WIP:fö\n\nThis is å test message\n") def test_config_precedence(self, _): # TODO(jroovers): this test really only test verbosity, we need to do some refactoring to gitlint.cli # to more easily test everything @@ -41,14 +31,14 @@ class LintConfigPrecedenceTests(BaseTestCase): with patch('gitlint.display.stderr', new=StringIO()) as stderr: result = self.cli.invoke(cli.cli, ["-vvv", "-c", "general.verbosity=2", "--config", config_path]) self.assertEqual(result.output, "") - self.assertEqual(stderr.getvalue(), u"1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP:fö\"\n") + self.assertEqual(stderr.getvalue(), "1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP:fö\"\n") # 2. environment variables with patch('gitlint.display.stderr', new=StringIO()) as stderr: result = self.cli.invoke(cli.cli, ["-c", "general.verbosity=2", "--config", config_path], env={"GITLINT_VERBOSITY": "3"}) self.assertEqual(result.output, "") - self.assertEqual(stderr.getvalue(), u"1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP:fö\"\n") + self.assertEqual(stderr.getvalue(), "1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP:fö\"\n") # 3. commandline -c flags with patch('gitlint.display.stderr', new=StringIO()) as stderr: @@ -66,9 +56,9 @@ class LintConfigPrecedenceTests(BaseTestCase): with patch('gitlint.display.stderr', new=StringIO()) as stderr: result = self.cli.invoke(cli.cli) self.assertEqual(result.output, "") - self.assertEqual(stderr.getvalue(), u"1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP:fö\"\n") + self.assertEqual(stderr.getvalue(), "1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP:fö\"\n") - @patch('gitlint.cli.get_stdin_data', return_value=u"WIP: This is å test") + @patch('gitlint.cli.get_stdin_data', return_value="WIP: This is å test") def test_ignore_precedence(self, get_stdin_data): with patch('gitlint.display.stderr', new=StringIO()) as stderr: # --ignore takes precedence over -c general.ignore @@ -77,11 +67,11 @@ class LintConfigPrecedenceTests(BaseTestCase): self.assertEqual(result.exit_code, 1) # We still expect the T5 violation, but no B6 violation as --ignore overwrites -c general.ignore self.assertEqual(stderr.getvalue(), - u"1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: This is å test\"\n") + "1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: This is å test\"\n") # test that we can also still configure a rule that is first ignored but then not with patch('gitlint.display.stderr', new=StringIO()) as stderr: - get_stdin_data.return_value = u"This is å test" + get_stdin_data.return_value = "This is å test" # --ignore takes precedence over -c general.ignore result = self.cli.invoke(cli.cli, ["-c", "general.ignore=title-max-length", "-c", "title-max-length.line-length=5", @@ -91,7 +81,7 @@ class LintConfigPrecedenceTests(BaseTestCase): # We still expect the T1 violation with custom config, # but no B6 violation as --ignore overwrites -c general.ignore - self.assertEqual(stderr.getvalue(), u"1: T1 Title exceeds max length (14>5): \"This is å test\"\n") + self.assertEqual(stderr.getvalue(), "1: T1 Title exceeds max length (14>5): \"This is å test\"\n") def test_general_option_after_rule_option(self): # We used to have a bug where we didn't process general options before setting specific options, this would diff --git a/gitlint/tests/config/test_rule_collection.py b/gitlint/tests/config/test_rule_collection.py index 089992c..5a50be0 100644 --- a/gitlint/tests/config/test_rule_collection.py +++ b/gitlint/tests/config/test_rule_collection.py @@ -10,34 +10,34 @@ class RuleCollectionTests(BaseTestCase): def test_add_rule(self): collection = RuleCollection() - collection.add_rule(rules.TitleMaxLength, u"my-rüle", {"my_attr": u"föo", "my_attr2": 123}) + collection.add_rule(rules.TitleMaxLength, "my-rüle", {"my_attr": "föo", "my_attr2": 123}) expected = rules.TitleMaxLength() - expected.id = u"my-rüle" - expected.my_attr = u"föo" + expected.id = "my-rüle" + expected.my_attr = "föo" expected.my_attr2 = 123 self.assertEqual(len(collection), 1) - self.assertDictEqual(collection._rules, OrderedDict({u"my-rüle": expected})) + self.assertDictEqual(collection._rules, OrderedDict({"my-rüle": expected})) # Need to explicitely compare expected attributes as the rule.__eq__ method does not compare these attributes self.assertEqual(collection._rules[expected.id].my_attr, expected.my_attr) self.assertEqual(collection._rules[expected.id].my_attr2, expected.my_attr2) def test_add_find_rule(self): collection = RuleCollection() - collection.add_rules([rules.TitleMaxLength, rules.TitleTrailingWhitespace], {"my_attr": u"föo"}) + collection.add_rules([rules.TitleMaxLength, rules.TitleTrailingWhitespace], {"my_attr": "föo"}) # find by id expected = rules.TitleMaxLength() rule = collection.find_rule('T1') self.assertEqual(rule, expected) - self.assertEqual(rule.my_attr, u"föo") + self.assertEqual(rule.my_attr, "föo") # find by name expected2 = rules.TitleTrailingWhitespace() rule = collection.find_rule('title-trailing-whitespace') self.assertEqual(rule, expected2) - self.assertEqual(rule.my_attr, u"föo") + self.assertEqual(rule.my_attr, "föo") # find non-existing rule = collection.find_rule(u'föo') @@ -45,8 +45,8 @@ class RuleCollectionTests(BaseTestCase): def test_delete_rules_by_attr(self): collection = RuleCollection() - collection.add_rules([rules.TitleMaxLength, rules.TitleTrailingWhitespace], {"foo": u"bår"}) - collection.add_rules([rules.BodyHardTab], {"hur": u"dûr"}) + collection.add_rules([rules.TitleMaxLength, rules.TitleTrailingWhitespace], {"foo": "bår"}) + collection.add_rules([rules.BodyHardTab], {"hur": "dûr"}) # Assert all rules are there as expected self.assertEqual(len(collection), 3) @@ -54,11 +54,11 @@ class RuleCollectionTests(BaseTestCase): self.assertEqual(collection.find_rule(expected_rule.id), expected_rule) # Delete rules by attr, assert that we still have the right rules in the collection - collection.delete_rules_by_attr("foo", u"bår") + collection.delete_rules_by_attr("foo", "bår") self.assertEqual(len(collection), 1) self.assertIsNone(collection.find_rule(rules.TitleMaxLength.id), None) self.assertIsNone(collection.find_rule(rules.TitleTrailingWhitespace.id), None) found = collection.find_rule(rules.BodyHardTab.id) self.assertEqual(found, rules.BodyHardTab()) - self.assertEqual(found.hur, u"dûr") + self.assertEqual(found.hur, "dûr") diff --git a/gitlint/tests/contrib/rules/test_conventional_commit.py b/gitlint/tests/contrib/rules/test_conventional_commit.py index 001af32..fb492df 100644 --- a/gitlint/tests/contrib/rules/test_conventional_commit.py +++ b/gitlint/tests/contrib/rules/test_conventional_commit.py @@ -20,28 +20,28 @@ class ContribConventionalCommitTests(BaseTestCase): # No violations when using a correct type and format for type in ["fix", "feat", "chore", "docs", "style", "refactor", "perf", "test", "revert", "ci", "build"]: - violations = rule.validate(type + u": föo", None) + violations = rule.validate(type + ": föo", None) self.assertListEqual([], violations) # assert violation on wrong type expected_violation = RuleViolation("CT1", "Title does not start with one of fix, feat, chore, docs," - " style, refactor, perf, test, revert, ci, build", u"bår: foo") - violations = rule.validate(u"bår: foo", None) + " style, refactor, perf, test, revert, ci, build", "bår: foo") + violations = rule.validate("bår: foo", None) self.assertListEqual([expected_violation], violations) # assert violation on wrong format expected_violation = RuleViolation("CT1", "Title does not follow ConventionalCommits.org format " - "'type(optional-scope): description'", u"fix föo") - violations = rule.validate(u"fix föo", None) + "'type(optional-scope): description'", "fix föo") + violations = rule.validate("fix föo", None) self.assertListEqual([expected_violation], violations) # assert no violation when adding new type - rule = ConventionalCommit({'types': [u"föo", u"bär"]}) - for typ in [u"föo", u"bär"]: - violations = rule.validate(typ + u": hür dur", None) + rule = ConventionalCommit({'types': ["föo", "bär"]}) + for typ in ["föo", "bär"]: + violations = rule.validate(typ + ": hür dur", None) self.assertListEqual([], violations) # assert violation when using incorrect type when types have been reconfigured - violations = rule.validate(u"fix: hür dur", None) - expected_violation = RuleViolation("CT1", u"Title does not start with one of föo, bär", u"fix: hür dur") + violations = rule.validate("fix: hür dur", None) + expected_violation = RuleViolation("CT1", "Title does not start with one of föo, bär", "fix: hür dur") self.assertListEqual([expected_violation], violations) diff --git a/gitlint/tests/contrib/rules/test_signedoff_by.py b/gitlint/tests/contrib/rules/test_signedoff_by.py index 934aec5..c92f1a6 100644 --- a/gitlint/tests/contrib/rules/test_signedoff_by.py +++ b/gitlint/tests/contrib/rules/test_signedoff_by.py @@ -19,14 +19,14 @@ class ContribSignedOffByTests(BaseTestCase): def test_signedoff_by(self): # No violations when 'Signed-Off-By' line is present rule = SignedOffBy() - violations = rule.validate(self.gitcommit(u"Föobar\n\nMy Body\nSigned-Off-By: John Smith")) + violations = rule.validate(self.gitcommit("Föobar\n\nMy Body\nSigned-Off-By: John Smith")) self.assertListEqual([], violations) # Assert violation when no 'Signed-Off-By' line is present - violations = rule.validate(self.gitcommit(u"Föobar\n\nMy Body")) + violations = rule.validate(self.gitcommit("Föobar\n\nMy Body")) expected_violation = RuleViolation("CC1", "Body does not contain a 'Signed-Off-By' line", line_nr=1) self.assertListEqual(violations, [expected_violation]) # Assert violation when no 'Signed-Off-By' in title but not in body - violations = rule.validate(self.gitcommit(u"Signed-Off-By\n\nFöobar")) + violations = rule.validate(self.gitcommit("Signed-Off-By\n\nFöobar")) self.assertListEqual(violations, [expected_violation]) diff --git a/gitlint/tests/contrib/test_contrib_rules.py b/gitlint/tests/contrib/test_contrib_rules.py index 84db2d5..8ab6539 100644 --- a/gitlint/tests/contrib/test_contrib_rules.py +++ b/gitlint/tests/contrib/test_contrib_rules.py @@ -6,8 +6,6 @@ from gitlint.contrib import rules as contrib_rules from gitlint.tests.contrib import rules as contrib_tests from gitlint import rule_finder, rules -from gitlint.utils import ustr - class ContribRuleTests(BaseTestCase): @@ -24,10 +22,9 @@ class ContribRuleTests(BaseTestCase): # Find all python files in the contrib dir and assert there's a corresponding test file for filename in os.listdir(self.CONTRIB_DIR): if filename.endswith(".py") and filename not in ["__init__.py"]: - expected_test_file = ustr(u"test_" + filename) - error_msg = u"Every Contrib Rule must have associated tests. " + \ - "Expected test file {0} not found.".format(os.path.join(contrib_tests_dir, - expected_test_file)) + expected_test_file = "test_" + filename + error_msg = "Every Contrib Rule must have associated tests. " + \ + f"Expected test file {os.path.join(contrib_tests_dir, expected_test_file)} not found." self.assertIn(expected_test_file, contrib_test_files, error_msg) def test_contrib_rule_naming_conventions(self): diff --git a/gitlint/tests/expected/cli/test_cli_hooks/test_run_hook_negative_1 b/gitlint/tests/expected/cli/test_cli_hooks/test_run_hook_negative_1 new file mode 100644 index 0000000..9082830 --- /dev/null +++ b/gitlint/tests/expected/cli/test_cli_hooks/test_run_hook_negative_1 @@ -0,0 +1,2 @@ +gitlint: checking commit message... +{git_repo} is not a git repository. diff --git a/gitlint/tests/expected/cli/test_cli_hooks/test_run_hook_negative_2 b/gitlint/tests/expected/cli/test_cli_hooks/test_run_hook_negative_2 new file mode 100644 index 0000000..bafbf29 --- /dev/null +++ b/gitlint/tests/expected/cli/test_cli_hooks/test_run_hook_negative_2 @@ -0,0 +1,2 @@ +gitlint: checking commit message... +Error: The 'staged' option (--staged) can only be used when using '--msg-filename' or when piping data to gitlint via stdin. 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) diff --git a/gitlint/tests/rules/test_body_rules.py b/gitlint/tests/rules/test_body_rules.py index f46760b..96ae998 100644 --- a/gitlint/tests/rules/test_body_rules.py +++ b/gitlint/tests/rules/test_body_rules.py @@ -8,65 +8,65 @@ class BodyRuleTests(BaseTestCase): rule = rules.BodyMaxLineLength() # assert no error - violation = rule.validate(u"å" * 80, None) + violation = rule.validate("å" * 80, None) self.assertIsNone(violation) # assert error on line length > 80 - expected_violation = rules.RuleViolation("B1", "Line exceeds max length (81>80)", u"å" * 81) - violations = rule.validate(u"å" * 81, None) + expected_violation = rules.RuleViolation("B1", "Line exceeds max length (81>80)", "å" * 81) + violations = rule.validate("å" * 81, None) self.assertListEqual(violations, [expected_violation]) # set line length to 120, and check no violation on length 73 rule = rules.BodyMaxLineLength({'line-length': 120}) - violations = rule.validate(u"å" * 73, None) + violations = rule.validate("å" * 73, None) self.assertIsNone(violations) # assert raise on 121 - expected_violation = rules.RuleViolation("B1", "Line exceeds max length (121>120)", u"å" * 121) - violations = rule.validate(u"å" * 121, None) + expected_violation = rules.RuleViolation("B1", "Line exceeds max length (121>120)", "å" * 121) + violations = rule.validate("å" * 121, None) self.assertListEqual(violations, [expected_violation]) def test_trailing_whitespace(self): rule = rules.BodyTrailingWhitespace() # assert no error - violations = rule.validate(u"å", None) + violations = rule.validate("å", None) self.assertIsNone(violations) # trailing space - expected_violation = rules.RuleViolation("B2", "Line has trailing whitespace", u"å ") - violations = rule.validate(u"å ", None) + expected_violation = rules.RuleViolation("B2", "Line has trailing whitespace", "å ") + violations = rule.validate("å ", None) self.assertListEqual(violations, [expected_violation]) # trailing tab - expected_violation = rules.RuleViolation("B2", "Line has trailing whitespace", u"å\t") - violations = rule.validate(u"å\t", None) + expected_violation = rules.RuleViolation("B2", "Line has trailing whitespace", "å\t") + violations = rule.validate("å\t", None) self.assertListEqual(violations, [expected_violation]) def test_hard_tabs(self): rule = rules.BodyHardTab() # assert no error - violations = rule.validate(u"This is ã test", None) + violations = rule.validate("This is ã test", None) self.assertIsNone(violations) # contains hard tab - expected_violation = rules.RuleViolation("B3", "Line contains hard tab characters (\\t)", u"This is å\ttest") - violations = rule.validate(u"This is å\ttest", None) + expected_violation = rules.RuleViolation("B3", "Line contains hard tab characters (\\t)", "This is å\ttest") + violations = rule.validate("This is å\ttest", None) self.assertListEqual(violations, [expected_violation]) def test_body_first_line_empty(self): rule = rules.BodyFirstLineEmpty() # assert no error - commit = self.gitcommit(u"Tïtle\n\nThis is the secōnd body line") + commit = self.gitcommit("Tïtle\n\nThis is the secōnd body line") violations = rule.validate(commit) self.assertIsNone(violations) # second line not empty - expected_violation = rules.RuleViolation("B4", "Second line is not empty", u"nöt empty", 2) + expected_violation = rules.RuleViolation("B4", "Second line is not empty", "nöt empty", 2) - commit = self.gitcommit(u"Tïtle\nnöt empty\nThis is the secönd body line") + commit = self.gitcommit("Tïtle\nnöt empty\nThis is the secönd body line") violations = rule.validate(commit) self.assertListEqual(violations, [expected_violation]) @@ -80,34 +80,34 @@ class BodyRuleTests(BaseTestCase): self.assertIsNone(violations) # assert no error - no body - commit = self.gitcommit(u"Tïtle\n") + commit = self.gitcommit("Tïtle\n") violations = rule.validate(commit) self.assertIsNone(violations) # body is too short - expected_violation = rules.RuleViolation("B5", "Body message is too short (8<20)", u"töoshort", 3) + expected_violation = rules.RuleViolation("B5", "Body message is too short (8<20)", "töoshort", 3) - commit = self.gitcommit(u"Tïtle\n\ntöoshort\n") + commit = self.gitcommit("Tïtle\n\ntöoshort\n") violations = rule.validate(commit) self.assertListEqual(violations, [expected_violation]) # assert error - short across multiple lines - expected_violation = rules.RuleViolation("B5", "Body message is too short (11<20)", u"secöndthïrd", 3) - commit = self.gitcommit(u"Tïtle\n\nsecönd\nthïrd\n") + expected_violation = rules.RuleViolation("B5", "Body message is too short (11<20)", "secöndthïrd", 3) + commit = self.gitcommit("Tïtle\n\nsecönd\nthïrd\n") violations = rule.validate(commit) self.assertListEqual(violations, [expected_violation]) # set line length to 120, and check violation on length 21 - expected_violation = rules.RuleViolation("B5", "Body message is too short (21<120)", u"å" * 21, 3) + expected_violation = rules.RuleViolation("B5", "Body message is too short (21<120)", "å" * 21, 3) rule = rules.BodyMinLength({'min-length': 120}) - commit = self.gitcommit(u"Title\n\n%s\n" % (u"å" * 21)) + commit = self.gitcommit("Title\n\n%s\n" % ("å" * 21)) violations = rule.validate(commit) self.assertListEqual(violations, [expected_violation]) # Make sure we don't get the error if the body-length is exactly the min-length rule = rules.BodyMinLength({'min-length': 8}) - commit = self.gitcommit(u"Tïtle\n\n%s\n" % (u"å" * 8)) + commit = self.gitcommit("Tïtle\n\n%s\n" % ("å" * 8)) violations = rule.validate(commit) self.assertIsNone(violations) @@ -115,14 +115,14 @@ class BodyRuleTests(BaseTestCase): rule = rules.BodyMissing() # assert no error - body is present - commit = self.gitcommit(u"Tïtle\n\nThis ïs the first body line\n") + commit = self.gitcommit("Tïtle\n\nThis ïs the first body line\n") violations = rule.validate(commit) self.assertIsNone(violations) # body is too short expected_violation = rules.RuleViolation("B6", "Body message is missing", None, 3) - commit = self.gitcommit(u"Tïtle\n") + commit = self.gitcommit("Tïtle\n") violations = rule.validate(commit) self.assertListEqual(violations, [expected_violation]) @@ -130,7 +130,7 @@ class BodyRuleTests(BaseTestCase): rule = rules.BodyMissing() # assert no error - merge commit - commit = self.gitcommit(u"Merge: Tïtle\n") + commit = self.gitcommit("Merge: Tïtle\n") violations = rule.validate(commit) self.assertIsNone(violations) @@ -144,37 +144,37 @@ class BodyRuleTests(BaseTestCase): rule = rules.BodyChangedFileMention() # assert no error when no files have changed and no files need to be mentioned - commit = self.gitcommit(u"This is a test\n\nHere is a mention of föo/test.py") + commit = self.gitcommit("This is a test\n\nHere is a mention of föo/test.py") violations = rule.validate(commit) self.assertIsNone(violations) # assert no error when no files have changed but certain files need to be mentioned on change - rule = rules.BodyChangedFileMention({'files': u"bar.txt,föo/test.py"}) - commit = self.gitcommit(u"This is a test\n\nHere is a mention of föo/test.py") + rule = rules.BodyChangedFileMention({'files': "bar.txt,föo/test.py"}) + commit = self.gitcommit("This is a test\n\nHere is a mention of föo/test.py") violations = rule.validate(commit) self.assertIsNone(violations) # assert no error if a file has changed and is mentioned - commit = self.gitcommit(u"This is a test\n\nHere is a mention of föo/test.py", [u"föo/test.py"]) + commit = self.gitcommit("This is a test\n\nHere is a mention of föo/test.py", ["föo/test.py"]) violations = rule.validate(commit) self.assertIsNone(violations) # assert no error if multiple files have changed and are mentioned - commit_msg = u"This is a test\n\nHere is a mention of föo/test.py\nAnd here is a mention of bar.txt" - commit = self.gitcommit(commit_msg, [u"föo/test.py", "bar.txt"]) + commit_msg = "This is a test\n\nHere is a mention of föo/test.py\nAnd here is a mention of bar.txt" + commit = self.gitcommit(commit_msg, ["föo/test.py", "bar.txt"]) violations = rule.validate(commit) self.assertIsNone(violations) # assert error if file has changed and is not mentioned - commit_msg = u"This is a test\n\nHere is å mention of\nAnd here is a mention of bar.txt" - commit = self.gitcommit(commit_msg, [u"föo/test.py", "bar.txt"]) + commit_msg = "This is a test\n\nHere is å mention of\nAnd here is a mention of bar.txt" + commit = self.gitcommit(commit_msg, ["föo/test.py", "bar.txt"]) violations = rule.validate(commit) - expected_violation = rules.RuleViolation("B7", u"Body does not mention changed file 'föo/test.py'", None, 4) + expected_violation = rules.RuleViolation("B7", "Body does not mention changed file 'föo/test.py'", None, 4) self.assertEqual([expected_violation], violations) # assert multiple errors if multiple files habe changed and are not mentioned - commit_msg = u"This is å test\n\nHere is a mention of\nAnd here is a mention of" - commit = self.gitcommit(commit_msg, [u"föo/test.py", "bar.txt"]) + commit_msg = "This is å test\n\nHere is a mention of\nAnd here is a mention of" + commit = self.gitcommit(commit_msg, ["föo/test.py", "bar.txt"]) violations = rule.validate(commit) expected_violation_2 = rules.RuleViolation("B7", "Body does not mention changed file 'bar.txt'", None, 4) self.assertEqual([expected_violation_2, expected_violation], violations) @@ -182,7 +182,7 @@ class BodyRuleTests(BaseTestCase): def test_body_match_regex(self): # We intentionally add 2 newlines at the end of our commit message as that's how git will pass the # message. This way we also test that the rule strips off the last line. - commit = self.gitcommit(u"US1234: åbc\nIgnored\nBödy\nFöo\nMy-Commit-Tag: föo\n\n") + commit = self.gitcommit("US1234: åbc\nIgnored\nBödy\nFöo\nMy-Commit-Tag: föo\n\n") # assert no violation on default regex (=everything allowed) rule = rules.BodyRegexMatches() @@ -191,25 +191,25 @@ class BodyRuleTests(BaseTestCase): # assert no violation on matching regex # (also note that first body line - in between title and rest of body - is ignored) - rule = rules.BodyRegexMatches({'regex': u"^Bödy(.*)"}) + rule = rules.BodyRegexMatches({'regex': "^Bödy(.*)"}) violations = rule.validate(commit) self.assertIsNone(violations) # assert we can do end matching (and last empty line is ignored) # (also note that first body line - in between title and rest of body - is ignored) - rule = rules.BodyRegexMatches({'regex': u"My-Commit-Tag: föo$"}) + rule = rules.BodyRegexMatches({'regex': "My-Commit-Tag: föo$"}) violations = rule.validate(commit) self.assertIsNone(violations) # common use-case: matching that a given line is present - rule = rules.BodyRegexMatches({'regex': u"(.*)Föo(.*)"}) + rule = rules.BodyRegexMatches({'regex': "(.*)Föo(.*)"}) violations = rule.validate(commit) self.assertIsNone(violations) # assert violation on non-matching body - rule = rules.BodyRegexMatches({'regex': u"^Tëst(.*)Foo"}) + rule = rules.BodyRegexMatches({'regex': "^Tëst(.*)Foo"}) violations = rule.validate(commit) - expected_violation = rules.RuleViolation("B8", u"Body does not match regex (^Tëst(.*)Foo)", None, 6) + expected_violation = rules.RuleViolation("B8", "Body does not match regex (^Tëst(.*)Foo)", None, 6) self.assertListEqual(violations, [expected_violation]) # assert no violation on None regex @@ -218,7 +218,7 @@ class BodyRuleTests(BaseTestCase): self.assertIsNone(violations) # Assert no issues when there's no body or a weird body variation - bodies = [u"åbc", u"åbc\n", u"åbc\nföo\n", u"åbc\n\n", u"åbc\nföo\nblå", u"åbc\nföo\nblå\n"] + bodies = ["åbc", "åbc\n", "åbc\nföo\n", "åbc\n\n", "åbc\nföo\nblå", "åbc\nföo\nblå\n"] for body in bodies: commit = self.gitcommit(body) rule = rules.BodyRegexMatches({'regex': ".*"}) diff --git a/gitlint/tests/rules/test_configuration_rules.py b/gitlint/tests/rules/test_configuration_rules.py index 121cb3a..479d9c2 100644 --- a/gitlint/tests/rules/test_configuration_rules.py +++ b/gitlint/tests/rules/test_configuration_rules.py @@ -6,7 +6,7 @@ from gitlint.config import LintConfig class ConfigurationRuleTests(BaseTestCase): def test_ignore_by_title(self): - commit = self.gitcommit(u"Releäse\n\nThis is the secōnd body line") + commit = self.gitcommit("Releäse\n\nThis is the secōnd body line") # No regex specified -> Config shouldn't be changed rule = rules.IgnoreByTitle() @@ -16,29 +16,29 @@ class ConfigurationRuleTests(BaseTestCase): self.assert_logged([]) # nothing logged -> nothing ignored # Matching regex -> expect config to ignore all rules - rule = rules.IgnoreByTitle({"regex": u"^Releäse(.*)"}) + rule = rules.IgnoreByTitle({"regex": "^Releäse(.*)"}) expected_config = LintConfig() expected_config.ignore = "all" rule.apply(config, commit) self.assertEqual(config, expected_config) - expected_log_message = u"DEBUG: gitlint.rules Ignoring commit because of rule 'I1': " + \ - u"Commit title 'Releäse' matches the regex '^Releäse(.*)', ignoring rules: all" + expected_log_message = "DEBUG: gitlint.rules Ignoring commit because of rule 'I1': " + \ + "Commit title 'Releäse' matches the regex '^Releäse(.*)', ignoring rules: all" self.assert_log_contains(expected_log_message) # Matching regex with specific ignore - rule = rules.IgnoreByTitle({"regex": u"^Releäse(.*)", + rule = rules.IgnoreByTitle({"regex": "^Releäse(.*)", "ignore": "T1,B2"}) expected_config = LintConfig() expected_config.ignore = "T1,B2" rule.apply(config, commit) self.assertEqual(config, expected_config) - expected_log_message = u"DEBUG: gitlint.rules Ignoring commit because of rule 'I1': " + \ - u"Commit title 'Releäse' matches the regex '^Releäse(.*)', ignoring rules: T1,B2" + expected_log_message = "DEBUG: gitlint.rules Ignoring commit because of rule 'I1': " + \ + "Commit title 'Releäse' matches the regex '^Releäse(.*)', ignoring rules: T1,B2" def test_ignore_by_body(self): - commit = self.gitcommit(u"Tïtle\n\nThis is\n a relëase body\n line") + commit = self.gitcommit("Tïtle\n\nThis is\n a relëase body\n line") # No regex specified -> Config shouldn't be changed rule = rules.IgnoreByBody() @@ -48,32 +48,32 @@ class ConfigurationRuleTests(BaseTestCase): self.assert_logged([]) # nothing logged -> nothing ignored # Matching regex -> expect config to ignore all rules - rule = rules.IgnoreByBody({"regex": u"(.*)relëase(.*)"}) + rule = rules.IgnoreByBody({"regex": "(.*)relëase(.*)"}) expected_config = LintConfig() expected_config.ignore = "all" rule.apply(config, commit) self.assertEqual(config, expected_config) - expected_log_message = u"DEBUG: gitlint.rules Ignoring commit because of rule 'I2': " + \ - u"Commit message line ' a relëase body' matches the regex '(.*)relëase(.*)'," + \ - u" ignoring rules: all" + expected_log_message = "DEBUG: gitlint.rules Ignoring commit because of rule 'I2': " + \ + "Commit message line ' a relëase body' matches the regex '(.*)relëase(.*)'," + \ + " ignoring rules: all" self.assert_log_contains(expected_log_message) # Matching regex with specific ignore - rule = rules.IgnoreByBody({"regex": u"(.*)relëase(.*)", + rule = rules.IgnoreByBody({"regex": "(.*)relëase(.*)", "ignore": "T1,B2"}) expected_config = LintConfig() expected_config.ignore = "T1,B2" rule.apply(config, commit) self.assertEqual(config, expected_config) - expected_log_message = u"DEBUG: gitlint.rules Ignoring commit because of rule 'I2': " + \ - u"Commit message line ' a relëase body' matches the regex '(.*)relëase(.*)', ignoring rules: T1,B2" + expected_log_message = "DEBUG: gitlint.rules Ignoring commit because of rule 'I2': " + \ + "Commit message line ' a relëase body' matches the regex '(.*)relëase(.*)', ignoring rules: T1,B2" self.assert_log_contains(expected_log_message) def test_ignore_body_lines(self): - commit1 = self.gitcommit(u"Tïtle\n\nThis is\n a relëase body\n line") - commit2 = self.gitcommit(u"Tïtle\n\nThis is\n a relëase body\n line") + commit1 = self.gitcommit("Tïtle\n\nThis is\n a relëase body\n line") + commit2 = self.gitcommit("Tïtle\n\nThis is\n a relëase body\n line") # no regex specified, nothing should have happened: # commit and config should remain identical, log should be empty @@ -85,22 +85,22 @@ class ConfigurationRuleTests(BaseTestCase): self.assert_logged([]) # Matching regex - rule = rules.IgnoreBodyLines({"regex": u"(.*)relëase(.*)"}) + rule = rules.IgnoreBodyLines({"regex": "(.*)relëase(.*)"}) config = LintConfig() rule.apply(config, commit1) # Our modified commit should be identical to a commit that doesn't contain the specific line - expected_commit = self.gitcommit(u"Tïtle\n\nThis is\n line") + expected_commit = self.gitcommit("Tïtle\n\nThis is\n line") # The original message isn't touched by this rule, this way we always have a way to reference back to it, # so assert it's not modified by setting it to the same as commit1 expected_commit.message.original = commit1.message.original self.assertEqual(commit1, expected_commit) self.assertEqual(config, LintConfig()) # config shouldn't have been modified - self.assert_log_contains(u"DEBUG: gitlint.rules Ignoring line ' a relëase body' because it " + - u"matches '(.*)relëase(.*)'") + self.assert_log_contains("DEBUG: gitlint.rules Ignoring line ' a relëase body' because it " + + "matches '(.*)relëase(.*)'") # Non-Matching regex: no changes expected - commit1 = self.gitcommit(u"Tïtle\n\nThis is\n a relëase body\n line") - rule = rules.IgnoreBodyLines({"regex": u"(.*)föobar(.*)"}) + commit1 = self.gitcommit("Tïtle\n\nThis is\n a relëase body\n line") + rule = rules.IgnoreBodyLines({"regex": "(.*)föobar(.*)"}) config = LintConfig() rule.apply(config, commit1) self.assertEqual(commit1, commit2) diff --git a/gitlint/tests/rules/test_meta_rules.py b/gitlint/tests/rules/test_meta_rules.py index 987aa88..568ca3f 100644 --- a/gitlint/tests/rules/test_meta_rules.py +++ b/gitlint/tests/rules/test_meta_rules.py @@ -8,25 +8,25 @@ class MetaRuleTests(BaseTestCase): rule = AuthorValidEmail() # valid email addresses - valid_email_addresses = [u"föo@bar.com", u"Jöhn.Doe@bar.com", u"jöhn+doe@bar.com", u"jöhn/doe@bar.com", - u"jöhn.doe@subdomain.bar.com"] + valid_email_addresses = ["föo@bar.com", "Jöhn.Doe@bar.com", "jöhn+doe@bar.com", "jöhn/doe@bar.com", + "jöhn.doe@subdomain.bar.com"] for email in valid_email_addresses: - commit = self.gitcommit(u"", author_email=email) + commit = self.gitcommit("", 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 = self.gitcommit(u"") + commit = self.gitcommit("") 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 Doe@foo.com", u" JöhnDoe@foo.com", - u"JöhnDoe@ foo.com", u"JöhnDoe@foo. com", u"JöhnDoe@foo. com", u"@bår.com", - u"föo@.com"] + invalid_email_addresses = ["föo@bar", "JöhnDoe", "Jöhn Doe", "Jöhn Doe@foo.com", " JöhnDoe@foo.com", + "JöhnDoe@ foo.com", "JöhnDoe@foo. com", "JöhnDoe@foo. com", "@bår.com", + "föo@.com"] for email in invalid_email_addresses: - commit = self.gitcommit(u"", author_email=email) + commit = self.gitcommit("", author_email=email) violations = rule.validate(commit) self.assertListEqual(violations, [RuleViolation("M1", "Author email for commit is invalid", email)]) @@ -35,25 +35,25 @@ class MetaRuleTests(BaseTestCase): # regex=None -> the rule isn't applied rule = AuthorValidEmail() rule.options['regex'].set(None) - emailadresses = [u"föo", None, u"hür dür"] + emailadresses = ["föo", None, "hür dür"] for email in emailadresses: - commit = self.gitcommit(u"", author_email=email) + commit = self.gitcommit("", author_email=email) violations = rule.validate(commit) self.assertIsNone(violations) # Custom domain - rule = AuthorValidEmail({'regex': u"[^@]+@bår.com"}) + rule = AuthorValidEmail({'regex': "[^@]+@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"] + "föo@bår.com", "Jöhn.Doe@bår.com", "jöhn+doe@bår.com", "jöhn/doe@bår.com"] for email in valid_email_addresses: - commit = self.gitcommit(u"", author_email=email) + commit = self.gitcommit("", author_email=email) violations = rule.validate(commit) self.assertIsNone(violations) # Invalid email addresses - invalid_email_addresses = [u"föo@hur.com"] + invalid_email_addresses = ["föo@hur.com"] for email in invalid_email_addresses: - commit = self.gitcommit(u"", author_email=email) + commit = self.gitcommit("", author_email=email) violations = rule.validate(commit) self.assertListEqual(violations, [RuleViolation("M1", "Author email for commit is invalid", email)]) diff --git a/gitlint/tests/rules/test_rules.py b/gitlint/tests/rules/test_rules.py index 58ee1c3..6fcf9bc 100644 --- a/gitlint/tests/rules/test_rules.py +++ b/gitlint/tests/rules/test_rules.py @@ -10,14 +10,14 @@ class RuleTests(BaseTestCase): # Ensure rules are not equal if they differ on their attributes for attr in ["id", "name", "target", "options"]: rule = Rule() - setattr(rule, attr, u"åbc") + setattr(rule, attr, "åbc") self.assertNotEqual(Rule(), rule) def test_rule_log(self): rule = Rule() - rule.log.debug(u"Tēst message") - self.assert_log_contains(u"DEBUG: gitlint.rules Tēst message") + rule.log.debug("Tēst message") + self.assert_log_contains("DEBUG: gitlint.rules Tēst message") def test_rule_violation_equality(self): - violation1 = RuleViolation(u"ïd1", u"My messåge", u"My cöntent", 1) + violation1 = RuleViolation("ïd1", "My messåge", "My cöntent", 1) self.object_equality_test(violation1, ["rule_id", "message", "content", "line_nr"]) diff --git a/gitlint/tests/rules/test_title_rules.py b/gitlint/tests/rules/test_title_rules.py index 049735e..e1be857 100644 --- a/gitlint/tests/rules/test_title_rules.py +++ b/gitlint/tests/rules/test_title_rules.py @@ -9,66 +9,66 @@ class TitleRuleTests(BaseTestCase): rule = TitleMaxLength() # assert no error - violation = rule.validate(u"å" * 72, None) + violation = rule.validate("å" * 72, None) self.assertIsNone(violation) # assert error on line length > 72 - expected_violation = RuleViolation("T1", "Title exceeds max length (73>72)", u"å" * 73) - violations = rule.validate(u"å" * 73, None) + expected_violation = RuleViolation("T1", "Title exceeds max length (73>72)", "å" * 73) + violations = rule.validate("å" * 73, None) self.assertListEqual(violations, [expected_violation]) # set line length to 120, and check no violation on length 73 rule = TitleMaxLength({'line-length': 120}) - violations = rule.validate(u"å" * 73, None) + violations = rule.validate("å" * 73, None) self.assertIsNone(violations) # assert raise on 121 - expected_violation = RuleViolation("T1", "Title exceeds max length (121>120)", u"å" * 121) - violations = rule.validate(u"å" * 121, None) + expected_violation = RuleViolation("T1", "Title exceeds max length (121>120)", "å" * 121) + violations = rule.validate("å" * 121, None) self.assertListEqual(violations, [expected_violation]) def test_trailing_whitespace(self): rule = TitleTrailingWhitespace() # assert no error - violations = rule.validate(u"å", None) + violations = rule.validate("å", None) self.assertIsNone(violations) # trailing space - expected_violation = RuleViolation("T2", "Title has trailing whitespace", u"å ") - violations = rule.validate(u"å ", None) + expected_violation = RuleViolation("T2", "Title has trailing whitespace", "å ") + violations = rule.validate("å ", None) self.assertListEqual(violations, [expected_violation]) # trailing tab - expected_violation = RuleViolation("T2", "Title has trailing whitespace", u"å\t") - violations = rule.validate(u"å\t", None) + expected_violation = RuleViolation("T2", "Title has trailing whitespace", "å\t") + violations = rule.validate("å\t", None) self.assertListEqual(violations, [expected_violation]) def test_hard_tabs(self): rule = TitleHardTab() # assert no error - violations = rule.validate(u"This is å test", None) + violations = rule.validate("This is å test", None) self.assertIsNone(violations) # contains hard tab - expected_violation = RuleViolation("T4", "Title contains hard tab characters (\\t)", u"This is å\ttest") - violations = rule.validate(u"This is å\ttest", None) + expected_violation = RuleViolation("T4", "Title contains hard tab characters (\\t)", "This is å\ttest") + violations = rule.validate("This is å\ttest", None) self.assertListEqual(violations, [expected_violation]) def test_trailing_punctuation(self): rule = TitleTrailingPunctuation() # assert no error - violations = rule.validate(u"This is å test", None) + violations = rule.validate("This is å test", None) self.assertIsNone(violations) # assert errors for different punctuations - punctuation = u"?:!.,;" + punctuation = "?:!.,;" for char in punctuation: - line = u"This is å test" + char # note that make sure to include some unicode! + line = "This is å test" + char # note that make sure to include some unicode! gitcontext = self.gitcontext(line) - expected_violation = RuleViolation("T3", u"Title has trailing punctuation ({0})".format(char), line) + expected_violation = RuleViolation("T3", f"Title has trailing punctuation ({char})", line) violations = rule.validate(line, gitcontext) self.assertListEqual(violations, [expected_violation]) @@ -76,40 +76,40 @@ class TitleRuleTests(BaseTestCase): rule = TitleMustNotContainWord() # no violations - violations = rule.validate(u"This is å test", None) + violations = rule.validate("This is å test", None) self.assertIsNone(violations) # no violation if WIP occurs inside a wor - violations = rule.validate(u"This is å wiping test", None) + violations = rule.validate("This is å wiping test", None) self.assertIsNone(violations) # match literally - violations = rule.validate(u"WIP This is å test", None) + violations = rule.validate("WIP This is å test", None) expected_violation = RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", - u"WIP This is å test") + "WIP This is å test") self.assertListEqual(violations, [expected_violation]) # match case insensitive - violations = rule.validate(u"wip This is å test", None) + violations = rule.validate("wip This is å test", None) expected_violation = RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", - u"wip This is å test") + "wip This is å test") self.assertListEqual(violations, [expected_violation]) # match if there is a colon after the word - violations = rule.validate(u"WIP:This is å test", None) + violations = rule.validate("WIP:This is å test", None) expected_violation = RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", - u"WIP:This is å test") + "WIP:This is å test") self.assertListEqual(violations, [expected_violation]) # match multiple words - rule = TitleMustNotContainWord({'words': u"wip,test,å"}) - violations = rule.validate(u"WIP:This is å test", None) + rule = TitleMustNotContainWord({'words': "wip,test,å"}) + violations = rule.validate("WIP:This is å test", None) expected_violation = RuleViolation("T5", "Title contains the word 'wip' (case-insensitive)", - u"WIP:This is å test") + "WIP:This is å test") expected_violation2 = RuleViolation("T5", "Title contains the word 'test' (case-insensitive)", - u"WIP:This is å test") - expected_violation3 = RuleViolation("T5", u"Title contains the word 'å' (case-insensitive)", - u"WIP:This is å test") + "WIP:This is å test") + expected_violation3 = RuleViolation("T5", "Title contains the word 'å' (case-insensitive)", + "WIP:This is å test") self.assertListEqual(violations, [expected_violation, expected_violation2, expected_violation3]) def test_leading_whitespace(self): @@ -130,12 +130,12 @@ class TitleRuleTests(BaseTestCase): self.assertListEqual(violations, [expected_violation]) # unicode test - expected_violation = RuleViolation("T6", "Title has leading whitespace", u" ☺") - violations = rule.validate(u" ☺", None) + expected_violation = RuleViolation("T6", "Title has leading whitespace", " ☺") + violations = rule.validate(" ☺", None) self.assertListEqual(violations, [expected_violation]) def test_regex_matches(self): - commit = self.gitcommit(u"US1234: åbc\n") + commit = self.gitcommit("US1234: åbc\n") # assert no violation on default regex (=everything allowed) rule = TitleRegexMatches() @@ -143,41 +143,41 @@ class TitleRuleTests(BaseTestCase): self.assertIsNone(violations) # assert no violation on matching regex - rule = TitleRegexMatches({'regex': u"^US[0-9]*: å"}) + rule = TitleRegexMatches({'regex': "^US[0-9]*: å"}) violations = rule.validate(commit.message.title, commit) self.assertIsNone(violations) # assert violation when no matching regex - rule = TitleRegexMatches({'regex': u"^UÅ[0-9]*"}) + rule = TitleRegexMatches({'regex': "^UÅ[0-9]*"}) violations = rule.validate(commit.message.title, commit) - expected_violation = RuleViolation("T7", u"Title does not match regex (^UÅ[0-9]*)", u"US1234: åbc") + expected_violation = RuleViolation("T7", "Title does not match regex (^UÅ[0-9]*)", "US1234: åbc") self.assertListEqual(violations, [expected_violation]) def test_min_line_length(self): rule = TitleMinLength() # assert no error - violation = rule.validate(u"å" * 72, None) + violation = rule.validate("å" * 72, None) self.assertIsNone(violation) # assert error on line length < 5 - expected_violation = RuleViolation("T8", "Title is too short (4<5)", u"å" * 4, 1) - violations = rule.validate(u"å" * 4, None) + expected_violation = RuleViolation("T8", "Title is too short (4<5)", "å" * 4, 1) + violations = rule.validate("å" * 4, None) self.assertListEqual(violations, [expected_violation]) # set line length to 3, and check no violation on length 4 rule = TitleMinLength({'min-length': 3}) - violations = rule.validate(u"å" * 4, None) + violations = rule.validate("å" * 4, None) self.assertIsNone(violations) # assert no violations on length 3 (this asserts we've implemented a *strict* less than) rule = TitleMinLength({'min-length': 3}) - violations = rule.validate(u"å" * 3, None) + violations = rule.validate("å" * 3, None) self.assertIsNone(violations) # assert raise on 2 - expected_violation = RuleViolation("T8", "Title is too short (2<3)", u"å" * 2, 1) - violations = rule.validate(u"å" * 2, None) + expected_violation = RuleViolation("T8", "Title is too short (2<3)", "å" * 2, 1) + violations = rule.validate("å" * 2, None) self.assertListEqual(violations, [expected_violation]) # assert raise on empty title diff --git a/gitlint/tests/rules/test_user_rules.py b/gitlint/tests/rules/test_user_rules.py index 52d0283..510a829 100644 --- a/gitlint/tests/rules/test_user_rules.py +++ b/gitlint/tests/rules/test_user_rules.py @@ -6,7 +6,6 @@ import sys from gitlint.tests.base import BaseTestCase from gitlint.rule_finder import find_rule_classes, assert_valid_rule_class from gitlint.rules import UserRuleError -from gitlint.utils import ustr from gitlint import options, rules @@ -25,7 +24,7 @@ class UserRuleTests(BaseTestCase): # - Other members of the my_commit_rules module are ignored # (such as func_should_be_ignored, global_variable_should_be_ignored) # - Rules are loaded non-recursively (user_rules/import_exception directory is ignored) - self.assertEqual("[<class 'my_commit_rules.MyUserCommitRule'>]", ustr(classes)) + self.assertEqual("[<class 'my_commit_rules.MyUserCommitRule'>]", str(classes)) # Assert that we added the new user_rules directory to the system path and modules self.assertIn(user_rule_path, sys.path) @@ -33,8 +32,8 @@ class UserRuleTests(BaseTestCase): # Do some basic asserts on our user rule self.assertEqual(classes[0].id, "UC1") - self.assertEqual(classes[0].name, u"my-üser-commit-rule") - expected_option = options.IntOption('violation-count', 1, u"Number of violåtions to return") + self.assertEqual(classes[0].name, "my-üser-commit-rule") + expected_option = options.IntOption('violation-count', 1, "Number of violåtions to return") self.assertListEqual(classes[0].options_spec, [expected_option]) self.assertTrue(hasattr(classes[0], "validate")) @@ -42,13 +41,13 @@ class UserRuleTests(BaseTestCase): # expected result rule_class = classes[0]() violations = rule_class.validate("false-commit-object (ignored)") - self.assertListEqual(violations, [rules.RuleViolation("UC1", u"Commit violåtion 1", u"Contënt 1", 1)]) + self.assertListEqual(violations, [rules.RuleViolation("UC1", "Commit violåtion 1", "Contënt 1", 1)]) # Have it return more violations rule_class.options['violation-count'].value = 2 violations = rule_class.validate("false-commit-object (ignored)") - self.assertListEqual(violations, [rules.RuleViolation("UC1", u"Commit violåtion 1", u"Contënt 1", 1), - rules.RuleViolation("UC1", u"Commit violåtion 2", u"Contënt 2", 2)]) + self.assertListEqual(violations, [rules.RuleViolation("UC1", "Commit violåtion 1", "Contënt 1", 1), + rules.RuleViolation("UC1", "Commit violåtion 2", "Contënt 2", 2)]) def test_extra_path_specified_by_file(self): # Test that find_rule_classes can handle an extra path given as a file name instead of a directory @@ -58,7 +57,7 @@ class UserRuleTests(BaseTestCase): rule_class = classes[0]() violations = rule_class.validate("false-commit-object (ignored)") - self.assertListEqual(violations, [rules.RuleViolation("UC1", u"Commit violåtion 1", u"Contënt 1", 1)]) + self.assertListEqual(violations, [rules.RuleViolation("UC1", "Commit violåtion 1", "Contënt 1", 1)]) def test_rules_from_init_file(self): # Test that we can import rules that are defined in __init__.py files @@ -68,8 +67,8 @@ class UserRuleTests(BaseTestCase): classes = find_rule_classes(user_rule_path) # convert classes to strings and sort them so we can compare them - class_strings = sorted([ustr(clazz) for clazz in classes]) - expected = [u"<class 'my_commit_rules.MyUserCommitRule'>", u"<class 'parent_package.InitFileRule'>"] + class_strings = sorted([str(clazz) for clazz in classes]) + expected = ["<class 'my_commit_rules.MyUserCommitRule'>", "<class 'parent_package.InitFileRule'>"] self.assertListEqual(class_strings, expected) def test_empty_user_classes(self): @@ -92,8 +91,8 @@ class UserRuleTests(BaseTestCase): find_rule_classes(user_rule_path) def test_find_rule_classes_nonexisting_path(self): - with self.assertRaisesMessage(UserRuleError, u"Invalid extra-path: föo/bar"): - find_rule_classes(u"föo/bar") + with self.assertRaisesMessage(UserRuleError, "Invalid extra-path: föo/bar"): + find_rule_classes("föo/bar") def test_assert_valid_rule_class(self): class MyLineRuleClass(rules.LineRule): @@ -132,7 +131,7 @@ class UserRuleTests(BaseTestCase): def test_assert_valid_rule_class_negative_parent(self): # rule class must extend from LineRule or CommitRule - class MyRuleClass(object): + class MyRuleClass: pass expected_msg = "User-defined rule class 'MyRuleClass' must extend from gitlint.rules.LineRule, " + \ @@ -160,8 +159,9 @@ class UserRuleTests(BaseTestCase): # Rule ids must not start with one of the reserved id letters for letter in ["T", "R", "B", "M", "I"]: MyRuleClass.id = letter + "1" - expected_msg = "The id '{0}' of 'MyRuleClass' is invalid. Gitlint reserves ids starting with R,T,B,M,I" - with self.assertRaisesMessage(UserRuleError, expected_msg.format(letter)): + expected_msg = f"The id '{letter}' of 'MyRuleClass' is invalid. " + \ + "Gitlint reserves ids starting with R,T,B,M,I" + with self.assertRaisesMessage(UserRuleError, expected_msg): assert_valid_rule_class(MyRuleClass) def test_assert_valid_rule_class_negative_name(self): @@ -186,17 +186,17 @@ class UserRuleTests(BaseTestCase): class MyRuleClass(parent_class): id = "UC1" - name = u"my-rüle-class" + name = "my-rüle-class" # if set, option_spec must be a list of gitlint options - MyRuleClass.options_spec = u"föo" + MyRuleClass.options_spec = "föo" expected_msg = "The options_spec attribute of user-defined rule class 'MyRuleClass' must be a list " + \ "of gitlint.options.RuleOption" with self.assertRaisesMessage(UserRuleError, expected_msg): assert_valid_rule_class(MyRuleClass) # option_spec is a list, but not of gitlint options - MyRuleClass.options_spec = [u"föo", 123] # pylint: disable=bad-option-value,redefined-variable-type + MyRuleClass.options_spec = ["föo", 123] # pylint: disable=bad-option-value,redefined-variable-type with self.assertRaisesMessage(UserRuleError, expected_msg): assert_valid_rule_class(MyRuleClass) @@ -206,14 +206,14 @@ class UserRuleTests(BaseTestCase): for clazz in baseclasses: class MyRuleClass(clazz): id = "UC1" - name = u"my-rüle-class" + name = "my-rüle-class" with self.assertRaisesMessage(UserRuleError, "User-defined rule class 'MyRuleClass' must have a 'validate' method"): assert_valid_rule_class(MyRuleClass) # validate attribute - not a method - MyRuleClass.validate = u"föo" + MyRuleClass.validate = "föo" with self.assertRaisesMessage(UserRuleError, "User-defined rule class 'MyRuleClass' must have a 'validate' method"): assert_valid_rule_class(MyRuleClass) @@ -221,21 +221,21 @@ class UserRuleTests(BaseTestCase): def test_assert_valid_rule_class_negative_apply(self): class MyRuleClass(rules.ConfigurationRule): id = "UCR1" - name = u"my-rüle-class" + name = "my-rüle-class" expected_msg = "User-defined Configuration rule class 'MyRuleClass' must have an 'apply' method" with self.assertRaisesMessage(UserRuleError, expected_msg): assert_valid_rule_class(MyRuleClass) # validate attribute - not a method - MyRuleClass.validate = u"föo" + MyRuleClass.validate = "föo" with self.assertRaisesMessage(UserRuleError, expected_msg): assert_valid_rule_class(MyRuleClass) def test_assert_valid_rule_class_negative_target(self): class MyRuleClass(rules.LineRule): id = "UC1" - name = u"my-rüle-class" + name = "my-rüle-class" def validate(self): pass @@ -247,7 +247,7 @@ class UserRuleTests(BaseTestCase): assert_valid_rule_class(MyRuleClass) # invalid target - MyRuleClass.target = u"föo" + MyRuleClass.target = "föo" with self.assertRaisesMessage(UserRuleError, expected_msg): assert_valid_rule_class(MyRuleClass) diff --git a/gitlint/tests/samples/user_rules/my_commit_rules.py b/gitlint/tests/samples/user_rules/my_commit_rules.py index 5456487..8b0907e 100644 --- a/gitlint/tests/samples/user_rules/my_commit_rules.py +++ b/gitlint/tests/samples/user_rules/my_commit_rules.py @@ -5,14 +5,14 @@ from gitlint.options import IntOption class MyUserCommitRule(CommitRule): - name = u"my-üser-commit-rule" + name = "my-üser-commit-rule" id = "UC1" - options_spec = [IntOption('violation-count', 1, u"Number of violåtions to return")] + options_spec = [IntOption('violation-count', 1, "Number of violåtions to return")] def validate(self, _commit): violations = [] for i in range(1, self.options['violation-count'].value + 1): - violations.append(RuleViolation(self.id, u"Commit violåtion %d" % i, u"Contënt %d" % i, i)) + violations.append(RuleViolation(self.id, "Commit violåtion %d" % i, "Contënt %d" % i, i)) return violations diff --git a/gitlint/tests/samples/user_rules/parent_package/__init__.py b/gitlint/tests/samples/user_rules/parent_package/__init__.py index 32c05fc..9ea5371 100644 --- a/gitlint/tests/samples/user_rules/parent_package/__init__.py +++ b/gitlint/tests/samples/user_rules/parent_package/__init__.py @@ -5,7 +5,7 @@ from gitlint.rules import CommitRule class InitFileRule(CommitRule): - name = u"my-init-cömmit-rule" + name = "my-init-cömmit-rule" id = "UC1" options_spec = [] diff --git a/gitlint/tests/samples/user_rules/parent_package/my_commit_rules.py b/gitlint/tests/samples/user_rules/parent_package/my_commit_rules.py index b73a305..b143e62 100644 --- a/gitlint/tests/samples/user_rules/parent_package/my_commit_rules.py +++ b/gitlint/tests/samples/user_rules/parent_package/my_commit_rules.py @@ -4,7 +4,7 @@ from gitlint.rules import CommitRule class MyUserCommitRule(CommitRule): - name = u"my-user-cömmit-rule" + name = "my-user-cömmit-rule" id = "UC2" options_spec = [] diff --git a/gitlint/tests/test_cache.py b/gitlint/tests/test_cache.py index 5d78953..4b1d47a 100644 --- a/gitlint/tests/test_cache.py +++ b/gitlint/tests/test_cache.py @@ -16,13 +16,13 @@ class CacheTests(BaseTestCase): @cache def foo(self): self.counter += 1 - return u"bår" + return "bår" @property - @cache(cachekey=u"hür") + @cache(cachekey="hür") def bar(self): self.counter += 1 - return u"fōo" + return "fōo" def test_cache(self): # Init new class with cached properties @@ -31,14 +31,14 @@ class CacheTests(BaseTestCase): self.assertDictEqual(myclass._cache, {}) # Assert that function is called on first access, cache is set - self.assertEqual(myclass.foo, u"bår") + self.assertEqual(myclass.foo, "bår") self.assertEqual(myclass.counter, 1) - self.assertDictEqual(myclass._cache, {"foo": u"bår"}) + self.assertDictEqual(myclass._cache, {"foo": "bår"}) # After function is not called on subsequent access, cache is still set - self.assertEqual(myclass.foo, u"bår") + self.assertEqual(myclass.foo, "bår") self.assertEqual(myclass.counter, 1) - self.assertDictEqual(myclass._cache, {"foo": u"bår"}) + self.assertDictEqual(myclass._cache, {"foo": "bår"}) def test_cache_custom_key(self): # Init new class with cached properties @@ -47,11 +47,11 @@ class CacheTests(BaseTestCase): self.assertDictEqual(myclass._cache, {}) # Assert that function is called on first access, cache is set with custom key - self.assertEqual(myclass.bar, u"fōo") + self.assertEqual(myclass.bar, "fōo") self.assertEqual(myclass.counter, 1) - self.assertDictEqual(myclass._cache, {u"hür": u"fōo"}) + self.assertDictEqual(myclass._cache, {"hür": "fōo"}) # After function is not called on subsequent access, cache is still set - self.assertEqual(myclass.bar, u"fōo") + self.assertEqual(myclass.bar, "fōo") self.assertEqual(myclass.counter, 1) - self.assertDictEqual(myclass._cache, {u"hür": u"fōo"}) + self.assertDictEqual(myclass._cache, {"hür": "fōo"}) diff --git a/gitlint/tests/test_display.py b/gitlint/tests/test_display.py index 1c64b34..167ef96 100644 --- a/gitlint/tests/test_display.py +++ b/gitlint/tests/test_display.py @@ -1,19 +1,8 @@ # -*- coding: utf-8 -*- -try: - # python 2.x - from StringIO import StringIO -except ImportError: - # python 3.x - from io import StringIO +from io import StringIO - -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 # pylint: disable=no-name-in-module, import-error from gitlint.display import Display from gitlint.config import LintConfig @@ -28,21 +17,21 @@ class DisplayTests(BaseTestCase): with patch('gitlint.display.stderr', new=StringIO()) as stderr: # Non exact outputting, should output both v and vv output with patch('gitlint.display.stdout', new=StringIO()) as stdout: - display.v(u"tëst") - display.vv(u"tëst2") + display.v("tëst") + display.vv("tëst2") # vvvv should be ignored regardless - display.vvv(u"tëst3.1") - display.vvv(u"tëst3.2", exact=True) - self.assertEqual(u"tëst\ntëst2\n", stdout.getvalue()) + display.vvv("tëst3.1") + display.vvv("tëst3.2", exact=True) + self.assertEqual("tëst\ntëst2\n", stdout.getvalue()) # exact outputting, should only output v with patch('gitlint.display.stdout', new=StringIO()) as stdout: - display.v(u"tëst", exact=True) - display.vv(u"tëst2", exact=True) + display.v("tëst", exact=True) + display.vv("tëst2", exact=True) # vvvv should be ignored regardless - display.vvv(u"tëst3.1") - display.vvv(u"tëst3.2", exact=True) - self.assertEqual(u"tëst2\n", stdout.getvalue()) + display.vvv("tëst3.1") + display.vvv("tëst3.2", exact=True) + self.assertEqual("tëst2\n", stdout.getvalue()) # standard error should be empty throughtout all of this self.assertEqual('', stderr.getvalue()) @@ -54,21 +43,21 @@ class DisplayTests(BaseTestCase): with patch('gitlint.display.stdout', new=StringIO()) as stdout: # Non exact outputting, should output both v and vv output with patch('gitlint.display.stderr', new=StringIO()) as stderr: - display.e(u"tëst") - display.ee(u"tëst2") + display.e("tëst") + display.ee("tëst2") # vvvv should be ignored regardless - display.eee(u"tëst3.1") - display.eee(u"tëst3.2", exact=True) - self.assertEqual(u"tëst\ntëst2\n", stderr.getvalue()) + display.eee("tëst3.1") + display.eee("tëst3.2", exact=True) + self.assertEqual("tëst\ntëst2\n", stderr.getvalue()) # exact outputting, should only output v with patch('gitlint.display.stderr', new=StringIO()) as stderr: - display.e(u"tëst", exact=True) - display.ee(u"tëst2", exact=True) + display.e("tëst", exact=True) + display.ee("tëst2", exact=True) # vvvv should be ignored regardless - display.eee(u"tëst3.1") - display.eee(u"tëst3.2", exact=True) - self.assertEqual(u"tëst2\n", stderr.getvalue()) + display.eee("tëst3.1") + display.eee("tëst3.2", exact=True) + self.assertEqual("tëst2\n", stderr.getvalue()) # standard output should be empty throughtout all of this self.assertEqual('', stdout.getvalue()) diff --git a/gitlint/tests/test_hooks.py b/gitlint/tests/test_hooks.py index 62f55e5..0ce5040 100644 --- a/gitlint/tests/test_hooks.py +++ b/gitlint/tests/test_hooks.py @@ -2,12 +2,7 @@ import os -try: - # python 2.x - from mock import patch, ANY, mock_open -except ImportError: - # python 3.x - from unittest.mock import patch, ANY, mock_open # pylint: disable=no-name-in-module, import-error +from unittest.mock import patch, ANY, mock_open from gitlint.tests.base import BaseTestCase from gitlint.config import LintConfig @@ -19,7 +14,7 @@ class HookTests(BaseTestCase): @patch('gitlint.hooks.git_hooks_dir') def test_commit_msg_hook_path(self, git_hooks_dir): - git_hooks_dir.return_value = os.path.join(u"/föo", u"bar") + git_hooks_dir.return_value = os.path.join("/föo", "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) @@ -37,8 +32,8 @@ class HookTests(BaseTestCase): @patch('gitlint.hooks.git_hooks_dir') 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") + lint_config.target = os.path.join("/hür", "dur") + git_hooks_dir.return_value = os.path.join("/föo", "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) @@ -54,11 +49,11 @@ class HookTests(BaseTestCase): @patch('gitlint.hooks.git_hooks_dir') 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") + lint_config.target = os.path.join("/hür", "dur") + git_hooks_dir.return_value = os.path.join("/föo", "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) + expected_msg = f"{lint_config.target} is not a git repository." with self.assertRaisesMessage(GitHookInstallerError, expected_msg): GitHookInstaller.install_commit_msg_hook(lint_config) isdir.assert_called_with(git_hooks_dir.return_value) @@ -69,7 +64,7 @@ class HookTests(BaseTestCase): 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) + \ + expected_msg = f"There is already a commit-msg hook file present in {expected_dst}.\n" + \ "gitlint currently does not support appending to an existing commit-msg file." with self.assertRaisesMessage(GitHookInstallerError, expected_msg): GitHookInstaller.install_commit_msg_hook(lint_config) @@ -81,8 +76,8 @@ class HookTests(BaseTestCase): @patch('gitlint.hooks.git_hooks_dir') 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") + git_hooks_dir.return_value = os.path.join("/föo", "bar", ".git", "hooks") + lint_config.target = os.path.join("/hür", "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) @@ -99,12 +94,12 @@ class HookTests(BaseTestCase): @patch('gitlint.hooks.git_hooks_dir') def test_uninstall_commit_msg_hook_negative(self, git_hooks_dir, isdir, path_exists, remove): 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") + lint_config.target = os.path.join("/hür", "dur") + git_hooks_dir.return_value = os.path.join("/föo", "bar", ".git", "hooks") # mock that the current directory is not a git repo isdir.return_value = False - expected_msg = u"{0} is not a git repository.".format(lint_config.target) + expected_msg = f"{lint_config.target} is not a git repository." with self.assertRaisesMessage(GitHookInstallerError, expected_msg): GitHookInstaller.uninstall_commit_msg_hook(lint_config) isdir.assert_called_with(git_hooks_dir.return_value) @@ -115,7 +110,7 @@ class HookTests(BaseTestCase): isdir.return_value = True path_exists.return_value = False expected_dst = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH) - expected_msg = u"There is no commit-msg hook present in {0}.".format(expected_dst) + expected_msg = f"There is no commit-msg hook present in {expected_dst}." with self.assertRaisesMessage(GitHookInstallerError, expected_msg): GitHookInstaller.uninstall_commit_msg_hook(lint_config) isdir.assert_called_with(git_hooks_dir.return_value) @@ -127,7 +122,7 @@ class HookTests(BaseTestCase): path_exists.return_value = True read_data = "#!/bin/sh\nfoo" expected_dst = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH) - expected_msg = u"The commit-msg hook in {0} was not installed by gitlint ".format(expected_dst) + \ + expected_msg = f"The commit-msg hook in {expected_dst} was not installed by gitlint " + \ "(or it was modified).\nUninstallation of 3th party or modified gitlint hooks " + \ "is not supported." with patch('gitlint.hooks.io.open', mock_open(read_data=read_data), create=True): diff --git a/gitlint/tests/test_lint.py b/gitlint/tests/test_lint.py index 3bf9a94..b743389 100644 --- a/gitlint/tests/test_lint.py +++ b/gitlint/tests/test_lint.py @@ -1,18 +1,8 @@ # -*- coding: utf-8 -*- -try: - # python 2.x - from StringIO import StringIO -except ImportError: - # python 3.x - from io import StringIO - -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 io import StringIO + +from unittest.mock import patch # pylint: disable=no-name-in-module, import-error from gitlint.tests.base import BaseTestCase from gitlint.lint import GitLinter @@ -27,14 +17,14 @@ class LintTests(BaseTestCase): gitcontext = self.gitcontext(self.get_sample("commit_message/sample1")) violations = linter.lint(gitcontext.commits[-1]) expected_errors = [RuleViolation("T3", "Title has trailing punctuation (.)", - u"Commit title contåining 'WIP', as well as trailing punctuation.", 1), + "Commit title contåining 'WIP', as well as trailing punctuation.", 1), RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", - u"Commit title contåining 'WIP', as well as trailing punctuation.", 1), + "Commit title contåining '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", u"This line has a tråiling space. ", 4), + RuleViolation("B2", "Line has trailing whitespace", "This line has a tråiling 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)] @@ -46,7 +36,7 @@ class LintTests(BaseTestCase): gitcontext = self.gitcontext(self.get_sample("commit_message/sample2")) violations = linter.lint(gitcontext.commits[-1]) expected = [RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", - u"Just a title contåining WIP", 1), + "Just a title contåining WIP", 1), RuleViolation("B6", "Body message is missing", None, 3)] self.assertListEqual(violations, expected) @@ -56,7 +46,7 @@ class LintTests(BaseTestCase): gitcontext = self.gitcontext(self.get_sample("commit_message/sample3")) violations = linter.lint(gitcontext.commits[-1]) - title = u" Commit title containing 'WIP', \tleading and tråiling whitespace and longer than 72 characters." + title = " Commit title containing 'WIP', \tleading and tråiling 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), @@ -64,12 +54,12 @@ class LintTests(BaseTestCase): 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)", - u"This is the first line is meånt to test a line that exceeds the maximum line " + + "This is the first line is meånt 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", u"This line has a tråiling tab.\t", 5), + RuleViolation("B2", "Line has trailing whitespace", "This line has a tråiling tab.\t", 5), RuleViolation("B3", "Line contains hard tab characters (\\t)", - u"This line has a tråiling tab.\t", 5)] + "This line has a tråiling tab.\t", 5)] self.assertListEqual(violations, expected) @@ -90,13 +80,13 @@ class LintTests(BaseTestCase): linter = GitLinter(config_builder.build()) violations = linter.lint(commit) - title = u" Commit title containing 'WIP', \tleading and tråiling whitespace and longer than 72 characters." + title = " Commit title containing 'WIP', \tleading and tråiling whitespace and longer than 72 characters." # expect only certain violations because sample5 has a 'gitlint-ignore: T3, T6, body-max-line-length' 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", u"This line should be ëmpty", 2), - RuleViolation("B2", "Line has trailing whitespace", u"This line has a tråiling space. ", 4), + RuleViolation("B4", "Second line is not empty", "This line should be ëmpty", 2), + RuleViolation("B2", "Line has trailing whitespace", "This line has a tråiling 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)] @@ -106,11 +96,11 @@ class LintTests(BaseTestCase): """ Lint sample2 but also add some metadata to the commit so we that gets linted as well """ linter = GitLinter(LintConfig()) gitcontext = self.gitcontext(self.get_sample("commit_message/sample2")) - gitcontext.commits[0].author_email = u"foo bår" + gitcontext.commits[0].author_email = "foo bår" violations = linter.lint(gitcontext.commits[-1]) - expected = [RuleViolation("M1", "Author email for commit is invalid", u"foo bår", None), + expected = [RuleViolation("M1", "Author email for commit is invalid", "foo bår", None), RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", - u"Just a title contåining WIP", 1), + "Just a title contåining WIP", 1), RuleViolation("B6", "Body message is missing", None, 3)] self.assertListEqual(violations, expected) @@ -123,7 +113,7 @@ class LintTests(BaseTestCase): expected = [RuleViolation("B4", "Second line is not empty", "This line should be empty", 2), RuleViolation("B3", "Line contains hard tab characters (\\t)", - u"This line has a tråiling tab.\t", 5)] + "This line has a tråiling tab.\t", 5)] self.assertListEqual(violations, expected) @@ -146,19 +136,19 @@ class LintTests(BaseTestCase): # Normally we'd expect a B6 violation, but that one is skipped because of the specific ignore set above expected = [RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", - u"Just a title contåining WIP", 1)] + "Just a title contåining WIP", 1)] self.assertListEqual(violations, expected) # Test ignoring body lines lint_config = LintConfig() linter = GitLinter(lint_config) - lint_config.set_rule_option("I3", "regex", u"(.*)tråiling(.*)") + lint_config.set_rule_option("I3", "regex", "(.*)tråiling(.*)") violations = linter.lint(self.gitcommit(self.get_sample("commit_message/sample1"))) expected_errors = [RuleViolation("T3", "Title has trailing punctuation (.)", - u"Commit title contåining 'WIP', as well as trailing punctuation.", 1), + "Commit title contåining 'WIP', as well as trailing punctuation.", 1), RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", - u"Commit title contåining 'WIP', as well as trailing punctuation.", 1), + "Commit title contåining '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 " + @@ -171,7 +161,7 @@ class LintTests(BaseTestCase): def test_lint_special_commit(self): for commit_type in ["merge", "revert", "squash", "fixup"]: - commit = self.gitcommit(self.get_sample("commit_message/{0}".format(commit_type))) + commit = self.gitcommit(self.get_sample(f"commit_message/{commit_type}")) lintconfig = LintConfig() linter = GitLinter(lintconfig) violations = linter.lint(commit) @@ -180,7 +170,7 @@ class LintTests(BaseTestCase): self.assertListEqual(violations, []) # Check that we do see violations if we disable 'ignore-merge-commits' - setattr(lintconfig, "ignore_{0}_commits".format(commit_type), False) + setattr(lintconfig, f"ignore_{commit_type}_commits", False) linter = GitLinter(lintconfig) violations = linter.lint(commit) self.assertTrue(len(violations) > 0) @@ -195,7 +185,7 @@ class LintTests(BaseTestCase): self.assertListEqual(violations, []) # Matching regexes shouldn't be a problem - rule_regexes = [("title-match-regex", u"Tïtle$"), ("body-match-regex", u"Sïgned-Off-By: (.*)$")] + rule_regexes = [("title-match-regex", "Tïtle$"), ("body-match-regex", "Sïgned-Off-By: (.*)$")] for rule_regex in rule_regexes: lintconfig.set_rule_option(rule_regex[0], "regex", rule_regex[1]) violations = linter.lint(commit) @@ -203,16 +193,16 @@ class LintTests(BaseTestCase): # Non-matching regexes should return violations rule_regexes = [("title-match-regex", ), ("body-match-regex",)] - lintconfig.set_rule_option("title-match-regex", "regex", u"^Tïtle") - lintconfig.set_rule_option("body-match-regex", "regex", u"Sügned-Off-By: (.*)$") - expected_violations = [RuleViolation("T7", u"Title does not match regex (^Tïtle)", u"Normal Commit Tïtle", 1), - RuleViolation("B8", u"Body does not match regex (Sügned-Off-By: (.*)$)", None, 6)] + lintconfig.set_rule_option("title-match-regex", "regex", "^Tïtle") + lintconfig.set_rule_option("body-match-regex", "regex", "Sügned-Off-By: (.*)$") + expected_violations = [RuleViolation("T7", "Title does not match regex (^Tïtle)", "Normal Commit Tïtle", 1), + RuleViolation("B8", "Body does not match regex (Sügned-Off-By: (.*)$)", None, 6)] violations = linter.lint(commit) self.assertListEqual(violations, expected_violations) def test_print_violations(self): - violations = [RuleViolation("RULE_ID_1", u"Error Messåge 1", "Violating Content 1", None), - RuleViolation("RULE_ID_2", "Error Message 2", u"Violåting Content 2", 2)] + violations = [RuleViolation("RULE_ID_1", "Error Messåge 1", "Violating Content 1", None), + RuleViolation("RULE_ID_2", "Error Message 2", "Violåting Content 2", 2)] linter = GitLinter(LintConfig()) # test output with increasing verbosity @@ -224,54 +214,54 @@ class LintTests(BaseTestCase): with patch('gitlint.display.stderr', new=StringIO()) as stderr: linter.config.verbosity = 1 linter.print_violations(violations) - expected = u"-: RULE_ID_1\n2: RULE_ID_2\n" + expected = "-: RULE_ID_1\n2: RULE_ID_2\n" self.assertEqual(expected, stderr.getvalue()) with patch('gitlint.display.stderr', new=StringIO()) as stderr: linter.config.verbosity = 2 linter.print_violations(violations) - expected = u"-: RULE_ID_1 Error Messåge 1\n2: RULE_ID_2 Error Message 2\n" + expected = "-: RULE_ID_1 Error Messåge 1\n2: RULE_ID_2 Error Message 2\n" self.assertEqual(expected, stderr.getvalue()) with patch('gitlint.display.stderr', new=StringIO()) as stderr: linter.config.verbosity = 3 linter.print_violations(violations) - expected = u"-: RULE_ID_1 Error Messåge 1: \"Violating Content 1\"\n" + \ - u"2: RULE_ID_2 Error Message 2: \"Violåting Content 2\"\n" + expected = "-: RULE_ID_1 Error Messåge 1: \"Violating Content 1\"\n" + \ + "2: RULE_ID_2 Error Message 2: \"Violåting Content 2\"\n" self.assertEqual(expected, stderr.getvalue()) def test_named_rules(self): """ Test that when named rules are present, both them and the original (non-named) rules executed """ lint_config = LintConfig() - for rule_name in [u"my-ïd", u"another-rule-ïd"]: + for rule_name in ["my-ïd", "another-rule-ïd"]: rule_id = TitleMustNotContainWord.id + ":" + rule_name lint_config.rules.add_rule(TitleMustNotContainWord, rule_id) - lint_config.set_rule_option(rule_id, "words", [u"Föo"]) + lint_config.set_rule_option(rule_id, "words", ["Föo"]) linter = GitLinter(lint_config) - violations = [RuleViolation("T5", u"Title contains the word 'WIP' (case-insensitive)", u"WIP: Föo bar", 1), - RuleViolation(u"T5:another-rule-ïd", u"Title contains the word 'Föo' (case-insensitive)", - u"WIP: Föo bar", 1), - RuleViolation(u"T5:my-ïd", u"Title contains the word 'Föo' (case-insensitive)", - u"WIP: Föo bar", 1)] - self.assertListEqual(violations, linter.lint(self.gitcommit(u"WIP: Föo bar\n\nFoo bår hur dur bla bla"))) + violations = [RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", "WIP: Föo bar", 1), + RuleViolation("T5:another-rule-ïd", "Title contains the word 'Föo' (case-insensitive)", + "WIP: Föo bar", 1), + RuleViolation("T5:my-ïd", "Title contains the word 'Föo' (case-insensitive)", + "WIP: Föo bar", 1)] + self.assertListEqual(violations, linter.lint(self.gitcommit("WIP: Föo bar\n\nFoo bår hur dur bla bla"))) def test_ignore_named_rules(self): """ Test that named rules can be ignored """ # Add named rule to lint config config_builder = LintConfigBuilder() - rule_id = TitleMustNotContainWord.id + u":my-ïd" - config_builder.set_option(rule_id, "words", [u"Föo"]) + rule_id = TitleMustNotContainWord.id + ":my-ïd" + config_builder.set_option(rule_id, "words", ["Föo"]) lint_config = config_builder.build() linter = GitLinter(lint_config) - commit = self.gitcommit(u"WIP: Föo bar\n\nFoo bår hur dur bla bla") + commit = self.gitcommit("WIP: Föo bar\n\nFoo bår hur dur bla bla") # By default, we expect both the violations of the regular rule as well as the named rule to show up - violations = [RuleViolation("T5", u"Title contains the word 'WIP' (case-insensitive)", u"WIP: Föo bar", 1), - RuleViolation(u"T5:my-ïd", u"Title contains the word 'Föo' (case-insensitive)", - u"WIP: Föo bar", 1)] + violations = [RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", "WIP: Föo bar", 1), + RuleViolation("T5:my-ïd", "Title contains the word 'Föo' (case-insensitive)", + "WIP: Föo bar", 1)] self.assertListEqual(violations, linter.lint(commit)) # ignore regular rule: only named rule violations show up @@ -283,5 +273,5 @@ class LintTests(BaseTestCase): self.assertListEqual(violations[:-1], linter.lint(commit)) # ignore named rule by name: only regular rule violations show up - lint_config.ignore = [TitleMustNotContainWord.name + u":my-ïd"] + lint_config.ignore = [TitleMustNotContainWord.name + ":my-ïd"] self.assertListEqual(violations[:-1], linter.lint(commit)) diff --git a/gitlint/tests/test_options.py b/gitlint/tests/test_options.py index 68f0f8c..fc3ccc1 100644 --- a/gitlint/tests/test_options.py +++ b/gitlint/tests/test_options.py @@ -9,25 +9,25 @@ from gitlint.options import IntOption, BoolOption, StrOption, ListOption, PathOp class RuleOptionTests(BaseTestCase): def test_option_equality(self): - options = {IntOption: 123, StrOption: u"foöbar", BoolOption: False, ListOption: ["a", "b"], - PathOption: ".", RegexOption: u"^foöbar(.*)"} + options = {IntOption: 123, StrOption: "foöbar", BoolOption: False, ListOption: ["a", "b"], + PathOption: ".", RegexOption: "^foöbar(.*)"} for clazz, val in options.items(): # 2 options are equal if their name, value and description match - option1 = clazz(u"test-öption", val, u"Test Dëscription") - option2 = clazz(u"test-öption", val, u"Test Dëscription") + option1 = clazz("test-öption", val, "Test Dëscription") + option2 = clazz("test-öption", val, "Test Dëscription") self.assertEqual(option1, option2) # Not equal: class, name, description, value are different - self.assertNotEqual(option1, IntOption(u"tëst-option1", 123, u"Test Dëscription")) - self.assertNotEqual(option1, StrOption(u"tëst-option1", u"åbc", u"Test Dëscription")) - self.assertNotEqual(option1, StrOption(u"tëst-option", u"åbcd", u"Test Dëscription")) - self.assertNotEqual(option1, StrOption(u"tëst-option", u"åbc", u"Test Dëscription2")) + self.assertNotEqual(option1, IntOption("tëst-option1", 123, "Test Dëscription")) + self.assertNotEqual(option1, StrOption("tëst-option1", "åbc", "Test Dëscription")) + self.assertNotEqual(option1, StrOption("tëst-option", "åbcd", "Test Dëscription")) + self.assertNotEqual(option1, StrOption("tëst-option", "åbc", "Test Dëscription2")) def test_int_option(self): # normal behavior - option = IntOption(u"tëst-name", 123, u"Tëst Description") - self.assertEqual(option.name, u"tëst-name") - self.assertEqual(option.description, u"Tëst Description") + option = IntOption("tëst-name", 123, "Tëst Description") + self.assertEqual(option.name, "tëst-name") + self.assertEqual(option.description, "Tëst Description") self.assertEqual(option.value, 123) # re-set value @@ -39,12 +39,12 @@ class RuleOptionTests(BaseTestCase): self.assertEqual(option.value, None) # error on negative int when not allowed - expected_error = u"Option 'tëst-name' must be a positive integer (current value: '-123')" + expected_error = "Option 'tëst-name' must be a positive integer (current value: '-123')" with self.assertRaisesMessage(RuleOptionError, expected_error): option.set(-123) # error on non-int value - expected_error = u"Option 'tëst-name' must be a positive integer (current value: 'foo')" + expected_error = "Option 'tëst-name' must be a positive integer (current value: 'foo')" with self.assertRaisesMessage(RuleOptionError, expected_error): option.set("foo") @@ -54,20 +54,20 @@ class RuleOptionTests(BaseTestCase): self.assertEqual(option.value, -456) # error on non-int value when negative int is allowed - expected_error = u"Option 'test-name' must be an integer (current value: 'foo')" + expected_error = "Option 'test-name' must be an integer (current value: 'foo')" with self.assertRaisesMessage(RuleOptionError, expected_error): option.set("foo") def test_str_option(self): # normal behavior - option = StrOption(u"tëst-name", u"föo", u"Tëst Description") - self.assertEqual(option.name, u"tëst-name") - self.assertEqual(option.description, u"Tëst Description") - self.assertEqual(option.value, u"föo") + option = StrOption("tëst-name", "föo", "Tëst Description") + self.assertEqual(option.name, "tëst-name") + self.assertEqual(option.description, "Tëst Description") + self.assertEqual(option.value, "föo") # re-set value - option.set(u"bår") - self.assertEqual(option.value, u"bår") + option.set("bår") + self.assertEqual(option.value, "bår") # conversion to str option.set(123) @@ -83,9 +83,9 @@ class RuleOptionTests(BaseTestCase): def test_boolean_option(self): # normal behavior - option = BoolOption(u"tëst-name", "true", u"Tëst Description") - self.assertEqual(option.name, u"tëst-name") - self.assertEqual(option.description, u"Tëst Description") + option = BoolOption("tëst-name", "true", "Tëst Description") + self.assertEqual(option.name, "tëst-name") + self.assertEqual(option.description, "Tëst Description") self.assertEqual(option.value, True) # re-set value @@ -97,25 +97,25 @@ class RuleOptionTests(BaseTestCase): self.assertEqual(option.value, True) # error on incorrect value - incorrect_values = [1, -1, "foo", u"bår", ["foo"], {'foo': "bar"}, None] + incorrect_values = [1, -1, "foo", "bår", ["foo"], {'foo': "bar"}, None] for value in incorrect_values: - with self.assertRaisesMessage(RuleOptionError, u"Option 'tëst-name' must be either 'true' or 'false'"): + with self.assertRaisesMessage(RuleOptionError, "Option 'tëst-name' must be either 'true' or 'false'"): option.set(value) def test_list_option(self): # normal behavior - option = ListOption(u"tëst-name", u"å,b,c,d", u"Tëst Description") - self.assertEqual(option.name, u"tëst-name") - self.assertEqual(option.description, u"Tëst Description") - self.assertListEqual(option.value, [u"å", u"b", u"c", u"d"]) + option = ListOption("tëst-name", "å,b,c,d", "Tëst Description") + self.assertEqual(option.name, "tëst-name") + self.assertEqual(option.description, "Tëst Description") + self.assertListEqual(option.value, ["å", "b", "c", "d"]) # re-set value - option.set(u"1,2,3,4") - self.assertListEqual(option.value, [u"1", u"2", u"3", u"4"]) + option.set("1,2,3,4") + self.assertListEqual(option.value, ["1", "2", "3", "4"]) # set list - option.set([u"foo", u"bår", u"test"]) - self.assertListEqual(option.value, [u"foo", u"bår", u"test"]) + option.set(["foo", "bår", "test"]) + self.assertListEqual(option.value, ["foo", "bår", "test"]) # None option.set(None) @@ -134,8 +134,8 @@ class RuleOptionTests(BaseTestCase): self.assertListEqual(option.value, []) # trailing comma - option.set(u"ë,f,g,") - self.assertListEqual(option.value, [u"ë", u"f", u"g"]) + option.set("ë,f,g,") + self.assertListEqual(option.value, ["ë", "f", "g"]) # leading and trailing whitespace should be trimmed, but only deduped within text option.set(" abc , def , ghi \t , jkl mno ") @@ -150,11 +150,11 @@ class RuleOptionTests(BaseTestCase): self.assertListEqual(option.value, ["123"]) def test_path_option(self): - option = PathOption(u"tëst-directory", ".", u"Tëst Description", type=u"dir") - self.assertEqual(option.name, u"tëst-directory") - self.assertEqual(option.description, u"Tëst Description") + option = PathOption("tëst-directory", ".", "Tëst Description", type="dir") + self.assertEqual(option.name, "tëst-directory") + self.assertEqual(option.description, "Tëst Description") self.assertEqual(option.value, os.getcwd()) - self.assertEqual(option.type, u"dir") + self.assertEqual(option.type, "dir") # re-set value option.set(self.SAMPLES_DIR) @@ -165,33 +165,32 @@ class RuleOptionTests(BaseTestCase): self.assertIsNone(option.value) # set to int - expected = u"Option tëst-directory must be an existing directory (current value: '1234')" + expected = "Option tëst-directory must be an existing directory (current value: '1234')" with self.assertRaisesMessage(RuleOptionError, expected): option.set(1234) # set to non-existing directory - non_existing_path = os.path.join(u"/föo", u"bar") - expected = u"Option tëst-directory must be an existing directory (current value: '{0}')" - with self.assertRaisesMessage(RuleOptionError, expected.format(non_existing_path)): + non_existing_path = os.path.join("/föo", "bar") + expected = f"Option tëst-directory must be an existing directory (current value: '{non_existing_path}')" + with self.assertRaisesMessage(RuleOptionError, expected): option.set(non_existing_path) # set to a file, should raise exception since option.type = dir sample_path = self.get_sample_path(os.path.join("commit_message", "sample1")) - expected = u"Option tëst-directory must be an existing directory (current value: '{0}')".format(sample_path) + expected = f"Option tëst-directory must be an existing directory (current value: '{sample_path}')" with self.assertRaisesMessage(RuleOptionError, expected): option.set(sample_path) # set option.type = file, file should now be accepted, directories not - option.type = u"file" + option.type = "file" option.set(sample_path) self.assertEqual(option.value, sample_path) - expected = u"Option tëst-directory must be an existing file (current value: '{0}')".format( - self.get_sample_path()) + expected = f"Option tëst-directory must be an existing file (current value: '{self.get_sample_path()}')" with self.assertRaisesMessage(RuleOptionError, expected): option.set(self.get_sample_path()) # set option.type = both, files and directories should now be accepted - option.type = u"both" + option.type = "both" option.set(sample_path) self.assertEqual(option.value, sample_path) option.set(self.get_sample_path()) @@ -199,27 +198,27 @@ class RuleOptionTests(BaseTestCase): # Expect exception if path type is invalid option.type = u'föo' - expected = u"Option tëst-directory type must be one of: 'file', 'dir', 'both' (current: 'föo')" + expected = "Option tëst-directory type must be one of: 'file', 'dir', 'both' (current: 'föo')" with self.assertRaisesMessage(RuleOptionError, expected): option.set("haha") def test_regex_option(self): # normal behavior - option = RegexOption(u"tëst-regex", u"^myrëgex(.*)foo$", u"Tëst Regex Description") - self.assertEqual(option.name, u"tëst-regex") - self.assertEqual(option.description, u"Tëst Regex Description") - self.assertEqual(option.value, re.compile(u"^myrëgex(.*)foo$", re.UNICODE)) + option = RegexOption("tëst-regex", "^myrëgex(.*)foo$", "Tëst Regex Description") + self.assertEqual(option.name, "tëst-regex") + self.assertEqual(option.description, "Tëst Regex Description") + self.assertEqual(option.value, re.compile("^myrëgex(.*)foo$", re.UNICODE)) # re-set value - option.set(u"[0-9]föbar.*") - self.assertEqual(option.value, re.compile(u"[0-9]föbar.*", re.UNICODE)) + option.set("[0-9]föbar.*") + self.assertEqual(option.value, re.compile("[0-9]föbar.*", re.UNICODE)) # set None option.set(None) self.assertIsNone(option.value) # error on invalid regex - incorrect_values = [u"foo(", 123, -1] + incorrect_values = ["foo(", 123, -1] for value in incorrect_values: - with self.assertRaisesRegex(RuleOptionError, u"Invalid regular expression"): + with self.assertRaisesRegex(RuleOptionError, "Invalid regular expression"): option.set(value) diff --git a/gitlint/tests/test_utils.py b/gitlint/tests/test_utils.py index 5841b63..4ec8bda 100644 --- a/gitlint/tests/test_utils.py +++ b/gitlint/tests/test_utils.py @@ -1,15 +1,10 @@ # -*- coding: utf-8 -*- +from unittest.mock import patch + from gitlint import utils from gitlint.tests.base import BaseTestCase -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 - class UtilsTests(BaseTestCase): @@ -24,7 +19,7 @@ class UtilsTests(BaseTestCase): self.assertEqual(utils.use_sh_library(), True) patched_env.get.assert_called_once_with("GITLINT_USE_SH_LIB", None) - for invalid_val in ["0", u"foöbar"]: + for invalid_val in ["0", "foöbar"]: patched_env.get.reset_mock() # reset mock call count patched_env.get.return_value = invalid_val self.assertEqual(utils.use_sh_library(), False, invalid_val) @@ -41,12 +36,12 @@ class UtilsTests(BaseTestCase): @patch('gitlint.utils.locale') def test_default_encoding_non_windows(self, mocked_locale): utils.PLATFORM_IS_WINDOWS = False - mocked_locale.getpreferredencoding.return_value = u"foöbar" - self.assertEqual(utils.getpreferredencoding(), u"foöbar") + mocked_locale.getpreferredencoding.return_value = "foöbar" + self.assertEqual(utils.getpreferredencoding(), "foöbar") mocked_locale.getpreferredencoding.assert_called_once() mocked_locale.getpreferredencoding.return_value = False - self.assertEqual(utils.getpreferredencoding(), u"UTF-8") + self.assertEqual(utils.getpreferredencoding(), "UTF-8") @patch('os.environ') def test_default_encoding_windows(self, patched_env): @@ -60,23 +55,23 @@ class UtilsTests(BaseTestCase): patched_env.get.side_effect = mocked_get # Assert getpreferredencoding reads env vars in order: LC_ALL, LC_CTYPE, LANG - mock_env = {"LC_ALL": u"ASCII", "LC_CTYPE": u"UTF-16", "LANG": u"CP1251"} - self.assertEqual(utils.getpreferredencoding(), u"ASCII") - mock_env = {"LC_CTYPE": u"UTF-16", "LANG": u"CP1251"} - self.assertEqual(utils.getpreferredencoding(), u"UTF-16") - mock_env = {"LANG": u"CP1251"} - self.assertEqual(utils.getpreferredencoding(), u"CP1251") + mock_env = {"LC_ALL": "ASCII", "LC_CTYPE": "UTF-16", "LANG": "CP1251"} + self.assertEqual(utils.getpreferredencoding(), "ASCII") + mock_env = {"LC_CTYPE": "UTF-16", "LANG": "CP1251"} + self.assertEqual(utils.getpreferredencoding(), "UTF-16") + mock_env = {"LANG": "CP1251"} + self.assertEqual(utils.getpreferredencoding(), "CP1251") # Assert split on dot - mock_env = {"LANG": u"foo.UTF-16"} - self.assertEqual(utils.getpreferredencoding(), u"UTF-16") + mock_env = {"LANG": "foo.UTF-16"} + self.assertEqual(utils.getpreferredencoding(), "UTF-16") # assert default encoding is UTF-8 mock_env = {} self.assertEqual(utils.getpreferredencoding(), "UTF-8") - mock_env = {"FOO": u"föo"} + mock_env = {"FOO": "föo"} self.assertEqual(utils.getpreferredencoding(), "UTF-8") # assert fallback encoding is UTF-8 in case we set an unavailable encoding - mock_env = {"LC_ALL": u"foo"} - self.assertEqual(utils.getpreferredencoding(), u"UTF-8") + mock_env = {"LC_ALL": "foo"} + self.assertEqual(utils.getpreferredencoding(), "UTF-8") |