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

Skip to content

Commit 66f2928

Browse files
committed
Issue #18492: Allow all resources when tests are not run by regrtest.py.
This changeset also includes cleanup allowed by this behavior change.
1 parent 8dd49fe commit 66f2928

14 files changed

Lines changed: 37 additions & 59 deletions

Lib/idlelib/FormatParagraph.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ def get_comment_header(line):
188188
return m.group(1)
189189

190190
if __name__ == "__main__":
191-
from test import support; support.use_resources = ['gui']
192191
import unittest
193192
unittest.main('idlelib.idle_test.test_formatparagraph',
194193
verbosity=2, exit=False)

Lib/idlelib/IdleHistory.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,5 @@ def store(self, source):
100100
self.prefix = None
101101

102102
if __name__ == "__main__":
103-
from test import support
104-
support.use_resources = ['gui']
105103
from unittest import main
106104
main('idlelib.idle_test.test_idlehistory', verbosity=2, exit=False)

Lib/idlelib/SearchEngine.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,5 @@ def get_line_col(index):
229229
return line, col
230230

231231
if __name__ == "__main__":
232-
from test import support; support.use_resources = ['gui']
233232
import unittest
234233
unittest.main('idlelib.idle_test.test_searchengine', verbosity=2, exit=False)

Lib/idlelib/idle_test/README.txt

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,19 @@ Once test_xyy is written, the following should go at the end of xyy.py,
2626
with xyz (lowercased) added after 'test_'.
2727
---
2828
if __name__ == "__main__":
29-
from test import support; support.use_resources = ['gui']
3029
import unittest
3130
unittest.main('idlelib.idle_test.test_', verbosity=2, exit=False)
3231
---
3332

3433

3534
2. Gui Tests
3635

37-
Gui tests need 'requires' and 'use_resources' from test.support
38-
(test.test_support in 2.7). A test is a gui test if it creates a Tk root or
39-
master object either directly or indirectly by instantiating a tkinter or
40-
idle class. For the benefit of buildbot machines that do not have a graphics
41-
screen, gui tests must be 'guarded' by "requires('gui')" in a setUp
42-
function or method. This will typically be setUpClass.
36+
Gui tests need 'requires' from test.support (test.test_support in 2.7). A
37+
test is a gui test if it creates a Tk root or master object either directly
38+
or indirectly by instantiating a tkinter or idle class. For the benefit of
39+
test processes that either have no graphical environment available or are not
40+
allowed to use it, gui tests must be 'guarded' by "requires('gui')" in a
41+
setUp function or method. This will typically be setUpClass.
4342

4443
To avoid interfering with other gui tests, all gui objects must be destroyed
4544
and deleted by the end of the test. If a widget, such as a Tk root, is created
@@ -57,11 +56,17 @@ and class attributes, also delete the widget.
5756
del cls.root
5857
---
5958

60-
Support.requires('gui') returns true if it is either called in a main module
61-
(which never happens on buildbots) or if use_resources contains 'gui'.
62-
Use_resources is set by test.regrtest but not by unittest. So when running
63-
tests in another module with unittest, we set it ourselves, as in the xyz.py
64-
template above.
59+
Support.requires('gui') causes the test(s) it guards to be skipped if any of
60+
a few conditions are met:
61+
- The tests are being run by regrtest.py, and it was started without
62+
enabling the "gui" resource with the "-u" command line option.
63+
- The tests are being run on Windows by a service that is not allowed to
64+
interact with the graphical environment.
65+
- The tests are being run on Mac OSX in a process that cannot make a window
66+
manager connection.
67+
- tkinter.Tk cannot be successfully instantiated for some reason.
68+
- test.support.use_resources has been set by something other than
69+
regrtest.py and does not contain "gui".
6570

6671
Since non-gui tests always run, but gui tests only sometimes, tests of non-gui
6772
operations should best avoid needing a gui. Methods that make incidental use of
@@ -88,8 +93,8 @@ python -m idlelib.idle_test.test_xyz
8893

8994
To run all idle_test/test_*.py tests, either interactively
9095
('>>>', with unittest imported) or from a command line, use one of the
91-
following. (Notes: unittest does not run gui tests; in 2.7, 'test ' (with the
92-
space) is 'test.regrtest '; where present, -v and -ugui can be omitted.)
96+
following. (Notes: in 2.7, 'test ' (with the space) is 'test.regrtest ';
97+
where present, -v and -ugui can be omitted.)
9398

