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

Skip to content

Commit 3481e21

Browse files
committed
Issue #21918: Convert test_tools.py to a sub-package of test.
Merge with 3.4.
2 parents 51af1de + 2b0a610 commit 3481e21

10 files changed

Lines changed: 283 additions & 195 deletions

File tree

Lib/test/test_tools/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Support functions for testing scripts in the Tools directory."""
2+
import os
3+
import unittest
4+
import importlib
5+
from test import support
6+
from fnmatch import fnmatch
7+
8+
basepath = os.path.dirname( # <src/install dir>
9+
os.path.dirname( # Lib
10+
os.path.dirname( # test
11+
os.path.dirname(__file__)))) # test_tools
12+
13+
toolsdir = os.path.join(basepath, 'Tools')
14+
scriptsdir = os.path.join(toolsdir, 'scripts')
15+
16+
def skip_if_missing():
17+
if not os.path.isdir(scriptsdir):
18+
raise unittest.SkipTest('scripts directory could not be found')
19+
20+
def import_tool(toolname):
21+
with support.DirsOnSysPath(scriptsdir):
22+
return importlib.import_module(toolname)
23+
24+
def load_tests(loader, standard_tests, pattern):
25+
this_dir = os.path.dirname(__file__)
26+
if pattern is None:
27+
pattern = "test*"
28+
with support.DirsOnSysPath():
29+
package_tests = loader.discover(start_dir=this_dir, pattern=pattern)
30+
standard_tests.addTests(package_tests)
31+
return standard_tests

Lib/test/test_tools/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from test.test_tools import load_tests
2+
import unittest
3+
4+
unittest.main()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Tests for the gprof2html script in the Tools directory."""
2+
3+
import os
4+
import sys
5+
import importlib
6+
import unittest
7+
from unittest import mock
8+
import tempfile
9+
10+
from test.test_tools import scriptsdir, skip_if_missing, import_tool
11+
12+
skip_if_missing()
13+
14+
class Gprof2htmlTests(unittest.TestCase):
15+
16+
def setUp(self):
17+
self.gprof = import_tool('gprof2html')
18+
oldargv = sys.argv
19+
def fixup():
20+
sys.argv = oldargv
21+
self.addCleanup(fixup)
22+
sys.argv = []
23+
24+
def test_gprof(self):
25+
# Issue #14508: this used to fail with an NameError.
26+
with mock.patch.object(self.gprof, 'webbrowser') as wmock, \
27+
tempfile.TemporaryDirectory() as tmpdir:
28+
fn = os.path.join(tmpdir, 'abc')
29+
open(fn, 'w').close()
30+
sys.argv = ['gprof2html', fn]
31+
self.gprof.main()
32+
self.assertTrue(wmock.open.called)
33+
34+
35+
if __name__ == '__main__':
36+
unittest.main()

