1- # -*- coding: utf-8 -*-
2- """Decorators for labeling test objects.
3-
4- Decorators that merely return a modified version of the original function
5- object are straightforward. Decorators that return a new function object need
6- to use nose.tools.make_decorator(original_function)(decorator) in returning the
7- decorator, in order to preserve metadata such as function name, setup and
8- teardown functions and so on - see nose.tools for more information.
9-
10- This module provides a set of useful decorators meant to be ready to use in
11- your own tests. See the bottom of the file for the ready-made ones, and if you
12- find yourself writing a new one that may be of generic use, add it here.
13-
14- Included decorators:
15-
16-
17- Lightweight testing that remains unittest-compatible.
18-
19- - An @as_unittest decorator can be used to tag any normal parameter-less
20- function as a unittest TestCase. Then, both nose and normal unittest will
21- recognize it as such. This will make it easier to migrate away from Nose if
22- we ever need/want to while maintaining very lightweight tests.
23-
24- NOTE: This file contains IPython-specific decorators. Using the machinery in
25- IPython.external.decorators, we import either numpy.testing.decorators if numpy is
26- available, OR use equivalent code in IPython.external._decorators, which
27- we've copied verbatim from numpy.
28-
29- """
30-
31- # Copyright (c) IPython Development Team.
32- # Distributed under the terms of the Modified BSD License.
33-
341import os
352import shutil
363import sys
374import tempfile
38- import unittest
395from importlib import import_module
406
41- from decorator import decorator
42-
437# Expose the unittest-driven decorators
448from .ipunittest import ipdoctest , ipdocstring
459
46- #-----------------------------------------------------------------------------
10+ # -----------------------------------------------------------------------------
4711# Classes and functions
48- #-----------------------------------------------------------------------------
12+ # -----------------------------------------------------------------------------
4913
5014# Utility functions
5115
@@ -95,10 +59,12 @@ def skip(msg=None):
9559 decorator : function
9660 Decorator, which, when applied to a function, causes SkipTest
9761 to be raised, with the optional message added.
98- """
62+ """
9963 if msg and not isinstance (msg , str ):
100- raise ValueError ('invalid object passed to `@skip` decorator, did you '
101- 'meant `@skip()` with brackets ?' )
64+ raise ValueError (
65+ "invalid object passed to `@skip` decorator, did you "
66+ "meant `@skip()` with brackets ?"
67+ )
10268 return skipif (True , msg )
10369
10470
@@ -107,7 +73,8 @@ def onlyif(condition, msg):
10773
10874 return skipif (not condition , msg )
10975
110- #-----------------------------------------------------------------------------
76+
77+ # -----------------------------------------------------------------------------
11178# Utility functions for decorators
11279def module_not_available (module ):
11380 """Can module be imported? Returns true if module does NOT import.
@@ -124,15 +91,15 @@ def module_not_available(module):
12491 return mod_not_avail
12592
12693
127- #-----------------------------------------------------------------------------
94+ # -----------------------------------------------------------------------------
12895# Decorators for public use
12996
13097# Decorators to skip certain tests on specific platforms.
131- skip_win32 = skipif (sys .platform == ' win32' ,
132- "This test does not run under Windows" )
133- skip_linux = skipif ( sys .platform .startswith (' linux' ),
134- "This test does not run under Linux" )
135- skip_osx = skipif (sys .platform == ' darwin' , "This test does not run under OS X" )
98+ skip_win32 = skipif (sys .platform == " win32" , "This test does not run under Windows" )
99+ skip_linux = skipif (
100+ sys .platform .startswith (" linux" ), "This test does not run under Linux"
101+ )
102+ skip_osx = skipif (sys .platform == " darwin" , "This test does not run under OS X" )
136103
137104
138105# Decorators to skip tests if not on specific platforms.
@@ -144,20 +111,23 @@ def module_not_available(module):
144111 not sys .platform .startswith ("darwin" ), "This test only runs under macOS"
145112)
146113
147- _x11_skip_cond = (sys .platform not in ('darwin' , 'win32' ) and
148- os .environ .get ('DISPLAY' , '' ) == '' )
114+ _x11_skip_cond = (
115+ sys .platform not in ("darwin" , "win32" ) and os .environ .get ("DISPLAY" , "" ) == ""
116+ )
149117_x11_skip_msg = "Skipped under *nix when X11/XOrg not available"
150118
151119skip_if_no_x11 = skipif (_x11_skip_cond , _x11_skip_msg )
152120
153121# Other skip decorators
154122
155123# generic skip without module
156- skip_without = lambda mod : skipif (module_not_available (mod ), "This test requires %s" % mod )
124+ skip_without = lambda mod : skipif (
125+ module_not_available (mod ), "This test requires %s" % mod
126+ )
157127
158- skipif_not_numpy = skip_without (' numpy' )
128+ skipif_not_numpy = skip_without (" numpy" )
159129
160- skipif_not_matplotlib = skip_without (' matplotlib' )
130+ skipif_not_matplotlib = skip_without (" matplotlib" )
161131
162132# A null 'decorator', useful to make more readable code that needs to pick
163133# between different decorators based on OS or other conditions
@@ -166,15 +136,18 @@ def module_not_available(module):
166136# Some tests only run where we can use unicode paths. Note that we can't just
167137# check os.path.supports_unicode_filenames, which is always False on Linux.
168138try :
169- f = tempfile .NamedTemporaryFile (prefix = u "tmp€" )
139+ f = tempfile .NamedTemporaryFile (prefix = "tmp€" )
170140except UnicodeEncodeError :
171141 unicode_paths = False
142+ # TODO: should this be finnally ?
172143else :
173144 unicode_paths = True
174145 f .close ()
175146
176- onlyif_unicode_paths = onlyif (unicode_paths , ("This test is only applicable "
177- "where we can use unicode in filenames." ))
147+ onlyif_unicode_paths = onlyif (
148+ unicode_paths ,
149+ ("This test is only applicable where we can use unicode in filenames." ),
150+ )
178151
179152
180153def onlyif_cmds_exist (* commands ):
0 commit comments