summaryrefslogtreecommitdiffstats
path: root/gitlint/tests/git/test_git_context.py
blob: b243d5e1059f051e1aa395e57653c5c99863b155 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- 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 gitlint.tests.base import BaseTestCase
from gitlint.git import GitContext


class GitContextTests(BaseTestCase):

    # Expected special_args passed to 'sh'
    expected_sh_special_args = {
        '_tty_out': False,
        '_cwd': u"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"
        ]

        expected_calls = [
            call("config", "--get", "core.commentchar", _ok_code=[0, 1], **self.expected_sh_special_args),
            call("rev-parse", "--abbrev-ref", "HEAD", **self.expected_sh_special_args)
        ]

        context = GitContext(u"fåke/path")
        self.assertEqual(sh.git.mock_calls, [])

        # gitcontext.comment_branch
        self.assertEqual(context.commentchar, u"#")
        self.assertEqual(sh.git.mock_calls, expected_calls[0:1])

        # gitcontext.current_branch
        self.assertEqual(context.current_branch, u"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
        ]

        context1 = GitContext(u"fåke/path")
        context1.commits = [u"fōo", u"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"]
        self.assertEqual(context1, context2)

        # INEQUALITY
        # Different commits
        context2.commits = [u"hür", u"dür"]
        self.assertNotEqual(context1, context2)

        # Different repository_path
        context2.commits = context1.commits
        context2.repository_path = u"ōther/path"
        self.assertNotEqual(context1, context2)

        # Different comment_char
        context3 = GitContext(u"fåke/path")
        context3.commits = [u"fōo", u"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
        ])
        self.assertNotEqual(context1, context3)

        # Different current_branch
        context4 = GitContext(u"fåke/path")
        context4.commits = [u"fōo", u"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
        ])
        self.assertNotEqual(context1, context4)