Completed
Pull Request — master (#1239)
by Lasse
01:31
created

test_empty_message()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 10
rs 9.4285
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_git_failure(self):
82
        # In this case use a reference to a non-existing commit, so just try
83
        # to log all commits on a newly created repository.
84
        self.assertEqual(self.run_uut(), [])
85
86
        git_error = self.msg_queue.get().message
87
        self.assertEqual(git_error[:4], "git:")
88
89
        self.assertTrue(self.msg_queue.empty())
90
91
    def test_empty_message(self):
92
        self.git_commit("")
93
94
        self.assertEqual(self.run_uut(),
95
                         ["HEAD commit has no message."])
96
        self.assertTrue(self.msg_queue.empty())
97
98
        self.assertEqual(self.run_uut(allow_empty_commit_message=True),
99
                         [])
100
        self.assertTrue(self.msg_queue.empty())
101
102
    def test_shortlog_checks(self):
103
        self.git_commit("This is a shortlog")
104
105
        self.assertEqual(self.run_uut(), [])
106
        self.assertTrue(self.msg_queue.empty())
107
108
        self.assertEqual(self.run_uut(shortlog_length=17),
109
                         ["Shortlog of HEAD commit is too long."])
110
        self.assertTrue(self.msg_queue.empty())
111
112
        self.git_commit("A shortlog that is too long is not good for history")
113
        self.assertEqual(self.run_uut(),
114
                         ["Shortlog of HEAD commit is too long."])
115
        self.assertTrue(self.msg_queue.empty())
116
117
    def test_body_checks(self):
118
        self.git_commit("Shortlog\n\nBody with details.")
119
120
        self.assertEqual(self.run_uut(), [])
121
        self.assertTrue(self.msg_queue.empty())
122
123
        self.git_commit("Shortlog only")
124
125
        self.assertEqual(self.run_uut(), [])
126
        self.assertTrue(self.msg_queue.empty())
127
128
        # Force a body.
129
        self.git_commit("Shortlog only ...")
130
        self.assertEqual(self.run_uut(force_body=True),
131
                         ["No commit message body at HEAD."])
132
        self.assertTrue(self.msg_queue.empty())
133
134
        # Miss a newline between shortlog and body.
135
        self.git_commit("Shortlog\nOops, body too early")
136
        self.assertEqual(self.run_uut(),
137
                         ["No newline between shortlog and body at HEAD."])
138
        self.assertTrue(self.msg_queue.empty())
139
140
        # And now too long lines.
141
        self.git_commit("Shortlog\n\n"
142
                        "This line is ok.\n"
143
                        "This line is by far too long (in this case).\n"
144
                        "This one too, blablablablablablablablabla.")
145
        self.assertEqual(self.run_uut(body_line_length=41),
146
                         ["Body of HEAD commit contains too long lines."])
147
        self.assertTrue(self.msg_queue.empty())
148
149
150
if __name__ == '__main__':
151
    unittest.main(verbosity=2)
152