9499
>>> unittest.main('idlelib.idle_test', verbosity=2, exit=False)
95100
python -m unittest -v idlelib.idle_test
@@ -98,13 +103,13 @@ python -m test.test_idle
98103

99104
The idle tests are 'discovered' by idlelib.idle_test.__init__.load_tests,
100105
which is also imported into test.test_idle. Normally, neither file should be
101-
changed when working on individual test modules. The third command runs runs
106+
changed when working on individual test modules. The third command runs
102107
unittest indirectly through regrtest. The same happens when the entire test
103108
suite is run with 'python -m test'. So that command must work for buildbots
104109
to stay green. Idle tests must not disturb the environment in a way that
105110
makes other tests fail (issue 18081).
106111

107112
To run an individual Testcase or test method, extend the dotted name given to
108-
unittest on the command line. (But gui tests will not this way.)
113+
unittest on the command line.
109114

110115
python -m unittest -v idlelib.idle_test.test_xyz.Test_case.test_meth

Lib/test/support/__init__.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -454,23 +454,17 @@ class ProcessSerialNumber(Structure):
454454
return _is_gui_available.result
455455

456456
def is_resource_enabled(resource):
457-
"""Test whether a resource is enabled. Known resources are set by
458-
regrtest.py."""
459-
return use_resources is not None and resource in use_resources
457+
"""Test whether a resource is enabled.
460458
461-
def requires(resource, msg=None):
462-
"""Raise ResourceDenied if the specified resource is not available.
463-
464-
If the caller's module is __main__ then automatically return True. The
465-
possibility of False being returned occurs when regrtest.py is
466-
executing.
459+
Known resources are set by regrtest.py. If not running under regrtest.py,
460+
all resources are assumed enabled unless use_resources has been set.
467461
"""
462+
return use_resources is None or resource in use_resources
463+
464+
def requires(resource, msg=None):
465+
"""Raise ResourceDenied if the specified resource is not available."""
468466
if resource == 'gui' and not _is_gui_available():
469467
raise ResourceDenied(_is_gui_available.reason)
470-
# see if the caller's module is __main__ - if so, treat as if
471-
# the resource was set
472-
if sys._getframe(1).f_globals.get("__name__") == "__main__":
473-
return
474468
if not is_resource_enabled(resource):
475469
if msg is None:
476470
msg = "Use of the %r resource not enabled" % resource

Lib/test/test_codecmaps_hk.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@ def test_main():
1616
support.run_unittest(__name__)
1717

1818
if __name__ == "__main__":
19-
support.use_resources = ['urlfetch']
2019
test_main()

Lib/test/test_decimal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5429,7 +5429,7 @@ def test_sizeof(self):
54295429
all_tests.insert(0, CheckAttributes)
54305430

54315431

5432-
def test_main(arith=False, verbose=None, todo_tests=None, debug=None):
5432+
def test_main(arith=None, verbose=None, todo_tests=None, debug=None):
54335433
""" Execute the tests.
54345434
54355435
Runs all arithmetic tests if arith is True or if the "decimal" resource
@@ -5439,7 +5439,7 @@ def test_main(arith=False, verbose=None, todo_tests=None, debug=None):
54395439
init(C)
54405440
init(P)
54415441
global TEST_ALL, DEBUG
5442-
TEST_ALL = arith or is_resource_enabled('decimal')
5442+
TEST_ALL = arith if arith is not None else is_resource_enabled('decimal')
54435443
DEBUG = debug
54445444

54455445
if todo_tests is None:

Lib/test/test_idle.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,4 @@
1313
load_tests = idletest.load_tests
1414

1515
if __name__ == '__main__':
16-
# Until unittest supports resources, we emulate regrtest's -ugui
17-
# so loaded tests run the same as if textually present here.
18-
# If any Idle test ever needs another resource, add it to the list.
19-
support.use_resources = ['gui'] # use_resources is initially None
2016
unittest.main(verbosity=2, exit=False)

Lib/test/test_imaplib.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,5 +501,4 @@ def load_tests(*args):
501501

502502

503503
if __name__ == "__main__":
504-
support.use_resources = ['network']
505504
unittest.main()

Lib/test/test_robotparser.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,5 +291,4 @@ def load_tests(loader, suite, pattern):
291291
return suite
292292

293293
if __name__=='__main__':
294-
support.use_resources = ['network']
295294
unittest.main()

0 commit comments

Comments
 (0)