Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move processing docstring warnings to logs #9177

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions awscli/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import platform
import shlex
import sys
from io import StringIO
from subprocess import PIPE, Popen

from docutils.core import publish_string
Expand Down Expand Up @@ -69,6 +70,7 @@ def __init__(self, output_stream=sys.stdout):
self.output_stream = output_stream

PAGER = None
warning_stream = StringIO()
_DEFAULT_DOCUTILS_SETTINGS_OVERRIDES = {
# The default for line length limit in docutils is 10,000. However,
# currently in the documentation, it inlines all possible enums in
Expand All @@ -77,7 +79,8 @@ def __init__(self, output_stream=sys.stdout):
# This is a temporary fix to allow the manpages for these commands
# to be rendered. Long term, we should avoid enumerating over all
# enums inline for the JSON syntax snippets.
'line_length_limit': 50_000
'line_length_limit': 50_000,
'warning_stream': warning_stream,
}

def get_pager_cmdline(self):
Expand Down Expand Up @@ -123,6 +126,13 @@ def _convert_doc_content(self, contents):
writer=manpage.Writer(),
settings_overrides=self._DEFAULT_DOCUTILS_SETTINGS_OVERRIDES,
)

warnings = self.warning_stream.getvalue().strip()
if warnings:
LOG.warning(
"Warnings processing docstring with docutils: %s", warnings
)

if self._exists_on_path('groff'):
cmdline = ['groff', '-m', 'man', '-T', 'ascii']
elif self._exists_on_path('mandoc'):
Expand Down Expand Up @@ -366,9 +376,7 @@ class ServiceHelpCommand(HelpCommand):
def __init__(
self, session, obj, command_table, arg_table, name, event_class
):
super().__init__(
session, obj, command_table, arg_table
)
super().__init__(session, obj, command_table, arg_table)
self._name = name
self._event_class = event_class

Expand Down
109 changes: 65 additions & 44 deletions tests/unit/test_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,26 @@
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from awscli.testutils import mock, unittest, skip_if_windows, FileCreator
import signal
import platform
import json
import sys
import os
import signal
import sys

from awscli.help import PosixHelpRenderer, ExecutableNotFoundError
from awscli.help import WindowsHelpRenderer, ProviderHelpCommand, HelpCommand
from awscli.help import TopicListerCommand, TopicHelpCommand
from awscli.argparser import HELP_BLURB
from awscli.compat import StringIO


class HelpSpyMixin(object):
from awscli.help import (
ExecutableNotFoundError,
HelpCommand,
PosixHelpRenderer,
ProviderHelpCommand,
TopicHelpCommand,
TopicListerCommand,
WindowsHelpRenderer,
)
from awscli.testutils import FileCreator, mock, skip_if_windows, unittest


class HelpSpyMixin:
def __init__(self):
self.exists_on_path = {}
self.popen_calls = []
Expand All @@ -51,7 +56,6 @@ def __init__(self, output_stream=sys.stdout):


class TestHelpPager(unittest.TestCase):

def setUp(self):
self.environ = {}
self.environ_patch = mock.patch('os.environ', self.environ)
Expand All @@ -62,38 +66,34 @@ def tearDown(self):
self.environ_patch.stop()

def test_no_env_vars(self):
self.assertEqual(self.renderer.get_pager_cmdline(),
self.renderer.PAGER.split())
self.assertEqual(
self.renderer.get_pager_cmdline(), self.renderer.PAGER.split()
)

def test_manpager(self):
pager_cmd = 'foobar'
os.environ['MANPAGER'] = pager_cmd
self.assertEqual(self.renderer.get_pager_cmdline(),
pager_cmd.split())
self.assertEqual(self.renderer.get_pager_cmdline(), pager_cmd.split())

def test_pager(self):
pager_cmd = 'fiebaz'
os.environ['PAGER'] = pager_cmd
self.assertEqual(self.renderer.get_pager_cmdline(),
pager_cmd.split())
self.assertEqual(self.renderer.get_pager_cmdline(), pager_cmd.split())

def test_both(self):
os.environ['MANPAGER'] = 'foobar'
os.environ['PAGER'] = 'fiebaz'
self.assertEqual(self.renderer.get_pager_cmdline(),
'foobar'.split())
self.assertEqual(self.renderer.get_pager_cmdline(), 'foobar'.split())

def test_manpager_with_args(self):
pager_cmd = 'less -X'
os.environ['MANPAGER'] = pager_cmd
self.assertEqual(self.renderer.get_pager_cmdline(),
pager_cmd.split())
self.assertEqual(self.renderer.get_pager_cmdline(), pager_cmd.split())

def test_pager_with_args(self):
pager_cmd = 'less -X --clearscreen'
os.environ['PAGER'] = pager_cmd
self.assertEqual(self.renderer.get_pager_cmdline(),
pager_cmd.split())
self.assertEqual(self.renderer.get_pager_cmdline(), pager_cmd.split())

