-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrepositoryLibTest.py
More file actions
307 lines (239 loc) · 10.3 KB
/
repositoryLibTest.py
File metadata and controls
307 lines (239 loc) · 10.3 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/python
#
# ----------------------------------------------------------------------------------------------------
# DESCRIPTION
# ----------------------------------------------------------------------------------------------------
## @file repositoryLibTest.py [ FILE ] - Configuration module.
## @package repositoryLibTest [ FILE ] - Configuration module.
#
# ----------------------------------------------------------------------------------------------------
# IMPORT
# ----------------------------------------------------------------------------------------------------
#
import brCore.fileSystemLib
import brGit.repositoryLib
import mock
import py.path
import pygit2
import pytest
import random
#
# ----------------------------------------------------------------------------------------------------
# CODE
# ----------------------------------------------------------------------------------------------------
#
REMOTE_NAME = 'origin'
REMOTE_URL = 'git://github.com/libgit2/pygit2.git'
# #
# # ----------------------------------------------------------------------------------------------------
# # FIXTURES
# # ----------------------------------------------------------------------------------------------------
# #
#
## @brief Fixture of a repository instance.
#
# @param tmpdir [ str | None ] - Temporary path to repository.
#
# @retval Repository - Repository object.
#
@pytest.fixture
def tmpRepo(tmpdir):
repoDir = tmpdir.join('tmpRepo').ensure(dir=True)
repo = brGit.repositoryLib.Repository.createRepository(repoDir.strpath)
repo.createCommit('Initial commit.')
return repo
#
# ----------------------------------------------------------------------------------------------------
# TESTS
# ----------------------------------------------------------------------------------------------------
#
#
## @brief [ CLASS ] - Tests for the Repository class.
class TestRepository:
#
## @brief Tests for the Repository class initialization.
#
# @retval None - This function does not return anything.
def testInit(self):
with pytest.raises(ValueError):
brGit.repositoryLib.Repository(None)
#
## @brief Test for the setRepository function.
#
# @param tmpRepo [ brGit.repositoryLib.Repository ] - test repository.
#
# @retval None - This function does not return anything.
def testSetRepository(self, tmpRepo):
with pytest.raises(ValueError):
tmpRepo.setRepository('/tmp/nopath/norepo')
#
## @brief Test branch related functions.
#
# @param tmpdir [ py.path.local ] - temp dir for test.
# @param tmpRepo [ brGit.repositoryLib.Repository ] - test repository.
#
# @retval None - This function does not return anything.
def testBranch(self, tmpdir, tmpRepo):
testBranch = 'master'
with mock.patch('pygit2.Repository.create_branch') as mockCreateBranch:
br1 = tmpRepo.createBranch('branch1', testBranch)
mockCreateBranch.assert_called_once()
mockCreateBranch.reset_mock()
br2 = tmpRepo.createBranch('branch2')
mockCreateBranch.assert_called_once()
pytest.raises(ValueError, "tmpRepo.createBranch('fakeBranch', sourceBranch='source')")
with mock.patch('pygit2.Repository.listall_branches') as mockListBranch:
tmpRepo.listBranches()
mockListBranch.assert_called_once()
with mock.patch('pygit2.Repository.checkout') as mockCheckout:
tmpRepo.checkoutBranch('master')
mockCheckout.assert_called_once()
pytest.raises(AttributeError, "tmpRepo.checkoutBranch('fakeBranch')")
pytest.raises(NameError, "tmpRepo.deleteBranch('fakeBranch')")
pytest.raises(AttributeError, "tmpRepo.checkoutBranch('fakeBranch')")
newRepo = brGit.repositoryLib.Repository.createRepository(tmpdir.join('newRepo').ensure(dir=True).strpath)
assert newRepo.currentBranch() is None, 'new repository should not have a branch'
#
## @brief Test stage and ustage a file.
#
# @param tmpRepo [ brGit.repositoryLib.Repository ] - Repository.
#
# @retval None - This function does not return anything.
def testStaging(self, tmpRepo):
filePath = 'test.py'
with open(brCore.fileSystemLib.Directory.join(tmpRepo.workDir(),filePath), 'w+') as f:
f.write("number: "+ str(random.uniform(1, 5)))
f.close()
with mock.patch('pygit2.Index.add') as mockAdd:
tmpRepo.add(filePath)
mockAdd.assert_called_once()
with mock.patch('pygit2.Index.remove') as mockRemove:
tmpRepo.unstage(filePath)
mockRemove.assert_called_once()
with mock.patch('pygit2.Index.add_all') as mockAddAll:
tmpRepo.addAll([filePath])
mockAdd.assert_called_once()
#
## @brief Test if it's a repository.
#
# @param tmpRepo [ brGit.repositoryLib.Repository ] - Repsoitory.
#
# @retval None - This function does not return anything.
def testIsRepo(self, tmpRepo):
with mock.patch('pygit2.Repository') as mockTestRepo:
tmpRepo.isRepository(tmpRepo.workDir())
mockTestRepo.assert_called_once()
assert tmpRepo.isRepository('fake/path') == False, 'fake path should not be a repository'
#
## @brief Test repository path.
#
# @param tmpRepo [ brGit.repositoryLib.Repository ] - Repsoitory.
#
# @retval None - This function does not return anything.
def testFindGitRepo(self, tmpRepo):
with mock.patch('pygit2.discover_repository') as mockFindRepo:
tmpRepo.findGitRepo(tmpRepo.workDir())
mockFindRepo.assert_called_once()
pytest.raises(ValueError, 'tmpRepo.findGitRepo(None)')
#
## @brief Test if status is correctly parsed.
#
# @param tmpRepo [ brGit.repositoryLib.Repository ] - Repsoitory.
#
# @retval None - This function does not return anything.
def testStatus(self, tmpRepo):
_status = pygit2.GIT_STATUS_CONFLICTED | pygit2.GIT_STATUS_IGNORED | pygit2.GIT_STATUS_INDEX_DELETED | \
pygit2.GIT_STATUS_INDEX_MODIFIED | pygit2.GIT_STATUS_INDEX_NEW | pygit2.GIT_STATUS_WT_DELETED | \
pygit2.GIT_STATUS_WT_MODIFIED | pygit2.GIT_STATUS_WT_NEW
_stList = tmpRepo.gitStatus(_status)
repoPath = py.path.local(tmpRepo.path())
repoPath.join('testfile').ensure(file=True)
repoPath.join('.gitignore').ensure(file=True).write('*.ignore')
repoPath.join('test.ignore').ensure(file=True)
with mock.patch.object(tmpRepo, 'status', wraps=tmpRepo.status) as mockStatus:
tmpRepo.displayStatus()
mockStatus.assert_called_once()
assert len(_stList) == 8, 'status should be parsed correctly'
_status = pygit2.GIT_STATUS_CURRENT
_stList = tmpRepo.gitStatus(_status)
assert _stList[0] == "Current", 'status should be parsed correctly'
#
## @brief Test if commit log is empty.
#
# @param tmpRepo [ brGit.repositoryLib.Repository ] - Repsoitory.
#
# @retval None - This function does not return anything.
def testCommitLog(self, tmpRepo):
with mock.patch('pygit2.Repository.walk') as mockCommitLog:
log = tmpRepo.commitLog()
mockCommitLog.assert_called()
#
## @brief Test commit.
#
# @param tmpRepo [ brGit.repositoryLib.Repository ] - Repsoitory.
#
# @retval None - This function does not return anything.
def testCommit(self, tmpRepo):
pytest.raises(ValueError, 'tmpRepo.createCommit(None)')
with mock.patch('pygit2.Repository.create_commit') as mockCommit:
tmpRepo.createCommit('test commit')
mockCommit.assert_called_once()
#
## @brief Test Remote.
#
# @param tmpRepo [ brGit.repositoryLib.Repository ] - Repsoitory.
#
# @retval None - This function does not return anything.
def testRemote(self, tmpRepo):
tmpRepo.createRemote(REMOTE_NAME, REMOTE_URL)
with mock.patch('pygit2.Remote.fetch') as mockFetch:
tmpRepo.fetch()
mockFetch.assert_called()
mockFetch.reset_mock()
tmpRepo.fetch(REMOTE_NAME)
mockFetch.assert_called()
tmpRepo.pull()
#
## @brief Test merge.
#
# @param tmpRepo [ brGit.repositoryLib.Repository ] - Repsoitory.
#
# @retval None - This function does not return anything.
def testMerge(self, tmpRepo):
headID = tmpRepo.head().target
pytest.raises(ValueError, 'tmpRepo.merge(headID)')
#
## @brief Test delete a repository.
#
# @param tmpRepo [ brGit.repositoryLib.Repository ] - Repsoitory.
#
# @retval None - This function does not return anything.
def testDeleteRepo(self, tmpRepo):
pytest.raises(NameError, "tmpRepo.deleteRepository('fake/path')")
#
## @brief Test for the getSSHConfig function.
#
# @param tmpdir [ py.path.local ] - temporary directory for the test.
#
# @retval None - This function does not return anything.
def testGetSSHConfig(self, tmpdir):
hostname = 'myhostname.com'
getSSHConfig = brGit.repositoryLib.Repository.AuthCallback.getSSHConfig
with mock.patch('os.path.expanduser', side_effect=lambda x: x.replace('~', tmpdir.strpath)):
config = getSSHConfig(url)
assert isinstance(config, dict), 'missing config should still give dict'
configFile = tmpdir.join('.ssh').join('config').ensure(file=True)
config = getSSHConfig(url)
assert isinstance(config, dict), 'empty config should still give dict'
configFile.write('\n'.join([
'host ' + hostname,
'\tIdentityFile ~/.ssh/id_rsa'
]))
config = getSSHConfig(url)
assert isinstance(config, dict), 'valid config should give dict'
assert config.get('identityfile'), 'should give lowercase attributes'
config2 = getSSHConfig('ssh://' + url)
assert config == config2, 'should give same dict with and without url scheme'
with pytest.raises(ValueError, message='invalid url should raise'):
getSSHConfig('/invalid/url.value@')