Failed Conditions
Pull Request — master (#1228)
by Mischa
05:04 queued 11s
created

bears.tests.vcs.git.GitCommitBearTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 136
Duplicated Lines 0 %
Metric Value
dl 0
loc 136
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 2 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 6 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
    def run_git_command(self, *args):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

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