Skip to content

Commit b31e294

Browse files
committed
pytest: use assert
1 parent ea0290e commit b31e294

File tree

9 files changed

+56
-85
lines changed

9 files changed

+56
-85
lines changed

test/test_commit.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,10 @@ def test_read_commit(self):
6767
assert COMMIT_SHA == str(commit.id)
6868
parents = commit.parents
6969
assert 1 == len(parents)
70-
self.assertEqual('c2792cfa289ae6321ecf2cd5806c2194b0fd070c',
71-
str(parents[0].id))
70+
assert 'c2792cfa289ae6321ecf2cd5806c2194b0fd070c' == str(parents[0].id)
7271
assert commit.message_encoding is None
73-
self.assertEqual(('Second test data commit.\n\n'
74-
'This commit has some additional text.\n'),
75-
commit.message)
72+
assert commit.message == ('Second test data commit.\n\n'
73+
'This commit has some additional text.\n')
7674
commit_time = 1288481576
7775
assert commit_time == commit.commit_time
7876
assert commit.committer == Signature('Dave Borowitz', 'dborowitz@google.com', commit_time, -420)
@@ -99,8 +97,7 @@ def test_new_commit(self):
9997
commit = repo[sha]
10098

10199
assert GIT_OBJ_COMMIT == commit.type
102-
self.assertEqual('98286caaab3f1fde5bf52c8369b2b0423bad743b',
103-
commit.hex)
100+
assert '98286caaab3f1fde5bf52c8369b2b0423bad743b' == commit.hex
104101
assert commit.message_encoding is None
105102
assert message == commit.message
106103
assert 12346 == commit.commit_time

test/test_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def test_iterator(self):
175175
lst = {}
176176

177177
for entry in config:
178-
self.assertGreater(entry.level, -1)
178+
assert entry.level > -1
179179
lst[entry.name] = entry.value
180180

181181
assert 'core.bare' in lst

test/test_diff.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,13 @@ def get_context_for_lines(diff):
217217
return map(lambda x: x.origin, lines)
218218

219219
entries = [p.delta.new_file.path for p in diff]
220-
self.assertAll(lambda x: commit_a.tree[x], entries)
221-
self.assertAll(lambda x: '-' == x, get_context_for_lines(diff))
220+
assert all(commit_a.tree[x] for x in entries)
221+
assert all('-' == x for x in get_context_for_lines(diff))
222222

223223
diff_swaped = commit_a.tree.diff_to_tree(swap=True)
224224
entries = [p.delta.new_file.path for p in diff_swaped]
225-
self.assertAll(lambda x: commit_a.tree[x], entries)
226-
self.assertAll(lambda x: '+' == x, get_context_for_lines(diff_swaped))
225+
assert all(commit_a.tree[x] for x in entries)
226+
assert all('+' == x for x in get_context_for_lines(diff_swaped))
227227

228228
def test_diff_revparse(self):
229229
diff = self.repo.diff('HEAD', 'HEAD~6')
@@ -281,10 +281,9 @@ def test_diff_ids(self):
281281
commit_a = self.repo[COMMIT_SHA1_1]
282282
commit_b = self.repo[COMMIT_SHA1_2]
283283
patch = commit_a.tree.diff_to_tree(commit_b.tree)[0]
284-
self.assertEqual(patch.delta.old_file.id.hex,
285-
'7f129fd57e31e935c6d60a0c794efe4e6927664b')
286-
self.assertEqual(patch.delta.new_file.id.hex,
287-
'af431f20fc541ed6d5afede3e2dc7160f6f01f16')
284+
delta = patch.delta
285+
assert delta.old_file.id.hex == '7f129fd57e31e935c6d60a0c794efe4e6927664b'
286+
assert delta.new_file.id.hex == 'af431f20fc541ed6d5afede3e2dc7160f6f01f16'
288287

