Skip to content

Commit

Permalink
Add regression tests of the git.util aliasing situation
Browse files Browse the repository at this point in the history
Although this situation is not inherently desirable, for backward
compatibility it cannot change at this time. It may be possible to
change it in the next major version of GitPython, but even then it
should not be changed accidentally, which can easily happen while
refactoring imports.

This tests the highest-risk accidental change (of those that are
currently known) of the kind that the temporary modattrs.py script
exists to help safeguard against. That script will be removed when
the immediately forthcoming import refactoring is complete, whereas
these test cases can be kept.

For information about the specific situation this helps ensure
isn't changed accidentally, see the new test cases' docstrings, as
well as the next commit (which will test modattrs.py and these test
cases by performing an incomplete change that would be a bug until
completed).

This commit adds three test cases. The first tests the unintuitive
aspect of the current situation:

- test_git_util_attribute_is_git_index_util

The other two test the intuitive aspects of it, i.e., they test
that changes (perhaps in an attempt to preserve the aspect needed
for backward compatibility) do not make `git.util` unusual in new
(and themselves incompatible) ways:

- test_git_index_util_attribute_is_git_index_util
- test_separate_git_util_module_exists

The latter tests should also clarify, for readers of the tests, the
limited nature of the condition the first test asserts.
  • Loading branch information
EliahKagan committed Mar 18, 2024
1 parent 1e5a944 commit 5b2771d
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/test_imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This module is part of GitPython and is released under the
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/

import sys

import git


def test_git_util_attribute_is_git_index_util():
"""The top-level module's ``util`` attribute is really :mod:`git.index.util`.
Although this situation is unintuitive and not a design goal, this has historically
been the case, and it should not be changed without considering the effect on
backward compatibility. In practice, it cannot be changed at least until the next
major version of GitPython. This test checks that it is not accidentally changed,
which could happen when refactoring imports.
"""
assert git.util is git.index.util


def test_git_index_util_attribute_is_git_index_util():
"""Nothing unusual is happening with git.index.util itself."""
assert git.index.util is sys.modules["git.index.util"]


def test_separate_git_util_module_exists():
"""The real git.util and git.index.util modules really are separate.
The real git.util module can be accessed to import a name ``...` by writing
``from git.util import ...``, and the module object can be accessed in sys.modules.
"""
assert sys.modules["git.util"] is not sys.modules["git.index.util"]

0 comments on commit 5b2771d

Please sign in to comment.