diff options
Diffstat (limited to 'gitlint-core/gitlint/tests/cli/test_cli.py')
-rw-r--r-- | gitlint-core/gitlint/tests/cli/test_cli.py | 101 |
1 files changed, 81 insertions, 20 deletions
diff --git a/gitlint-core/gitlint/tests/cli/test_cli.py b/gitlint-core/gitlint/tests/cli/test_cli.py index d18efe9..c006375 100644 --- a/gitlint-core/gitlint/tests/cli/test_cli.py +++ b/gitlint-core/gitlint/tests/cli/test_cli.py @@ -1,22 +1,15 @@ -import io import os -import sys import platform - -import arrow - +import sys from io import StringIO - -from click.testing import CliRunner - from unittest.mock import patch +import arrow +from click.testing import CliRunner +from gitlint import __version__, cli from gitlint.shell import CommandNotFound - from gitlint.tests.base import BaseTestCase -from gitlint import cli -from gitlint import __version__ -from gitlint.utils import DEFAULT_ENCODING +from gitlint.utils import FILE_ENCODING, TERMINAL_ENCODING class CLITests(BaseTestCase): @@ -46,7 +39,8 @@ class CLITests(BaseTestCase): "gitlint_version": __version__, "GITLINT_USE_SH_LIB": BaseTestCase.GITLINT_USE_SH_LIB, "target": os.path.realpath(os.getcwd()), - "DEFAULT_ENCODING": DEFAULT_ENCODING, + "TERMINAL_ENCODING": TERMINAL_ENCODING, + "FILE_ENCODING": FILE_ENCODING, } def test_version(self): @@ -107,6 +101,40 @@ class CLITests(BaseTestCase): @patch("gitlint.cli.get_stdin_data", return_value=False) @patch("gitlint.git.sh") + def test_lint_multiple_commits_csv(self, sh, _): + """Test for --commits option""" + + # fmt: off + sh.git.side_effect = [ + "6f29bf81a8322a04071bb794666e48c443a90360\n", # git rev-list <SHA> + "25053ccec5e28e1bb8f7551fdbb5ab213ada2401\n", + "4da2656b0dadc76c7ee3fd0243a96cb64007f125\n", + # git log --pretty <FORMAT> <SHA> + "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 + "3\t5\tcommit-1/file-1\n1\t4\tcommit-1/file-2\n", # git diff-tree + "commit-1-branch-1\ncommit-1-branch-2\n", # git branch --contains <sha> + # git log --pretty <FORMAT> <SHA> + "test åuthor2\x00test-email3@föo.com\x002016-12-04 15:28:15 +0100\x00åbc\n" + "commït-title2\n\ncommït-body2", + "8\t3\tcommit-2/file-1\n1\t5\tcommit-2/file-2\n", # git diff-tree + "commit-2-branch-1\ncommit-2-branch-2\n", # git branch --contains <sha> + # git log --pretty <FORMAT> <SHA> + "test åuthor3\x00test-email3@föo.com\x002016-12-05 15:28:15 +0100\x00åbc\n" + "commït-title3\n\ncommït-body3", + "7\t2\tcommit-3/file-1\n1\t7\tcommit-3/file-2\n", # git diff-tree + "commit-3-branch-1\ncommit-3-branch-2\n", # git branch --contains <sha> + ] + # fmt: on + + with patch("gitlint.display.stderr", new=StringIO()) as stderr: + result = self.cli.invoke(cli.cli, ["--commits", "6f29bf81,25053cce,4da2656b"]) + self.assertEqual(stderr.getvalue(), self.get_expected("cli/test_cli/test_lint_multiple_commits_csv_1")) + self.assertEqual(result.exit_code, 3) + + @patch("gitlint.cli.get_stdin_data", return_value=False) + @patch("gitlint.git.sh") def test_lint_multiple_commits_config(self, sh, _): """Test for --commits option where some of the commits have gitlint config in the commit message""" @@ -225,8 +253,7 @@ class CLITests(BaseTestCase): self.assertEqual(result.exit_code, 2) @patch("gitlint.cli.get_stdin_data", return_value=False) - @patch("gitlint.git.sh") - def test_lint_commit_negative(self, sh, _): + def test_lint_commit_negative(self, _): """Negative test for --commit option""" # Try using --commit and --commits at the same time (not allowed) @@ -298,6 +325,11 @@ class CLITests(BaseTestCase): self.assertEqual(result.output, "") expected_kwargs = self.get_system_info_dict() + changed_files_stats = ( + f" {os.path.join('commit-1', 'file-1')}: 1 additions, 5 deletions\n" + f" {os.path.join('commit-1', 'file-2')}: 8 additions, 9 deletions" + ) + expected_kwargs.update({"changed_files_stats": changed_files_stats}) expected_logs = self.get_expected("cli/test_cli/test_lint_staged_stdin_2", expected_kwargs) self.assert_logged(expected_logs) @@ -318,7 +350,7 @@ class CLITests(BaseTestCase): with self.tempdir() as tmpdir: msg_filename = os.path.join(tmpdir, "msg") - with open(msg_filename, "w", encoding=DEFAULT_ENCODING) as f: + with open(msg_filename, "w", encoding=FILE_ENCODING) as f: f.write("WIP: msg-filename tïtle\n") with patch("gitlint.display.stderr", new=StringIO()) as stderr: @@ -328,6 +360,11 @@ class CLITests(BaseTestCase): self.assertEqual(result.output, "") expected_kwargs = self.get_system_info_dict() + changed_files_stats = ( + f" {os.path.join('commit-1', 'file-1')}: 3 additions, 4 deletions\n" + f" {os.path.join('commit-1', 'file-2')}: 4 additions, 7 deletions" + ) + expected_kwargs.update({"changed_files_stats": changed_files_stats}) expected_logs = self.get_expected("cli/test_cli/test_lint_staged_msg_filename_2", expected_kwargs) self.assert_logged(expected_logs) @@ -368,7 +405,7 @@ class CLITests(BaseTestCase): with self.tempdir() as tmpdir: msg_filename = os.path.join(tmpdir, "msg") - with open(msg_filename, "w", encoding=DEFAULT_ENCODING) as f: + with open(msg_filename, "w", encoding=FILE_ENCODING) as f: f.write("Commït title\n") with patch("gitlint.display.stderr", new=StringIO()) as stderr: @@ -458,6 +495,25 @@ class CLITests(BaseTestCase): self.assertEqual(result.exit_code, 6) expected_kwargs = self.get_system_info_dict() + changed_files_stats1 = ( + f" {os.path.join('commit-1', 'file-1')}: 5 additions, 8 deletions\n" + f" {os.path.join('commit-1', 'file-2')}: 2 additions, 9 deletions" + ) + changed_files_stats2 = ( + f" {os.path.join('commit-2', 'file-1')}: 5 additions, 8 deletions\n" + f" {os.path.join('commit-2', 'file-2')}: 7 additions, 9 deletions" + ) + changed_files_stats3 = ( + f" {os.path.join('commit-3', 'file-1')}: 1 additions, 4 deletions\n" + f" {os.path.join('commit-3', 'file-2')}: 3 additions, 4 deletions" + ) + expected_kwargs.update( + { + "changed_files_stats1": changed_files_stats1, + "changed_files_stats2": changed_files_stats2, + "changed_files_stats3": changed_files_stats3, + } + ) expected_kwargs.update({"config_path": config_path}) expected_logs = self.get_expected("cli/test_cli/test_debug_1", expected_kwargs) self.assert_logged(expected_logs) @@ -548,7 +604,7 @@ class CLITests(BaseTestCase): # Non existing file config_path = self.get_sample_path("föo") result = self.cli.invoke(cli.cli, ["--config", config_path]) - expected_string = f"Error: Invalid value for '-C' / '--config': File '{config_path}' does not exist." + expected_string = f"Error: Invalid value for '-C' / '--config': File {config_path!r} does not exist." self.assertEqual(result.output.split("\n")[3], expected_string) self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE) @@ -569,7 +625,7 @@ class CLITests(BaseTestCase): # Non existing file config_path = self.get_sample_path("föo") result = self.cli.invoke(cli.cli, env={"GITLINT_CONFIG": config_path}) - expected_string = f"Error: Invalid value for '-C' / '--config': File '{config_path}' does not exist." + expected_string = f"Error: Invalid value for '-C' / '--config': File {config_path!r} does not exist." self.assertEqual(result.output.split("\n")[3], expected_string) self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE) @@ -578,6 +634,11 @@ class CLITests(BaseTestCase): result = self.cli.invoke(cli.cli, env={"GITLINT_CONFIG": config_path}) self.assertEqual(result.exit_code, self.CONFIG_ERROR_CODE) + def test_config_error(self): + result = self.cli.invoke(cli.cli, ["-c", "foo.bar=hur"]) + self.assertEqual(result.output, "Config Error: No such rule 'foo'\n") + self.assertEqual(result.exit_code, self.CONFIG_ERROR_CODE) + @patch("gitlint.cli.get_stdin_data", return_value=False) def test_target(self, _): """Test for the --target option""" @@ -602,7 +663,7 @@ class CLITests(BaseTestCase): 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 = f"Error: Invalid value for '--target': Directory '{target_path}' is a file." + expected_msg = f"Error: Invalid value for '--target': Directory {target_path!r} is a file." self.assertEqual(result.output.split("\n")[3], expected_msg) @patch("gitlint.config.LintConfigGenerator.generate_config") |