Make some pylint encouraged cleanups
Mostly doc strings. Writing the docstring for assert_in_or_print_output exposed a typo in the environment variable being read to get the max length check. I suspect nobody was using this, so just changing it.
This commit is contained in:
parent
a0c589e2c6
commit
ac7a013362
@ -283,6 +283,7 @@ class HTTPTestCase(unittest.TestCase):
|
||||
|
||||
@staticmethod
|
||||
def _replacer_regex(key):
|
||||
"""Compose a regular expression for test template variables."""
|
||||
return r"\$%s\[(?P<quote>['\"])(?P<arg>.+?)(?P=quote)\]" % key
|
||||
|
||||
def _response_replace(self, message):
|
||||
@ -438,6 +439,11 @@ class HTTPTestCase(unittest.TestCase):
|
||||
return query_string
|
||||
|
||||
def assert_in_or_print_output(self, expected, iterable):
|
||||
"""Assert the iterable contains expected or print some output.
|
||||
|
||||
If the output is long, it is limited by either GABBI_MAX_CHARS_OUTPUT
|
||||
in the environment or the MAX_CHARS_OUTPUT constant.
|
||||
"""
|
||||
if utils.not_binary(self.content_type):
|
||||
if expected in iterable:
|
||||
return
|
||||
@ -448,7 +454,7 @@ class HTTPTestCase(unittest.TestCase):
|
||||
else:
|
||||
full_response = self.output
|
||||
|
||||
max_chars = os.getenv('GABBIT_MAX_CHARS_OUTPUT', MAX_CHARS_OUTPUT)
|
||||
max_chars = os.getenv('GABBI_MAX_CHARS_OUTPUT', MAX_CHARS_OUTPUT)
|
||||
response = full_response[0:max_chars]
|
||||
is_truncated = (len(response) != len(full_response))
|
||||
|
||||
|
@ -226,8 +226,8 @@ def build_tests(path, loader, host=None, port=8001, intercept=None,
|
||||
if intercept:
|
||||
host = str(uuid.uuid4())
|
||||
test_yaml = load_yaml(test_file)
|
||||
test_base_name = '%s_%s' % (test_loader_name, os.path.splitext(
|
||||
os.path.basename(test_file))[0])
|
||||
test_base_name = '%s_%s' % (
|
||||
test_loader_name, os.path.splitext(os.path.basename(test_file))[0])
|
||||
file_suite = test_suite_from_yaml(loader, test_base_name, test_yaml,
|
||||
path, host, port, fixture_module,
|
||||
intercept, prefix)
|
||||
@ -303,4 +303,8 @@ def _validate_defaults(defaults):
|
||||
|
||||
|
||||
def _is_method_shortcut(key):
|
||||
"""Is this test key indicating a request method.
|
||||
|
||||
It is a request method if it is all upper case.
|
||||
"""
|
||||
return key.isupper()
|
||||
|
@ -10,25 +10,24 @@
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
"""Handlers for processing the body of a response in various ways.
|
||||
|
||||
A response handler is a class that adds functionality for making assertions
|
||||
about an HTTP response.
|
||||
|
||||
A subclass may implement two methods: ``action`` and ``preprocess``.
|
||||
|
||||
``preprocess`` takes one argument, the ``TestCase``. It is called exactly
|
||||
once for each test before looping across the assertions. It is used, rarely,
|
||||
to copy the ``test.output`` into a useful form (such as a parsed DOM).
|
||||
|
||||
``action`` takes two or three arguments. If ``test_key_value`` is a list
|
||||
``action`` is called with the test case and a single list item. If
|
||||
``test_key_value`` is a dict then ``action`` is called with the test case
|
||||
and a key and value pair.
|
||||
"""
|
||||
"""Handlers for processing the body of a response in various ways."""
|
||||
|
||||
|
||||
class ResponseHandler(object):
|
||||
"""Add functionality for making assertions about an HTTP response.
|
||||
|
||||
A subclass may implement two methods: ``action`` and ``preprocess``.
|
||||
|
||||
``preprocess`` takes one argument, the ``TestCase``. It is called exactly
|
||||
once for each test before looping across the assertions. It is used,
|
||||
rarely, to copy the ``test.output`` into a useful form (such as a parsed
|
||||
DOM).
|
||||
|
||||
``action`` takes two or three arguments. If ``test_key_value`` is a list
|
||||
``action`` is called with the test case and a single list item. If
|
||||
``test_key_value`` is a dict then ``action`` is called with the test case
|
||||
and a key and value pair.
|
||||
"""
|
||||
|
||||
test_key_suffix = ''
|
||||
test_key_value = []
|
||||
@ -61,6 +60,7 @@ class ResponseHandler(object):
|
||||
pass
|
||||
|
||||
def _register(self, test_class):
|
||||
"""Register this handler on the provided test class."""
|
||||
test_class.base_test[self._key] = self.test_key_value
|
||||
if self not in test_class.response_handlers:
|
||||
test_class.response_handlers.append(self)
|
||||
@ -71,7 +71,7 @@ class ResponseHandler(object):
|
||||
return False
|
||||
|
||||
def __ne__(self, other):
|
||||
return (not self.__eq__(other))
|
||||
return not self.__eq__(other)
|
||||
|
||||
|
||||
class StringResponseHandler(ResponseHandler):
|
||||
@ -91,7 +91,7 @@ class JSONResponseHandler(ResponseHandler):
|
||||
test_key_suffix = 'json_paths'
|
||||
test_key_value = {}
|
||||
|
||||
def action(self, test, path, value):
|
||||
def action(self, test, path, value=None):
|
||||
"""Test json_paths against json data."""
|
||||
# NOTE: This process has some advantages over other process that
|
||||
# might come along because the JSON data has already been
|
||||
@ -120,7 +120,7 @@ class HeadersResponseHandler(ResponseHandler):
|
||||
test_key_suffix = 'headers'
|
||||
test_key_value = {}
|
||||
|
||||
def action(self, test, header, value):
|
||||
def action(self, test, header, value=None):
|
||||
header = header.lower() # case-insensitive comparison
|
||||
|
||||
response = test.response
|
||||
|
@ -101,12 +101,14 @@ class VerboseHttp(httplib2.Http):
|
||||
return (response, content)
|
||||
|
||||
def _print_headers(self, headers, prefix=''):
|
||||
"""Output request or response headers."""
|
||||
if self._show_headers:
|
||||
for key in headers:
|
||||
if key not in self.HEADER_BLACKLIST:
|
||||
self._print_header(key, headers[key], prefix=prefix)
|
||||
|
||||
def _print_body(self, headers, content):
|
||||
"""Output body if not binary."""
|
||||
if self._show_body and utils.not_binary(
|
||||
utils.extract_content_type(headers)[0]):
|
||||
self._verbose_output('')
|
||||
@ -114,6 +116,7 @@ class VerboseHttp(httplib2.Http):
|
||||
utils.decode_response_content(headers, content))
|
||||
|
||||
def _print_header(self, name, value, prefix='', stream=None):
|
||||
"""Output one single header."""
|
||||
header = self.colorize(self.COLORMAP['header'], "%s:" % name)
|
||||
self._verbose_output("%s %s" % (header, value), prefix=prefix,
|
||||
stream=stream)
|
||||
|
@ -10,6 +10,7 @@
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
"""Extend jsonpath_rw to add a len command."""
|
||||
|
||||
import jsonpath_rw
|
||||
|
||||
|
@ -10,6 +10,7 @@
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
"""Implementation of a command line runner of single gabbi files."""
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
@ -110,7 +111,7 @@ def run():
|
||||
port = None
|
||||
|
||||
# Initialize the extensions for response handling.
|
||||
for handler in (driver.RESPONSE_HANDLERS + custom_response_handlers):
|
||||
for handler in driver.RESPONSE_HANDLERS + custom_response_handlers:
|
||||
handler(case.HTTPTestCase)
|
||||
|
||||
data = yaml.safe_load(sys.stdin.read())
|
||||
|
@ -30,7 +30,7 @@ def create_url(base_url, host, port=None, prefix='', ssl=False):
|
||||
scheme = 'http'
|
||||
netloc = host
|
||||
|
||||
if (port and not _port_follows_standard(port, ssl)):
|
||||
if port and not _port_follows_standard(port, ssl):
|
||||
netloc = '%s:%s' % (host, port)
|
||||
|
||||
if ssl:
|
||||
|
Loading…
x
Reference in New Issue
Block a user