diff options
Diffstat (limited to 'gitlint/tests/cli/test_cli.py')
-rw-r--r-- | gitlint/tests/cli/test_cli.py | 64 |
1 files changed, 61 insertions, 3 deletions
diff --git a/gitlint/tests/cli/test_cli.py b/gitlint/tests/cli/test_cli.py index bf35e96..59ec7af 100644 --- a/gitlint/tests/cli/test_cli.py +++ b/gitlint/tests/cli/test_cli.py @@ -26,6 +26,7 @@ class CLITests(BaseTestCase): USAGE_ERROR_CODE = 253 GIT_CONTEXT_ERROR_CODE = 254 CONFIG_ERROR_CODE = 255 + GITLINT_SUCCESS_CODE = 0 def setUp(self): super(CLITests, self).setUp() @@ -180,6 +181,39 @@ class CLITests(BaseTestCase): self.assertEqual(stderr.getvalue(), expected) self.assertEqual(result.exit_code, 2) + @patch('gitlint.cli.get_stdin_data', return_value=False) + @patch('gitlint.git.sh') + def test_lint_commit(self, sh, _): + """ Test for --commit option """ + + sh.git.side_effect = [ + "6f29bf81a8322a04071bb794666e48c443a90360\n", # git log -1 <SHA> --pretty=%H + # git log --pretty <FORMAT> <SHA> + "test åuthor1\x00test-email1@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n" + "WIP: 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 + ] + + with patch('gitlint.display.stderr', new=StringIO()) as stderr: + result = self.cli.invoke(cli.cli, ["--commit", "foo"]) + self.assertEqual(result.output, "") + + self.assertEqual(stderr.getvalue(), self.get_expected("cli/test_cli/test_lint_commit_1")) + 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, _): + """ Negative test for --commit option """ + + # Try using --commit and --commits at the same time (not allowed) + result = self.cli.invoke(cli.cli, ["--commit", "foo", "--commits", "foo...bar"]) + expected_output = "Error: --commit and --commits are mutually exclusive, use one or the other.\n" + self.assertEqual(result.output, expected_output) + self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE) + @patch('gitlint.cli.get_stdin_data', return_value=u'WIP: tïtle \n') def test_input_stream(self, _): """ Test for linting when a message is passed via stdin """ @@ -283,6 +317,30 @@ class CLITests(BaseTestCase): "'--msg-filename' or when piping data to gitlint via stdin.\n")) @patch('gitlint.cli.get_stdin_data', return_value=False) + @patch('gitlint.git.sh') + def test_fail_without_commits(self, sh, _): + """ Test for --debug option """ + + sh.git.side_effect = [ + "", # First invocation of git rev-list + "" # Second invocation of git rev-list + ] + + with patch('gitlint.display.stderr', new=StringIO()) as stderr: + # By default, gitlint should silently exit with code GITLINT_SUCCESS when there are no commits + result = self.cli.invoke(cli.cli, ["--commits", "foo..bar"]) + self.assertEqual(stderr.getvalue(), "") + self.assertEqual(result.exit_code, cli.GITLINT_SUCCESS) + self.assert_log_contains("DEBUG: gitlint.cli No commits in range \"foo..bar\"") + + # When --fail-without-commits is set, gitlint should hard fail with code USAGE_ERROR_CODE + self.clearlog() + result = self.cli.invoke(cli.cli, ["--commits", "foo..bar", "--fail-without-commits"]) + self.assertEqual(result.output, 'Error: No commits in range "foo..bar"\n') + self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE) + self.assert_log_contains("DEBUG: gitlint.cli No commits in range \"foo..bar\"") + + @patch('gitlint.cli.get_stdin_data', return_value=False) def test_msg_filename(self, _): expected_output = "3: B6 Body message is missing\n" @@ -405,7 +463,7 @@ class CLITests(BaseTestCase): result = self.cli.invoke(cli.cli, ["--contrib", "contrib-title-conventional-commits,CC1"]) expected_output = self.get_expected('cli/test_cli/test_contrib_1') self.assertEqual(stderr.getvalue(), expected_output) - self.assertEqual(result.exit_code, 3) + self.assertEqual(result.exit_code, 2) @patch('gitlint.cli.get_stdin_data', return_value="Test tïtle\n") def test_contrib_negative(self, _): @@ -475,7 +533,7 @@ class CLITests(BaseTestCase): def test_generate_config(self, generate_config): """ Test for the generate-config subcommand """ result = self.cli.invoke(cli.cli, ["generate-config"], input="tëstfile\n") - self.assertEqual(result.exit_code, 0) + self.assertEqual(result.exit_code, self.GITLINT_SUCCESS_CODE) 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) @@ -517,7 +575,7 @@ class CLITests(BaseTestCase): result = self.cli.invoke(cli.cli, ["--commits", "master...HEAD"]) self.assert_log_contains("DEBUG: gitlint.cli No commits in range \"master...HEAD\"") - self.assertEqual(result.exit_code, 0) + self.assertEqual(result.exit_code, self.GITLINT_SUCCESS_CODE) @patch('gitlint.cli.get_stdin_data', return_value="WIP: tëst tïtle") def test_named_rules(self, _): |