Skip to content

Commit

Permalink
chore(test): Add test for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tcjennings committed Dec 16, 2024
1 parent 5b10c89 commit 7ed95cd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion python/lsst/ctrl/mpexec/cli/cmd/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from collections.abc import Iterator, Sequence
from contextlib import contextmanager
from functools import partial
from importlib import import_module
from tempfile import NamedTemporaryFile
from typing import Any

Expand Down Expand Up @@ -146,7 +147,7 @@ def coverage_context(kwargs: dict[str, Any]) -> Iterator[None]:
return
# Lazily import coverage only when we might need it
try:
import coverage
coverage = import_module("coverage")
except ModuleNotFoundError:
raise click.ClickException("coverage was requested but the coverage package is not installed.")
with NamedTemporaryFile("w") as rcfile:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_cliScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import os
import tempfile
import unittest
import unittest.mock

import click
import lsst.utils.tests
from lsst.ctrl.mpexec.cli import opt, script
from lsst.ctrl.mpexec.cli.cmd.commands import coverage_context
from lsst.ctrl.mpexec.cli.pipetask import cli as pipetaskCli
from lsst.ctrl.mpexec.showInfo import ShowInfo
from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg
Expand Down Expand Up @@ -203,6 +205,25 @@ def cli(**kwargs):
self.assertNotEqual(result.exit_code, 0)


class CoverageTestCase(unittest.TestCase):
"""Test coverage context manager."""

@unittest.mock.patch.dict("sys.modules", coverage=unittest.mock.MagicMock())
def testWithCoverage(self):
"""Test that the coverage context manager runs when invoked."""
with coverage_context({"coverage": True}):
self.assertTrue(True)

@unittest.mock.patch("lsst.ctrl.mpexec.cli.cmd.commands.import_module", side_effect=ModuleNotFoundError())
def testWithMissingCoverage(self, mock_import):
"""Test that the coverage context manager complains when coverage is
not available.
"""
with self.assertRaises(click.exceptions.ClickException):
with coverage_context({"coverage": True}):
pass

Check warning on line 224 in tests/test_cliScript.py

View check run for this annotation

Codecov / codecov/patch

tests/test_cliScript.py#L224

Added line #L224 was not covered by tests


if __name__ == "__main__":
lsst.utils.tests.init()
unittest.main()

0 comments on commit 7ed95cd

Please sign in to comment.