Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0494c2a

Browse files
committed
Close #18952: correctly download test support data
When test.support was converted to a package, it started silently skipping the tests which needed to download support data to run. This change refactors the affected code, and also tidies up test.support.findfile to remove the unused *here* parameter, document the *subdir* parameter and rename the *filename* parameter to avoid shadowing the file builtin and be consistent with the documentation. The unexpected skips were noticed and reported by Zachary Ware
1 parent 21101f7 commit 0494c2a

3 files changed

Lines changed: 27 additions & 13 deletions

File tree

Doc/library/test.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,15 @@ The :mod:`test.support` module defines the following functions:
263263
Used when tests are executed by :mod:`test.regrtest`.
264264

265265

266-
.. function:: findfile(filename)
266+
.. function:: findfile(filename, subdir=None)
267267

268268
Return the path to the file named *filename*. If no match is found
269269
*filename* is returned. This does not equal a failure since it could be the
270270
path to the file.
271271

272+
Setting *subdir* indicates a relative path to use to find the file
273+
rather than looking directly in the path directories.
274+
272275

273276
.. function:: run_unittest(\*classes)
274277

Lib/test/support/__init__.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -853,24 +853,31 @@ def temp_umask(umask):
853853
finally:
854854
os.umask(oldmask)
855855

856-
# TEST_HOME refers to the top level directory of the "test" package
856+
# TEST_HOME_DIR refers to the top level directory of the "test" package
857857
# that contains Python's regression test suite
858-
TEST_HOME = os.path.dirname(os.path.abspath(__file__))
858+
TEST_SUPPORT_DIR = os.path.dirname(os.path.abspath(__file__))
859+
TEST_HOME_DIR = os.path.dirname(TEST_SUPPORT_DIR)
859860

860-
def findfile(file, here=TEST_HOME, subdir=None):
861+
# TEST_DATA_DIR is used as a target download location for remote resources
862+
TEST_DATA_DIR = os.path.join(TEST_HOME_DIR, "data")
863+
864+
def findfile(filename, subdir=None):
861865
"""Try to find a file on sys.path or in the test directory. If it is not
862866
found the argument passed to the function is returned (this does not
863-
necessarily signal failure; could still be the legitimate path)."""
864-
if os.path.isabs(file):
865-
return file
867+
necessarily signal failure; could still be the legitimate path).
868+
869+
Setting *subdir* indicates a relative path to use to find the file
870+
rather than looking directly in the path directories.
871+
"""
872+
if os.path.isabs(filename):
873+
return filename
866874
if subdir is not None:
867-
file = os.path.join(subdir, file)
868-
path = sys.path
869-
path = [os.path.dirname(here)] + path
875+
filename = os.path.join(subdir, filename)
876+
path = [TEST_HOME_DIR] + sys.path
870877
for dn in path:
871-
fn = os.path.join(dn, file)
878+
fn = os.path.join(dn, filename)
872879
if os.path.exists(fn): return fn
873-
return file
880+
return filename
874881

875882
def create_empty_file(filename):
876883
"""Create an empty file. If the file already exists, truncate it."""
@@ -907,7 +914,7 @@ def open_urlresource(url, *args, **kw):
907914

908915
filename = urllib.parse.urlparse(url)[2].split('/')[-1] # '/': it's URL!
909916

910-
fn = os.path.join(os.path.dirname(__file__), "data", filename)
917+
fn = os.path.join(TEST_DATA_DIR, filename)
911918

912919
def check_valid_file(fn):
913920
f = open(fn, *args, **kw)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ IDLE
332332
Tests
333333
-----
334334

335+
- Issue #18952: Fix regression in support data downloads introduced when
336+
test.support was converted to a package. Regression noticed by Zachary
337+
Ware.
338+
335339
- Issue #12037: Fix test_email for desktop Windows.
336340

337341
- Issue #15507: test_subprocess's test_send_signal could fail if the test

0 commit comments

Comments
 (0)