289288
def test_hunk_content(self):
290289
commit_a = self.repo[COMMIT_SHA1_1]
@@ -304,11 +303,11 @@ def test_find_similar(self):
304303
#~ --find-copies-harder during rename transformion...
305304
diff = commit_a.tree.diff_to_tree(commit_b.tree,
306305
GIT_DIFF_INCLUDE_UNMODIFIED)
307-
self.assertAll(lambda x: x.delta.status != GIT_DELTA_RENAMED, diff)
308-
self.assertAll(lambda x: x.delta.status_char() != 'R', diff)
306+
assert all(x.delta.status != GIT_DELTA_RENAMED for x in diff)
307+
assert all(x.delta.status_char() != 'R' for x in diff)
309308
diff.find_similar()
310-
self.assertAny(lambda x: x.delta.status == GIT_DELTA_RENAMED, diff)
311-
self.assertAny(lambda x: x.delta.status_char() == 'R', diff)
309+
assert any(x.delta.status == GIT_DELTA_RENAMED for x in diff)
310+
assert any(x.delta.status_char() == 'R' for x in diff)
312311

313312
def test_diff_stats(self):
314313
commit_a = self.repo[COMMIT_SHA1_1]

test/test_index.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ def test_mode(self):
167167

168168
def test_bare_index(self):
169169
index = pygit2.Index(os.path.join(self.repo.path, 'index'))
170-
self.assertEqual([x.hex for x in index],
171-
[x.hex for x in self.repo.index])
170+
assert [x.hex for x in index] == [x.hex for x in self.repo.index]
172171

173172
with pytest.raises(pygit2.GitError): index.add('bye.txt')
174173

@@ -210,7 +209,7 @@ def test_create_entry(self):
210209
class StandaloneIndexTest(utils.RepoTestCase):
211210

212211
def test_create_empty(self):
213-
index = Index()
212+
Index()
214213

215214
def test_create_empty_read_tree_as_string(self):
216215
index = Index()

test/test_refs.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,22 @@ def test_list_all_reference_objects(self):
4545

4646
refs = [(ref.name, ref.target.hex)
4747
for ref in repo.references.objects]
48-
self.assertEqual(sorted(refs),
49-
[('refs/heads/i18n',
50-
'5470a671a80ac3789f1a6a8cefbcf43ce7af0563'),
51-
('refs/heads/master',
52-
'2be5719152d4f82c7302b1c0932d8e5f0a4a0e98')])
48+
assert sorted(refs) == [
49+
('refs/heads/i18n', '5470a671a80ac3789f1a6a8cefbcf43ce7af0563'),
50+
('refs/heads/master', '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98')]
5351

5452
def test_list_all_references(self):
5553
repo = self.repo
5654

5755
# Without argument
58-
self.assertEqual(sorted(repo.references),
59-
['refs/heads/i18n', 'refs/heads/master'])
56+
assert sorted(repo.references) == ['refs/heads/i18n',
57+
'refs/heads/master']
6058

6159
# We add a symbolic reference
6260
repo.create_reference('refs/tags/version1', 'refs/heads/master')
63-
self.assertEqual(sorted(repo.references),
64-
['refs/heads/i18n', 'refs/heads/master',
65-
'refs/tags/version1'])
61+
assert sorted(repo.references) == ['refs/heads/i18n',
62+
'refs/heads/master',
63+
'refs/tags/version1']
6664

6765
def test_head(self):
6866
head = self.repo.head
@@ -236,27 +234,24 @@ def test_peel(self):
236234
class ReferencesTest(utils.RepoTestCase):
237235
def test_list_all_reference_objects(self):
238236
repo = self.repo
239-
240237
refs = [(ref.name, ref.target.hex)
241238
for ref in repo.listall_reference_objects()]
242-
self.assertEqual(sorted(refs),
243-
[('refs/heads/i18n',
244-
'5470a671a80ac3789f1a6a8cefbcf43ce7af0563'),
245-
('refs/heads/master',
246-
'2be5719152d4f82c7302b1c0932d8e5f0a4a0e98')])
239+
240+
assert sorted(refs) == [
241+
('refs/heads/i18n', '5470a671a80ac3789f1a6a8cefbcf43ce7af0563'),
242+
('refs/heads/master', '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98')]
247243

