Coverage for tests/test_cliPluginLoader.py : 33%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22"""Unit tests for the daf_butler CLI plugin loader.
23"""
25import click
26import click.testing
27from collections import defaultdict
28from contextlib import contextmanager
29import os
30import unittest
31from unittest.mock import patch
32import yaml
34from lsst.daf.butler.cli import butler, cmd
37@click.command()
38def command_test():
39 click.echo("test command")
42@contextmanager
43def command_test_env(runner):
44 """A context manager that creates (and then cleans up) an environment that
45 provides a plugin command named 'command-test'.
47 Parameters
48 ----------
49 runner : click.testing.CliRunner
50 The test runner to use to create the isolated filesystem.
51 """
52 with runner.isolated_filesystem():
53 with open("resources.yaml", "w") as f:
54 f.write(yaml.dump({"cmd": {"import": "test_cliPluginLoader", "commands": ["command-test"]}}))
55 with patch.dict("os.environ", {"DAF_BUTLER_PLUGINS": os.path.realpath(f.name)}):
56 yield
59@contextmanager
60def duplicate_command_test_env(runner):
61 """A context manager that creates (and then cleans up) an environment that
62 declares a plugin command named 'create', which will conflict with the
63 daf_butler 'create' command.
65 Parameters
66 ----------
67 runner : click.testing.CliRunner
68 The test runner to use to create the isolated filesystem.
69 """
70 with runner.isolated_filesystem():
71 with open("resources.yaml", "w") as f:
72 f.write(yaml.dump({"cmd": {"import": "test_cliPluginLoader", "commands": ["create"]}}))
73 with patch.dict("os.environ", {"DAF_BUTLER_PLUGINS": os.path.realpath(f.name)}):
74 yield
77class Suite(unittest.TestCase):
79 def setUp(self):
80 butler.cli.commands = None
82 def tearDown(self):
83 butler.cli.commands = None
85 def test_loadAndExecutePluginCommand(self):
86 """Test that a plugin command can be loaded and executed."""
87 runner = click.testing.CliRunner()
88 with command_test_env(runner):
89 result = runner.invoke(butler.cli, "command-test")
90 self.assertEqual(result.exit_code, 0, result.output)
91 self.assertEqual(result.stdout, "test command\n")
93 def test_loadAndExecuteLocalCommand(self):
94 """Test that a command in daf_butler can be loaded and executed."""
95 runner = click.testing.CliRunner()
96 with runner.isolated_filesystem():
97 result = runner.invoke(butler.cli, ["create", "test_repo"])
98 self.assertEqual(result.exit_code, 0, result.output)
99 self.assertTrue(os.path.exists("test_repo"))
101 def test_loadTopHelp(self):
102 """Test that an expected command is produced by 'butler --help'"""
103 runner = click.testing.CliRunner()
104 with command_test_env(runner):
105 result = runner.invoke(butler.cli, "--help")
106 self.assertEqual(result.exit_code, 0, result.stdout)
107 self.assertIn("command-test", result.stdout)
109 def test_getLocalCommands(self):
110 """Test getting the daf_butler CLI commands."""
111 localCommands = butler.LoaderCLI._getLocalCommands()
112 for command in cmd.__all__:
113 command = command.replace("_", "-")
114 self.assertEqual(localCommands[command], ["lsst.daf.butler.cli.cmd"])
116 def test_mergeCommandLists(self):
117 """Verify dicts of command to list-of-source-package get merged
118 properly."""
119 first = defaultdict(list, {"a": [1]})
120 second = defaultdict(list, {"b": [2]})
121 self.assertEqual(butler.LoaderCLI._mergeCommandLists(first, second), {"a": [1], "b": [2]})
122 first = defaultdict(list, {"a": [1]})
123 second = defaultdict(list, {"a": [2]})
124 self.assertEqual(butler.LoaderCLI._mergeCommandLists(first, second), {"a": [1, 2]})
126 def test_listCommands_duplicate(self):
127 """Test executing a command in a situation where duplicate commands are
128 present and verify it fails to run.
129 """
130 self.maxDiff = None
131 runner = click.testing.CliRunner()
132 with duplicate_command_test_env(runner):
133 result = runner.invoke(butler.cli, ["create", "test_repo"])
134 self.assertEqual(result.exit_code, 1, result.output)
135 self.assertEqual(result.output, "Error: Command 'create' "
136 "exists in packages lsst.daf.butler.cli.cmd, test_cliPluginLoader. "
137 "Duplicate commands are not supported, aborting.\n")
140if __name__ == "__main__": 140 ↛ 141line 140 didn't jump to line 141, because the condition on line 140 was never true
141 unittest.main()