Coverage for tests/test_cliOption.py : 29%

Hot-keys 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
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 the daf_butler dataset-type CLI option.
23"""
25import click
26import click.testing
27import unittest
28import yaml
30from lsst.daf.butler.cli.opt import config_file_option, config_option, dataset_type_option, directory_argument
31from lsst.daf.butler.cli.utils import clickResultMsg
34class DatasetTypeTestCase(unittest.TestCase):
36 @staticmethod
37 @click.command()
38 @dataset_type_option(help="the dataset type")
39 def cli(dataset_type):
40 click.echo(dataset_type, nl=False)
42 def test_single(self):
43 """test a single argument"""
44 runner = click.testing.CliRunner()
45 result = runner.invoke(DatasetTypeTestCase.cli, ["--dataset-type", "one"])
46 self.assertEqual(result.exit_code, 0)
47 self.assertEqual(result.stdout, "['one']")
49 def test_multiple(self):
50 """test multiple arguments, using the long and short option names"""
51 runner = click.testing.CliRunner()
52 result = runner.invoke(DatasetTypeTestCase.cli, ["--dataset-type", "one", "-d", "two"])
53 self.assertEqual(result.exit_code, 0)
54 self.assertEqual(result.stdout, "['one', 'two']")
56 def test_singlePair(self):
57 """test a single comma-separated value pair"""
58 runner = click.testing.CliRunner()
59 result = runner.invoke(DatasetTypeTestCase.cli, ["--dataset-type", "one,two"])
60 self.assertEqual(result.exit_code, 0)
61 self.assertEqual(result.stdout, "['one', 'two']")
63 def test_multiplePair(self):
64 """test multiple comma-separated value pairs"""
65 runner = click.testing.CliRunner()
66 result = runner.invoke(DatasetTypeTestCase.cli, ["--dataset-type", "one,two", "-d", "three,four"])
67 self.assertEqual(result.exit_code, 0)
68 self.assertEqual(result.stdout, "['one', 'two', 'three', 'four']")
70 def test_help(self):
71 """test capture of the help text"""
72 runner = click.testing.CliRunner()
73 result = runner.invoke(DatasetTypeTestCase.cli, ["--help"])
74 self.assertEqual(result.exit_code, 0)
75 self.assertIn("the dataset type", result.stdout)
78class ConfigTestCase(unittest.TestCase):
80 @staticmethod
81 @click.command()
82 @config_option(help="foo bar baz")
83 def cli(config):
84 click.echo(yaml.dump(config), nl=False)
86 def test_basic(self):
87 """test arguments"""
88 runner = click.testing.CliRunner()
89 result = runner.invoke(ConfigTestCase.cli, ["--config", "a=1", "-c", "b=2,c=3"])
90 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
91 self.assertEqual(yaml.safe_load(result.stdout), dict(a="1", b="2", c="3"))
93 def test_missing(self):
94 @click.command()
95 @config_option(required=True)
96 def cli(config):
97 pass
98 runner = click.testing.CliRunner()
99 result = runner.invoke(cli, [])
100 self.assertNotEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
101 self.assertIn('Missing option "-c" / "--config"', result.output)
103 def test_help(self):
104 """test capture of the help text"""
105 runner = click.testing.CliRunner()
106 result = runner.invoke(ConfigTestCase.cli, ["--help"])
107 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
108 self.assertIn("foo bar baz", result.stdout)
111class ConfigFileTestCase(unittest.TestCase):
113 @staticmethod
114 @click.command()
115 @config_file_option(help="foo bar baz")
116 def cli(config_file):
117 click.echo(config_file, nl=False)
119 def test_basic(self):
120 """test arguments"""
121 runner = click.testing.CliRunner()
122 result = runner.invoke(ConfigFileTestCase.cli, ["--config-file", "path/to/file"])
123 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
124 self.assertEqual("path/to/file", result.stdout)
126 def test_missing(self):
127 @click.command()
128 @config_file_option(required=True)
129 def cli(config):
130 pass
131 runner = click.testing.CliRunner()
132 result = runner.invoke(cli, [])
133 self.assertNotEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
134 self.assertIn('Missing option "-C" / "--config-file"', result.output)
136 def test_help(self):
137 """test capture of the help text"""
138 runner = click.testing.CliRunner()
139 result = runner.invoke(ConfigFileTestCase.cli, ["--help"])
140 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
141 self.assertIn("foo bar baz", result.stdout)
144class DirectoryArgumentTestCase(unittest.TestCase):
146 def test_required(self):
147 """test arguments"""
148 @click.command()
149 @directory_argument(required=True)
150 def cli(directory):
151 click.echo(directory, nl=False)
152 runner = click.testing.CliRunner()
153 result = runner.invoke(cli, ["this_dir"])
154 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
155 self.assertEqual("this_dir", result.stdout)
156 result = runner.invoke(cli, [])
157 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result))
158 self.assertIn('Missing argument "DIRECTORY"', result.stdout)
160 def test_notRequired(self):
161 """test arguments"""
162 @click.command()
163 @directory_argument()
164 def cli(directory):
165 click.echo(directory, nl=False)
166 runner = click.testing.CliRunner()
167 result = runner.invoke(cli, ["this_dir"])
168 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
169 self.assertEqual("this_dir", result.stdout)
170 result = runner.invoke(cli, [])
171 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
172 self.assertEqual("", result.stdout)
174 def test_customHelp(self):
175 @click.command()
176 @directory_argument(help="custom help")
177 def cli(directory):
178 click.echo(directory, nl=False)
179 runner = click.testing.CliRunner()
180 result = runner.invoke(cli, ["--help"])
181 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
182 self.assertIn("custom help", result.stdout)
184 def test_defaultHelp(self):
185 @click.command()
186 @directory_argument()
187 def cli(directory):
188 click.echo(directory, nl=False)
189 runner = click.testing.CliRunner()
190 result = runner.invoke(cli, ["--help"])
191 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
192 self.assertIn(directory_argument.default_help, result.stdout)
195if __name__ == "__main__": 195 ↛ 196line 195 didn't jump to line 196, because the condition on line 195 was never true
196 unittest.main()