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