-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
executable file
·196 lines (152 loc) · 8.14 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python3
#
# Copyright this project and it's contributors
# SPDX-License-Identifier: Apache-2.0
#
# encoding=utf8
import os
import git
import unittest
from unittest.mock import Mock
from contrib_check.org import Org
from contrib_check.repo import Repo
from contrib_check.commit import Commit
class TestCommit(unittest.TestCase):
@classmethod
def setUpClass(self):
self._mock_repo = Mock()
self._mock_commit_merge = Mock()
self._mock_commit_merge.parents = [1,2,3]
self._mock_commit = Mock()
self._mock_commit.parents = [1]
# test for not having a signoff
def testHasNoDCOSignOff(self):
commit = Commit(self._mock_commit,self._mock_repo)
commit.git_commit_object.message = "has no signoff"
self.assertFalse(commit.hasDCOSignOff(), "Commit message didn't have a signoff")
# test for having a signoff
def testHasDCOSignOff(self):
commit = Commit(self._mock_commit,self._mock_repo)
commit.git_commit_object.message = "has a signoff Signed-off-by: John Mertic <[email protected]>"
self.assertTrue(commit.hasDCOSignOff(), "Commit message had a signoff")
def testFoundPastDCOSignoff(self):
commit = Commit(self._mock_commit,self._mock_repo)
commit.git_commit_object.hexsha = '11ac960e1070eacc2fe92ac9a3d1753400e1fd4b'
commit.repo_object.past_signoffs = [
"I, personname hereby sign-off-by all of my past commits to this repo subject to the Developer Certificate of Origin (DCO), Version 1.1. In the past I have used emails: [[email protected]]\n\n11ac960e1070eacc2fe92ac9a3d1753400e1fd4b This is a commit".encode()
]
self.assertTrue(commit.hasDCOPastSignoff(), "Commit message had a past signoff")
def testFoundNoPastDCOSignoff(self):
commit = Commit(self._mock_commit,self._mock_repo)
commit.git_commit_object.hexsha = 'c1d322dfba0ed7a770d74074990ac51a9efedcd0'
commit.repo_object.past_signoffs = [
"I, personname hereby sign-off-by all of my past commits to this repo subject to the Developer Certificate of Origin (DCO), Version 1.1. In the past I have used emails: [[email protected]]\n\n11ac960e1070eacc2fe92ac9a3d1753400e1fd4b This is a commit".encode()
]
self.assertFalse(commit.hasDCOPastSignoff(), "Commit message had a past signoff")
def testDCOSignoffRequiredMergeCommit(self):
commit = Commit(self._mock_commit_merge,self._mock_repo)
self.assertFalse(commit.isDCOSignOffRequired(), "Merge commits don't require a DCO signoff")
def testDCOSignoffRequiredNormalCommit(self):
commit = Commit(self._mock_commit,self._mock_repo)
self.assertTrue(commit.isDCOSignOffRequired(), "All non-merge commits require a DCO signoff")
def testDCOSignoffCheckMergeCommit(self):
commit = Commit(self._mock_commit_merge,self._mock_repo)
commit.git_commit_object.message = "has no signoff"
self.assertTrue(commit.checkDCOSignoff(), "Commit message didn't have a signoff, but is a merge commit so that's ok")
def testDCOSignoffCheckNormalCommitNoSignoffPastSignoff(self):
commit = Commit(self._mock_commit_merge,self._mock_repo)
commit.git_commit_object.hexsha = '11ac960e1070eacc2fe92ac9a3d1753400e1fd4b'
commit.repo_object.past_signoffs = [
['dco-signoffs',"I, personname hereby sign-off-by all of my past commits to this repo subject to the Developer Certificate of Origin (DCO), Version 1.1. In the past I have used emails: [[email protected]]\n\n11ac960e1070eacc2fe92ac9a3d1753400e1fd4b This is a commit".encode() ]
]
commit.git_commit_object.message = "has no signoff"
self.assertTrue(commit.checkDCOSignoff(), "Commit message didn't have a signoff, but it has a past DCO signoff so that's ok")
class TestOrg(unittest.TestCase):
githubOrgRepos = [
type("gh_repo",(object,),{
"html_url": "https://github.com/testorg/repo1",
"name":"repo1",
"archived":False
}),
type("gh_repo",(object,),{
"html_url": "https://github.com/testorg/repo2",
"name":"repo2",
"archived":False
}),
type("gh_repo",(object,),{
"html_url": "https://github.com/testorg/repo3",
"name":"repo3",
"archived":True
}),
]
@classmethod
def tearDownClass(cls):
if os.path.exists("testorg-repo1.csv"):
os.remove("testorg-repo1.csv")
if os.path.exists("testorg-repo2.csv"):
os.remove("testorg-repo2.csv")
if os.path.exists("testorg-repo3.csv"):
os.remove("testorg-repo3.csv")
@unittest.mock.patch.dict(os.environ,{'GITHUB_TOKEN':'test123'})
def testInitNoLoadRepos(self):
org = Org("testorg",load_repos=False)
self.assertEqual(org.repos,[])
def testOrgTypeSetGithubNoTokenDefined(self):
names_to_remove = {"GITHUB_TOKEN"}
modified_environ = {
k: v for k, v in os.environ.items() if k not in names_to_remove
}
with unittest.mock.patch.dict(os.environ, modified_environ, clear=True):
self.assertRaisesRegex(Exception,'Github token',Org,'foo')
@unittest.mock.patch.dict(os.environ,{'GITHUB_TOKEN':'test123'})
@unittest.mock.patch.object(git.Repo,'clone_from')
def testLoadOrgRepos(self,mock_method):
with unittest.mock.patch.object(Org,'_getGithubReposForOrg',return_value=self.githubOrgRepos) as mock:
org = Org("testorg")
self.assertEqual(org.repos[0].name,"repo1")
self.assertEqual(org.repos[1].name,"repo2")
self.assertEqual(len(org.repos),2)
@unittest.mock.patch.dict(os.environ,{'GITHUB_TOKEN':'test123'})
@unittest.mock.patch.object(git.Repo,'clone_from')
def testLoadOrgReposIgnoreRepo(self,mock_method):
with unittest.mock.patch.object(Org,'_getGithubReposForOrg',return_value=self.githubOrgRepos) as mock:
org = Org("testorg",ignore_repos=['repo1'])
self.assertEqual(org.repos[0].name,"repo2")
self.assertEqual(len(org.repos),1)
@unittest.mock.patch.dict(os.environ,{'GITHUB_TOKEN':'test123'})
@unittest.mock.patch.object(git.Repo,'clone_from')
def testLoadOrgReposOnlyRepo(self,mock_method):
with unittest.mock.patch.object(Org,'_getGithubReposForOrg',return_value=self.githubOrgRepos) as mock:
org = Org("testorg",only_repos=['repo1'])
self.assertEqual(org.repos[0].name,"repo1")
self.assertEqual(len(org.repos),1)
@unittest.mock.patch.dict(os.environ,{'GITHUB_TOKEN':'test123'})
@unittest.mock.patch.object(git.Repo,'clone_from')
def testLoadOrgReposIncludeArchives(self,mock_method):
with unittest.mock.patch.object(Org,'_getGithubReposForOrg',return_value=self.githubOrgRepos) as mock:
org = Org("testorg",skip_archived=False)
self.assertEqual(org.repos[0].name,"repo1")
self.assertEqual(org.repos[1].name,"repo2")
self.assertEqual(org.repos[2].name,"repo3")
self.assertEqual(len(org.repos),3)
class TestRepo(unittest.TestCase):
@unittest.mock.patch.object(git.Repo,'clone_from')
def testInitGithub(self,mock_method):
repo = Repo("https://github.com/foo/bar")
self.assertEqual(repo.name,"bar")
self.assertEqual(repo.html_url,"https://github.com/foo/bar")
self.assertEqual(repo.csv_filename,"foo-bar.csv")
self.assertTrue(os.path.isfile("foo-bar.csv"))
if os.path.isfile("foo-bar.csv"):
os.remove("foo-bar.csv")
@unittest.mock.patch.object(git.Repo,'clone_from')
def testInitLocal(self,mock_method):
repo = Repo(".")
self.assertEqual(repo.name,os.path.basename(os.path.realpath(".")))
self.assertEqual(repo.html_url,"")
self.assertEqual(repo.csv_filename,repo.name+".csv")
self.assertTrue(os.path.isfile(repo.name+".csv"))
if os.path.isfile(repo.name+".csv"):
os.remove(repo.name+".csv")
if __name__ == '__main__':
unittest.main()