Failed Conditions
Pull Request — master (#1228)
by Mischa
01:51
created

bears.tests.vcs.git.GitCommitBearTest   A

Complexity

Total Complexity 16

Size/Duplication

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

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