Debian Codegraph @

src/lazr/uri/tests/test_docs.py

raw symbols SCIP
1 # Copyright 2009 Canonical Ltd. All rights reserved.
2 #
3 # This file is part of lazr.uri
4 #
5 # lazr.uri 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
7 # the Free Software Foundation, version 3 of the License.
8 #
9 # lazr.uri is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12 # License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with lazr.uri. If not, see <http://www.gnu.org/licenses/>.
16
17 """Test harness for doctests."""
18
19 __all__ = [
20 "load_tests",
21 ]
22
23 import doctest
24 import importlib.util
25 from pathlib import Path
26
27 DOCTEST_FLAGS = (
28 doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE | doctest.REPORT_NDIFF
29 )
30
31
32 def find_doctests(suffix):
33 """Find doctests matching a certain suffix."""
34 doctest_files = []
35 spec = importlib.util.find_spec("lazr.uri")
36 if not spec or not spec.submodule_search_locations:
37 return doctest_files
38 docs_path = Path(list(spec.submodule_search_locations)[0]) / "docs"
39 for path in docs_path.iterdir():
40 if path.name.endswith(suffix):
41 doctest_files.append(str(path.resolve()))
42 return doctest_files
43
44
45 def load_tests(loader, tests, pattern):
46 """Load all the doctests."""
47 tests.addTest(
48 doctest.DocFileSuite(
49 *find_doctests(".rst"),
50 module_relative=False,
51 optionflags=DOCTEST_FLAGS,
52 )
53 )
54 return tests