248244
def test_list_all_references(self):
249245
repo = self.repo
250246

251247
# Without argument
252-
self.assertEqual(sorted(repo.listall_references()),
253-
['refs/heads/i18n', 'refs/heads/master'])
248+
assert sorted(repo.listall_references()) == ['refs/heads/i18n', 'refs/heads/master']
254249

255250
# We add a symbolic reference
256251
repo.create_reference('refs/tags/version1', 'refs/heads/master')
257-
self.assertEqual(sorted(repo.listall_references()),
258-
['refs/heads/i18n', 'refs/heads/master',
259-
'refs/tags/version1'])
252+
assert sorted(repo.listall_references()) == ['refs/heads/i18n',
253+
'refs/heads/master',
254+
'refs/tags/version1']
260255

261256
def test_head(self):
262257
head = self.repo.head

test/test_remote.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,10 @@ def update_tips(self, name, old, new):
273273
# We do a fetch in order to establish the connection to the remote.
274274
# Prune operation requires an active connection.
275275
remote.fetch(prune=pygit2.GIT_FETCH_NO_PRUNE)
276-
self.assertIn('origin/i18n', self.clone_repo.branches)
276+
assert 'origin/i18n' in self.clone_repo.branches
277277
remote.prune(callbacks)
278-
self.assertEqual(pruned, ['refs/remotes/origin/i18n'])
279-
self.assertNotIn('origin/i18n', self.clone_repo.branches)
278+
assert pruned == ['refs/remotes/origin/i18n']
279+
assert 'origin/i18n' not in self.clone_repo.branches
280280

281281
class Utf8BranchTest(utils.Utf8BranchRepoTestCase):
282282
def test_fetch(self):
@@ -318,12 +318,12 @@ def test_push_when_up_to_date_succeeds(self):
318318

