Coverage for tests/test_cliUtils.py : 35%

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 shared CLI options.
23"""
25import click
26import unittest
28from lsst.daf.butler.cli import butler
29from lsst.daf.butler.cli.utils import (clickResultMsg, LogCliRunner, Mocker, mockEnvVar, MWArgument, MWOption,
30 unwrap)
31from lsst.daf.butler.cli.opt import directory_argument, repo_argument
34class MockerTestCase(unittest.TestCase):
36 def test_callMock(self):
37 """Test that a mocked subcommand calls the Mocker and can be verified.
38 """
39 runner = LogCliRunner(env=mockEnvVar)
40 result = runner.invoke(butler.cli, ["create", "repo"])
41 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
42 Mocker.mock.assert_called_with(repo="repo", seed_config=None, standalone=False, override=False,
43 outfile=None)
46class ArgumentHelpGeneratorTestCase(unittest.TestCase):
48 @staticmethod
49 @click.command()
50 # Use custom help in the arguments so that any changes to default help text
51 # do not break this test unnecessarily.
52 @repo_argument(help="repo help text")
53 @directory_argument(help="directory help text")
54 def cli():
55 pass
57 def test_help(self):
58 """Tests `utils.addArgumentHelp` and its use in repo_argument and
59 directory_argument; verifies that the argument help gets added to the
60 command fucntion help, and that it's added in the correct order. See
61 addArgumentHelp for more details."""
62 runner = LogCliRunner()
63 result = runner.invoke(ArgumentHelpGeneratorTestCase.cli, ["--help"])
64 expected = """Usage: cli [OPTIONS] [REPO] [DIRECTORY]
66 directory help text
68 repo help text
70Options:
71 --help Show this message and exit.
72"""
73 self.assertIn(expected, result.output)
76class UnwrapStringTestCase(unittest.TestCase):
78 def test_leadingNewline(self):
79 testStr = """
80 foo bar
81 baz """
82 self.assertEqual(unwrap(testStr), "foo bar baz")
84 def test_leadingContent(self):
85 testStr = """foo bar
86 baz """
87 self.assertEqual(unwrap(testStr), "foo bar baz")
89 def test_trailingNewline(self):
90 testStr = """
91 foo bar
92 baz
93 """
94 self.assertEqual(unwrap(testStr), "foo bar baz")
96 def test_oneLine(self):
97 testStr = """foo bar baz"""
98 self.assertEqual(unwrap(testStr), "foo bar baz")
100 def test_oneLineWithLeading(self):
101 testStr = """
102 foo bar baz"""
103 self.assertEqual(unwrap(testStr), "foo bar baz")
105 def test_oneLineWithTrailing(self):
106 testStr = """foo bar baz
107 """
108 self.assertEqual(unwrap(testStr), "foo bar baz")
111class MWOptionTest(unittest.TestCase):
113 def setUp(self):
114 self.runner = LogCliRunner()
116 def test_addElipsisToMultiple(self):
117 """Verify that MWOption adds elipsis to the option metavar when
118 `multiple=True`
120 The default behavior of click is to not add elipsis to options that
121 have `multiple=True`."""
122 @click.command()
123 @click.option("--things", cls=MWOption, multiple=True)
124 def cmd(things):
125 pass
126 result = self.runner.invoke(cmd, ["--help"])
127 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
128 expectedOutut = """Options:
129 --things TEXT ..."""
130 self.assertIn(expectedOutut, result.output)
132 def test_addElipsisToNargs(self):
133 """Verify that MWOption adds " ..." after the option metavar when
134 `nargs` is set to more than 1 and less than 1.
136 The default behavior of click is to add elipsis when nargs does not
137 equal 1, but it does not put a space before the elipsis and we prefer
138 a space between the metavar and the elipsis."""
139 for numberOfArgs in (0, 1, 2): # nargs must be >= 0 for an option
140 @click.command()
141 @click.option("--things", cls=MWOption, nargs=numberOfArgs)
142 def cmd(things):
143 pass
144 result = self.runner.invoke(cmd, ["--help"])
145 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
146 expectedOutut = f"""Options:
147 --things TEXT{' ...' if numberOfArgs != 1 else ''}"""
148 self.assertIn(expectedOutut, result.output)
151class MWArgumentTest(unittest.TestCase):
153 def setUp(self):
154 self.runner = LogCliRunner()
156 def test_addElipsisToNargs(self):
157 """Verify that MWOption adds " ..." after the option metavar when
158 `nargs` != 1.
160 The default behavior of click is to add elipsis when nargs does not
161 equal 1, but it does not put a space before the elipsis and we prefer
162 a space between the metavar and the elipsis."""
163 # nargs can be -1 for any number of args, or >= 1 for a specified
164 # number of arguments.
165 for numberOfArgs in (-1, 1, 2):
166 for required in (True, False):
167 @click.command()
168 @click.argument("things", cls=MWArgument, required=required, nargs=numberOfArgs)
169 def cmd(things):
170 pass
171 result = self.runner.invoke(cmd, ["--help"])
172 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
173 expectedOutut = (f"Usage: cmd [OPTIONS] {'THINGS' if required else '[THINGS]'}"
174 f"{' ...' if numberOfArgs != 1 else ''}")
175 self.assertIn(expectedOutut, result.output)
178if __name__ == "__main__": 178 ↛ 179line 178 didn't jump to line 179, because the condition on line 178 was never true
179 unittest.main()