| 1 |
# Copyright (C) 2004-2014 by Barry A. Warsaw |
| 2 |
# |
| 3 |
# This file is part of flufl.password. |
| 4 |
# |
| 5 |
# flufl.password is free software: you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU Lesser General Public License as published by the |
| 7 |
# Free Software Foundation, either version 3 of the License, or (at your |
| 8 |
# option) any later version. |
| 9 |
# |
| 10 |
# flufl.password is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 12 |
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 13 |
# License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU Lesser General Public License |
| 16 |
# along with flufl.password. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
"""Test harness for doctests.""" |
| 19 |
|
| 20 |
from __future__ import absolute_import, print_function, unicode_literals |
| 21 |
|
| 22 |
__metaclass__ = type |
| 23 |
__all__ = [ |
| 24 |
'additional_tests', |
| 25 |
] |
| 26 |
|
| 27 |
|
| 28 |
import doctest |
| •29 |
import importlib.util |
| 30 |
import unittest |
| •31 |
from pathlib import Path |
| 32 |
|
| 33 |
|
| 34 |
COMMASPACE = ', ' |
| 35 |
DOT = '.' |
| 36 |
DOCTEST_FLAGS = ( |
| 37 |
doctest.ELLIPSIS | |
| 38 |
doctest.NORMALIZE_WHITESPACE | |
| 39 |
doctest.REPORT_NDIFF) |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
def stop(): |
| 44 |
"""Call into pdb.set_trace()""" |
| 45 |
# Do the import here so that you get the wacky special hacked pdb instead |
| 46 |
# of Python's normal pdb. |
| 47 |
import pdb |
| 48 |
pdb.set_trace() |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
def show(thing): |
| 53 |
# Python 2 and 3 display bytes objects differently, even with print. This |
| 54 |
# makes it more difficult to write doctests that work in both versions of |
| 55 |
# Python, because hashed passwords are all bytes. The root cause is that |
| 56 |
# in Python 2, `bytes` is just an alias for `str` and in Python 3, the |
| 57 |
# repr of bytes includes the b'' prefix. Use this instead of print() in |
| 58 |
# the doctests. |
| 59 |
if isinstance(thing, bytes) and (bytes is not str): |
| 60 |
# There are lots of ways we could do this. |
| 61 |
thing = repr(thing)[2:-1] |
| 62 |
print(thing) |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
def setup(testobj): |
| 67 |
"""Test setup.""" |
| 68 |
# Make sure future statements in our doctests match the Python code. When |
| 69 |
# run with 2to3, the future import gets removed and these names are not |
| 70 |
# defined. |
| 71 |
try: |
| 72 |
testobj.globs['absolute_import'] = absolute_import |
| 73 |
testobj.globs['print_function'] = print_function |
| 74 |
testobj.globs['unicode_literals'] = unicode_literals |
| 75 |
except NameError: |
| 76 |
pass |
| 77 |
testobj.globs['show'] = show |
| 78 |
testobj.globs['stop'] = stop |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
def additional_tests(): |
| 83 |
"Run the doc tests (README.rst and docs/*, if any exist)" |
| •84 |
doctest_files = [] |
| 85 |
spec = importlib.util.find_spec('flufl.password') |
| 86 |
if not spec or not spec.submodule_search_locations: |
| 87 |
return doctest_files |
| 88 |
path = Path(next(iter(spec.submodule_search_locations))) |
| 89 |
doctest_files = [str((path / 'README.rst').resolve())] |
| 90 |
docs_path = path / 'docs' |
| 91 |
for path in docs_path.iterdir(): |
| 92 |
if path.name.endswith('.rst'): |
| 93 |
doctest_files.append(str(path.resolve())) |
| 94 |
kwargs = dict(module_relative=False, |
| 95 |
optionflags=DOCTEST_FLAGS, |
| 96 |
setUp=setup, |
| 97 |
) |
| 98 |
return unittest.TestSuite(( |
| 99 |
doctest.DocFileSuite(*doctest_files, **kwargs))) |