Skip to content

Commit

Permalink
Implement a workaround for relative Content Handlers (#312)
Browse files Browse the repository at this point in the history
Allow content handlers relative to the local directory if
-l argument is provided to gabbi-run

Fixes #311
  • Loading branch information
scottwallacesh committed Mar 21, 2022
1 parent 28ae08d commit 8511156
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions gabbi/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def run():
host, port, prefix, force_ssl = utils.host_info_from_target(
args.target, args.prefix)

handler_objects = initialize_handlers(args.response_handlers)
handler_objects = initialize_handlers(
args.response_handlers, args.local_handlers)

quiet = args.quiet
verbosity = args.verbosity
Expand Down Expand Up @@ -158,14 +159,24 @@ def run_suite(handle, handler_objects, host, port, prefix, force_ssl=False,
return result.wasSuccessful()


def initialize_handlers(response_handlers):
def initialize_handlers(response_handlers, local_handlers):
# Save PyPath
pypath = sys.path

# Check if a relative import was provided
if local_handlers:
# Allow relative imports
sys.path.insert(0, '.')
custom_response_handlers = []
handler_objects = []
for import_path in response_handlers or []:
for handler in load_response_handlers(import_path):
custom_response_handlers.append(handler)
for handler in handlers.RESPONSE_HANDLERS + custom_response_handlers:
handler_objects.append(handler())

# Restore PyPath
sys.path = pypath
return handler_objects


Expand Down Expand Up @@ -243,6 +254,13 @@ def _make_argparser():
help='Custom response handler. Should be an import path of the '
'form package.module or package.module:class.'
)
parser.add_argument(
'-l', '--local-handlers',
default=False,
dest='local_handlers',
action='store_true',
help='Response handlers may be relative to the current directory.'
)
parser.add_argument(
'-v', '--verbose',
dest='verbosity',
Expand Down

0 comments on commit 8511156

Please sign in to comment.