diff --git a/Lib/unittest/loader.py b/Lib/unittest/loader.py index a52950dad224ee..869e851c255c51 100644 --- a/Lib/unittest/loader.py +++ b/Lib/unittest/loader.py @@ -356,7 +356,10 @@ def _get_name_from_path(self, path): return '.' path = _splitext(os.path.normpath(path)) - _relpath = os.path.relpath(path, self._top_level_dir) + path_real = os.path.realpath(path) + tld_real = os.path.realpath(self._top_level_dir) + _relpath = os.path.relpath(path_real, tld_real) + assert not os.path.isabs(_relpath), "Path must be within the project" assert not _relpath.startswith('..'), "Path must be within the project" diff --git a/Misc/NEWS.d/next/Tests/2026-04-29-11-20-43.gh-issue-149141.Ue1Qww.rst b/Misc/NEWS.d/next/Tests/2026-04-29-11-20-43.gh-issue-149141.Ue1Qww.rst new file mode 100644 index 00000000000000..9d57c46ee546cb --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-04-29-11-20-43.gh-issue-149141.Ue1Qww.rst @@ -0,0 +1,4 @@ +Test discovery would fail when the path to the source tree contains a +symlink. Paths that take an alternate route to the same place (eg just the +real path) would not be considered a subpath, and cause an assert. Instead, +always resolve to the real path before comparing.