Skip to content

Commit

Permalink
Merge pull request #173 from lsst/tickets/DM-40817
Browse files Browse the repository at this point in the history
DM-40817: Allow skipping modules in ImportTestCase
  • Loading branch information
arunkannawadi committed Sep 19, 2023
2 parents 42b57e1 + 413e2b0 commit 3bf5ff2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/changes/DM-40817.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow for skipping files in ImportTestCase using SKIP_FILES class variable.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ dependencies = [
"deprecated >= 1.2",
"pyyaml >= 5.1",
"astropy >= 5.0",
"threadpoolctl"
"threadpoolctl",
"importlib_resources",
]
dynamic = ["version"]
[project.urls]
Expand Down
23 changes: 21 additions & 2 deletions python/lsst/utils/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@
import tempfile
import unittest
import warnings
from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
from importlib import resources
from collections.abc import Callable, Container, Iterable, Iterator, Mapping, Sequence

if sys.version_info < (3, 10, 0):
import importlib_resources as resources
else:
from importlib import resources

from typing import Any, ClassVar

import numpy
Expand Down Expand Up @@ -346,6 +351,16 @@ class ImportTestCase(unittest.TestCase):
PACKAGES: ClassVar[Iterable[str]] = ()
"""Packages to be imported."""

SKIP_FILES: ClassVar[Mapping[str, Container[str]]] = {}
"""Files to be skipped importing; specified as key-value pairs.
The key is the package name and the value is a set of files names in that
package to skip.
Note: Files with names not ending in .py or beginning with leading double
underscores are always skipped.
"""

_n_registered = 0
"""Number of packages registered for testing by this class."""

Expand Down Expand Up @@ -383,10 +398,14 @@ def test_import(*args: Any, mod=mod) -> None:
def assertImport(self, root_pkg):
for file in resources.files(root_pkg).iterdir():
file = file.name
# When support for python 3.9 is dropped, this could be updated to
# use match case construct.
if not file.endswith(".py"):
continue
if file.startswith("__"):
continue
if file in self.SKIP_FILES.get(root_pkg, ()):
continue
root, _ = os.path.splitext(file)
module_name = f"{root_pkg}.{root}"
with self.subTest(module=module_name):
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ deprecated >= 1.2
pyyaml >5.1
astropy >= 5.0
threadpoolctl
importlib_resources
11 changes: 11 additions & 0 deletions tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,16 @@ def test_counter(self):
self.assertEqual(self._n_registered, 1)


class TestSkipImports(ImportTestCase):
"""Test that we can run the import tests with some modules skipped."""

PACKAGES = ("import_test.two.three",)
SKIP_FILES = {"import_test.two.three": {"runtime.py", "fail.py"}}

def test_import(self):
"""Test that we can import utils."""
import import_test # noqa: F401


if __name__ == "__main__":
unittest.main()

0 comments on commit 3bf5ff2

Please sign in to comment.