
Add a new way of formatting our output in a consistent way. This turns print_list and print_dict into a formatter that has the same API as any other formatter and allows users to create their own formatters and plug them into cratonclient. This includes tests for the base level formatter and our two default formatters as well as some refactoring to allow users to specify their own --format. At the moment, however, the subcommand shells do *not* use the pluggable formatter decided by the user. That change and all of the downstream effects it has on testing is going to be *very* significant and deserves its own commit as this one is large enough. Change-Id: I6649ebce57d5ddf2d4aeb689e77e3c17ef3a2e97
75 lines
2.4 KiB
Python
75 lines
2.4 KiB
Python
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is 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.
|
|
"""Base TestCase class for Formatter tests."""
|
|
import argparse
|
|
|
|
import mock
|
|
import six
|
|
|
|
from cratonclient.tests import base
|
|
|
|
|
|
class FormatterTestCase(base.TestCase):
|
|
"""Base-level Formatter TestCase class."""
|
|
|
|
def patch_stdout(self, autostart=True):
|
|
"""Patch sys.stdout and capture all output to it.
|
|
|
|
This will automatically start the patch by default.
|
|
|
|
:param bool autostart:
|
|
Start patching sys.stdout immediately or delay that until later.
|
|
"""
|
|
self.stdout_patcher = mock.patch('sys.stdout', new=six.StringIO())
|
|
if autostart:
|
|
self.stdout = self.stdout_patcher.start()
|
|
self.addCleanup(self.unpatch_stdout)
|
|
|
|
def unpatch_stdout(self):
|
|
"""Safely unpatch standard out."""
|
|
if getattr(self.stdout_patcher, 'is_local', None) is not None:
|
|
self.stdout_patcher.stop()
|
|
|
|
def stripped_stdout(self):
|
|
"""Return the newline stripped standard-out captured string."""
|
|
stdout = self.stdout.getvalue().rstrip('\n')
|
|
self.unpatch_stdout()
|
|
return stdout
|
|
|
|
def args_for(self, **kwargs):
|
|
"""Return an instantiated argparse Namsepace.
|
|
|
|
Using the specified keyword arguments, create and return a Namespace
|
|
object from argparse for testing purposes.
|
|
:returns:
|
|
Instantiated namespace.
|
|
:rtype:
|
|
argparse.Namespace
|
|
"""
|
|
return argparse.Namespace(**kwargs)
|
|
|
|
def resource_info(self, **kwargs):
|
|
"""Return a dictionary with resource information.
|
|
|
|
:returns:
|
|
Dictionary with basic id and name as well as the provided keyword
|
|
arguments.
|
|
:rtype:
|
|
dict
|
|
"""
|
|
info = {
|
|
'id': 1,
|
|
'name': 'Test Resource',
|
|
}
|
|
info.update(kwargs)
|
|
return info
|