Debian Codegraph @

flufl/password/tests/test_documentation.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 os
29 import atexit
30 import doctest
31 import unittest
32
33 from pkg_resources import (
34 resource_filename, resource_exists, resource_listdir, cleanup_resources)
35
36
37 COMMASPACE = ', '
38 DOT = '.'
39 DOCTEST_FLAGS = (
40 doctest.ELLIPSIS |
41 doctest.NORMALIZE_WHITESPACE |
42 doctest.REPORT_NDIFF)
43
44
45
46 def stop():
47 """Call into pdb.set_trace()"""
48 # Do the import here so that you get the wacky special hacked pdb instead
49 # of Python's normal pdb.
50 import pdb
51 pdb.set_trace()
52
53
54
55 def show(thing):
56 # Python 2 and 3 display bytes objects differently, even with print. This
57 # makes it more difficult to write doctests that work in both versions of
58 # Python, because hashed passwords are all bytes. The root cause is that
59 # in Python 2, `bytes` is just an alias for `str` and in Python 3, the
60 # repr of bytes includes the b'' prefix. Use this instead of print() in
61 # the doctests.
62 if isinstance(thing, bytes) and (bytes is not str):
63 # There are lots of ways we could do this.
64 thing = repr(thing)[2:-1]
65 print(thing)
66
67
68
69 def setup(testobj):
70 """Test setup."""
71 # Make sure future statements in our doctests match the Python code. When
72 # run with 2to3, the future import gets removed and these names are not
73 # defined.
74 try:
75 testobj.globs['absolute_import'] = absolute_import
76 testobj.globs['print_function'] = print_function
77 testobj.globs['unicode_literals'] = unicode_literals
78 except NameError:
79 pass
80 testobj.globs['show'] = show
81 testobj.globs['stop'] = stop
82
83
84
85 def additional_tests():
86 "Run the doc tests (README.rst and docs/*, if any exist)"
87 doctest_files = [
88 os.path.abspath(resource_filename('flufl.password', 'README.rst'))]
89 if resource_exists('flufl.password', 'docs'):
90 for name in resource_listdir('flufl.password', 'docs'):
91 if name.endswith('.rst'):
92 doctest_files.append(
93 os.path.abspath(
94 resource_filename('flufl.password', 'docs/%s' % name)))
95 kwargs = dict(module_relative=False,
96 optionflags=DOCTEST_FLAGS,
97 setUp=setup,
98 )
99 atexit.register(cleanup_resources)
100 return unittest.TestSuite((
101 doctest.DocFileSuite(*doctest_files, **kwargs)))