<s> """<STR_LIT>""" <EOL> import collections <EOL> import inspect <EOL> import os <EOL> import re <EOL> import pytest <EOL> from _pytest import python <EOL> import six <EOL> from . import exceptions <EOL> from . import fixtures <EOL> from . feature import ( <EOL> Feature , <EOL> force_encode , <EOL> force_unicode , <EOL> get_features , <EOL> ) <EOL> from . steps import ( <EOL> execute , <EOL> get_caller_function , <EOL> get_caller_module , <EOL> get_step_fixture_name , <EOL> inject_fixture , <EOL> recreate_function , <EOL> ) <EOL> from . types import GIVEN <EOL> from . utils import get_args <EOL> if six . PY3 : <EOL> import runpy <EOL> def execfile ( filename , init_globals ) : <EOL> """<STR_LIT>""" <EOL> result = runpy . run_path ( filename , init_globals = init_globals ) <EOL> init_globals . update ( result ) <EOL> PYTHON_REPLACE_REGEX = re . compile ( "<STR_LIT>" ) <EOL> ALPHA_REGEX = re . compile ( "<STR_LIT>" ) <EOL> def find_argumented_step_fixture_name ( name , type_ , fixturemanager , request = None ) : <EOL> """<STR_LIT>""" <EOL> for fixturename , fixturedefs in list ( fixturemanager . _arg2fixturedefs . items ( ) ) : <EOL> for fixturedef in fixturedefs : <EOL> parser = getattr ( fixturedef . func , "<STR_LIT>" , None ) <EOL> match = parser . is_matching ( name ) if parser else None <EOL> if match : <EOL> converters = getattr ( fixturedef . func , "<STR_LIT>" , { } ) <EOL> for arg , value in parser . parse_arguments ( name ) . items ( ) : <EOL> if arg in converters : <EOL> value = converters [ arg ] ( value ) <EOL> if request : <EOL> inject_fixture ( request , arg , value ) <EOL> parser_name = get_step_fixture_name ( parser . name , type_ ) <EOL> if request : <EOL> try : <EOL> request . getfuncargvalue ( parser_name ) <EOL> except python . FixtureLookupError : <EOL> continue <EOL> return parser_name <EOL> def _find_step_function ( request , step , scenario , encoding ) : <EOL> """<STR_LIT>""" <EOL> name = step . name <EOL> try : <EOL> return request . getfuncargvalue ( get_step_fixture_name ( name , step . type , encoding ) ) <EOL> except python . FixtureLookupError : <EOL> try : <EOL> name = find_argumented_step_fixture_name ( name , step . type , request . _fixturemanager , request ) <EOL> if name : <EOL> return request . getfuncargvalue ( name ) <EOL> raise <EOL> except python . FixtureLookupError : <EOL> raise exceptions . StepDefinitionNotFoundError ( <EOL> u"""<STR_LIT>""" <EOL> """<STR_LIT>""" . format ( <EOL> step = step , <EOL> scenario = scenario , <EOL> feature = scenario . feature , <EOL> ) <EOL> ) <EOL> def _execute_step_function ( request , scenario , step , step_func ) : <EOL> """<STR_LIT>""" <EOL> kw = dict ( <EOL> request = request , <EOL> feature = scenario . feature , <EOL> scenario = scenario , <EOL> step = step , <EOL> step_func = step_func , <EOL> ) <EOL> request . config . hook . pytest_bdd_before_step ( ** kw ) <EOL> kw [ "<STR_LIT>" ] = { } <EOL> try : <EOL> kwargs = dict ( ( arg , request . getfuncargvalue ( arg ) ) for arg in get_args ( step_func ) ) <EOL> kw [ "<STR_LIT>" ] = kwargs <EOL> request . config . hook . pytest_bdd_before_step_call ( ** kw ) <EOL> step_func ( ** kwargs ) <EOL> request . config . hook . pytest_bdd_after_step ( ** kw ) <EOL> except Exception as exception : <EOL> request . config . hook . pytest_bdd_step_error ( exception = exception , ** kw ) <EOL> raise <EOL> def _execute_scenario ( feature , scenario , request , encoding ) : <EOL> """<STR_LIT>""" <EOL> request . config . hook . pytest_bdd_before_scenario ( <EOL> request = request , <EOL> feature = feature , <EOL> scenario = scenario , <EOL> ) <EOL> try : <EOL> givens = set ( ) <EOL> for step in scenario . steps : <EOL> try : <EOL> step_func = _find_step_function ( request , step , scenario , encoding = encoding ) <EOL> except exceptions . StepDefinitionNotFoundError as exception : <EOL> request . config . hook . pytest_bdd_step_func_lookup_error ( <EOL> request = request , <EOL> feature = feature , <EOL> scenario = scenario , <EOL> step = step , <EOL> exception = exception , <EOL> ) <EOL> raise <EOL> try : <EOL> if step . type == GIVEN : <EOL> if step_func . fixture in givens : <EOL> raise exceptions . GivenAlreadyUsed ( <EOL> u'<STR_LIT>' . format ( <EOL> step_func . fixture , step . name , <EOL> ) <EOL> ) <EOL> givens . add ( step_func . fixture ) <EOL> except exceptions . ScenarioValidationError as exception : <EOL> request . config . hook . pytest_bdd_step_validation_error ( <EOL> request = request , <EOL> feature = feature , <EOL> scenario = scenario , <EOL> step = step , <EOL> step_func = step_func , <EOL> exception = exception , <EOL> ) <EOL> raise <EOL> _execute_step_function ( request , scenario , step , step_func ) <EOL> finally : <EOL> request . config . hook . pytest_bdd_after_scenario ( <EOL> request = request , <EOL> feature = feature , <EOL> scenario = scenario , <EOL> ) <EOL> FakeRequest = collections . namedtuple ( "<STR_LIT>" , [ "<STR_LIT>" ] ) <EOL> def get_fixture ( caller_module , fixture , path = None ) : <EOL> """<STR_LIT>""" <EOL> def call_fixture ( function ) : <EOL> args = [ ] <EOL> if "<STR_LIT>" in get_args ( function ) : <EOL> args = [ FakeRequest ( module = caller_module ) ] <EOL> return function ( * args ) <EOL> if path is None : <EOL> if hasattr ( caller_module , fixture ) : <EOL> return call_fixture ( getattr ( caller_module , fixture ) ) <EOL> path = os . path . dirname ( caller_module . __file__ ) <EOL> if os . path . exists ( os . path . join ( path , "<STR_LIT>" ) ) : <EOL> file_path = os . path . join ( path , "<STR_LIT>" ) <EOL> if os . path . exists ( file_path ) : <EOL> globs = { } <EOL> execfile ( file_path , globs ) <EOL> if fixture in globs : <EOL> return call_fixture ( globs [ fixture ] ) <EOL> else : <EOL> return call_fixture ( getattr ( fixtures , fixture ) ) <EOL> return get_fixture ( caller_module , fixture , path = os . path . dirname ( path ) ) <EOL> def _get_scenario_decorator ( feature , feature_name , scenario , scenario_name , caller_module , caller_function , encoding ) : <EOL> """<STR_LIT>""" <EOL> g = locals ( ) <EOL> g [ "<STR_LIT>" ] = _execute_scenario <EOL> scenario_name = force_encode ( scenario_name , encoding ) <EOL> def decorator ( _pytestbdd_function ) : <EOL> if isinstance ( _pytestbdd_function , python . FixtureRequest ) : <EOL> raise exceptions . ScenarioIsDecoratorOnly ( <EOL> "<STR_LIT>" , <EOL> ) <EOL> g . update ( locals ( ) ) <EOL> args = get_args ( _pytestbdd_function ) <EOL> function_args = list ( args ) <EOL> for arg in scenario . get_example_params ( ) : <EOL> if arg not in function_args : <EOL> function_args . append ( arg ) <EOL> if "<STR_LIT>" not in function_args : <EOL> function_args . append ( "<STR_LIT>" ) <EOL> code = """<STR_LIT>""" . format ( <EOL> name = _pytestbdd_function . __name__ , <EOL> function_args = "<STR_LIT:U+002CU+0020>" . join ( function_args ) , <EOL> args = "<STR_LIT:U+002CU+0020>" . join ( args ) ) <EOL> execute ( code , g ) <EOL> _scenario = recreate_function ( <EOL> g [ _pytestbdd_function . __name__ ] , <EOL> module = caller_module , <EOL> firstlineno = caller_function . f_lineno , <EOL> ) <EOL> for param_set in scenario . get_params ( ) : <EOL> if param_set : <EOL> _scenario = pytest . mark . parametrize ( * param_set ) ( _scenario ) <EOL> for tag in scenario . tags . union ( feature . tags ) : <EOL> _scenario = getattr ( pytest . mark , tag ) ( _scenario ) <EOL> _scenario . __doc__ = "<STR_LIT>" . format ( <EOL> feature_name = feature_name , scenario_name = scenario_name ) <EOL> _scenario . __scenario__ = scenario <EOL> scenario . test_function = _scenario <EOL> return _scenario <EOL> return recreate_function ( decorator , module = caller_module , firstlineno = caller_function . f_lineno ) <EOL> def scenario ( feature_name , scenario_name , encoding = "<STR_LIT:utf-8>" , example_converters = None , <EOL> caller_module = None , caller_function = None ) : <EOL> """<STR_LIT>""" <EOL> scenario_name = force_unicode ( scenario_name , encoding ) <EOL> caller_module = caller_module or get_caller_module ( ) <EOL> caller_function = caller_function or get_caller_function ( ) <EOL> base_path = get_fixture ( caller_module , "<STR_LIT>" ) <EOL> strict_gherkin = get_fixture ( caller_module , "<STR_LIT>" ) <EOL> feature = Feature . get_feature ( base_path , feature_name , encoding = encoding , strict_gherkin = strict_gherkin ) <EOL> try : <EOL> scenario = feature . scenarios [ scenario_name ] <EOL> except KeyError : <EOL> raise exceptions . ScenarioNotFound ( <EOL> u'<STR_LIT>' . format ( <EOL> scenario_name = scenario_name , <EOL> feature_name = feature . name or "<STR_LIT>" , <EOL> feature_filename = feature . filename , <EOL> ) <EOL> ) <EOL> scenario . example_converters = example_converters <EOL> scenario . validate ( ) <EOL> return _get_scenario_decorator ( <EOL> feature , <EOL> feature_name , <EOL> scenario , <EOL> scenario_name , <EOL> caller_module , <EOL> caller_function , <EOL> encoding , <EOL> ) <EOL> def make_python_name ( string ) : <EOL> """<STR_LIT>""" <EOL> string = re . sub ( PYTHON_REPLACE_REGEX , "<STR_LIT>" , string . replace ( "<STR_LIT:U+0020>" , "<STR_LIT:_>" ) ) <EOL> return re . sub ( ALPHA_REGEX , "<STR_LIT>" , string ) . lower ( ) <EOL> def get_python_name_generator ( name ) : <EOL> """<STR_LIT>""" <EOL> python_name = make_python_name ( name ) <EOL> suffix = '<STR_LIT>' <EOL> index = <NUM_LIT:0> <EOL> def get_name ( ) : <EOL> return '<STR_LIT>' . format ( python_name , suffix ) <EOL> while True : <EOL> yield get_name ( ) <EOL> index += <NUM_LIT:1> <EOL> suffix = '<STR_LIT>' . format ( index ) <EOL> def scenarios ( * feature_paths , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> frame = inspect . stack ( ) [ <NUM_LIT:1> ] <EOL> module = inspect . getmodule ( frame [ <NUM_LIT:0> ] ) <EOL> base_path = get_fixture ( module , "<STR_LIT>" ) <EOL> strict_gherkin = get_fixture ( module , "<STR_LIT>" ) <EOL> abs_feature_paths = [ ] <EOL> for path in feature_paths : <EOL> if not os . path . isabs ( path ) : <EOL> path = os . path . abspath ( os . path . join ( base_path , path ) ) <EOL> abs_feature_paths . append ( path ) <EOL> found = False <EOL> module_scenarios = frozenset ( <EOL> ( attr . __scenario__ . feature . filename , attr . __scenario__ . name ) <EOL> for name , attr in module . __dict__ . items ( ) if hasattr ( attr , '<STR_LIT>' ) ) <EOL> index = <NUM_LIT:10> <EOL> for feature in get_features ( abs_feature_paths , strict_gherkin = strict_gherkin ) : <EOL> for scenario_name , scenario_object in feature . scenarios . items ( ) : <EOL> if ( scenario_object . feature . filename , scenario_name ) not in module_scenarios : <EOL> @ scenario ( feature . filename , scenario_name , ** kwargs ) <EOL> def _scenario ( ) : <EOL> pass <EOL> for test_name in get_python_name_generator ( scenario_name ) : <EOL> if test_name not in module . __dict__ : <EOL> _scenario = recreate_function ( _scenario , module = module , firstlineno = index * <NUM_LIT:4> ) <EOL> index += <NUM_LIT:1> <EOL> module . __dict__ [ test_name ] = _scenario <EOL> break <EOL> found = True <EOL> if not found : <EOL> raise exceptions . NoScenariosFound ( abs_feature_paths ) </s>
