From adc22e1a439a93521b977bc384bb4e624baea6a8 Mon Sep 17 00:00:00 2001 From: Kurt Griffiths Date: Fri, 7 Jul 2017 08:03:43 -0600 Subject: [PATCH] refactor(print_routes): Clean up test code and DRY traverse() (#1080) --- falcon/cmd/print_routes.py | 19 +++++++++++-------- tests/test_cmd_print_api.py | 28 +++++++++++++++------------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/falcon/cmd/print_routes.py b/falcon/cmd/print_routes.py index f5cac67..af8959c 100644 --- a/falcon/cmd/print_routes.py +++ b/falcon/cmd/print_routes.py @@ -55,15 +55,18 @@ def traverse(roots, parent='', verbose=False): for method, func in root.method_map.items(): if func.__name__ != 'method_not_allowed': if isinstance(func, partial): - print('-->{0} {1}:{2}'.format( - method, - inspect.getsourcefile(func.func), - inspect.getsourcelines(func.func)[1])) + real_func = func.func else: - print('-->{0} {1}:{2}'.format( - method, - inspect.getsourcefile(func), - inspect.getsourcelines(func)[1])) + real_func = func + + source_file = inspect.getsourcefile(real_func) + + print('-->{0} {1}:{2}'.format( + method, + source_file, + source_file[1] + )) + if root.children: traverse(root.children, parent + '/' + root.raw_segment, verbose) diff --git a/tests/test_cmd_print_api.py b/tests/test_cmd_print_api.py index 018fa26..a77ed5f 100644 --- a/tests/test_cmd_print_api.py +++ b/tests/test_cmd_print_api.py @@ -17,27 +17,29 @@ _api.add_route('/test', DummyResource()) def test_traverse_with_verbose(): - """Ensure traverse finds the proper routes and adds verbose output - for a method function as well as the OPTIONS partial.""" + """Ensure traverse() finds the proper routes and outputs verbose info.""" + output = six.moves.StringIO() with redirected(stdout=output): print_routes.traverse(_api._router._roots, verbose=True) - route, method, options = output.getvalue().strip().split('\n') + route, get_info, options_info = output.getvalue().strip().split('\n') assert '-> /test' == route - # Check in both methods and options for the GET method - # because method map is not ordered - assert 'GET' in method + options - if 'GET' in method: - assert 'OPTIONS' in options - assert 'falcon/responders.py:' in options - else: - assert 'OPTIONS' in method - assert 'falcon/responders.py:' in method + + # NOTE(kgriffs) We might receive these in either order, since the + # method map is not ordered, so check and swap if necessary. + if options_info.startswith('-->GET'): + get_info, options_info = options_info, get_info + + assert options_info.startswith('-->OPTIONS') + assert 'falcon/responders.py:' in options_info + + assert get_info.startswith('-->GET') + assert 'tests/test_cmd_print_api.py:' in get_info def test_traverse(): - """Ensure traverse finds the proper routes.""" + """Ensure traverse() finds the proper routes.""" output = six.moves.StringIO() with redirected(stdout=output): print_routes.traverse(_api._router._roots, verbose=False)