Lib/test/test_tools/test_md5sum.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""Tests for the md5sum script in the Tools directory."""
2+
3+
import os
4+
import sys
5+
import unittest
6+
from test import support
7+
from test.script_helper import assert_python_ok, assert_python_failure
8+
9+
from test.test_tools import scriptsdir, import_tool, skip_if_missing
10+
11+
skip_if_missing()
12+
13+
class MD5SumTests(unittest.TestCase):
14+
@classmethod
15+
def setUpClass(cls):
16+
cls.script = os.path.join(scriptsdir, 'md5sum.py')
17+
os.mkdir(support.TESTFN)
18+
cls.fodder = os.path.join(support.TESTFN, 'md5sum.fodder')
19+
with open(cls.fodder, 'wb') as f:
20+
f.write(b'md5sum\r\ntest file\r\n')
21+
cls.fodder_md5 = b'd38dae2eb1ab346a292ef6850f9e1a0d'
22+
cls.fodder_textmode_md5 = b'a8b07894e2ca3f2a4c3094065fa6e0a5'
23+
24+
@classmethod
25+
def tearDownClass(cls):
26+
support.rmtree(support.TESTFN)
27+
28+
def test_noargs(self):
29+
rc, out, err = assert_python_ok(self.script)
30+
self.assertEqual(rc, 0)
31+
self.assertTrue(
32+
out.startswith(b'd41d8cd98f00b204e9800998ecf8427e <stdin>'))
33+
self.assertFalse(err)
34+
35+
def test_checksum_fodder(self):
36+
rc, out, err = assert_python_ok(self.script, self.fodder)
37+
self.assertEqual(rc, 0)
38+
self.assertTrue(out.startswith(self.fodder_md5))
39+
for part in self.fodder.split(os.path.sep):
40+
self.assertIn(part.encode(), out)
41+
self.assertFalse(err)
42+
43+
def test_dash_l(self):
44+
rc, out, err = assert_python_ok(self.script, '-l', self.fodder)
45+
self.assertEqual(rc, 0)
46+
self.assertIn(self.fodder_md5, out)
47+
parts = self.fodder.split(os.path.sep)
48+
self.assertIn(parts[-1].encode(), out)
49+
self.assertNotIn(parts[-2].encode(), out)
50+
51+
def test_dash_t(self):
52+
rc, out, err = assert_python_ok(self.script, '-t', self.fodder)
53+
self.assertEqual(rc, 0)
54+
self.assertTrue(out.startswith(self.fodder_textmode_md5))
55+
self.assertNotIn(self.fodder_md5, out)
56+
57+
def test_dash_s(self):
58+
rc, out, err = assert_python_ok(self.script, '-s', '512', self.fodder)
59+
self.assertEqual(rc, 0)
60+
self.assertIn(self.fodder_md5, out)
61+
62+
def test_multiple_files(self):
63+
rc, out, err = assert_python_ok(self.script, self.fodder, self.fodder)
64+
self.assertEqual(rc, 0)
65+
lines = out.splitlines()
66+
self.assertEqual(len(lines), 2)
67+
self.assertEqual(*lines)
68+
69+
def test_usage(self):
70+
rc, out, err = assert_python_failure(self.script, '-h')
71+
self.assertEqual(rc, 2)
72+
self.assertEqual(out, b'')
73+
self.assertGreater(err, b'')
74+
75+
76+
if __name__ == '__main__':
77+
unittest.main()

Lib/test/test_tools/test_pdeps.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Tests for the pdeps script in the Tools directory."""
2+
3+
import os
4+
import sys
5+
import unittest
6+
import tempfile
7+
from test import support
8+
9+
from test.test_tools import scriptsdir, skip_if_missing, import_tool
10+
11+
skip_if_missing()
12+
13+
14+
class PdepsTests(unittest.TestCase):
15+
16+
@classmethod
17+
def setUpClass(self):
18+
self.pdeps = import_tool('pdeps')
19+
20+
def test_process_errors(self):
21+
# Issue #14492: m_import.match(line) can be None.
22+
with tempfile.TemporaryDirectory() as tmpdir:
23+
fn = os.path.join(tmpdir, 'foo')
24+
with open(fn, 'w') as stream:
25+
stream.write("#!/this/will/fail")
26+
self.pdeps.process(fn, {})
27+
28+
def test_inverse_attribute_error(self):
29+
# Issue #14492: this used to fail with an AttributeError.
30+
self.pdeps.inverse({'a': []})
31+
32+
33+
if __name__ == '__main__':
34+
unittest.main()
Lines changed: 4 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,16 @@
1-
"""Tests for scripts in the Tools directory.
2-
3-
This file contains regression tests for some of the scripts found in the
4-
Tools directory of a Python checkout or tarball, such as reindent.py.
5-
"""
1+
"""Tests for the pindent script in the Tools directory."""
62

