Coverage for tests/test_cliPluginLoader.py: 38%

Shortcuts 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

77 statements  

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/>. 

21 

22"""Unit tests for the daf_butler CLI plugin loader. 

23""" 

24 

25import click 

26from collections import defaultdict 

27from contextlib import contextmanager 

28import os 

29import unittest 

30from unittest.mock import patch 

31import yaml 

32 

33from lsst.daf.butler.cli import butler, cmd 

34from lsst.daf.butler.cli.utils import command_test_env, LogCliRunner 

35 

36 

37@click.command() 

38def command_test(): 

39 click.echo("test command") 

40 

41 

42@contextmanager 

43def duplicate_command_test_env(runner): 

44 """A context manager that creates (and then cleans up) an environment that 

45 declares a plugin command named 'create', which will conflict with the 

46 daf_butler 'create' command. 

47 

48 Parameters 

49 ---------- 

50 runner : click.testing.CliRunner 

51 The test runner to use to create the isolated filesystem. 

52 """ 

53 with runner.isolated_filesystem(): 

54 with open("resources.yaml", "w") as f: 

55 f.write(yaml.dump({"cmd": {"import": "test_cliPluginLoader", "commands": ["create"]}})) 

56 with patch.dict("os.environ", {"DAF_BUTLER_PLUGINS": os.path.realpath(f.name)}): 

57 yield 

58 

59 

60class FailedLoadTest(unittest.TestCase): 

61 

62 def setUp(self): 

63 self.runner = LogCliRunner() 

64 

65 def test_unimportablePlugin(self): 

66 with command_test_env(self.runner, "test_cliPluginLoader", "non-existant-command-function"): 

67 with self.assertLogs() as cm: 

68 result = self.runner.invoke(butler.cli, "--help") 

69 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}") 

70 expectedErrMsg = "Could not import plugin from " \ 

71 "test_cliPluginLoader.non_existant_command_function, skipping." 

72 self.assertIn(expectedErrMsg, " ".join(cm.output)) 

73 

74 def test_unimportableLocalPackage(self): 

75 class FailCLI(butler.LoaderCLI): 

76 localCmdPkg = "lsst.daf.butler.cli.cmds" # should not be an importable location 

77 

78 @click.command(cls=FailCLI) 

79 def cli(): 

80 pass 

81 

82 with self.assertLogs() as cm: 

83 result = self.runner.invoke(cli) 

84 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}") 

85 expectedErrMsg = f"Could not import plugin from {FailCLI.localCmdPkg}, skipping." 

86 self.assertIn(expectedErrMsg, " ".join(cm.output)) 

87 

88 

89class PluginLoaderTest(unittest.TestCase): 

90 

91 def setUp(self): 

92 self.runner = LogCliRunner() 

93 

94 def test_loadAndExecutePluginCommand(self): 

95 """Test that a plugin command can be loaded and executed.""" 

96 with command_test_env(self.runner, "test_cliPluginLoader", "command-test"): 

97 result = self.runner.invoke(butler.cli, "command-test") 

98 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}") 

99 self.assertEqual(result.stdout, "test command\n") 

100 

101 def test_loadAndExecuteLocalCommand(self): 

102 """Test that a command in daf_butler can be loaded and executed.""" 

103 with self.runner.isolated_filesystem(): 

104 result = self.runner.invoke(butler.cli, ["create", "test_repo"]) 

105 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}") 

106 self.assertTrue(os.path.exists("test_repo")) 

107 

108 def test_loadTopHelp(self): 

109 """Test that an expected command is produced by 'butler --help'""" 

110 with command_test_env(self.runner, "test_cliPluginLoader", "command-test"): 

111 result = self.runner.invoke(butler.cli, "--help") 

112 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}") 

113 self.assertIn("command-test", result.stdout) 

114 

115 def test_getLocalCommands(self): 

116 """Test getting the daf_butler CLI commands.""" 

117 localCommands = butler.ButlerCLI().getLocalCommands() 

118 # the number of local commands should equal the number of functions 

119 # in cmd.__all__ 

120 self.assertEqual(len(localCommands), len(cmd.__all__)) 

121 

122 def test_mergeCommandLists(self): 

123 """Verify dicts of command to list-of-source-package get merged 

124 properly.""" 

125 first = defaultdict(list, {"a": [1]}) 

126 second = defaultdict(list, {"b": [2]}) 

127 self.assertEqual(butler.LoaderCLI._mergeCommandLists(first, second), {"a": [1], "b": [2]}) 

128 first = defaultdict(list, {"a": [1]}) 

129 second = defaultdict(list, {"a": [2]}) 

130 self.assertEqual(butler.LoaderCLI._mergeCommandLists(first, second), {"a": [1, 2]}) 

131 

132 def test_listCommands_duplicate(self): 

133 """Test executing a command in a situation where duplicate commands are 

134 present and verify it fails to run. 

135 """ 

136 self.maxDiff = None 

137 with duplicate_command_test_env(self.runner): 

138 result = self.runner.invoke(butler.cli, ["create", "test_repo"]) 

139 self.assertEqual(result.exit_code, 1, f"output: {result.output} exception: {result.exception}") 

140 self.assertEqual(result.output, "Error: Command 'create' " 

141 "exists in packages lsst.daf.butler.cli.cmd, test_cliPluginLoader. " 

142 "Duplicate commands are not supported, aborting.\n") 

143 

144 

145if __name__ == "__main__": 145 ↛ 146line 145 didn't jump to line 146, because the condition on line 145 was never true

146 unittest.main()