Coverage for tests/test_cliCmdConfigDump.py: 20%

115 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-03-22 02:07 -0700

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 daf_butler CLI config-dump command. 

23""" 

24 

25import os 

26import os.path 

27import unittest 

28 

29import click 

30import yaml 

31from lsst.daf.butler.cli import butler 

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

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

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

35from lsst.daf.butler.tests import CliCmdTestBase 

36 

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

38 

39 

40class ConfigDumpTest(CliCmdTestBase, unittest.TestCase): 

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

42 

43 @staticmethod 

44 def defaultExpected(): 

45 return dict() 

46 

47 @staticmethod 

48 def command(): 

49 return config_dump 

50 

51 

52class ConfigDumpUseTest(unittest.TestCase): 

53 """Test executing the command.""" 

54 

55 def setUp(self): 

56 self.runner = LogCliRunner() 

57 

58 def test_stdout(self): 

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

60 with self.runner.isolated_filesystem(): 

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

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

63 

64 # test dumping to stdout: 

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

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

67 # check for some expected keywords: 

68 cfg = yaml.safe_load(result.stdout) 

69 self.assertIn("datastore", cfg) 

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

71 self.assertIn("storageClasses", cfg) 

72 

73 def test_file(self): 

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

75 with self.runner.isolated_filesystem(): 

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

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

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

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

80 # check for some expected keywords: 

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

82 cfg = yaml.safe_load(f) 

83 self.assertIn("datastore", cfg) 

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

85 self.assertIn("storageClasses", cfg) 

86 

87 def test_subset(self): 

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

89 with self.runner.isolated_filesystem(): 

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

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

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

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

94 cfg = yaml.safe_load(result.stdout) 

95 # count the keys in the datastore config 

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

97 self.assertIn("cls", cfg) 

98 self.assertIn("create", cfg) 

99 self.assertIn("formatters", cfg) 

100 self.assertIn("records", cfg) 

101 self.assertIn("root", cfg) 

102 self.assertIn("templates", cfg) 

103 

104 # Test that a subset that returns a scalar quantity does work. 

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

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

107 self.assertEqual(result.stdout.strip(), ".datastore.root: <butlerRoot>") 

108 

109 def test_invalidSubset(self): 

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

111 with self.runner.isolated_filesystem(): 

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

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

114 # test dumping to stdout: 

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

116 self.assertEqual(result.exit_code, 1) 

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

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

119 

120 def test_presets(self): 

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

122 with self.runner.isolated_filesystem(): 

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

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

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

126 

127 # Run with a presets file 

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

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

130 cfg = yaml.safe_load(result.stdout) 

131 # Look for datastore information 

132 self.assertIn("formatters", cfg) 

133 self.assertIn("root", cfg) 

134 

135 # Now run with an explicit subset and presets 

136 result = self.runner.invoke( 

137 butler.cli, ["config-dump", "here", f"-@{overrides_path}", "--subset", ".registry"] 

138 ) 

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

140 cfg = yaml.safe_load(result.stdout) 

141 # Look for datastore information 

142 self.assertNotIn("formatters", cfg) 

143 self.assertIn("managers", cfg) 

144 

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

146 # presets. 

147 result = self.runner.invoke( 

148 butler.cli, ["config-dump", "here", "--subset", ".registry", "--options-file", overrides_path] 

149 ) 

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

151 cfg = yaml.safe_load(result.stdout) 

152 # Look for datastore information 

153 self.assertNotIn("formatters", cfg) 

154 self.assertIn("managers", cfg) 

155 

156 configfile = "overrides.yaml" 

157 outfile = "repodef.yaml" 

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

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

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

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

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

163 

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

165 # name is mapped correctly. 

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

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

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

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

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

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

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

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

174 

175 def test_presetsDashedName(self): 

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

177 

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

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

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

181 @click.command() 

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

183 @options_file_option() 

184 def cmd(test_option): 

185 print(test_option) 

186 

187 configfile = "overrides.yaml" 

188 val = "foo" 

189 with self.runner.isolated_filesystem(): 

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

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

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

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

194 self.assertTrue(val in result.output) 

195 

196 

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

198 unittest.main()