summaryrefslogtreecommitdiffstats
path: root/gitlint/tests/cli
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2021-01-25 13:26:08 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2021-01-25 13:26:08 +0000
commit3313b4f9c3c5d6a579588e77068ca3ae3edffe2b (patch)
tree81fccb2a4db79b0de9b0d64701581add3a9a1d21 /gitlint/tests/cli
parentAdding upstream version 0.14.0. (diff)
downloadgitlint-3313b4f9c3c5d6a579588e77068ca3ae3edffe2b.tar.xz
gitlint-3313b4f9c3c5d6a579588e77068ca3ae3edffe2b.zip
Adding upstream version 0.15.0.upstream/0.15.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gitlint/tests/cli')
-rw-r--r--gitlint/tests/cli/test_cli.py234
-rw-r--r--gitlint/tests/cli/test_cli_hooks.py121
2 files changed, 178 insertions, 177 deletions
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']):