Hide keyboard shortcuts

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/>. 

21 

22import abc 

23import click 

24import click.testing 

25import copy 

26 

27from ..cli.utils import clickResultMsg, mockEnvVar, Mocker 

28from ..cli import butler 

29 

30 

31class CliCmdTestBase(abc.ABC): 

32 """A test case base that is used to verify click command functions import 

33 and call their respective script fucntions correctly. 

34 """ 

35 

36 @classmethod 

37 @property 

38 @abc.abstractmethod 

39 def defaultExpected(cls): 

40 pass 

41 

42 @classmethod 

43 @property 

44 @abc.abstractmethod 

45 def command(cls): 

46 """Get the click.Command being tested.""" 

47 pass 

48 

49 def setUp(self): 

50 self.runner = click.testing.CliRunner(env=mockEnvVar) 

51 

52 def makeExpected(self, **kwargs): 

53 expected = copy.copy(self.defaultExpected) 

54 expected.update(kwargs) 

55 return expected 

56 

57 def run_command(self, inputs): 

58 """Use the CliRunner with the mock environment variable set to execute 

59 a butler subcommand and parameters specified in inputs. 

60 

61 Parameters 

62 ---------- 

63 inputs : [`str`] 

64 A list of strings that begins with the subcommand name and is 

65 followed by arguments, option keys and option values. 

66 

67 Returns 

68 ------- 

69 result : `click.testing.Result` 

70 The Result object contains the results from calling 

71 self.runner.invoke. 

72 """ 

73 return self.runner.invoke(butler.cli, inputs) 

74 

75 def run_test(self, inputs, expectedKwargs): 

76 """Run the subcommand specified in inputs and verify a successful 

77 outcome where exit code = 0 and the mock object has been called with 

78 the expected arguments. 

79 

80 Parameters 

81 ---------- 

82 inputs : [`str`] 

83 A list of strings that begins with the subcommand name and is 

84 followed by arguments, option keys and option values. 

85 expectedKwargs : `dict` [`str`, `str`] 

86 The arguments that the subcommand function is expected to have been 

87 called with. Keys are the argument name and values are the argument 

88 value. 

89 """ 

90 result = self.run_command(inputs) 

91 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

92 Mocker.mock.assert_called_with(**expectedKwargs) 

93 

94 def run_missing(self, inputs, expectedMsg): 

95 """Run the subcommand specified in inputs and verify a failed outcome 

96 where exit code != 0 and an expected message has been written to 

97 stdout. 

98 

99 Parameters 

100 ---------- 

101 inputs : [`str`] 

102 A list of strings that begins with the subcommand name and is 

103 followed by arguments, option keys and option values. 

104 expectedMsg : `str` 

105 An error message that should be present in stdout after running the 

106 subcommand. 

107 """ 

108 result = self.run_command(inputs) 

109 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result)) 

110 self.assertIn(expectedMsg, result.stdout) 

111 

112 def test_help(self): 

113 self.assertFalse(self.command.get_short_help_str().endswith("..."), 

114 msg="The command help message is being truncated to " 

115 f"\"{self.command.get_short_help_str()}\". It should be shortened, or define " 

116 "@command(short_help=\"something short and helpful\")")