73
import os
84
import sys
9-
import importlib._bootstrap
10-
import importlib.machinery
115
import unittest
12-
from unittest import mock
13-
import shutil
146
import subprocess
15-
import sysconfig
16-
import tempfile
177
import textwrap
188
from test import support
19-
from test.script_helper import assert_python_ok, assert_python_failure
20-
21-
if not sysconfig.is_python_build():
22-
# XXX some installers do contain the tools, should we detect that
23-
# and run the tests in that case too?
24-
raise unittest.SkipTest('test irrelevant for an installed Python')
25-
26-
basepath = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
27-
'Tools')
28-
scriptsdir = os.path.join(basepath, 'scripts')
9+
from test.script_helper import assert_python_ok
2910

11+
from test.test_tools import scriptsdir, skip_if_missing
3012

31-
class ReindentTests(unittest.TestCase):
32-
script = os.path.join(scriptsdir, 'reindent.py')
33-
34-
def test_noargs(self):
35-
assert_python_ok(self.script)
36-
37-
def test_help(self):
38-
rc, out, err = assert_python_ok(self.script, '-h')
39-
self.assertEqual(out, b'')
40-
self.assertGreater(err, b'')
13+
skip_if_missing()
4114

4215

4316
class PindentTests(unittest.TestCase):
@@ -362,162 +335,5 @@ def test_oneline(self):
362335
self.pindent_test(clean, closed)
363336

364337

