Skip to content

Commit 91b59ab

Browse files
joejoevictorcopybara-github
authored andcommitted
Ported HelpUtilsTest.testHelpStringObject to HelpTest.testHelpTextNoDefaultsObject
PiperOrigin-RevId: 244206507 Change-Id: I7448ab5a2aff08bafbd8bd8976dadcef19584e28
1 parent 925ae49 commit 91b59ab

3 files changed

Lines changed: 41 additions & 23 deletions

File tree

‎fire/helptext.py‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import inspect
4141

4242
from fire import completion
43-
from fire import docstrings
4443
from fire import formatting
4544
from fire import inspectutils
4645
from fire import value_types
@@ -59,7 +58,6 @@ def HelpString(component, trace=None, verbose=False):
5958
String suitable for display giving information about the component.
6059
"""
6160
info = inspectutils.Info(component)
62-
info['docstring_info'] = docstrings.parse(info['docstring'])
6361

6462
is_error_screen = False
6563
if trace:
@@ -332,10 +330,15 @@ def HelpTextForObject(component, info, trace=None, verbose=False):
332330
possible_actions.append('COMMAND')
333331
command_item_strings = []
334332
for command_name, command in commands:
335-
command_docstring_info = docstrings.parse(
336-
inspectutils.Info(command)['docstring'])
337-
command_item_strings.append(
338-
_CreateItem(command_name, command_docstring_info.summary))
333+
command_info = inspectutils.Info(command)
334+
command_item = command_name
335+
if 'docstring_info' in command_info:
336+
command_docstring_info = command_info['docstring_info']
337+
if command_docstring_info and command_docstring_info.summary:
338+
command_item = _CreateItem(command_name,
339+
command_docstring_info.summary)
340+
341+
command_item_strings.append(command_item)
339342
usage_details_sections.append(
340343
('COMMANDS', _NewChoicesSection('COMMAND', command_item_strings)))
341344

@@ -344,12 +347,14 @@ def HelpTextForObject(component, info, trace=None, verbose=False):
344347
value_item_strings = []
345348
for value_name, value in values:
346349
del value
347-
init_docstring_info = docstrings.parse(
348-
inspectutils.Info(component.__class__.__init__)['docstring'])
349-
for arg_info in init_docstring_info.args:
350-
if arg_info.name == value_name:
351-
value_item_strings.append(_CreateItem(value_name,
352-
arg_info.description))
350+
init_info = inspectutils.Info(component.__class__.__init__)
351+
value_item = value_name
352+
if 'docstring_info' in init_info:
353+
init_docstring_info = init_info['docstring_info']
354+
for arg_info in init_docstring_info.args:
355+
if arg_info.name == value_name:
356+
value_item = _CreateItem(value_name, arg_info.description)
357+
value_item_strings.append(value_item)
353358
usage_details_sections.append(
354359
('VALUES', _NewChoicesSection('VALUE', value_item_strings)))
355360

‎fire/helptext_test.py‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@
3131

3232
class HelpTest(testutils.BaseTestCase):
3333

34-
def assertTextInSection(self, text, actual_output):
35-
self.assertIn(textwrap.dedent(text).lstrip('\n'), actual_output)
36-
37-
def assertTextNotInSection(self, text, actual_output):
38-
self.assertNotIn(textwrap.dedent(text).lstrip('\n'), actual_output)
39-
4034
def testHelpTextNoDefaults(self):
4135
component = tc.NoDefaults
4236
# TODO(joejoevictor): We should have inspectutils.Info to generate
@@ -47,11 +41,27 @@ def testHelpTextNoDefaults(self):
4741
component=component,
4842
info=info,
4943
trace=trace.FireTrace(component, name='NoDefaults'))
44+
self.assertIn('NAME\n NoDefaults', help_screen)
45+
self.assertIn('SYNOPSIS\n NoDefaults', help_screen)
46+
self.assertNotIn('DESCRIPTION', help_screen)
47+
self.assertNotIn('NOTES', help_screen)
5048

51-
self.assertTextInSection('NAME\n NoDefaults', help_screen)
52-
self.assertTextInSection('SYNOPSIS\n NoDefaults', help_screen)
53-
self.assertTextNotInSection('DESCRIPTION', help_screen)
54-
self.assertTextNotInSection('NOTES', help_screen)
49+
def testHelpTextNoDefaultsObject(self):
50+
component = tc.NoDefaults()
51+
info = inspectutils.Info(component)
52+
info['docstring_info'] = docstrings.parse(info['docstring'])
53+
help_screen = helptext.HelpText(
54+
component=component,
55+
info=info,
56+
trace=trace.FireTrace(component, name='NoDefaults'))
57+
self.assertIn('NAME\n NoDefaults', help_screen)
58+
self.assertIn('SYNOPSIS\n NoDefaults COMMAND', help_screen)
59+
self.assertNotIn('DESCRIPTION', help_screen)
60+
self.assertIn('COMMANDS\n COMMAND is one of the followings:',
61+
help_screen)
62+
self.assertIn('double', help_screen)
63+
self.assertIn('triple', help_screen)
64+
self.assertNotIn('NOTES', help_screen)
5565

5666
def setUp(self):
5767
os.environ['ANSI_COLORS_DISABLED'] = '1'
@@ -60,7 +70,6 @@ def testHelpScreen(self):
6070
component = tc.ClassWithDocstring()
6171
t = trace.FireTrace(component, name='ClassWithDocstring')
6272
info = inspectutils.Info(component)
63-
info['docstring_info'] = docstrings.parse(info['docstring'])
6473
help_output = helptext.HelpText(component, info, t)
6574
expected_output = """
6675
NAME

‎fire/inspectutils.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from __future__ import print_function
2020

2121
import inspect
22+
from fire import docstrings
2223

2324
import six
2425

@@ -175,6 +176,9 @@ def Info(component):
175176
except (TypeError, IOError):
176177
info['line'] = None
177178

179+
if 'docstring' in info:
180+
info['docstring_info'] = docstrings.parse(info['docstring'])
181+
178182
return info
179183

180184

0 commit comments

Comments
 (0)