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

bears.tests.vcs.git.GitCommitBearTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 140
Duplicated Lines 0 %
Metric Value
dl 0
loc 140
rs 10
wmc 14

11 Methods

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