Coverage for tests/test_cliCmdConfigDump.py: 27%

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

112 statements  

1 

2# This file is part of daf_butler. 

3# 

4# Developed for the LSST Data Management System. 

5# This product includes software developed by the LSST Project 

6# (http://www.lsst.org). 

7# See the COPYRIGHT file at the top-level directory of this distribution 

8# for details of code ownership. 

9# 

10# This program is free software: you can redistribute it and/or modify 

11# it under the terms of the GNU General Public License as published by 

12# the Free Software Foundation, either version 3 of the License, or 

13# (at your option) any later version. 

14# 

15# This program is distributed in the hope that it will be useful, 

16# but WITHOUT ANY WARRANTY; without even the implied warranty of 

17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

18# GNU General Public License for more details. 

19# 

20# You should have received a copy of the GNU General Public License 

21# along with this program. If not, see <http://www.gnu.org/licenses/>. 

22 

23"""Unit tests for daf_butler CLI config-dump command. 

24""" 

25 

26import click 

27import os.path 

28import os 

29import unittest 

30import yaml 

31 

32from lsst.daf.butler.cli import butler 

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

34from lsst.daf.butler.cli.opt import options_file_option 

35from lsst.daf.butler.cli.utils import clickResultMsg, LogCliRunner 

36from lsst.daf.butler.tests import CliCmdTestBase 

37 

38 

39TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

40 

41 

42class ConfigDumpTest(CliCmdTestBase, unittest.TestCase): 

43 

44 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.configDump" 

45 

46 @staticmethod 

47 def defaultExpected(): 

48 return dict() 

49 

50 @staticmethod 

51 def command(): 

52 return config_dump 

53 

54 

55class ConfigDumpUseTest(unittest.TestCase): 

56 """Test executing the command.""" 

57 

58 def setUp(self): 

59 self.runner = LogCliRunner() 

60 

61 def test_stdout(self): 

62 """Test dumping the config to stdout.""" 

63 with self.runner.isolated_filesystem(): 

64 result = self.runner.invoke(butler.cli, ["create", "here"]) 

65 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

66 

67 # test dumping to stdout: 

68 result = self.runner.invoke(butler.cli, ["config-dump", "here"]) 

69 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

70 # check for some expected keywords: 

71 cfg = yaml.safe_load(result.stdout) 

72 self.assertIn("datastore", cfg) 

73 self.assertIn("composites", cfg["datastore"]) 

74 self.assertIn("storageClasses", cfg) 

75 

76 def test_file(self): 

77 """test dumping the config to a file.""" 

78 with self.runner.isolated_filesystem(): 

79 result = self.runner.invoke(butler.cli, ["create", "here"]) 

80 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

81 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--file=there"]) 

82 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

83 # check for some expected keywords: 

84 with open("there", "r") as f: 

85 cfg = yaml.safe_load(f) 

86 self.assertIn("datastore", cfg) 

87 self.assertIn("composites", cfg["datastore"]) 

88 self.assertIn("storageClasses", cfg) 

89 

90 def test_subset(self): 

91 """Test selecting a subset of the config.""" 

92 with self.runner.isolated_filesystem(): 

93 result = self.runner.invoke(butler.cli, ["create", "here"]) 

94 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

95 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--subset", "datastore"]) 

96 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

97 cfg = yaml.safe_load(result.stdout) 

98 # count the keys in the datastore config 

99 self.assertEqual(len(cfg), 8) 

100 self.assertIn("cls", cfg) 

101 self.assertIn("create", cfg) 

102 self.assertIn("formatters", cfg) 

103 self.assertIn("records", cfg) 

104 self.assertIn("root", cfg) 

105 self.assertIn("templates", cfg) 

106 

107 def test_invalidSubset(self): 

108 """Test selecting a subset key that does not exist in the config.""" 

109 with self.runner.isolated_filesystem(): 

110 result = self.runner.invoke(butler.cli, ["create", "here"]) 

111 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

112 # test dumping to stdout: 

113 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--subset", "foo"]) 

114 self.assertEqual(result.exit_code, 1) 

115 # exception type is click.Exit, and its argument is a return code 

116 self.assertEqual(result.exception.args, (1,)) 

117 

118 def test_presets(self): 

119 """Test that file overrides can set command line options in bulk. 

120 """ 

121 with self.runner.isolated_filesystem(): 

122 result = self.runner.invoke(butler.cli, ["create", "here"]) 

123 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

124 overrides_path = os.path.join(TESTDIR, "data", "config-overrides.yaml") 

125 

126 # Run with a presets file 

127 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--options-file", overrides_path]) 

128 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

129 cfg = yaml.safe_load(result.stdout) 

130 # Look for datastore information 

131 self.assertIn("formatters", cfg) 

132 self.assertIn("root", cfg) 

133 

134 # Now run with an explicit subset and presets 

135 result = self.runner.invoke(butler.cli, ["config-dump", "here", f"-@{overrides_path}", 

136 "--subset", ".registry"]) 

137 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

138 cfg = yaml.safe_load(result.stdout) 

139 # Look for datastore information 

140 self.assertNotIn("formatters", cfg) 

141 self.assertIn("managers", cfg) 

142 

143 # Now with subset before presets -- explicit always trumps 

144 # presets. 

145 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--subset", ".registry", 

146 "--options-file", overrides_path]) 

147 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

148 cfg = yaml.safe_load(result.stdout) 

149 # Look for datastore information 

150 self.assertNotIn("formatters", cfg) 

151 self.assertIn("managers", cfg) 

152 

153 configfile = "overrides.yaml" 

154 outfile = "repodef.yaml" 

155 # Check that a misspelled command option causes an error: 

156 with open(configfile, "w") as f: 

157 f.write(yaml.dump({"config-dump": {"fil": outfile}})) 

158 result = self.runner.invoke(butler.cli, ["config-dump", "here", f"-@{configfile}"]) 

159 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result)) 

160 

161 # Check that an option that declares a different command argument 

162 # name is mapped correctly. 

163 # Note that the option `config-dump --file` 

164 # becomes the `outfile` argument in `def config_dump(..., outfile)` 

165 # and we use the option name "file" in the presets file. 

166 with open(configfile, "w") as f: 

167 f.write(yaml.dump({"config-dump": {"file": outfile}})) 

168 result = self.runner.invoke(butler.cli, ["config-dump", "here", f"-@{configfile}"]) 

169 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

170 self.assertTrue(os.path.exists(outfile)) 

171 

172 def test_presetsDashedName(self): 

173 """Test file overrides when the option has a dash in its name. 

174 """ 

175 

176 # Instead of using `butler config-dump` as we do in other tests, 

177 # create a small command for testing, because config-dump does 

178 # not have any options with dashes in the name. 

179 @click.command() 

180 @click.option("--test-option") 

181 @options_file_option() 

182 def cmd(test_option): 

183 print(test_option) 

184 

185 configfile = "overrides.yaml" 

186 val = "foo" 

187 with self.runner.isolated_filesystem(): 

188 with open(configfile, "w") as f: 

189 f.write(yaml.dump({"cmd": {"test-option": val}})) 

190 result = self.runner.invoke(cmd, ["-@", configfile]) 

191 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

192 self.assertTrue(val in result.output) 

193 

194 

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

196 unittest.main()