Coverage for python/lsst/daf/butler/tests/cliOptionTestBase.py : 46%

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/>.
22import abc
23import click
24import click.testing
25import unittest
28class CliOptionTestBase(unittest.TestCase, abc.ABC):
29 """A test case base that is used to verify click Options.
30 """
32 def setUp(self):
33 self.runner = click.testing.CliRunner()
35 def run_command(self, cmd, args):
36 """Run a command.
38 Parameters
39 ----------
40 cmd : click.Command
41 The command function to call
42 args : [`str`]
43 The arguments to pass to the function call.
45 Returns
46 -------
47 [type]
48 [description]
49 """
50 return self.runner.invoke(cmd, args)
52 def run_test(self, cmd, cmdArgs, verifyFunc, verifyArgs=None):
53 result = self.run_command(cmd, cmdArgs)
54 verifyFunc(result, verifyArgs)
56 def run_help_test(self, cmd, expcectedHelpText):
57 result = self.runner.invoke(cmd, ["--help"])
58 # remove all whitespace to work around line-wrap differences.
59 self.assertIn("".join(expcectedHelpText.split()), "".join(result.output.split()))
61 @property
62 @abc.abstractmethod
63 def optionClass(self):
64 pass
66 def help_test(self):
67 @click.command()
68 @self.optionClass()
69 def cli():
70 pass
72 self.run_help_test(cli, self.optionClass.defaultHelp)
74 def custom_help_test(self):
75 @click.command()
76 @self.optionClass(help="foobarbaz")
77 def cli(collection_type):
78 pass
80 self.run_help_test(cli, "foobarbaz")