Skip to content

Commit 48441a9

Browse files
committed
Lint test/ (not just git/), fix warnings and a bug
This expands flake8 linting to include the test suite, and fixes the resulting warnings. Four code changes are especially notable: - Unit tests from which documentation is automatically generated contain a few occurrences of "# @NoEffect". These suppressions are extended to "# noqa: B015 # @NoEffect" so the corresponding suppression is also applied for flake8. This is significant because it actually changes what appears in the code examples in the generated documentation. However, since the "@NoEffect" annotation was considered acceptable, this may be okay too. The resulting examples did not become excessively long. - Expressions like "[c for c in commits_for_file_generator]" appear in some unit tests from which documentation is automatically generated. The simpler form "list(commits_for_file_generator)" can replace them. This changes the examples in the documentation, but for the better, since that form is simpler (and also a better practice in general, thus a better thing to show readers). So I made those substitutions. - In test_repo.TestRepo.test_git_work_tree_env, the code intended to unpatch environment variables after the test was ineffective, instead causing os.environ to refer to an ordinary dict object that does not affect environment variables when written to. This uses unittest.mock.patch.dict instead, so the variables are unpatched and subsequent writes to environment variables in the test process are effective. - In test_submodule.TestSubmodule._do_base_tests, the expression statement "csm.module().head.ref.tracking_branch() is not None" appeared to be intended as an assertion, in spite of having been annoated @NoEffect. This is in light of the context and because, if the goal were only to exercise the function call, the "is not None" part would be superfluous. So I made it an assertion.
1 parent c1ec9cb commit 48441a9

9 files changed

+32
-36
lines changed

Diff for: .flake8

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ignore = E265,E266,E731,E704,
2626
D,
2727
RST, RST3
2828

29-
exclude = .tox,.venv,build,dist,doc,git/ext/,test
29+
exclude = .tox,.venv,build,dist,doc,git/ext/
3030

3131
rst-roles = # for flake8-RST-docstrings
3232
attr,class,func,meth,mod,obj,ref,term,var # used by sphinx

Diff for: .pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ repos:
99
flake8-comprehensions==3.14.0,
1010
flake8-typing-imports==1.14.0,
1111
]
12-
exclude: ^doc|^git/ext/|^test/
12+
exclude: ^doc|^git/ext/
1313

1414
- repo: https://github.com/pre-commit/pre-commit-hooks
1515
rev: v4.4.0

Diff for: test/test_commit.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def __init__(self, *args, **kwargs):
277277
super(Child, self).__init__(*args, **kwargs)
278278

279279
child_commits = list(Child.iter_items(self.rorepo, "master", paths=("CHANGES", "AUTHORS")))
280-
assert type(child_commits[0]) == Child
280+
assert type(child_commits[0]) is Child
281281

282282
def test_iter_items(self):
283283
# pretty not allowed
@@ -525,12 +525,12 @@ def test_trailers(self):
525525

526526
# check that trailer stays empty for multiple msg combinations
527527
msgs = [
528-
f"Subject\n",
529-
f"Subject\n\nBody with some\nText\n",
530-
f"Subject\n\nBody with\nText\n\nContinuation but\n doesn't contain colon\n",
531-
f"Subject\n\nBody with\nText\n\nContinuation but\n only contains one :\n",
532-
f"Subject\n\nBody with\nText\n\nKey: Value\nLine without colon\n",
533-
f"Subject\n\nBody with\nText\n\nLine without colon\nKey: Value\n",
528+
"Subject\n",
529+
"Subject\n\nBody with some\nText\n",
530+
"Subject\n\nBody with\nText\n\nContinuation but\n doesn't contain colon\n",
531+
"Subject\n\nBody with\nText\n\nContinuation but\n only contains one :\n",
532+
"Subject\n\nBody with\nText\n\nKey: Value\nLine without colon\n",
533+
"Subject\n\nBody with\nText\n\nLine without colon\nKey: Value\n",
534534
]
535535

536536
for msg in msgs:

