Skip to content

Commit 5ee4fb8

Browse files
committed
test: use os_helper
1 parent 39b9e1f commit 5ee4fb8

File tree

95 files changed

+1106
-1032
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1106
-1032
lines changed

Lib/test/libregrtest/cmdline.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import sys
44
from test import support
5+
from test.support import os_helper
56

67

78
USAGE = """\
@@ -280,7 +281,7 @@ def _create_parser():
280281
def relative_filename(string):
281282
# CWD is replaced with a temporary dir before calling main(), so we
282283
# join it with the saved CWD so it ends up where the user expects.
283-
return os.path.join(support.SAVEDCWD, string)
284+
return os.path.join(os_helper.SAVEDCWD, string)
284285

285286

286287
def huntrleaks(string):

Lib/test/libregrtest/main.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from test.libregrtest.setup import setup_tests
2121
from test.libregrtest.utils import removepy, count, format_duration, printlist
2222
from test import support
23+
from test.support import os_helper
2324

2425

2526
# When tests are run from the Python build directory, it is best practice
@@ -199,7 +200,7 @@ def find_tests(self, tests):
199200
# regex to match 'test_builtin' in line:
200201
# '0:00:00 [ 4/400] test_builtin -- test_dict took 1 sec'
201202
regex = re.compile(r'\btest_[a-zA-Z0-9_]+\b')
202-
with open(os.path.join(support.SAVEDCWD, self.ns.fromfile)) as fp:
203+
with open(os.path.join(os_helper.SAVEDCWD, self.ns.fromfile)) as fp:
203204
for line in fp:
204205
line = line.split('#', 1)[0]
205206
line = line.strip()
@@ -542,7 +543,7 @@ def save_xml_result(self):
542543
for k, v in totals.items():
543544
root.set(k, str(v))
544545

545-
xmlpath = os.path.join(support.SAVEDCWD, self.ns.xmlpath)
546+
xmlpath = os.path.join(os_helper.SAVEDCWD, self.ns.xmlpath)
546547
with open(xmlpath, 'wb') as f:
547548
for s in ET.tostringlist(root):
548549
f.write(s)
@@ -568,8 +569,8 @@ def main(self, tests=None, **kwargs):
568569
# Run the tests in a context manager that temporarily changes the CWD to a
569570
# temporary and writable directory. If it's not possible to create or
570571
# change the CWD, the original CWD will be used. The original CWD is
571-
# available from support.SAVEDCWD.
572-
with support.temp_cwd(test_cwd, quiet=True):
572+
# available from os_helper.SAVEDCWD.
573+
with os_helper.temp_cwd(test_cwd, quiet=True):
573574
self._main(tests, kwargs)
574575

575576
def getloadavg(self):

Lib/test/libregrtest/refleak.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def get_pooled_int(value):
6161
return int_pool.setdefault(value, value)
6262

6363
nwarmup, ntracked, fname = ns.huntrleaks
64-
fname = os.path.join(support.SAVEDCWD, fname)
64+
fname = os.path.join(os_helper.SAVEDCWD, fname)
6565
repcount = nwarmup + ntracked
6666

6767
# Pre-allocate to ensure that the loop doesn't allocate anything new

Lib/test/libregrtest/runtest.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import unittest
1212

1313
from test import support
14+
from test.support import os_helper
1415
from test.libregrtest.refleak import dash_R, clear_caches
1516
from test.libregrtest.save_env import saved_test_environment
1617
from test.libregrtest.utils import print_warning
@@ -298,7 +299,7 @@ def cleanup_test_droppings(test_name, verbose):
298299
# since if a test leaves a file open, it cannot be deleted by name (while
299300
# there's nothing we can do about that here either, we can display the
300301
# name of the offending test, which is a real help).
301-
for name in (support.TESTFN,
302+
for name in (os_helper.TESTFN,
302303
"db_home",
303304
):
304305
if not os.path.exists(name):

Lib/test/libregrtest/runtest_mp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def run_test_in_subprocess(testname, ns):
4646
stderr=subprocess.PIPE,
4747
universal_newlines=True,
4848
close_fds=(os.name != 'nt'),
49-
cwd=support.SAVEDCWD)
49+
cwd=os_helper.SAVEDCWD)
5050

5151

5252
def run_tests_worker(worker_args):

Lib/test/libregrtest/save_env.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import threading
1010
import warnings
1111
from test import support
12+
from test.support import os_helper
1213
from test.libregrtest.utils import print_warning
1314
try:
1415
import _multiprocessing, multiprocessing.process
@@ -228,12 +229,12 @@ def get_files(self):
228229
return sorted(fn + ('/' if os.path.isdir(fn) else '')
229230
for fn in os.listdir())
230231
def restore_files(self, saved_value):
231-
fn = support.TESTFN
232+
fn = os_helper.TESTFN
232233
if fn not in saved_value and (fn + '/') not in saved_value:
233234
if os.path.isfile(fn):
234-
support.unlink(fn)
235+
os_helper.unlink(fn)
235236
elif os.path.isdir(fn):
236-
support.rmtree(fn)
237+
os_helper.rmtree(fn)
237238

238239
_lc = [getattr(locale, lc) for lc in dir(locale)
239240
if lc.startswith('LC_')]

Lib/test/libregrtest/win_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def start(self):
5555

5656
# Spawn off the load monitor
5757
command = ['typeperf', COUNTER_NAME, '-si', str(SAMPLING_INTERVAL)]
58-
self.p = subprocess.Popen(command, stdout=command_stdout, cwd=support.SAVEDCWD)
58+
self.p = subprocess.Popen(command, stdout=command_stdout, cwd=os_helper.SAVEDCWD)
5959

6060
# Close our copy of the write end of the pipe
6161
os.close(command_stdout)

Lib/test/list_tests.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from functools import cmp_to_key
99

1010
from test import support, seq_tests
11+
from test.support import os_helper
1112

1213

1314
class CommonTest(seq_tests.CommonTest):
@@ -73,12 +74,12 @@ def test_print(self):
7374
d.append(d)
7475
d.append(400)
7576
try:
76-
with open(support.TESTFN, "w") as fo:
77+
with open(os_helper.TESTFN, "w") as fo:
7778
fo.write(str(d))
78-
with open(support.TESTFN, "r") as fo:
79+
with open(os_helper.TESTFN, "r") as fo:
7980
self.assertEqual(fo.read(), repr(d))
8081
finally:
81-
os.remove(support.TESTFN)
82+
os.remove(os_helper.TESTFN)
8283

8384
def test_set_subscript(self):
8485
a = self.type2test(range(20))

Lib/test/test__osx_support.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import unittest
1010

1111
import test.support
12+
from test.support import os_helper
1213

1314
import _osx_support
1415

@@ -19,7 +20,7 @@ def setUp(self):
1920
self.maxDiff = None
2021
self.prog_name = 'bogus_program_xxxx'
2122
self.temp_path_dir = os.path.abspath(os.getcwd())
22-
self.env = test.support.EnvironmentVarGuard()
23+
self.env = os_helper.EnvironmentVarGuard()
2324
self.addCleanup(self.env.__exit__)
2425
for cv in ('CFLAGS', 'LDFLAGS', 'CPPFLAGS',
2526
'BASECFLAGS', 'BLDSHARED', 'LDSHARED', 'CC',

Lib/test/test_argparse.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from io import StringIO
1414

1515
from test import support
16+
from test.support import os_helper
1617
from unittest import mock
1718
class StdIOBuffer(StringIO):
1819
pass
@@ -23,7 +24,7 @@ def setUp(self):
2324
# The tests assume that line wrapping occurs at 80 columns, but this
2425
# behaviour can be overridden by setting the COLUMNS environment
2526
# variable. To ensure that this width is used, set COLUMNS to 80.
26-
env = support.EnvironmentVarGuard()
27+
env = os_helper.EnvironmentVarGuard()
2728
env['COLUMNS'] = '80'
2829
self.addCleanup(env.__exit__)
2930

@@ -3244,7 +3245,7 @@ class TestShortColumns(HelpTestCase):
32443245
but we don't want any exceptions thrown in such cases. Only ugly representation.
32453246
'''
32463247
def setUp(self):
3247-
env = support.EnvironmentVarGuard()
3248+
env = os_helper.EnvironmentVarGuard()
32483249
env.set("COLUMNS", '15')
32493250
self.addCleanup(env.__exit__)
32503251

Lib/test/test_array.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import unittest
66
from test import support
7-
from test.support import _2G
7+
from test.support import _2G, os_helper
88
import weakref
99
import pickle
1010
import operator
@@ -371,13 +371,13 @@ def test_insert(self):
371371
def test_tofromfile(self):
372372
a = array.array(self.typecode, 2*self.example)
373373
self.assertRaises(TypeError, a.tofile)
374-
support.unlink(support.TESTFN)
375-
f = open(support.TESTFN, 'wb')
374+
os_helper.unlink(os_helper.TESTFN)
375+
f = open(os_helper.TESTFN, 'wb')
376376
try:
377377
a.tofile(f)
378378
f.close()
379379
b = array.array(self.typecode)
380-
f = open(support.TESTFN, 'rb')
380+
f = open(os_helper.TESTFN, 'rb')
381381
self.assertRaises(TypeError, b.fromfile)
382382
b.fromfile(f, len(self.example))
383383
self.assertEqual(b, array.array(self.typecode, self.example))
@@ -388,27 +388,27 @@ def test_tofromfile(self):
388388
finally:
389389
if not f.closed:
390390
f.close()
391-
support.unlink(support.TESTFN)
391+
os_helper.unlink(os_helper.TESTFN)
392392

393393
def test_fromfile_ioerror(self):
394394
# Issue #5395: Check if fromfile raises a proper OSError
395395
# instead of EOFError.
396396
a = array.array(self.typecode)
397-
f = open(support.TESTFN, 'wb')
397+
f = open(os_helper.TESTFN, 'wb')
398398
try:
399399
self.assertRaises(OSError, a.fromfile, f, len(self.example))
400400
finally:
401401
f.close()
402-
support.unlink(support.TESTFN)
402+
os_helper.unlink(os_helper.TESTFN)
403403

404404
def test_filewrite(self):
405405
a = array.array(self.typecode, 2*self.example)
406-
f = open(support.TESTFN, 'wb')
406+
f = open(os_helper.TESTFN, 'wb')
407407
try:
408408
f.write(a)
409409
f.close()
410410
b = array.array(self.typecode)
411-
f = open(support.TESTFN, 'rb')
411+
f = open(os_helper.TESTFN, 'rb')
412412
b.fromfile(f, len(self.example))
413413
self.assertEqual(b, array.array(self.typecode, self.example))
414414
self.assertNotEqual(a, b)
@@ -418,7 +418,7 @@ def test_filewrite(self):
418418
finally:
419419
if not f.closed:
420420
f.close()
421-
support.unlink(support.TESTFN)
421+
os_helper.unlink(os_helper.TESTFN)
422422

423423
def test_tofromlist(self):
424424
a = array.array(self.typecode, 2*self.example)

Lib/test/test_asyncore.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import threading
1111

1212
from test import support
13+
from test.support import os_helper
1314
from io import BytesIO
1415

1516
if support.PGO:
@@ -91,7 +92,7 @@ def bind_af_aware(sock, addr):
9192
"""Helper function to bind a socket according to its family."""
9293
if HAS_UNIX_SOCKETS and sock.family == socket.AF_UNIX:
9394
# Make sure the path doesn't exist.
94-
support.unlink(addr)
95+
os_helper.unlink(addr)
9596
support.bind_unix_socket(sock, addr)
9697
else:
9798
sock.bind(addr)
@@ -368,14 +369,14 @@ def test_send(self):
368369
class FileWrapperTest(unittest.TestCase):
369370
def setUp(self):
370371
self.d = b"It's not dead, it's sleeping!"
371-
with open(support.TESTFN, 'wb') as file:
372+
with open(os_helper.TESTFN, 'wb') as file:
372373
file.write(self.d)
373374

374375
def tearDown(self):
375-
support.unlink(support.TESTFN)
376+
os_helper.unlink(os_helper.TESTFN)
376377

377378
def test_recv(self):
378-
fd = os.open(support.TESTFN, os.O_RDONLY)
379+
fd = os.open(os_helper.TESTFN, os.O_RDONLY)
379380
w = asyncore.file_wrapper(fd)
380381
os.close(fd)
381382

@@ -389,20 +390,20 @@ def test_recv(self):
389390
def test_send(self):
390391
d1 = b"Come again?"
391392
d2 = b"I want to buy some cheese."
392-
fd = os.open(support.TESTFN, os.O_WRONLY | os.O_APPEND)
393+
fd = os.open(os_helper.TESTFN, os.O_WRONLY | os.O_APPEND)
393394
w = asyncore.file_wrapper(fd)
394395
os.close(fd)
395396

396397
w.write(d1)
397398
w.send(d2)
398399
w.close()
399-
with open(support.TESTFN, 'rb') as file:
400+
with open(os_helper.TESTFN, 'rb') as file:
400401
self.assertEqual(file.read(), self.d + d1 + d2)
401402

402403
@unittest.skipUnless(hasattr(asyncore, 'file_dispatcher'),
403404
'asyncore.file_dispatcher required')
404405
def test_dispatcher(self):
405-
fd = os.open(support.TESTFN, os.O_RDONLY)
406+
fd = os.open(os_helper.TESTFN, os.O_RDONLY)
406407
data = []
407408
class FileDispatcher(asyncore.file_dispatcher):
408409
def handle_read(self):
@@ -414,7 +415,7 @@ def handle_read(self):
414415

415416
def test_resource_warning(self):
416417
# Issue #11453
417-
fd = os.open(support.TESTFN, os.O_RDONLY)
418+
fd = os.open(os_helper.TESTFN, os.O_RDONLY)
418419
f = asyncore.file_wrapper(fd)
419420

420421
os.close(fd)
@@ -423,7 +424,7 @@ def test_resource_warning(self):
423424
support.gc_collect()
424425

425426
def test_close_twice(self):
426-
fd = os.open(support.TESTFN, os.O_RDONLY)
427+
fd = os.open(os_helper.TESTFN, os.O_RDONLY)
427428
f = asyncore.file_wrapper(fd)
428429
os.close(fd)
429430

@@ -803,10 +804,10 @@ class TestAPI_UseIPv6Sockets(BaseTestAPI):
803804
class TestAPI_UseUnixSockets(BaseTestAPI):
804805
if HAS_UNIX_SOCKETS:
805806
family = socket.AF_UNIX
806-
addr = support.TESTFN
807+
addr = os_helper.TESTFN
807808

808809
def tearDown(self):
809-
support.unlink(self.addr)
810+
os_helper.unlink(self.addr)
810811
BaseTestAPI.tearDown(self)
811812

812813
class TestAPI_UseIPv4Select(TestAPI_UseIPv4Sockets, unittest.TestCase):

Lib/test/test_base64.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import binascii
55
import os
66
from array import array
7-
from test.support import script_helper
7+
from test.support import script_helper, os_helper
88

99

1010
class LegacyBase64TestCase(unittest.TestCase):
@@ -683,8 +683,8 @@ def test_ErrorHeritage(self):
683683

684684
class TestMain(unittest.TestCase):
685685
def tearDown(self):
686-
if os.path.exists(support.TESTFN):
687-
os.unlink(support.TESTFN)
686+
if os.path.exists(os_helper.TESTFN):
687+
os.unlink(os_helper.TESTFN)
688688

689689
def get_output(self, *args):
690690
return script_helper.assert_python_ok('-m', 'base64', *args).out
@@ -698,9 +698,9 @@ def test_encode_decode(self):
698698
))
699699

700700
def test_encode_file(self):
701-
with open(support.TESTFN, 'wb') as fp:
701+
with open(os_helper.TESTFN, 'wb') as fp:
702702
fp.write(b'a\xffb\n')
703-
output = self.get_output('-e', support.TESTFN)
703+
output = self.get_output('-e', os_helper.TESTFN)
704704
self.assertEqual(output.rstrip(), b'Yf9iCg==')
705705

706706
def test_encode_from_stdin(self):
@@ -710,9 +710,9 @@ def test_encode_from_stdin(self):
710710
self.assertIsNone(err)
711711

712712
def test_decode(self):
713-
with open(support.TESTFN, 'wb') as fp:
713+
with open(os_helper.TESTFN, 'wb') as fp:
714714
fp.write(b'Yf9iCg==')
715-
output = self.get_output('-d', support.TESTFN)
715+
output = self.get_output('-d', os_helper.TESTFN)
716716
self.assertEqual(output.rstrip(), b'a\xffb')
717717

718718
if __name__ == '__main__':

0 commit comments

Comments
 (0)