Coverage for tests/test_cliCmdConfigDump.py: 20%
115 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-01-05 10:36 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-01-05 10:36 +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/>.
22"""Unit tests for daf_butler CLI config-dump command.
23"""
25import os
26import os.path
27import unittest
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
37TESTDIR = os.path.abspath(os.path.dirname(__file__))
40class ConfigDumpTest(CliCmdTestBase, unittest.TestCase):
42 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.configDump"
44 @staticmethod
45 def defaultExpected():
46 return dict()
48 @staticmethod
49 def command():
50 return config_dump
53class ConfigDumpUseTest(unittest.TestCase):
54 """Test executing the command."""
56 def setUp(self):
57 self.runner = LogCliRunner()
59 def test_stdout(self):
60 """Test dumping the config to stdout."""
61 with self.runner.isolated_filesystem():
62 result = self.runner.invoke(butler.cli, ["create", "here"])
63 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
65 # test dumping to stdout:
66 result = self.runner.invoke(butler.cli, ["config-dump", "here"])
67 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
68 # check for some expected keywords:
69 cfg = yaml.safe_load(result.stdout)
70 self.assertIn("datastore", cfg)
71 self.assertIn("composites", cfg["datastore"])
72 self.assertIn("storageClasses", cfg)
74 def test_file(self):
75 """test dumping the config to a file."""
76 with self.runner.isolated_filesystem():
77 result = self.runner.invoke(butler.cli, ["create", "here"])
78 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
79 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--file=there"])
80 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
81 # check for some expected keywords:
82 with open("there", "r") as f:
83 cfg = yaml.safe_load(f)
84 self.assertIn("datastore", cfg)
85 self.assertIn("composites", cfg["datastore"])
86 self.assertIn("storageClasses", cfg)
88 def test_subset(self):
89 """Test selecting a subset of the config."""
90 with self.runner.isolated_filesystem():
91 result = self.runner.invoke(butler.cli, ["create", "here"])
92 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
93 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--subset", "datastore"])
94 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
95 cfg = yaml.safe_load(result.stdout)
96 # count the keys in the datastore config
97 self.assertEqual(len(cfg), 8)
98 self.assertIn("cls", cfg)
99 self.assertIn("create", cfg)
100 self.assertIn("formatters", cfg)
101 self.assertIn("records", cfg)
102 self.assertIn("root", cfg)
103 self.assertIn("templates", cfg)
105 # Test that a subset that returns a scalar quantity does work.
106 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--subset", ".datastore.root"])
107 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
108 self.assertEqual(result.stdout.strip(), ".datastore.root: <butlerRoot>")
110 def test_invalidSubset(self):
111 """Test selecting a subset key that does not exist in the config."""
112 with self.runner.isolated_filesystem():
113 result = self.runner.invoke(butler.cli, ["create", "here"])
114 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
115 # test dumping to stdout:
116 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--subset", "foo"])
117 self.assertEqual(result.exit_code, 1)
118 # exception type is click.Exit, and its argument is a return code
119 self.assertEqual(result.exception.args, (1,))
121 def test_presets(self):
122 """Test that file overrides can set command line options in bulk."""
123 with self.runner.isolated_filesystem():
124 result = self.runner.invoke(butler.cli, ["create", "here"])
125 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
126 overrides_path = os.path.join(TESTDIR, "data", "config-overrides.yaml")
128 # Run with a presets file
129 result = self.runner.invoke(butler.cli, ["config-dump", "here", "--options-file", overrides_path])
130 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
131 cfg = yaml.safe_load(result.stdout)
132 # Look for datastore information
133 self.assertIn("formatters", cfg)
134 self.assertIn("root", cfg)
136 # Now run with an explicit subset and presets
137 result = self.runner.invoke(
138 butler.cli, ["config-dump", "here", f"-@{overrides_path}", "--subset", ".registry"]
139 )
140 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
141 cfg = yaml.safe_load(result.stdout)
142 # Look for datastore information
143 self.assertNotIn("formatters", cfg)
144 self.assertIn("managers", cfg)
146 # Now with subset before presets -- explicit always trumps
147 # presets.
148 result = self.runner.invoke(
149 butler.cli, ["config-dump", "here", "--subset", ".registry", "--options-file", overrides_path]
150 )
151 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
152 cfg = yaml.safe_load(result.stdout)
153 # Look for datastore information
154 self.assertNotIn("formatters", cfg)
155 self.assertIn("managers", cfg)
157 configfile = "overrides.yaml"
158 outfile = "repodef.yaml"
159 # Check that a misspelled command option causes an error:
160 with open(configfile, "w") as f:
161 f.write(yaml.dump({"config-dump": {"fil": outfile}}))
162 result = self.runner.invoke(butler.cli, ["config-dump", "here", f"-@{configfile}"])
163 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result))
165 # Check that an option that declares a different command argument
166 # name is mapped correctly.
167 # Note that the option `config-dump --file`
168 # becomes the `outfile` argument in `def config_dump(..., outfile)`
169 # and we use the option name "file" in the presets file.
170 with open(configfile, "w") as f:
171 f.write(yaml.dump({"config-dump": {"file": outfile}}))
172 result = self.runner.invoke(butler.cli, ["config-dump", "here", f"-@{configfile}"])
173 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
174 self.assertTrue(os.path.exists(outfile))
176 def test_presetsDashedName(self):
177 """Test file overrides when the option has a dash in its name."""
179 # Instead of using `butler config-dump` as we do in other tests,
180 # create a small command for testing, because config-dump does
181 # not have any options with dashes in the name.
182 @click.command()
183 @click.option("--test-option")
184 @options_file_option()
185 def cmd(test_option):
186 print(test_option)
188 configfile = "overrides.yaml"
189 val = "foo"
190 with self.runner.isolated_filesystem():
191 with open(configfile, "w") as f:
192 f.write(yaml.dump({"cmd": {"test-option": val}}))
193 result = self.runner.invoke(cmd, ["-@", configfile])
194 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
195 self.assertTrue(val in result.output)
198if __name__ == "__main__": 198 ↛ 199line 198 didn't jump to line 199, because the condition on line 198 was never true
199 unittest.main()