319319
def test_push_non_fast_forward_commits_to_remote_fails(self):
320320
tip = self.origin[self.origin.head.target]
321-
oid = self.origin.create_commit(
321+
self.origin.create_commit(
322322
'refs/heads/master', tip.author, tip.author, 'some commit',
323323
tip.tree.id, [tip.id]
324324
)
325325
tip = self.clone[self.clone.head.target]
326-
oid = self.clone.create_commit(
326+
self.clone.create_commit(
327327
'refs/heads/master', tip.author, tip.author, 'other commit',
328328
tip.tree.id, [tip.id]
329329
)

test/test_repository.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,8 @@ def test_lookup_commit(self):
148148
commit = self.repo[commit_sha]
149149
assert commit_sha == commit.hex
150150
assert GIT_OBJ_COMMIT == commit.type
151-
self.assertEqual(('Second test data commit.\n\n'
152-
'This commit has some additional text.\n'),
153-
commit.message)
151+
assert commit.message == ('Second test data commit.\n\n'
152+
'This commit has some additional text.\n')
154153

155154
def test_lookup_commit_prefix(self):
156155
commit_sha = '5fe808e8953c12735680c257f56600cb0de44b10'
@@ -232,12 +231,12 @@ def create_conflict_file(repo, branch, content):
232231

233232
(a, t, o) = index.conflicts['conflict']
234233
diff = self.repo.merge_file_from_index(a, t, o)
235-
self.assertEqual(diff, '''<<<<<<< conflict
234+
assert diff == '''<<<<<<< conflict
236235
ASCII - abc
237236
=======
238237
Unicode - äüö
239238
>>>>>>> conflict
240-
''')
239+
'''
241240

242241
class RepositoryTest_II(utils.RepoTestCase):
243242

@@ -337,8 +336,7 @@ def test_merge_base(self):
337336
commit = self.repo.merge_base(
338337
'5ebeeebb320790caf276b9fc8b24546d63316533',
339338
'4ec4389a8068641da2d6578db0419484972284c8')
340-
self.assertEqual(commit.hex,
341-
'acecd5ea2924a4b900e7e149496e1f4b57976e51')
339+
assert commit.hex == 'acecd5ea2924a4b900e7e149496e1f4b57976e51'
342340

343341
# Create a commit without any merge base to any other
344342
sig = pygit2.Signature("me", "me@example.com")
@@ -362,13 +360,15 @@ def test_descendent_of(self):
362360
'5ebeeebb320790caf276b9fc8b24546d63316533')
363361

364362
def test_ahead_behind(self):
365-
ahead, behind = self.repo.ahead_behind('5ebeeebb320790caf276b9fc8b24546d63316533',
366-
'4ec4389a8068641da2d6578db0419484972284c8')
363+
ahead, behind = self.repo.ahead_behind(
364+
'5ebeeebb320790caf276b9fc8b24546d63316533',
365+
'4ec4389a8068641da2d6578db0419484972284c8')
367366
assert 1 == ahead
368367
assert 2 == behind
369368

370-
ahead, behind = self.repo.ahead_behind('4ec4389a8068641da2d6578db0419484972284c8',
371-
'5ebeeebb320790caf276b9fc8b24546d63316533')
369+
ahead, behind = self.repo.ahead_behind(
370+
'4ec4389a8068641da2d6578db0419484972284c8',
371+
'5ebeeebb320790caf276b9fc8b24546d63316533')
372372
assert 2 == ahead
373373
assert 1 == behind
374374

test/test_signature.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ def test_default(self):
4545
assert encoding == 'utf-8'
4646
assert signature.name == signature.raw_name.decode(encoding)
4747
assert signature.name.encode(encoding) == signature.raw_name
48-
self.assertEqual(signature.email,
49-
signature.raw_email.decode(encoding))
50-
self.assertEqual(signature.email.encode(encoding),
51-
signature.raw_email)
48+
assert signature.email == signature.raw_email.decode(encoding)
49+
assert signature.email.encode(encoding) == signature.raw_email
5250

5351
def test_ascii(self):
5452
with pytest.raises(UnicodeEncodeError):
@@ -61,10 +59,8 @@ def test_latin1(self):
6159
assert encoding == signature._encoding
6260
assert signature.name == signature.raw_name.decode(encoding)
6361
assert signature.name.encode(encoding) == signature.raw_name
64-
self.assertEqual(signature.email,
65-
signature.raw_email.decode(encoding))
66-
self.assertEqual(signature.email.encode(encoding),
67-
signature.raw_email)
62+
assert signature.email == signature.raw_email.decode(encoding)
63+
assert signature.email.encode(encoding) == signature.raw_email
6864

6965
def test_now(self):
7066
encoding = 'utf-8'
@@ -73,8 +69,6 @@ def test_now(self):
7369
assert encoding == signature._encoding
7470
assert signature.name == signature.raw_name.decode(encoding)
7571
assert signature.name.encode(encoding) == signature.raw_name
76-
self.assertEqual(signature.email,
77-
signature.raw_email.decode(encoding))
78-
self.assertEqual(signature.email.encode(encoding),
79-
signature.raw_email)
72+
assert signature.email == signature.raw_email.decode(encoding)
73+
assert signature.email.encode(encoding) == signature.raw_email
8074
assert abs(signature.time - time.time()) < 5

test/utils.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import shutil
3434
import socket
3535
import stat
36-
import sys
3736
import tarfile
3837
import tempfile
3938
import unittest
@@ -115,18 +114,6 @@ def tearDown(self):
115114
gc.collect()
116115
rmtree(self._temp_dir)
117116

118-
def assertRaisesAssign(self, exc_class, instance, name, value):
119-
try:
120-
setattr(instance, name, value)
121-
except:
122-
assert exc_class == sys.exc_info()[0]
123-
124-
def assertAll(self, func, entries):
125-
return self.assertTrue(all(func(x) for x in entries))
126-
127-
def assertAny(self, func, entries):
128-
return self.assertTrue(any(func(x) for x in entries))
129-
130117
def assertRaisesWithArg(self, exc_class, arg, func, *args, **kwargs):
131118
with pytest.raises(exc_class) as excinfo:
132119
func(*args, **kwargs)

0 commit comments

Comments
 (0)