Hide keyboard shortcuts

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

21 

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

23""" 

24 

25import click 

26import click.testing 

27from collections import defaultdict 

28from contextlib import contextmanager 

29import os 

30import unittest 

31from unittest.mock import patch 

32import yaml 

33 

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

35 

36 

37@click.command() 

38def command_test(): 

39 click.echo("test command") 

40 

41 

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

46 

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 

57 

58 

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. 

64 

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 

75 

76 

77class PluginLoaderTest(unittest.TestCase): 

78 

79 def test_loadAndExecutePluginCommand(self): 

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

81 runner = click.testing.CliRunner() 

82 with command_test_env(runner): 

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

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

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

86 

87 def test_loadAndExecuteLocalCommand(self): 

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

89 runner = click.testing.CliRunner() 

90 with runner.isolated_filesystem(): 

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

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

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

94 

95 def test_loadTopHelp(self): 

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

97 runner = click.testing.CliRunner() 

98 with command_test_env(runner): 

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

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

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

102 

103 def test_getLocalCommands(self): 

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

105 localCommands = butler.LoaderCLI._getLocalCommands() 

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

107 # in cmd.__all__ 

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

109 for command, pkg in localCommands.items(): 

110 self.assertEqual(pkg, ["lsst.daf.butler.cli.cmd"]) 

111 

112 def test_mergeCommandLists(self): 

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

114 properly.""" 

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

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

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

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

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

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

121 

122 def test_listCommands_duplicate(self): 

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

124 present and verify it fails to run. 

125 """ 

126 self.maxDiff = None 

127 runner = click.testing.CliRunner() 

128 with duplicate_command_test_env(runner): 

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

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

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

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

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

134 

135 

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

137 unittest.main()