LAL  7.5.0.1-b72065a
test_python_imports.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2020 Cardiff University
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 2 of the License, or (at your
8 # option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19 """Test suite to validate that all modules inside a package can be imported
20 """
21 
22 import os
23 import sys
24 from importlib import import_module
25 from pathlib import Path
26 
27 import pytest
28 
29 # name of this package
30 PACKAGE = "lal"
31 
32 # are we currently in the TEST phase of a conda build?
33 CONDA_BUILD_TEST = os.getenv("CONDA_BUILD_STATE") == "TEST"
34 
35 # default run arguments
36 DEFAULT_PYTEST_ARGUMENTS = [
37  "-v",
38  "-rs",
39  "--junit-xml=junit-imports.xml",
40 ]
41 
42 # paths
43 HERE = Path(__file__).parent.absolute()
44 BUILDDIR = (HERE / Path(".")).resolve()
45 SRCDIR = (HERE / Path(".")).resolve()
46 TOPBUILDDIR = (HERE / Path("../..")).resolve()
47 
48 # path to exclusion file
49 EXCLUDEFILE = SRCDIR / "exclude-modules.txt"
50 
51 # find directory in which to search for module files
52 try:
53  # first, just try to import the package, and use that path
54  PYTHONMODDIR = Path(import_module(PACKAGE).__path__[0])
55 except ImportError: # failed to import, which is bad
56  # fall back to searching the src tree
57  PYTHONMODDIR = TOPBUILDDIR / "python" / PACKAGE
58 
59 
60 # -- utilities ----------------------------------
61 
62 def read_exclude_file(source):
63  """Read all excluded file paths from the given source file
64  """
65  excludes = set()
66  try:
67  with open(str(source), "r") as fobj:
68  for line in fobj:
69  if isinstance(line, bytes):
70  line = line.decode("utf-8")
71  content = line.strip().split("#", 1)[0].strip()
72  if content:
73  excludes.add(content)
74  except FileNotFoundError as exc:
75  warnings.warn(str(exc))
76  return excludes
77 
78 
79 def find_modules(path):
80  """Returns the paths to all python module files
81  """
82  return {str(x.relative_to(path.parent)) for x in path.rglob("*.py")}
83 
84 
85 def path_to_name(filepath):
86  """Returns the module name for a given file path
87 
88  >>> path_to_name("lal/__init__.py")
89  "lal"
90  >>> path_to_name("lal/module.py")
91  "lal.module"
92  """
93  name = filepath.replace(".py", "").replace(r"/", ".")
94  if name.endswith(".__init__"):
95  return name[:-9]
96  return name
97 
98 
99 # -- test ---------------------------------------
100 
101 EXCLUDE = set(map(path_to_name, read_exclude_file(EXCLUDEFILE)))
102 MODULES = [path_to_name(x) for x in sorted(find_modules(PYTHONMODDIR))]
103 
104 # only parametrize if we have something to loop over, this allows pytest
105 # to exit with the 'no tests collected' code (5) which should be considered
106 # as an error for LALSuite subpackages
107 if MODULES:
108  @pytest.mark.parametrize("module", MODULES)
109  def test_import(module):
110  """Test that the named module can be imported
111  """
112  if module in EXCLUDE:
113  pytest.skip("excluded {}".format(str(module)))
114  import_module(module)
115 
116 
117 # -- command-line -------------------------------
118 
119 if __name__ == "__main__":
120  args = sys.argv[1:] or DEFAULT_PYTEST_ARGUMENTS
121  sys.exit(pytest.main(args=[__file__] + args))
static int replace(int c, void *param)
Definition: LALString.c:373
def test_import(module)
Test that the named module can be imported.
def path_to_name(filepath)
Returns the module name for a given file path.
def read_exclude_file(source)
Read all excluded file paths from the given source file.
def find_modules(path)
Returns the paths to all python module files.