Coverage for tests/test_cliCmdConfigDump.py: 20%
113 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-04-13 02:34 -0700
« prev ^ index » next coverage.py v6.5.0, created at 2023-04-13 02:34 -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/>.
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 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.configDump"
43 @staticmethod
44 def defaultExpected():
45 return dict()
47 @staticmethod
48 def command():
49 return config_dump
52class ConfigDumpUseTest(unittest.TestCase):
53 """Test executing the command."""
55 def setUp(self):
56 self.runner = LogCliRunner()
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))
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)
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)
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)
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>")
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,))
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")
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)
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)
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)
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))
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))
175 def test_presetsDashedName(self):
176 """Test file overrides when the option has a dash in its name."""
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)
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)
197if __name__ == "__main__":
198 unittest.main()