Completed
Pull Request — master (#1228)
by Mischa
01:24
created

bears.tests.vcs.git.GitCommitBearTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 151
Duplicated Lines 0 %
Metric Value
wmc 16
dl 0
loc 151
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A run_uut() 0 10 2
A setUp() 0 10 1
A test_empty_message() 0 10 1
A test_good_commit_messages() 0 19 1
A run_git_command() 0 3 1
A test_shortlog_checks() 0 14 1
A _windows_rmtree_remove_readonly() 0 4 1
A test_check_prerequisites() 0 11 3
A git_commit() 0 9 1
A tearDown() 0 7 2
B test_body_checks() 0 31 1
A test_git_failure() 0 9 1
1
from queue import Queue
2
from tempfile import mkdtemp
3
import os
4
import platform
5
import stat
6
import sys
7
import shutil
8
import unittest
9
10
sys.path.insert(0, ".")
11
from coalib.misc.Shell import run_shell_command
12
from coalib.settings.Section import Section
13
from bears.vcs.git.GitCommitBear import GitCommitBear
14
from bears.tests.BearTestHelper import generate_skip_decorator
15
16
17
@generate_skip_decorator(GitCommitBear)
18
class GitCommitBearTest(unittest.TestCase):
19
20
    @staticmethod
21
    def run_git_command(*args, stdin=None):
22
        run_shell_command(" ".join(("git",) + args), stdin)
23
24
    @staticmethod
25
    def git_commit(msg):
26
        # Use stdin mode from git, since -m on Windows cmd does not support
27
        # multiline messages.
28
        GitCommitBearTest.run_git_command("commit",
29
                                          "--allow-empty",
30
                                          "--allow-empty-message",
31
                                          "--file=-",
32
                                          stdin=msg)
33
34
    def run_uut(self, *args, **kwargs):
35
        """
36
        Runs the unit-under-test (via `self.uut.run()`) and collects the
37
        messages of the yielded results as a list.
38
39
        :param args:   Positional arguments to forward to the run function.
40
        :param kwargs: Keyword arguments to forward to the run function.
41
        :return:       A list of the message strings.
42
        """
43
        return list(result.message for result in self.uut.run(*args, **kwargs))
44
45
    def setUp(self):
46
        self.msg_queue = Queue()
47
        self.uut = GitCommitBear(None, Section(""), self.msg_queue)
48
49
        self._old_cwd = os.getcwd()
50
        self.gitdir = mkdtemp()
51
        os.chdir(self.gitdir)
52
        self.run_git_command("init")
53
        self.run_git_command("config", "user.email [email protected]")
54
        self.run_git_command("config", "user.name coala")
55
56
    @staticmethod
57
    def _windows_rmtree_remove_readonly(func, path, excinfo):
58
        os.chmod(path, stat.S_IWRITE)
59
        func(path)
60
61
    def tearDown(self):
62
        os.chdir(self._old_cwd)
63
        if platform.system() == "Windows":
64
            onerror = self._windows_rmtree_remove_readonly
65
        else:
66
            onerror = None
67
        shutil.rmtree(self.gitdir, onerror=onerror)
68
69
    def test_check_prerequisites(self):
70
        _shutil_which = shutil.which
71
        try:
72
            shutil.which = lambda *args, **kwargs: None
73
            self.assertEqual(GitCommitBear.check_prerequisites(),
74
                             "git is not installed.")
75
76
            shutil.which = lambda *args, **kwargs: "path/to/git"
77
            self.assertTrue(GitCommitBear.check_prerequisites())
78
        finally:
79
            shutil.which = _shutil_which
80
81
    def test_good_commit_messages(self):
82
        self.git_commit("A little nice commit")
83
        self.assertEqual(self.run_uut(), [])
84
        self.assertTrue(self.msg_queue.empty())
85
86
        self.git_commit("Commits messages that nearly exceeds default limit")
87
        self.assertEqual(self.run_uut(), [])
88
        self.assertTrue(self.msg_queue.empty())
89
90
        self.git_commit("Commits message with a body\n\nBody\nAnother body")
91
        self.assertEqual(self.run_uut(), [])
92
        self.assertTrue(self.msg_queue.empty())
93
94
        self.git_commit(
95
            "Commits message with a body\n\n"
96
            "nearly exceeding the default length of a body, but not quite. "
97
            "haaaaaaands")
98
        self.assertEqual(self.run_uut(), [])
99
        self.assertTrue(self.msg_queue.empty())
100
101
    def test_git_failure(self):
102
        # In this case use a reference to a non-existing commit, so just try
103
        # to log all commits on a newly created repository.
104
        self.assertEqual(self.run_uut(), [])
105
106
        git_error = self.msg_queue.get().message
107
        self.assertEqual(git_error[:4], "git:")
108
109
        self.assertTrue(self.msg_queue.empty())
110
111
    def test_empty_message(self):
112
        self.git_commit("")
113
114
        self.assertEqual(self.run_uut(),
115
                         ["HEAD commit has no message."])
116
        self.assertTrue(self.msg_queue.empty())
117
118
        self.assertEqual(self.run_uut(allow_empty_commit_message=True),
119
                         [])
120
        self.assertTrue(self.msg_queue.empty())
121
122
    def test_shortlog_checks(self):
123
        self.git_commit("This is a shortlog")
124
125
        self.assertEqual(self.run_uut(), [])
126
        self.assertTrue(self.msg_queue.empty())
127
128
        self.assertEqual(self.run_uut(shortlog_length=17),
129
                         ["Shortlog of HEAD commit is too long."])
130
        self.assertTrue(self.msg_queue.empty())
131
132
        self.git_commit("A shortlog that is too long is not good for history")
133
        self.assertEqual(self.run_uut(),
134
                         ["Shortlog of HEAD commit is too long."])
135
        self.assertTrue(self.msg_queue.empty())
136
137
    def test_body_checks(self):
138
        self.git_commit("Shortlog\n\nBody with details.")
139
140
        self.assertEqual(self.run_uut(), [])
141
        self.assertTrue(self.msg_queue.empty())
142
143
        self.git_commit("Shortlog only")
144
145
        self.assertEqual(self.run_uut(), [])
146
        self.assertTrue(self.msg_queue.empty())
147
148
        # Force a body.
149
        self.git_commit("Shortlog only ...")
150
        self.assertEqual(self.run_uut(force_body=True),
151
                         ["No commit message body at HEAD."])
152
        self.assertTrue(self.msg_queue.empty())
153
154
        # Miss a newline between shortlog and body.
155
        self.git_commit("Shortlog\nOops, body too early")
156
        self.assertEqual(self.run_uut(),
157
                         ["No newline between shortlog and body at HEAD."])
158
        self.assertTrue(self.msg_queue.empty())
159
160
        # And now too long lines.
161
        self.git_commit("Shortlog\n\n"
162
                        "This line is ok.\n"
163
                        "This line is by far too long (in this case).\n"
164
                        "This one too, blablablablablablablablabla.")
165
        self.assertEqual(self.run_uut(body_line_length=41),
166
                         ["Body of HEAD commit contains too long lines."])
167
        self.assertTrue(self.msg_queue.empty())
168
169
170
if __name__ == '__main__':
171
    unittest.main(verbosity=2)
172