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
|
|
|
stdout, stderr = run_shell_command(cmd) |
23
|
|
|
print("STDOUT:", repr(stdout)) |
24
|
|
|
print("STDERR:", repr(stderr)) |
25
|
|
|
|
26
|
|
|
@staticmethod |
27
|
|
|
def git_commit(msg): |
28
|
|
|
GitCommitBearTest.run_git_command("commit", |
29
|
|
|
"--allow-empty", |
30
|
|
|
"--allow-empty-message", |
31
|
|
|
"-m", |
32
|
|
|
prepare_string_argument(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
|
|
|
def tearDown(self): |
57
|
|
|
os.chdir(self._old_cwd) |
58
|
|
|
shutil.rmtree(self.gitdir) |
59
|
|
|
|
60
|
|
|
def test_check_prerequisites(self): |
61
|
|
|
_shutil_which = shutil.which |
62
|
|
|
try: |
63
|
|
|
shutil.which = lambda *args, **kwargs: None |
64
|
|
|
self.assertEqual(GitCommitBear.check_prerequisites(), |
65
|
|
|
"git is not installed.") |
66
|
|
|
|
67
|
|
|
shutil.which = lambda *args, **kwargs: "path/to/git" |
68
|
|
|
self.assertTrue(GitCommitBear.check_prerequisites()) |
69
|
|
|
finally: |
70
|
|
|
shutil.which = _shutil_which |
71
|
|
|
|
72
|
|
|
def test_good_commit_messages(self): |
73
|
|
|
self.git_commit("A little nice commit") |
74
|
|
|
self.assertEqual(self.run_uut(), []) |
75
|
|
|
self.assertTrue(self.msg_queue.empty()) |
76
|
|
|
|
77
|
|
|
self.git_commit("Commits messages that nearly exceeds default limit") |
78
|
|
|
self.assertEqual(self.run_uut(), []) |
79
|
|
|
self.assertTrue(self.msg_queue.empty()) |
80
|
|
|
|
81
|
|
|
self.git_commit("Commits message with a body\n\nBody\nAnother body") |
82
|
|
|
self.assertEqual(self.run_uut(), []) |
83
|
|
|
self.assertTrue(self.msg_queue.empty()) |
84
|
|
|
|
85
|
|
|
self.git_commit( |
86
|
|
|
"Commits message with a body\n\n" |
87
|
|
|
"nearly exceeding the default length of a body, but not quite. " |
88
|
|
|
"haaaaaaands") |
89
|
|
|
self.assertEqual(self.run_uut(), []) |
90
|
|
|
self.assertTrue(self.msg_queue.empty()) |
91
|
|
|
|
92
|
|
|
def test_git_failure(self): |
93
|
|
|
# In this case use a reference to a non-existing commit, so just try |
94
|
|
|
# to log all commits on a newly created repository. |
95
|
|
|
self.assertEqual(self.run_uut(), []) |
96
|
|
|
|
97
|
|
|
git_error = self.msg_queue.get().message |
98
|
|
|
self.assertEqual(git_error[:4], "git:") |
99
|
|
|
|
100
|
|
|
self.assertTrue(self.msg_queue.empty()) |
101
|
|
|
|
102
|
|
|
def test_empty_message(self): |
103
|
|
|
self.git_commit("") |
104
|
|
|
|
105
|
|
|
self.assertEqual(self.run_uut(), |
106
|
|
|
["HEAD commit has no message."]) |
107
|
|
|
self.assertTrue(self.msg_queue.empty()) |
108
|
|
|
|
109
|
|
|
self.assertEqual(self.run_uut(allow_empty_commit_message=True), |
110
|
|
|
[]) |
111
|
|
|
self.assertTrue(self.msg_queue.empty()) |
112
|
|
|
|
113
|
|
|
def test_shortlog_checks(self): |
114
|
|
|
self.git_commit("This is a shortlog") |
115
|
|
|
|
116
|
|
|
self.assertEqual(self.run_uut(), []) |
117
|
|
|
self.assertTrue(self.msg_queue.empty()) |
118
|
|
|
|
119
|
|
|
self.assertEqual(self.run_uut(shortlog_length=17), |
120
|
|
|
["Shortlog of HEAD commit is too long."]) |
121
|
|
|
self.assertTrue(self.msg_queue.empty()) |
122
|
|
|
|
123
|
|
|
self.git_commit("A shortlog that is too long is not good for history") |
124
|
|
|
self.assertEqual(self.run_uut(), |
125
|
|
|
["Shortlog of HEAD commit is too long."]) |
126
|
|
|
self.assertTrue(self.msg_queue.empty()) |
127
|
|
|
|
128
|
|
|
def test_body_checks(self): |
129
|
|
|
self.git_commit("Shortlog\n\nBody with details.") |
130
|
|
|
|
131
|
|
|
self.assertEqual(self.run_uut(), []) |
132
|
|
|
self.assertTrue(self.msg_queue.empty()) |
133
|
|
|
|
134
|
|
|
self.git_commit("Shortlog only") |
135
|
|
|
|
136
|
|
|
self.assertEqual(self.run_uut(), []) |
137
|
|
|
self.assertTrue(self.msg_queue.empty()) |
138
|
|
|
|
139
|
|
|
# Force a body. |
140
|
|
|
self.git_commit("Shortlog only ...") |
141
|
|
|
self.assertEqual(self.run_uut(force_body=True), |
142
|
|
|
["No commit message body at HEAD."]) |
143
|
|
|
self.assertTrue(self.msg_queue.empty()) |
144
|
|
|
|
145
|
|
|
# Miss a newline between shortlog and body. |
146
|
|
|
self.git_commit("Shortlog\nOops, body too early") |
147
|
|
|
self.assertEqual(self.run_uut(), |
148
|
|
|
["No newline between shortlog and body at HEAD."]) |
149
|
|
|
self.assertTrue(self.msg_queue.empty()) |
150
|
|
|
|
151
|
|
|
# And now too long lines. |
152
|
|
|
self.git_commit("Shortlog\n\n" |
153
|
|
|
"This line is ok.\n" |
154
|
|
|
"This line is by far too long (in this case).\n" |
155
|
|
|
"This one too, blablablablablablablablabla.") |
156
|
|
|
self.assertEqual(self.run_uut(body_line_length=41), |
157
|
|
|
["Body of HEAD commit contains too long lines."]) |
158
|
|
|
self.assertTrue(self.msg_queue.empty()) |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
if __name__ == '__main__': |
162
|
|
|
unittest.main(verbosity=2) |
163
|
|
|
|