Fix #5273: doctest: add :skipif: option for doctest directives

This option allows conditional skipping of doctests.
This commit is contained in:
Antti Kaihola 2018-08-07 10:51:49 +03:00
parent b01256dbaf
commit 10f01d5539

View File

@ -90,6 +90,16 @@ class TestDirective(SphinxDirective):
def run(self): def run(self):
# type: () -> List[nodes.Node] # type: () -> List[nodes.Node]
if 'skipif' in self.options:
condition = self.options['skipif']
context = {}
if self.config.doctest_global_setup:
exec(self.config.doctest_global_setup, context)
should_skip = eval(condition, context)
if self.config.doctest_global_cleanup:
exec(self.config.doctest_global_cleanup, context)
if should_skip:
return []
# use ordinary docutils nodes for test code: they get special attributes # use ordinary docutils nodes for test code: they get special attributes
# so that our builder recognizes them, and the other builders are happy. # so that our builder recognizes them, and the other builders are happy.
code = '\n'.join(self.content) code = '\n'.join(self.content)
@ -155,11 +165,11 @@ class TestDirective(SphinxDirective):
class TestsetupDirective(TestDirective): class TestsetupDirective(TestDirective):
option_spec = {} # type: Dict option_spec = {'skipif': directives.unchanged_required} # type: Dict
class TestcleanupDirective(TestDirective): class TestcleanupDirective(TestDirective):
option_spec = {} # type: Dict option_spec = {'skipif': directives.unchanged_required} # type: Dict
class DoctestDirective(TestDirective): class DoctestDirective(TestDirective):
@ -167,6 +177,7 @@ class DoctestDirective(TestDirective):
'hide': directives.flag, 'hide': directives.flag,
'options': directives.unchanged, 'options': directives.unchanged,
'pyversion': directives.unchanged_required, 'pyversion': directives.unchanged_required,
'skipif': directives.unchanged_required,
} }
@ -174,6 +185,7 @@ class TestcodeDirective(TestDirective):
option_spec = { option_spec = {
'hide': directives.flag, 'hide': directives.flag,
'pyversion': directives.unchanged_required, 'pyversion': directives.unchanged_required,
'skipif': directives.unchanged_required,
} }
@ -182,6 +194,7 @@ class TestoutputDirective(TestDirective):
'hide': directives.flag, 'hide': directives.flag,
'options': directives.unchanged, 'options': directives.unchanged,
'pyversion': directives.unchanged_required, 'pyversion': directives.unchanged_required,
'skipif': directives.unchanged_required,
} }