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