Skip to content

Commit

Permalink
Merge pull request gitpython-developers#1668 from EliahKagan/black
Browse files Browse the repository at this point in the history
Format tests with black and auto-exclude untracked paths
  • Loading branch information
Byron authored Sep 20, 2023
2 parents 1256b16 + 15c736d commit a5a6464
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 68 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ mypy -p git
For automatic code formatting, run:

```bash
black git
black .
```

Configuration for flake8 is in the `./.flake8` file.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ omit = ["*/git/ext/*"]
[tool.black]
line-length = 120
target-version = ['py37']
exclude = "git/ext/gitdb"
extend-exclude = "git/ext/gitdb"
1 change: 0 additions & 1 deletion test/performance/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@


class TestObjDBPerformance(TestBigRepoR):

large_data_size_bytes = 1000 * 1000 * 10 # some MiB should do it
moderate_data_size_bytes = 1000 * 1000 * 1 # just 1 MiB

Expand Down
17 changes: 8 additions & 9 deletions test/test_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def assert_commit_serialization(self, rwrepo, commit_id, print_performance_info=

class TestCommit(TestCommitSerialization):
def test_bake(self):

commit = self.rorepo.commit("2454ae89983a4496a445ce347d7a41c0bb0ea7ae")
# commits have no dict
self.assertRaises(AttributeError, setattr, commit, "someattr", 1)
Expand Down Expand Up @@ -170,15 +169,15 @@ def test_renames(self):

def check_entries(path, changes):
expected = {
".github/workflows/Future.yml" : {
'insertions': 57,
'deletions': 0,
'lines': 57
".github/workflows/Future.yml": {
"insertions": 57,
"deletions": 0,
"lines": 57,
},
".github/workflows/test_pytest.yml" : {
'insertions': 0,
'deletions': 55,
'lines': 55
".github/workflows/test_pytest.yml": {
"insertions": 0,
"deletions": 55,
"lines": 55,
},
}
assert path in expected
Expand Down
28 changes: 14 additions & 14 deletions test/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def test_rename_override(self, rw_dir):
# create and commit file_a.txt
repo = Repo.init(rw_dir)
file_a = osp.join(rw_dir, "file_a.txt")
with open(file_a, "w", encoding='utf-8') as outfile:
with open(file_a, "w", encoding="utf-8") as outfile:
outfile.write("hello world\n")
repo.git.add(Git.polish_url(file_a))
repo.git.commit(message="Added file_a.txt")
Expand All @@ -429,21 +429,21 @@ def test_rename_override(self, rw_dir):

# create and commit file_b.txt with similarity index of 52
file_b = osp.join(rw_dir, "file_b.txt")
with open(file_b, "w", encoding='utf-8') as outfile:
with open(file_b, "w", encoding="utf-8") as outfile:
outfile.write("hello world\nhello world")
repo.git.add(Git.polish_url(file_b))
repo.git.commit(message="Removed file_a.txt. Added file_b.txt")

commit_a = repo.commit('HEAD')
commit_b = repo.commit('HEAD~1')
commit_a = repo.commit("HEAD")
commit_b = repo.commit("HEAD~1")

# check default diff command with renamed files enabled
diffs = commit_b.diff(commit_a)
self.assertEqual(1, len(diffs))
diff = diffs[0]
self.assertEqual(True, diff.renamed_file)
self.assertEqual('file_a.txt', diff.rename_from)
self.assertEqual('file_b.txt', diff.rename_to)
self.assertEqual("file_a.txt", diff.rename_from)
self.assertEqual("file_b.txt", diff.rename_to)

# check diff with rename files disabled
diffs = commit_b.diff(commit_a, no_renames=True)
Expand All @@ -452,31 +452,31 @@ def test_rename_override(self, rw_dir):
# check fileA.txt deleted
diff = diffs[0]
self.assertEqual(True, diff.deleted_file)
self.assertEqual('file_a.txt', diff.a_path)
self.assertEqual("file_a.txt", diff.a_path)

# check fileB.txt added
diff = diffs[1]
self.assertEqual(True, diff.new_file)
self.assertEqual('file_b.txt', diff.a_path)
self.assertEqual("file_b.txt", diff.a_path)

# check diff with high similarity index
diffs = commit_b.diff(commit_a, split_single_char_options=False, M='75%')
diffs = commit_b.diff(commit_a, split_single_char_options=False, M="75%")
self.assertEqual(2, len(diffs))

# check fileA.txt deleted
diff = diffs[0]
self.assertEqual(True, diff.deleted_file)
self.assertEqual('file_a.txt', diff.a_path)
self.assertEqual("file_a.txt", diff.a_path)

# check fileB.txt added
diff = diffs[1]
self.assertEqual(True, diff.new_file)
self.assertEqual('file_b.txt', diff.a_path)
self.assertEqual("file_b.txt", diff.a_path)

# check diff with low similarity index
diffs = commit_b.diff(commit_a, split_single_char_options=False, M='40%')
diffs = commit_b.diff(commit_a, split_single_char_options=False, M="40%")
self.assertEqual(1, len(diffs))
diff = diffs[0]
self.assertEqual(True, diff.renamed_file)
self.assertEqual('file_a.txt', diff.rename_from)
self.assertEqual('file_b.txt', diff.rename_to)
self.assertEqual("file_a.txt", diff.rename_from)
self.assertEqual("file_b.txt", diff.rename_to)
2 changes: 1 addition & 1 deletion test/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ def test_references_and_objects(self, rw_dir):
@pytest.mark.xfail(
sys.platform == "cygwin",
reason="Cygwin GitPython can't find SHA for submodule",
raises=ValueError
raises=ValueError,
)
def test_submodules(self):
# [1-test_submodules]
Expand Down
2 changes: 1 addition & 1 deletion test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ def test_commit_msg_hook_fail(self, rw_repo):
else:
raise AssertionError("Should have caught a HookExecutionError")

@with_rw_repo('HEAD')
@with_rw_repo("HEAD")
def test_index_add_pathlike(self, rw_repo):
git_dir = Path(rw_repo.git_dir)

Expand Down
27 changes: 12 additions & 15 deletions test/test_quick_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@ def tearDown(self):

@with_rw_directory
def test_init_repo_object(self, path_to_dir):

# [1-test_init_repo_object]
# $ git init <path/to/dir>

from git import Repo

repo = Repo.init(path_to_dir)
# ![1-test_init_repo_object]
# ![1-test_init_repo_object]

# [2-test_init_repo_object]
repo = Repo(path_to_dir)
# ![2-test_init_repo_object]

@with_rw_directory
def test_cloned_repo_object(self, local_dir):

from git import Repo
import git

# code to clone from url
# [1-test_cloned_repo_object]
# $ git clone <url> <local_dir>
Expand All @@ -44,9 +43,9 @@ def test_cloned_repo_object(self, local_dir):
# [2-test_cloned_repo_object]
# We must make a change to a file so that we can add the update to git

update_file = 'dir1/file2.txt' # we'll use local_dir/dir1/file2.txt
with open(f"{local_dir}/{update_file}", 'a') as f:
f.write('\nUpdate version 2')
update_file = "dir1/file2.txt" # we'll use local_dir/dir1/file2.txt
with open(f"{local_dir}/{update_file}", "a") as f:
f.write("\nUpdate version 2")
# ![2-test_cloned_repo_object]

# [3-test_cloned_repo_object]
Expand Down Expand Up @@ -82,7 +81,7 @@ def test_cloned_repo_object(self, local_dir):

# Untracked files - create new file
# [7-test_cloned_repo_object]
f = open(f'{local_dir}/untracked.txt', 'w') # creates an empty file
f = open(f"{local_dir}/untracked.txt", "w") # creates an empty file
f.close()
# ![7-test_cloned_repo_object]

Expand All @@ -95,8 +94,8 @@ def test_cloned_repo_object(self, local_dir):
# [9-test_cloned_repo_object]
# Let's modify one of our tracked files

with open(f'{local_dir}/Downloads/file3.txt', 'w') as f:
f.write('file3 version 2') # overwrite file 3
with open(f"{local_dir}/Downloads/file3.txt", "w") as f:
f.write("file3 version 2") # overwrite file 3
# ![9-test_cloned_repo_object]

# [10-test_cloned_repo_object]
Expand Down Expand Up @@ -126,7 +125,7 @@ def test_cloned_repo_object(self, local_dir):
# ![11.1-test_cloned_repo_object]
# [11.2-test_cloned_repo_object]
# lets add untracked.txt
repo.index.add(['untracked.txt'])
repo.index.add(["untracked.txt"])
diffs = repo.index.diff(repo.head.commit)
for d in diffs:
print(d.a_path)
Expand All @@ -146,9 +145,7 @@ def test_cloned_repo_object(self, local_dir):
# dir1/file2.txt
# ![11.3-test_cloned_repo_object]



'''Trees and Blobs'''
"""Trees and Blobs"""

# Latest commit tree
# [12-test_cloned_repo_object]
Expand Down Expand Up @@ -195,7 +192,7 @@ def print_files_from_git(root, level=0):

# Printing text files
# [17-test_cloned_repo_object]
print_file = 'dir1/file2.txt'
print_file = "dir1/file2.txt"
tree[print_file] # the head commit tree

# Output <git.Blob "SHA1-HEX-HASH">
Expand All @@ -221,4 +218,4 @@ def print_files_from_git(root, level=0):

# Output
# file 2 version 1
# ![18.1-test_cloned_repo_object]
# ![18.1-test_cloned_repo_object]
30 changes: 18 additions & 12 deletions test/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ def test_clone_from_with_path_contains_unicode(self):
self.fail("Raised UnicodeEncodeError")

@with_rw_directory
@skip("the referenced repository was removed, and one needs to setup a new password controlled repo under the orgs control")
@skip(
"the referenced repository was removed, and one needs to setup a new password controlled repo under the orgs control"
)
def test_leaking_password_in_clone_logs(self, rw_dir):
password = "fakepassword1234"
try:
Expand Down Expand Up @@ -391,7 +393,9 @@ def test_clone_from_unsafe_options_allowed(self, rw_repo):
for i, unsafe_option in enumerate(unsafe_options):
destination = tmp_dir / str(i)
assert not destination.exists()
Repo.clone_from(rw_repo.working_dir, destination, multi_options=[unsafe_option], allow_unsafe_options=True)
Repo.clone_from(
rw_repo.working_dir, destination, multi_options=[unsafe_option], allow_unsafe_options=True
)
assert destination.exists()

@with_rw_repo("HEAD")
Expand Down Expand Up @@ -755,8 +759,8 @@ def test_blame_complex_revision(self, git):
@mock.patch.object(Git, "_call_process")
def test_blame_accepts_rev_opts(self, git):
res = self.rorepo.blame("HEAD", "README.md", rev_opts=["-M", "-C", "-C"])
expected_args = ['blame', 'HEAD', '-M', '-C', '-C', '--', 'README.md']
boilerplate_kwargs = {"p" : True, "stdout_as_string": False}
expected_args = ["blame", "HEAD", "-M", "-C", "-C", "--", "README.md"]
boilerplate_kwargs = {"p": True, "stdout_as_string": False}
git.assert_called_once_with(*expected_args, **boilerplate_kwargs)

@skipIf(
Expand Down Expand Up @@ -1115,7 +1119,7 @@ def test_repo_odbtype(self):
@pytest.mark.xfail(
sys.platform == "cygwin",
reason="Cygwin GitPython can't find submodule SHA",
raises=ValueError
raises=ValueError,
)
def test_submodules(self):
self.assertEqual(len(self.rorepo.submodules), 1) # non-recursive
Expand Down Expand Up @@ -1415,14 +1419,16 @@ def test_ignored_items_reported(self):

gi = tmp_dir / "repo" / ".gitignore"

with open(gi, 'w') as file:
file.write('ignored_file.txt\n')
file.write('ignored_dir/\n')
with open(gi, "w") as file:
file.write("ignored_file.txt\n")
file.write("ignored_dir/\n")

assert temp_repo.ignored(['included_file.txt', 'included_dir/file.txt']) == []
assert temp_repo.ignored(['ignored_file.txt']) == ['ignored_file.txt']
assert temp_repo.ignored(['included_file.txt', 'ignored_file.txt']) == ['ignored_file.txt']
assert temp_repo.ignored(['included_file.txt', 'ignored_file.txt', 'included_dir/file.txt', 'ignored_dir/file.txt']) == ['ignored_file.txt', 'ignored_dir/file.txt']
assert temp_repo.ignored(["included_file.txt", "included_dir/file.txt"]) == []
assert temp_repo.ignored(["ignored_file.txt"]) == ["ignored_file.txt"]
assert temp_repo.ignored(["included_file.txt", "ignored_file.txt"]) == ["ignored_file.txt"]
assert temp_repo.ignored(
["included_file.txt", "ignored_file.txt", "included_dir/file.txt", "ignored_dir/file.txt"]
) == ["ignored_file.txt", "ignored_dir/file.txt"]

def test_ignored_raises_error_w_symlink(self):
with tempfile.TemporaryDirectory() as tdir:
Expand Down
35 changes: 24 additions & 11 deletions test/test_submodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ def _patch_git_config(name, value):

# This is recomputed each time the context is entered, for compatibility with
# existing GIT_CONFIG_* environment variables, even if changed in this process.
patcher = mock.patch.dict(os.environ, {
"GIT_CONFIG_COUNT": str(pair_index + 1),
f"GIT_CONFIG_KEY_{pair_index}": name,
f"GIT_CONFIG_VALUE_{pair_index}": value,
})
patcher = mock.patch.dict(
os.environ,
{
"GIT_CONFIG_COUNT": str(pair_index + 1),
f"GIT_CONFIG_KEY_{pair_index}": name,
f"GIT_CONFIG_VALUE_{pair_index}": value,
},
)

with patcher:
yield
Expand Down Expand Up @@ -469,7 +472,7 @@ def test_base_bare(self, rwrepo):
@pytest.mark.xfail(
sys.platform == "cygwin",
reason="Cygwin GitPython can't find submodule SHA",
raises=ValueError
raises=ValueError,
)
@skipIf(
HIDE_WINDOWS_KNOWN_ERRORS,
Expand Down Expand Up @@ -914,17 +917,17 @@ def test_ignore_non_submodule_file(self, rwdir):
os.mkdir(smp)

with open(osp.join(smp, "a"), "w", encoding="utf-8") as f:
f.write('test\n')
f.write("test\n")

with open(osp.join(rwdir, ".gitmodules"), "w", encoding="utf-8") as f:
f.write("[submodule \"a\"]\n")
f.write('[submodule "a"]\n')
f.write(" path = module\n")
f.write(" url = https://github.com/chaconinc/DbConnector\n")

parent.git.add(Git.polish_url(osp.join(smp, "a")))
parent.git.add(Git.polish_url(osp.join(rwdir, ".gitmodules")))

parent.git.commit(message='test')
parent.git.commit(message="test")

assert len(parent.submodules) == 0

Expand Down Expand Up @@ -1200,7 +1203,12 @@ def test_submodule_add_unsafe_options_allowed(self, rw_repo):
# The options will be allowed, but the command will fail.
with self.assertRaises(GitCommandError):
Submodule.add(
rw_repo, "new", "new", str(tmp_dir), clone_multi_options=[unsafe_option], allow_unsafe_options=True
rw_repo,
"new",
"new",
str(tmp_dir),
clone_multi_options=[unsafe_option],
allow_unsafe_options=True,
)
assert not tmp_file.exists()

Expand All @@ -1211,7 +1219,12 @@ def test_submodule_add_unsafe_options_allowed(self, rw_repo):
for unsafe_option in unsafe_options:
with self.assertRaises(GitCommandError):
Submodule.add(
rw_repo, "new", "new", str(tmp_dir), clone_multi_options=[unsafe_option], allow_unsafe_options=True
rw_repo,
"new",
"new",
str(tmp_dir),
clone_multi_options=[unsafe_option],
allow_unsafe_options=True,
)

@with_rw_repo("HEAD")
Expand Down
2 changes: 1 addition & 1 deletion test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def test_lock_file(self):
@pytest.mark.xfail(
sys.platform == "cygwin",
reason="Cygwin fails here for some reason, always",
raises=AssertionError
raises=AssertionError,
)
def test_blocking_lock_file(self):
my_file = tempfile.mktemp()
Expand Down
Loading

0 comments on commit a5a6464

Please sign in to comment.