From a2aa51f5702b18016c25d943499941323952704d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 19 Nov 2022 15:52:46 +0100 Subject: Adding upstream version 0.18.0. Signed-off-by: Daniel Baumann --- gitlint-core/gitlint/tests/git/test_git.py | 65 +++++++++++++++++------------- 1 file changed, 37 insertions(+), 28 deletions(-) (limited to 'gitlint-core/gitlint/tests/git/test_git.py') diff --git a/gitlint-core/gitlint/tests/git/test_git.py b/gitlint-core/gitlint/tests/git/test_git.py index 7b9b7c6..9c73bd9 100644 --- a/gitlint-core/gitlint/tests/git/test_git.py +++ b/gitlint-core/gitlint/tests/git/test_git.py @@ -1,7 +1,6 @@ -# -*- coding: utf-8 -*- import os -from unittest.mock import patch +from unittest.mock import patch, call from gitlint.shell import ErrorReturnCode, CommandNotFound @@ -10,25 +9,23 @@ from gitlint.git import GitContext, GitContextError, GitNotInstalledError, git_c class GitTests(BaseTestCase): - # Expected special_args passed to 'sh' - expected_sh_special_args = { - '_tty_out': False, - '_cwd': "fåke/path" - } + expected_sh_special_args = {"_tty_out": False, "_cwd": "fåke/path"} - @patch('gitlint.git.sh') + @patch("gitlint.git.sh") def test_get_latest_commit_command_not_found(self, sh): sh.git.side_effect = CommandNotFound("git") - 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." + 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("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) - @patch('gitlint.git.sh') + @patch("gitlint.git.sh") def test_get_latest_commit_git_error(self, sh): # Current directory not a git repo err = b"fatal: Not a git repository (or any of the parent directories): .git" @@ -51,10 +48,10 @@ class GitTests(BaseTestCase): # assert that commit message was read using git command sh.git.assert_called_once_with("log", "-1", "--pretty=%H", **self.expected_sh_special_args) - @patch('gitlint.git.sh') + @patch("gitlint.git.sh") def test_git_no_commits_error(self, sh): # No commits: returned by 'git log' - err = b"fatal: your current branch 'master' does not have any commits yet" + err = b"fatal: your current branch 'main' does not have any commits yet" sh.git.side_effect = ErrorReturnCode("git log -1 --pretty=%H", b"", err) @@ -64,25 +61,38 @@ class GitTests(BaseTestCase): # assert that commit message was read using git command sh.git.assert_called_once_with("log", "-1", "--pretty=%H", **self.expected_sh_special_args) - sh.git.reset_mock() + @patch("gitlint.git.sh") + def test_git_no_commits_get_branch(self, sh): + """Check that we can still read the current branch name when there's no commits. This is useful when + when trying to lint the first commit using the --staged flag. + """ # Unknown reference 'HEAD' commits: returned by 'git rev-parse' - err = (b"HEAD" - b"fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree." - b"Use '--' to separate paths from revisions, like this:" - b"'git [...] -- [...]'") + err = ( + b"HEAD" + b"fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree." + b"Use '--' to separate paths from revisions, like this:" + b"'git [...] -- [...]'" + ) sh.git.side_effect = [ "#\n", # git config --get core.commentchar - ErrorReturnCode("rev-parse --abbrev-ref HEAD", b"", err) + ErrorReturnCode("rev-parse --abbrev-ref HEAD", b"", err), + "test-branch", # git branch --show-current ] - with self.assertRaisesMessage(GitContextError, expected_msg): - context = GitContext.from_commit_msg("test") - context.current_branch + context = GitContext.from_commit_msg("test") + self.assertEqual(context.current_branch, "test-branch") - # assert that commit message was read using git command - sh.git.assert_called_with("rev-parse", "--abbrev-ref", "HEAD", _tty_out=False, _cwd=None) + # assert that we try using `git rev-parse` first, and if that fails (as will be the case with the first commit), + # we fallback to `git branch --show-current` to determine the current branch name. + expected_calls = [ + call("config", "--get", "core.commentchar", _tty_out=False, _cwd=None, _ok_code=[0, 1]), + call("rev-parse", "--abbrev-ref", "HEAD", _tty_out=False, _cwd=None), + call("branch", "--show-current", _tty_out=False, _cwd=None), + ] + + self.assertEqual(sh.git.mock_calls, expected_calls) @patch("gitlint.git._git") def test_git_commentchar(self, git): @@ -93,11 +103,10 @@ class GitTests(BaseTestCase): git.return_value = "ä" self.assertEqual(git_commentchar(), "ä") - git.return_value = ';\n' - self.assertEqual(git_commentchar(os.path.join("/föo", "bar")), ';') + git.return_value = ";\n" + 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("/föo", "bar")) + git.assert_called_with("config", "--get", "core.commentchar", _ok_code=[0, 1], _cwd=os.path.join("/föo", "bar")) @patch("gitlint.git._git") def test_git_hooks_dir(self, git): -- cgit v1.2.3