365-
class TestSundryScripts(unittest.TestCase):
366-
# At least make sure the rest don't have syntax errors. When tests are
367-
# added for a script it should be added to the whitelist below.
368-
369-
# scripts that have independent tests.
370-
whitelist = ['reindent.py', 'pdeps.py', 'gprof2html', 'md5sum.py']
371-
# scripts that can't be imported without running
372-
blacklist = ['make_ctype.py']
373-
# scripts that use windows-only modules
374-
windows_only = ['win_add2path.py']
375-
# blacklisted for other reasons
376-
other = ['analyze_dxp.py']
377-
378-
skiplist = blacklist + whitelist + windows_only + other
379-
380-
def setUp(self):
381-
cm = support.DirsOnSysPath(scriptsdir)
382-
cm.__enter__()
383-
self.addCleanup(cm.__exit__)
384-
385-
def test_sundry(self):
386-
for fn in os.listdir(scriptsdir):
387-
if fn.endswith('.py') and fn not in self.skiplist:
388-
__import__(fn[:-3])
389-
390-
@unittest.skipIf(sys.platform != "win32", "Windows-only test")
391-
def test_sundry_windows(self):
392-
for fn in self.windows_only:
393-
__import__(fn[:-3])
394-
395-
@unittest.skipIf(not support.threading, "test requires _thread module")
396-
def test_analyze_dxp_import(self):
397-
if hasattr(sys, 'getdxp'):
398-
import analyze_dxp
399-
else:
400-
with self.assertRaises(RuntimeError):
401-
import analyze_dxp
402-
403-
404-
class PdepsTests(unittest.TestCase):
405-
406-
@classmethod
407-
def setUpClass(self):
408-
path = os.path.join(scriptsdir, 'pdeps.py')
409-
spec = importlib.util.spec_from_file_location('pdeps', path)
410-
self.pdeps = importlib._bootstrap._load(spec)
411-
412-
@classmethod
413-
def tearDownClass(self):
414-
if 'pdeps' in sys.modules:
415-
del sys.modules['pdeps']
416-
417-
def test_process_errors(self):
418-
# Issue #14492: m_import.match(line) can be None.
419-
with tempfile.TemporaryDirectory() as tmpdir:
420-
fn = os.path.join(tmpdir, 'foo')
421-
with open(fn, 'w') as stream:
422-
stream.write("#!/this/will/fail")
423-
self.pdeps.process(fn, {})
424-
425-
def test_inverse_attribute_error(self):
426-
# Issue #14492: this used to fail with an AttributeError.
427-
self.pdeps.inverse({'a': []})
428-
429-
430-
class Gprof2htmlTests(unittest.TestCase):
431-
432-
def setUp(self):
433-
path = os.path.join(scriptsdir, 'gprof2html.py')
434-
spec = importlib.util.spec_from_file_location('gprof2html', path)
435-
self.gprof = importlib._bootstrap._load(spec)
436-
oldargv = sys.argv
437-
def fixup():
438-
sys.argv = oldargv
439-
self.addCleanup(fixup)
440-
sys.argv = []
441-
442-
def test_gprof(self):
443-
# Issue #14508: this used to fail with an NameError.
444-
with mock.patch.object(self.gprof, 'webbrowser') as wmock, \
445-
tempfile.TemporaryDirectory() as tmpdir:
446-
fn = os.path.join(tmpdir, 'abc')
447-
open(fn, 'w').close()
448-
sys.argv = ['gprof2html', fn]
449-
self.gprof.main()
450-
self.assertTrue(wmock.open.called)
451-
452-
453-
class MD5SumTests(unittest.TestCase):
454-
455-
@classmethod
456-
def setUpClass(cls):
457-
cls.script = os.path.join(scriptsdir, 'md5sum.py')
458-
os.mkdir(support.TESTFN)
459-
cls.fodder = os.path.join(support.TESTFN, 'md5sum.fodder')
460-
with open(cls.fodder, 'wb') as f:
461-
f.write(b'md5sum\r\ntest file\r\n')
462-
cls.fodder_md5 = b'd38dae2eb1ab346a292ef6850f9e1a0d'
463-
cls.fodder_textmode_md5 = b'a8b07894e2ca3f2a4c3094065fa6e0a5'
464-
465-
@classmethod
466-
def tearDownClass(cls):
467-
support.rmtree(support.TESTFN)
468-
469-
def test_noargs(self):
470-
rc, out, err = assert_python_ok(self.script)
471-
self.assertEqual(rc, 0)
472-
self.assertTrue(
473-
out.startswith(b'd41d8cd98f00b204e9800998ecf8427e <stdin>'))
474-
self.assertFalse(err)
475-
476-
def test_checksum_fodder(self):
477-
rc, out, err = assert_python_ok(self.script, self.fodder)
478-
self.assertEqual(rc, 0)
479-
self.assertTrue(out.startswith(self.fodder_md5))
480-
for part in self.fodder.split(os.path.sep):
481-
self.assertIn(part.encode(), out)
482-
self.assertFalse(err)
483-
484-
def test_dash_l(self):
485-
rc, out, err = assert_python_ok(self.script, '-l', self.fodder)
486-
self.assertEqual(rc, 0)
487-
self.assertIn(self.fodder_md5, out)
488-
parts = self.fodder.split(os.path.sep)
489-
self.assertIn(parts[-1].encode(), out)
490-
self.assertNotIn(parts[-2].encode(), out)
491-
492-
def test_dash_t(self):
493-
rc, out, err = assert_python_ok(self.script, '-t', self.fodder)
494-
self.assertEqual(rc, 0)
495-
self.assertTrue(out.startswith(self.fodder_textmode_md5))
496-
self.assertNotIn(self.fodder_md5, out)
497-
498-
def test_dash_s(self):
499-
rc, out, err = assert_python_ok(self.script, '-s', '512', self.fodder)
500-
self.assertEqual(rc, 0)
501-
self.assertIn(self.fodder_md5, out)
502-
503-
def test_multiple_files(self):
504-
rc, out, err = assert_python_ok(self.script, self.fodder, self.fodder)
505-
self.assertEqual(rc, 0)
506-
lines = out.splitlines()
507-
self.assertEqual(len(lines), 2)
508-
self.assertEqual(*lines)
509-
510-
def test_usage(self):
511-
rc, out, err = assert_python_failure(self.script, '-h')
512-
self.assertEqual(rc, 2)
513-
self.assertEqual(out, b'')
514-
self.assertGreater(err, b'')
515-
516-
517-
# Run the tests in Tools/parser/test_unparse.py
518-
with support.DirsOnSysPath(os.path.join(basepath, 'parser')):
519-
from test_unparse import UnparseTestCase
520-
from test_unparse import DirectoryTestCase
521-
522338
if __name__ == '__main__':
523339
unittest.main()

0 commit comments

Comments
 (0)