Coverage for tests/test_cliPluginLoader.py: 30%

77 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-08-31 10:07 +0000

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 os 

26import unittest 

27from collections import defaultdict 

28from contextlib import contextmanager 

29from unittest.mock import patch 

30 

31import click 

32import yaml 

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

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

35 

36 

37@click.command() 

38def command_test(): 

39 click.echo(message="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 def setUp(self): 

62 self.runner = LogCliRunner() 

63 

64 def test_unimportablePlugin(self): 

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

66 with self.assertLogs() as cm: 

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

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

69 expectedErrMsg = ( 

70 "Could not import plugin from " 

71 "test_cliPluginLoader.non_existant_command_function, skipping." 

72 ) 

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

74 

75 def test_unimportableLocalPackage(self): 

76 class FailCLI(butler.LoaderCLI): 

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

78 

79 @click.command(cls=FailCLI) 

80 def cli(): 

81 pass 

82 

83 with self.assertLogs() as cm: 

84 result = self.runner.invoke(cli) 

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

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

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

88 

89 

90class PluginLoaderTest(unittest.TestCase): 

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( 

141 result.output, 

142 "Error: Command 'create' " 

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

144 "Duplicate commands are not supported, aborting.\n", 

145 ) 

146 

147 

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

149 unittest.main()