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 Suite(unittest.TestCase): 

78 

79 def setUp(self): 

80 butler.cli.commands = None 

81 

82 def test_loadAndExecutePluginCommand(self): 

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

84 runner = click.testing.CliRunner() 

85 with command_test_env(runner): 

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

87 self.assertEqual(result.exit_code, 0, result.output) 

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

89 

90 def test_loadAndExecuteLocalCommand(self): 

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

92 runner = click.testing.CliRunner() 

93 with runner.isolated_filesystem(): 

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

95 self.assertEqual(result.exit_code, 0, result.output) 

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

97 

98 def test_loadTopHelp(self): 

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

100 runner = click.testing.CliRunner() 

101 with command_test_env(runner): 

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

103 self.assertEqual(result.exit_code, 0, result.stdout) 

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

105 

106 def test_getLocalCommands(self): 

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

108 localCommands = butler.LoaderCLI._getLocalCommands() 

109 for command in cmd.__all__: 

110 command = command.replace("_", "-") 

111 self.assertEqual(localCommands[command], ["lsst.daf.butler.cli.cmd"]) 

112 

113 def test_mergeCommandLists(self): 

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

115 properly.""" 

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

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

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

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

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

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

122 

123 def test_listCommands_duplicate(self): 

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

125 present and verify it fails to run. 

126 """ 

127 self.maxDiff = None 

128 runner = click.testing.CliRunner() 

129 with duplicate_command_test_env(runner): 

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

131 self.assertEqual(result.exit_code, 1, result.output) 

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

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

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

135 

136 

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

138 unittest.main()