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

bears.tests.vcs.git.GitCommitBearTest   A

Complexity

Total Complexity 14

Size/Duplication

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