@skip_if_windows('Requires posix system.')
def test_no_groff_or_mandoc_exists(self):
Expand Down Expand Up @@ -131,8 +131,10 @@ def test_no_pager_exists(self):
def test_shlex_split_for_pager_var(self):
pager_cmd = '/bin/sh -c "col -bx | vim -c \'set ft=man\' -"'
os.environ['PAGER'] = pager_cmd
self.assertEqual(self.renderer.get_pager_cmdline(),
['/bin/sh', '-c', "col -bx | vim -c 'set ft=man' -"])
self.assertEqual(
self.renderer.get_pager_cmdline(),
['/bin/sh', '-c', "col -bx | vim -c 'set ft=man' -"],
)

def test_can_render_contents(self):
renderer = FakePosixHelpRenderer()
Expand All @@ -142,6 +144,20 @@ def test_can_render_contents(self):
renderer.render('foo')
self.assertEqual(renderer.popen_calls[-1][0], (['less', '-R'],))

def test_can_render_with_warnings(self):
renderer = FakePosixHelpRenderer()
renderer.exists_on_path['groff'] = True
renderer.exists_on_path['less'] = True
renderer.mock_popen.communicate.return_value = ('rendered', '')
# Triggers "Anonymous hyperlink mismatch" warning
content_with_warnings = "`click<http://click.pocoo.org/>`__"

with mock.patch('awscli.help.LOG.warning') as mock_log_warning:
renderer.render(content_with_warnings)
mock_log_warning.assert_called_once()

self.assertEqual(renderer.popen_calls[-1][0], (['less', '-R'],))

def test_can_page_output_on_windows(self):
renderer = FakeWindowsHelpRenderer()
renderer.mock_popen.communicate.return_value = ('rendered', '')
Expand Down Expand Up @@ -183,8 +199,9 @@ class TestHelpCommand(TestHelpCommandBase):
We do this by subclassing from HelpCommand and ensure it is behaving
as expected.
"""

def setUp(self):
super(TestHelpCommand, self).setUp()
super().setUp()
self.doc_handler_mock = mock.Mock()
self.subcommand_mock = mock.Mock()
self.renderer = mock.Mock()
Expand Down Expand Up @@ -221,7 +238,7 @@ def test_invalid_subcommand(self):

class TestProviderHelpCommand(TestHelpCommandBase):
def setUp(self):
super(TestProviderHelpCommand, self).setUp()
super().setUp()
self.session.provider = None
self.command_table = {}
self.arg_table = {}
Expand All @@ -230,24 +247,27 @@ def setUp(self):
self.usage = None

# Create a temporary index file for ``aws help [command]`` to use.
self.tags_dict = {
'topic-name-1': {},
'topic-name-2': {}
}
self.tags_dict = {'topic-name-1': {}, 'topic-name-2': {}}
json_index = self.file_creator.create_file('index.json', '')
with open(json_index, 'w') as f:
json.dump(self.tags_dict, f, indent=4, sort_keys=True)
self.json_patch = mock.patch(
'awscli.topictags.TopicTagDB.index_file', json_index)
'awscli.topictags.TopicTagDB.index_file', json_index
)
self.json_patch.start()

self.cmd = ProviderHelpCommand(self.session, self.command_table,
self.arg_table, self.description,
self.synopsis, self.usage)
self.cmd = ProviderHelpCommand(
self.session,
self.command_table,
self.arg_table,
self.description,
self.synopsis,
self.usage,
)

def tearDown(self):
self.json_patch.stop()
super(TestProviderHelpCommand, self).tearDown()
super().tearDown()

def test_related_items(self):
self.assertEqual(self.cmd.related_items, ['aws help topics'])
Expand All @@ -263,20 +283,21 @@ def test_subcommand_table(self):

# Ensure the topics are there as well
self.assertIn('topic-name-1', subcommand_table)
self.assertIsInstance(subcommand_table['topic-name-1'],
TopicHelpCommand)
self.assertIsInstance(
subcommand_table['topic-name-1'], TopicHelpCommand
)
self.assertEqual(subcommand_table['topic-name-1'].name, 'topic-name-1')

self.assertIn('topic-name-2', subcommand_table)
self.assertIsInstance(subcommand_table['topic-name-2'],
TopicHelpCommand)
self.assertEqual(subcommand_table['topic-name-2'].name,
'topic-name-2')
self.assertIsInstance(
subcommand_table['topic-name-2'], TopicHelpCommand
)
self.assertEqual(subcommand_table['topic-name-2'].name, 'topic-name-2')


class TestTopicListerCommand(TestHelpCommandBase):
def setUp(self):
super(TestTopicListerCommand, self).setUp()
super().setUp()
self.cmd = TopicListerCommand(self.session)

def test_event_class(self):
Expand All @@ -288,7 +309,7 @@ def test_name(self):

class TestTopicHelpCommand(TestHelpCommandBase):
def setUp(self):
super(TestTopicHelpCommand, self).setUp()
super().setUp()
self.name = 'topic-name-1'
self.cmd = TopicHelpCommand(self.session, self.name)

Expand Down