Diff for: test/test_diff.py

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import ddt
88
import shutil
99
import tempfile
10-
import unittest
1110
from git import (
1211
Repo,
1312
GitCommandError,

Diff for: test/test_docs.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,9 @@ def test_references_and_objects(self, rw_dir):
263263
# [8-test_references_and_objects]
264264
hc = repo.head.commit
265265
hct = hc.tree
266-
hc != hct # @NoEffect
267-
hc != repo.tags[0] # @NoEffect
268-
hc == repo.head.reference.commit # @NoEffect
266+
hc != hct # noqa: B015 # @NoEffect
267+
hc != repo.tags[0] # noqa: B015 # @NoEffect
268+
hc == repo.head.reference.commit # noqa: B015 # @NoEffect
269269
# ![8-test_references_and_objects]
270270

271271
# [9-test_references_and_objects]
@@ -369,7 +369,7 @@ def test_references_and_objects(self, rw_dir):
369369
# The index contains all blobs in a flat list
370370
assert len(list(index.iter_blobs())) == len([o for o in repo.head.commit.tree.traverse() if o.type == "blob"])
371371
# Access blob objects
372-
for (_path, _stage), entry in index.entries.items():
372+
for (_path, _stage), _entry in index.entries.items():
373373
pass
374374
new_file_path = os.path.join(repo.working_tree_dir, "new-file-name")
375375
open(new_file_path, "w").close()

Diff for: test/test_quick_doc.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import pytest
2-
3-
41
from test.lib import TestBase
52
from test.lib.helper import with_rw_directory
63

@@ -25,10 +22,11 @@ def test_init_repo_object(self, path_to_dir):
2522
repo = Repo(path_to_dir)
2623
# ![2-test_init_repo_object]
2724

25+
del repo # Avoids "assigned to but never used" warning. Doesn't go in the docs.
26+
2827
@with_rw_directory
2928
def test_cloned_repo_object(self, local_dir):
3029
from git import Repo
31-
import git
3230

3331
# code to clone from url
3432
# [1-test_cloned_repo_object]
@@ -72,7 +70,7 @@ def test_cloned_repo_object(self, local_dir):
7270

7371
# [6-test_cloned_repo_object]
7472
commits_for_file_generator = repo.iter_commits(all=True, max_count=10, paths=update_file)
75-
commits_for_file = [c for c in commits_for_file_generator]
73+
commits_for_file = list(commits_for_file_generator)
7674
commits_for_file
7775

7876
# Outputs: [<git.Commit "SHA1-HEX_HASH-2">,
@@ -136,7 +134,7 @@ def test_cloned_repo_object(self, local_dir):
136134

137135
# Compare commit to commit
138136
# [11.3-test_cloned_repo_object]
139-
first_commit = [c for c in repo.iter_commits(all=True)][-1]
137+
first_commit = list(repo.iter_commits(all=True))[-1]
140138
diffs = repo.head.commit.diff(first_commit)
141139
for d in diffs:
142140
print(d.a_path)
@@ -154,7 +152,7 @@ def test_cloned_repo_object(self, local_dir):
154152

155153
# Previous commit tree
156154
# [13-test_cloned_repo_object]
157-
prev_commits = [c for c in repo.iter_commits(all=True, max_count=10)] # last 10 commits from all branches
155+
prev_commits = list(repo.iter_commits(all=True, max_count=10)) # last 10 commits from all branches
158156
tree = prev_commits[0].tree
159157
# ![13-test_cloned_repo_object]
160158

@@ -210,7 +208,7 @@ def print_files_from_git(root, level=0):
210208

211209
# print previous tree
212210
# [18.1-test_cloned_repo_object]
213-
commits_for_file = [c for c in repo.iter_commits(all=True, paths=print_file)]
211+
commits_for_file = list(repo.iter_commits(all=True, paths=print_file))
214212
tree = commits_for_file[-1].tree # gets the first commit tree
215213
blob = tree[print_file]
216214

Diff for: test/test_remote.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def _do_test_push_result(self, results, remote):
160160
# END error checking
161161
# END for each info
162162

163-
if any([info.flags & info.ERROR for info in results]):
163+
if any(info.flags & info.ERROR for info in results):
164164
self.assertRaises(GitCommandError, results.raise_if_error)
165165
else:
166166
# No errors, so this should do nothing

Diff for: test/test_repo.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ def test_clone_from_with_path_contains_unicode(self):
252252

253253
@with_rw_directory
254254
@skip(
255-
"the referenced repository was removed, and one needs to setup a new password controlled repo under the orgs control"
255+
"""The referenced repository was removed, and one needs to set up a new
256+
password controlled repo under the org's control."""
256257
)
257258
def test_leaking_password_in_clone_logs(self, rw_dir):
258259
password = "fakepassword1234"
@@ -758,9 +759,9 @@ def test_blame_complex_revision(self, git):
758759

759760
@mock.patch.object(Git, "_call_process")
760761
def test_blame_accepts_rev_opts(self, git):
761-
res = self.rorepo.blame("HEAD", "README.md", rev_opts=["-M", "-C", "-C"])
762762
expected_args = ["blame", "HEAD", "-M", "-C", "-C", "--", "README.md"]
763763
boilerplate_kwargs = {"p": True, "stdout_as_string": False}
764+
self.rorepo.blame("HEAD", "README.md", rev_opts=["-M", "-C", "-C"])
764765
git.assert_called_once_with(*expected_args, **boilerplate_kwargs)
765766

766767
@skipIf(
@@ -971,7 +972,7 @@ def _assert_rev_parse(self, name):
971972
# history with number
972973
ni = 11
973974
history = [obj.parents[0]]
974-
for pn in range(ni):
975+
for _ in range(ni):
975976
history.append(history[-1].parents[0])
976977
# END get given amount of commits
977978

@@ -1329,6 +1330,7 @@ def test_git_work_tree_env(self, rw_dir):
13291330
# move .git directory to a subdirectory
13301331
# set GIT_DIR and GIT_WORK_TREE appropriately
13311332
# check that repo.working_tree_dir == rw_dir
1333+
13321334
self.rorepo.clone(join_path_native(rw_dir, "master_repo"))
13331335

13341336
repo_dir = join_path_native(rw_dir, "master_repo")
@@ -1338,16 +1340,12 @@ def test_git_work_tree_env(self, rw_dir):
13381340
os.mkdir(new_subdir)
13391341
os.rename(old_git_dir, new_git_dir)
13401342

1341-
oldenv = os.environ.copy()
1342-
os.environ["GIT_DIR"] = new_git_dir
1343-
os.environ["GIT_WORK_TREE"] = repo_dir
1343+
to_patch = {"GIT_DIR": new_git_dir, "GIT_WORK_TREE": repo_dir}
13441344

1345-
try:
1345+
with mock.patch.dict(os.environ, to_patch):
13461346
r = Repo()
13471347
self.assertEqual(r.working_tree_dir, repo_dir)
13481348
self.assertEqual(r.working_dir, repo_dir)
1349-
finally:
1350-
os.environ = oldenv
13511349

13521350
@with_rw_directory
13531351
def test_rebasing(self, rw_dir):

Diff for: test/test_submodule.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def _do_base_tests(self, rwrepo):
111111

112112
# force it to reread its information
113113
del smold._url
114-
smold.url == sm.url # @NoEffect
114+
smold.url == sm.url # noqa: B015 # @NoEffect
115115

116116
# test config_reader/writer methods
117117
sm.config_reader()
@@ -248,7 +248,7 @@ def _do_base_tests(self, rwrepo):
248248
assert csm.module_exists()
249249

250250
# tracking branch once again
251-
csm.module().head.ref.tracking_branch() is not None # @NoEffect
251+
assert csm.module().head.ref.tracking_branch() is not None
252252

253253
# this flushed in a sub-submodule
254254
assert len(list(rwrepo.iter_submodules())) == 2
@@ -480,8 +480,9 @@ def test_base_bare(self, rwrepo):
480480
File "C:\\projects\\gitpython\\git\\cmd.py", line 559, in execute
481481
raise GitCommandNotFound(command, err)
482482
git.exc.GitCommandNotFound: Cmd('git') not found due to: OSError('[WinError 6] The handle is invalid')
483-
cmdline: git clone -n --shared -v C:\\projects\\gitpython\\.git Users\\appveyor\\AppData\\Local\\Temp\\1\\tmplyp6kr_rnon_bare_test_root_module""",
484-
) # noqa E501
483+
cmdline: git clone -n --shared -v C:\\projects\\gitpython\\.git Users\\appveyor\\AppData\\Local\\Temp\\1\\tmplyp6kr_rnon_bare_test_root_module
484+
""", # noqa E501
485+
)
485486
@with_rw_repo(k_subm_current, bare=False)
486487
def test_root_module(self, rwrepo):
487488
# Can query everything without problems

0 commit comments

Comments
 (0)