<s> from __future__ import absolute_import , print_function , with_statement <EOL> from collections import defaultdict <EOL> from platform import python_implementation <EOL> import os . path <EOL> import sys <EOL> import warnings <EOL> import tempfile <EOL> import unittest <EOL> import six <EOL> from six import StringIO <EOL> from mock import Mock , patch <EOL> from nose . tools import * <EOL> from behave . model import Table <EOL> from behave . step_registry import StepRegistry <EOL> from behave import parser , runner <EOL> from behave . configuration import ConfigError <EOL> from behave . formatter . base import StreamOpener <EOL> _text = six . text_type <EOL> class TestContext ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> r = Mock ( ) <EOL> self . config = r . config = Mock ( ) <EOL> r . config . verbose = False <EOL> self . context = runner . Context ( r ) <EOL> def test_user_mode_shall_restore_behave_mode ( self ) : <EOL> initial_mode = runner . Context . BEHAVE <EOL> eq_ ( self . context . _mode , initial_mode ) <EOL> with self . context . user_mode ( ) : <EOL> eq_ ( self . context . _mode , runner . Context . USER ) <EOL> self . context . thing = "<STR_LIT>" <EOL> eq_ ( self . context . _mode , initial_mode ) <EOL> def test_user_mode_shall_restore_behave_mode_if_assert_fails ( self ) : <EOL> initial_mode = runner . Context . BEHAVE <EOL> eq_ ( self . context . _mode , initial_mode ) <EOL> try : <EOL> with self . context . user_mode ( ) : <EOL> eq_ ( self . context . _mode , runner . Context . USER ) <EOL> assert False , "<STR_LIT>" <EOL> except AssertionError : <EOL> eq_ ( self . context . _mode , initial_mode ) <EOL> def test_user_mode_shall_restore_behave_mode_if_exception_is_raised ( self ) : <EOL> initial_mode = runner . Context . BEHAVE <EOL> eq_ ( self . context . _mode , initial_mode ) <EOL> try : <EOL> with self . context . user_mode ( ) : <EOL> eq_ ( self . context . _mode , runner . Context . USER ) <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> except RuntimeError : <EOL> eq_ ( self . context . _mode , initial_mode ) <EOL> def test_use_with_user_mode__shall_restore_initial_mode ( self ) : <EOL> initial_mode = runner . Context . BEHAVE <EOL> self . context . _mode = initial_mode <EOL> with self . context . use_with_user_mode ( ) : <EOL> eq_ ( self . context . _mode , runner . Context . USER ) <EOL> self . context . thing = "<STR_LIT>" <EOL> eq_ ( self . context . _mode , initial_mode ) <EOL> def test_use_with_user_mode__shall_restore_initial_mode_with_error ( self ) : <EOL> initial_mode = runner . Context . BEHAVE <EOL> self . context . _mode = initial_mode <EOL> try : <EOL> with self . context . use_with_user_mode ( ) : <EOL> eq_ ( self . context . _mode , runner . Context . USER ) <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> except RuntimeError : <EOL> eq_ ( self . context . _mode , initial_mode ) <EOL> def test_use_with_behave_mode__shall_restore_initial_mode ( self ) : <EOL> initial_mode = runner . Context . USER <EOL> self . context . _mode = initial_mode <EOL> with self . context . _use_with_behave_mode ( ) : <EOL> eq_ ( self . context . _mode , runner . Context . BEHAVE ) <EOL> self . context . thing = "<STR_LIT>" <EOL> eq_ ( self . context . _mode , initial_mode ) <EOL> def test_use_with_behave_mode__shall_restore_initial_mode_with_error ( self ) : <EOL> initial_mode = runner . Context . USER <EOL> self . context . _mode = initial_mode <EOL> try : <EOL> with self . context . _use_with_behave_mode ( ) : <EOL> eq_ ( self . context . _mode , runner . Context . BEHAVE ) <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> except RuntimeError : <EOL> eq_ ( self . context . _mode , initial_mode ) <EOL> def test_context_contains ( self ) : <EOL> eq_ ( "<STR_LIT>" in self . context , False ) <EOL> self . context . thing = "<STR_LIT>" <EOL> eq_ ( "<STR_LIT>" in self . context , True ) <EOL> self . context . _push ( ) <EOL> eq_ ( "<STR_LIT>" in self . context , True ) <EOL> def test_attribute_set_at_upper_level_visible_at_lower_level ( self ) : <EOL> self . context . thing = "<STR_LIT>" <EOL> self . context . _push ( ) <EOL> eq_ ( self . context . thing , "<STR_LIT>" ) <EOL> def test_attribute_set_at_lower_level_not_visible_at_upper_level ( self ) : <EOL> self . context . _push ( ) <EOL> self . context . thing = "<STR_LIT>" <EOL> self . context . _pop ( ) <EOL> assert getattr ( self . context , "<STR_LIT>" , None ) is None <EOL> def test_attributes_set_at_upper_level_visible_at_lower_level ( self ) : <EOL> self . context . thing = "<STR_LIT>" <EOL> self . context . _push ( ) <EOL> eq_ ( self . context . thing , "<STR_LIT>" ) <EOL> self . context . other_thing = "<STR_LIT>" <EOL> self . context . _push ( ) <EOL> eq_ ( self . context . thing , "<STR_LIT>" ) <EOL> eq_ ( self . context . other_thing , "<STR_LIT>" ) <EOL> self . context . third_thing = "<STR_LIT>" <EOL> self . context . _push ( ) <EOL> eq_ ( self . context . thing , "<STR_LIT>" ) <EOL> eq_ ( self . context . other_thing , "<STR_LIT>" ) <EOL> eq_ ( self . context . third_thing , "<STR_LIT>" ) <EOL> def test_attributes_set_at_lower_level_not_visible_at_upper_level ( self ) : <EOL> self . context . thing = "<STR_LIT>" <EOL> self . context . _push ( ) <EOL> self . context . other_thing = "<STR_LIT>" <EOL> self . context . _push ( ) <EOL> self . context . third_thing = "<STR_LIT>" <EOL> eq_ ( self . context . thing , "<STR_LIT>" ) <EOL> eq_ ( self . context . other_thing , "<STR_LIT>" ) <EOL> eq_ ( self . context . third_thing , "<STR_LIT>" ) <EOL> self . context . _pop ( ) <EOL> eq_ ( self . context . thing , "<STR_LIT>" ) <EOL> eq_ ( self . context . other_thing , "<STR_LIT>" ) <EOL> assert getattr ( self . context , "<STR_LIT>" , None ) is None , "<STR_LIT>" % self . context . third_thing <EOL> self . context . _pop ( ) <EOL> eq_ ( self . context . thing , "<STR_LIT>" ) <EOL> assert getattr ( self . context , "<STR_LIT>" , None ) is None , "<STR_LIT>" % self . context . other_thing <EOL> assert getattr ( self . context , "<STR_LIT>" , None ) is None , "<STR_LIT>" % self . context . third_thing <EOL> def test_masking_existing_user_attribute_when_verbose_causes_warning ( self ) : <EOL> warns = [ ] <EOL> def catch_warning ( * args , ** kwargs ) : <EOL> warns . append ( args [ <NUM_LIT:0> ] ) <EOL> old_showwarning = warnings . showwarning <EOL> warnings . showwarning = catch_warning <EOL> self . config . verbose = True <EOL> with self . context . use_with_user_mode ( ) : <EOL> self . context . thing = "<STR_LIT>" <EOL> self . context . _push ( ) <EOL> self . context . thing = "<STR_LIT>" <EOL> warnings . showwarning = old_showwarning <EOL> print ( repr ( warns ) ) <EOL> assert warns , "<STR_LIT>" <EOL> warning = warns [ <NUM_LIT:0> ] <EOL> assert isinstance ( warning , runner . ContextMaskWarning ) , "<STR_LIT>" <EOL> info = warning . args [ <NUM_LIT:0> ] <EOL> assert info . startswith ( "<STR_LIT>" ) , "<STR_LIT>" % info <EOL> assert "<STR_LIT>" in info , "<STR_LIT>" % ( "<STR_LIT>" , info ) <EOL> assert "<STR_LIT>" in info , '<STR_LIT>' % ( info , ) <EOL> def test_masking_existing_user_attribute_when_not_verbose_causes_no_warning ( self ) : <EOL> warns = [ ] <EOL> def catch_warning ( * args , ** kwargs ) : <EOL> warns . append ( args [ <NUM_LIT:0> ] ) <EOL> old_showwarning = warnings . showwarning <EOL> warnings . showwarning = catch_warning <EOL> self . config . verbose = False <EOL> with self . context . use_with_user_mode ( ) : <EOL> self . context . thing = "<STR_LIT>" <EOL> self . context . _push ( ) <EOL> self . context . thing = "<STR_LIT>" <EOL> warnings . showwarning = old_showwarning <EOL> assert not warns <EOL> def test_behave_masking_user_attribute_causes_warning ( self ) : <EOL> warns = [ ] <EOL> def catch_warning ( * args , ** kwargs ) : <EOL> warns . append ( args [ <NUM_LIT:0> ] ) <EOL> old_showwarning = warnings . showwarning <EOL> warnings . showwarning = catch_warning <EOL> with self . context . use_with_user_mode ( ) : <EOL> self . context . thing = "<STR_LIT>" <EOL> self . context . _push ( ) <EOL> self . context . thing = "<STR_LIT>" <EOL> warnings . showwarning = old_showwarning <EOL> print ( repr ( warns ) ) <EOL> assert warns , "<STR_LIT>" <EOL> warning = warns [ <NUM_LIT:0> ] <EOL> assert isinstance ( warning , runner . ContextMaskWarning ) , "<STR_LIT>" <EOL> info = warning . args [ <NUM_LIT:0> ] <EOL> assert info . startswith ( "<STR_LIT>" ) , "<STR_LIT>" % info <EOL> assert "<STR_LIT>" in info , "<STR_LIT>" % ( "<STR_LIT>" , info ) <EOL> filename = __file__ . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if python_implementation ( ) == "<STR_LIT>" : <EOL> filename = filename . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> assert filename in info , "<STR_LIT>" % ( filename , info ) <EOL> def test_setting_root_attribute_that_masks_existing_causes_warning ( self ) : <EOL> warns = [ ] <EOL> def catch_warning ( * args , ** kwargs ) : <EOL> warns . append ( args [ <NUM_LIT:0> ] ) <EOL> old_showwarning = warnings . showwarning <EOL> warnings . showwarning = catch_warning <EOL> with self . context . use_with_user_mode ( ) : <EOL> self . context . _push ( ) <EOL> self . context . thing = "<STR_LIT>" <EOL> self . context . _set_root_attribute ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> warnings . showwarning = old_showwarning <EOL> print ( repr ( warns ) ) <EOL> assert warns <EOL> warning = warns [ <NUM_LIT:0> ] <EOL> assert isinstance ( warning , runner . ContextMaskWarning ) <EOL> info = warning . args [ <NUM_LIT:0> ] <EOL> assert info . startswith ( "<STR_LIT>" ) , "<STR_LIT>" % info <EOL> assert "<STR_LIT>" in info , "<STR_LIT>" % ( "<STR_LIT>" , info ) <EOL> filename = __file__ . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if python_implementation ( ) == "<STR_LIT>" : <EOL> filename = filename . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> assert filename in info , "<STR_LIT>" % ( filename , info ) <EOL> def test_context_deletable ( self ) : <EOL> eq_ ( "<STR_LIT>" in self . context , False ) <EOL> self . context . thing = "<STR_LIT>" <EOL> eq_ ( "<STR_LIT>" in self . context , True ) <EOL> del self . context . thing <EOL> eq_ ( "<STR_LIT>" in self . context , False ) <EOL> @ raises ( AttributeError ) <EOL> def test_context_deletable_raises ( self ) : <EOL> eq_ ( "<STR_LIT>" in self . context , False ) <EOL> self . context . thing = "<STR_LIT>" <EOL> eq_ ( "<STR_LIT>" in self . context , True ) <EOL> self . context . _push ( ) <EOL> eq_ ( "<STR_LIT>" in self . context , True ) <EOL> del self . context . thing <EOL> class ExampleSteps ( object ) : <EOL> text = None <EOL> table = None <EOL> @ staticmethod <EOL> def step_passes ( context ) : <EOL> pass <EOL> @ staticmethod <EOL> def step_fails ( context ) : <EOL> assert False , "<STR_LIT>" <EOL> @ classmethod <EOL> def step_with_text ( cls , context ) : <EOL> assert context . text is not None , "<STR_LIT>" <EOL> cls . text = context . text <EOL> @ classmethod <EOL> def step_with_table ( cls , context ) : <EOL> assert context . table , "<STR_LIT>" <EOL> cls . table = context . table <EOL> @ classmethod <EOL> def register_steps_with ( cls , step_registry ) : <EOL> step_definitions = [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cls . step_passes ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cls . step_fails ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cls . step_with_text ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cls . step_with_table ) , <EOL> ] <EOL> for keyword , string , func in step_definitions : <EOL> step_registry . add_step_definition ( keyword , string , func ) <EOL> class TestContext_ExecuteSteps ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> step_registry = None <EOL> def setUp ( self ) : <EOL> if not self . step_registry : <EOL> self . step_registry = StepRegistry ( ) <EOL> ExampleSteps . register_steps_with ( self . step_registry ) <EOL> ExampleSteps . text = None <EOL> ExampleSteps . table = None <EOL> runner_ = Mock ( ) <EOL> self . config = runner_ . config = Mock ( ) <EOL> runner_ . config . verbose = False <EOL> runner_ . config . stdout_capture = False <EOL> runner_ . config . stderr_capture = False <EOL> runner_ . config . log_capture = False <EOL> runner_ . step_registry = self . step_registry <EOL> self . context = runner . Context ( runner_ ) <EOL> runner_ . context = self . context <EOL> self . context . feature = Mock ( ) <EOL> self . context . feature . parser = parser . Parser ( ) <EOL> self . context . runner = runner_ <EOL> def test_execute_steps_with_simple_steps ( self ) : <EOL> doc = u"""<STR_LIT>""" . lstrip ( ) <EOL> with patch ( "<STR_LIT>" , self . step_registry ) : <EOL> result = self . context . execute_steps ( doc ) <EOL> eq_ ( result , True ) <EOL> def test_execute_steps_with_failing_step ( self ) : <EOL> doc = u"""<STR_LIT>""" . lstrip ( ) <EOL> with patch ( "<STR_LIT>" , self . step_registry ) : <EOL> try : <EOL> result = self . context . execute_steps ( doc ) <EOL> except AssertionError as e : <EOL> ok_ ( "<STR_LIT>" in _text ( e ) ) <EOL> def test_execute_steps_with_undefined_step ( self ) : <EOL> doc = u"""<STR_LIT>""" . lstrip ( ) <EOL> with patch ( "<STR_LIT>" , self . step_registry ) : <EOL> try : <EOL> result = self . context . execute_steps ( doc ) <EOL> except AssertionError as e : <EOL> ok_ ( "<STR_LIT>" in _text ( e ) ) <EOL> def test_execute_steps_with_text ( self ) : <EOL> doc = u'''<STR_LIT>''' . lstrip ( ) <EOL> with patch ( "<STR_LIT>" , self . step_registry ) : <EOL> result = self . context . execute_steps ( doc ) <EOL> expected_text = "<STR_LIT>" <EOL> eq_ ( result , True ) <EOL> eq_ ( expected_text , ExampleSteps . text ) <EOL> def test_execute_steps_with_table ( self ) : <EOL> doc = u"""<STR_LIT>""" . lstrip ( ) <EOL> with patch ( "<STR_LIT>" , self . step_registry ) : <EOL> result = self . context . execute_steps ( doc ) <EOL> expected_table = Table ( [ u"<STR_LIT:Name>" , u"<STR_LIT>" ] , <NUM_LIT:0> , [ <EOL> [ u"<STR_LIT>" , u"<STR_LIT>" ] , <EOL> [ u"<STR_LIT>" , u"<STR_LIT>" ] , <EOL> ] ) <EOL> eq_ ( result , True ) <EOL> eq_ ( expected_table , ExampleSteps . table ) <EOL> def test_context_table_is_restored_after_execute_steps_without_table ( self ) : <EOL> doc = u"""<STR_LIT>""" . lstrip ( ) <EOL> with patch ( "<STR_LIT>" , self . step_registry ) : <EOL> original_table = "<STR_LIT>" <EOL> self . context . table = original_table <EOL> self . context . execute_steps ( doc ) <EOL> eq_ ( self . context . table , original_table ) <EOL> def test_context_table_is_restored_after_execute_steps_with_table ( self ) : <EOL> doc = u"""<STR_LIT>""" . lstrip ( ) <EOL> with patch ( "<STR_LIT>" , self . step_registry ) : <EOL> original_table = "<STR_LIT>" <EOL> self . context . table = original_table <EOL> self . context . execute_steps ( doc ) <EOL> eq_ ( self . context . table , original_table ) <EOL> def test_context_text_is_restored_after_execute_steps_without_text ( self ) : <EOL> doc = u"""<STR_LIT>""" . lstrip ( ) <EOL> with patch ( "<STR_LIT>" , self . step_registry ) : <EOL> original_text = "<STR_LIT>" <EOL> self . context . text = original_text <EOL> self . context . execute_steps ( doc ) <EOL> eq_ ( self . context . text , original_text ) <EOL> def test_context_text_is_restored_after_execute_steps_with_text ( self ) : <EOL> doc = u'''<STR_LIT>''' . lstrip ( ) <EOL> with patch ( "<STR_LIT>" , self . step_registry ) : <EOL> original_text = "<STR_LIT>" <EOL> self . context . text = original_text <EOL> self . context . execute_steps ( doc ) <EOL> eq_ ( self . context . text , original_text ) <EOL> @ raises ( ValueError ) <EOL> def test_execute_steps_should_fail_when_called_without_feature ( self ) : <EOL> doc = u"""<STR_LIT>""" . lstrip ( ) <EOL> with patch ( "<STR_LIT>" , self . step_registry ) : <EOL> self . context . feature = None <EOL> self . context . execute_steps ( doc ) <EOL> def create_mock_config ( ) : <EOL> config = Mock ( ) <EOL> config . steps_dir = "<STR_LIT>" <EOL> config . environment_file = "<STR_LIT>" <EOL> return config <EOL> class TestRunner ( object ) : <EOL> def test_load_hooks_execfiles_hook_file ( self ) : <EOL> with patch ( "<STR_LIT>" ) as ef : <EOL> with patch ( "<STR_LIT>" ) as exists : <EOL> exists . return_value = True <EOL> base_dir = "<STR_LIT>" <EOL> hooks_path = os . path . join ( base_dir , "<STR_LIT>" ) <EOL> r = runner . Runner ( create_mock_config ( ) ) <EOL> r . base_dir = base_dir <EOL> r . load_hooks ( ) <EOL> exists . assert_called_with ( hooks_path ) <EOL> ef . assert_called_with ( hooks_path , r . hooks ) <EOL> def test_run_hook_runs_a_hook_that_exists ( self ) : <EOL> r = runner . Runner ( None ) <EOL> r . config = Mock ( ) <EOL> r . config . dry_run = False <EOL> r . hooks [ "<STR_LIT>" ] = hook = Mock ( ) <EOL> args = ( runner . Context ( Mock ( ) ) , Mock ( ) , Mock ( ) ) <EOL> r . run_hook ( "<STR_LIT>" , * args ) <EOL> hook . assert_called_with ( * args ) <EOL> def test_run_hook_does_not_runs_a_hook_that_exists_if_dry_run ( self ) : <EOL> r = runner . Runner ( None ) <EOL> r . config = Mock ( ) <EOL> r . config . dry_run = True <EOL> r . hooks [ "<STR_LIT>" ] = hook = Mock ( ) <EOL> args = ( runner . Context ( Mock ( ) ) , Mock ( ) , Mock ( ) ) <EOL> r . run_hook ( "<STR_LIT>" , * args ) <EOL> assert len ( hook . call_args_list ) == <NUM_LIT:0> <EOL> def test_setup_capture_creates_stringio_for_stdout ( self ) : <EOL> r = runner . Runner ( Mock ( ) ) <EOL> r . config . stdout_capture = True <EOL> r . config . log_capture = False <EOL> r . context = Mock ( ) <EOL> r . setup_capture ( ) <EOL> assert r . stdout_capture is not None <EOL> assert isinstance ( r . stdout_capture , StringIO ) <EOL> def test_setup_capture_does_not_create_stringio_if_not_wanted ( self ) : <EOL> r = runner . Runner ( Mock ( ) ) <EOL> r . config . stdout_capture = False <EOL> r . config . stderr_capture = False <EOL> r . config . log_capture = False <EOL> r . setup_capture ( ) <EOL> assert r . stdout_capture is None <EOL> @ patch ( "<STR_LIT>" ) <EOL> def test_setup_capture_creates_memory_handler_for_logging ( self , handler ) : <EOL> r = runner . Runner ( Mock ( ) ) <EOL> r . config . stdout_capture = False <EOL> r . config . log_capture = True <EOL> r . context = Mock ( ) <EOL> r . setup_capture ( ) <EOL> assert r . log_capture is not None <EOL> handler . assert_called_with ( r . config ) <EOL> r . log_capture . inveigle . assert_called_with ( ) <EOL> def test_setup_capture_does_not_create_memory_handler_if_not_wanted ( self ) : <EOL> r = runner . Runner ( Mock ( ) ) <EOL> r . config . stdout_capture = False <EOL> r . config . stderr_capture = False <EOL> r . config . log_capture = False <EOL> r . setup_capture ( ) <EOL> assert r . log_capture is None <EOL> def test_start_stop_capture_switcheroos_sys_stdout ( self ) : <EOL> old_stdout = sys . stdout <EOL> sys . stdout = new_stdout = Mock ( ) <EOL> r = runner . Runner ( Mock ( ) ) <EOL> r . config . stdout_capture = True <EOL> r . config . log_capture = False <EOL> r . context = Mock ( ) <EOL> r . setup_capture ( ) <EOL> r . start_capture ( ) <EOL> eq_ ( sys . stdout , r . stdout_capture ) <EOL> r . stop_capture ( ) <EOL> eq_ ( sys . stdout , new_stdout ) <EOL> sys . stdout = old_stdout <EOL> def test_start_stop_capture_leaves_sys_stdout_alone_if_off ( self ) : <EOL> r = runner . Runner ( Mock ( ) ) <EOL> r . config . stdout_capture = False <EOL> r . config . log_capture = False <EOL> old_stdout = sys . stdout <EOL> r . start_capture ( ) <EOL> eq_ ( sys . stdout , old_stdout ) <EOL> r . stop_capture ( ) <EOL> eq_ ( sys . stdout , old_stdout ) <EOL> def test_teardown_capture_removes_log_tap ( self ) : <EOL> r = runner . Runner ( Mock ( ) ) <EOL> r . config . stdout_capture = False <EOL> r . config . log_capture = True <EOL> r . log_capture = Mock ( ) <EOL> r . teardown_capture ( ) <EOL> r . log_capture . abandon . assert_called_with ( ) <EOL> def test_exec_file ( self ) : <EOL> fn = tempfile . mktemp ( ) <EOL> with open ( fn , "<STR_LIT:w>" ) as f : <EOL> f . write ( "<STR_LIT>" ) <EOL> g = { } <EOL> l = { } <EOL> runner . exec_file ( fn , g , l ) <EOL> assert "<STR_LIT>" in l <EOL> assert "<STR_LIT>" in l , '<STR_LIT>' % ( g , l ) <EOL> eq_ ( l [ "<STR_LIT>" ] , fn ) <EOL> def test_run_returns_true_if_everything_passed ( self ) : <EOL> r = runner . Runner ( Mock ( ) ) <EOL> r . setup_capture = Mock ( ) <EOL> r . setup_paths = Mock ( ) <EOL> r . run_with_paths = Mock ( ) <EOL> r . run_with_paths . return_value = True <EOL> assert r . run ( ) <EOL> def test_run_returns_false_if_anything_failed ( self ) : <EOL> r = runner . Runner ( Mock ( ) ) <EOL> r . setup_capture = Mock ( ) <EOL> r . setup_paths = Mock ( ) <EOL> r . run_with_paths = Mock ( ) <EOL> r . run_with_paths . return_value = False <EOL> assert not r . run ( ) <EOL> class TestRunWithPaths ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . config = Mock ( ) <EOL> self . config . reporters = [ ] <EOL> self . config . logging_level = None <EOL> self . config . logging_filter = None <EOL> self . config . outputs = [ Mock ( ) , StreamOpener ( stream = sys . stdout ) ] <EOL> self . config . format = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . runner = runner . Runner ( self . config ) <EOL> self . load_hooks = self . runner . load_hooks = Mock ( ) <EOL> self . load_step_definitions = self . runner . load_step_definitions = Mock ( ) <EOL> self . run_hook = self . runner . run_hook = Mock ( ) <EOL> self . run_step = self . runner . run_step = Mock ( ) <EOL> self . feature_locations = self . runner . feature_locations = Mock ( ) <EOL> self . calculate_summaries = self . runner . calculate_summaries = Mock ( ) <EOL> self . formatter_class = patch ( "<STR_LIT>" ) <EOL> formatter_class = self . formatter_class . start ( ) <EOL> formatter_class . return_value = self . formatter = Mock ( ) <EOL> def tearDown ( self ) : <EOL> self . formatter_class . stop ( ) <EOL> def test_loads_hooks_and_step_definitions ( self ) : <EOL> self . feature_locations . return_value = [ ] <EOL> self . runner . run_with_paths ( ) <EOL> assert self . load_hooks . called <EOL> assert self . load_step_definitions . called <EOL> def test_runs_before_all_and_after_all_hooks ( self ) : <EOL> self . runner . feature_locations = self . run_hook <EOL> self . runner . feature_locations . return_value = [ ] <EOL> self . runner . context = Mock ( ) <EOL> self . runner . run_with_paths ( ) <EOL> eq_ ( self . run_hook . call_args_list , [ <EOL> ( ( ) , { } ) , <EOL> ( ( "<STR_LIT>" , self . runner . context ) , { } ) , <EOL> ( ( "<STR_LIT>" , self . runner . context ) , { } ) , <EOL> ] ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> def test_parses_feature_files_and_appends_to_feature_list ( self , abspath , <EOL> parse_file ) : <EOL> feature_locations = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> feature = Mock ( ) <EOL> feature . tags = [ ] <EOL> feature . __iter__ = Mock ( return_value = iter ( [ ] ) ) <EOL> feature . run . return_value = False <EOL> self . runner . feature_locations . return_value = feature_locations <EOL> abspath . side_effect = lambda x : x . upper ( ) <EOL> self . config . lang = "<STR_LIT>" <EOL> self . config . format = [ "<STR_LIT>" ] <EOL> self . config . outputs = [ StreamOpener ( stream = sys . stdout ) ] <EOL> self . config . output . encoding = None <EOL> self . config . exclude = lambda s : False <EOL> self . config . junit = False <EOL> self . config . summary = False <EOL> parse_file . return_value = feature <EOL> self . runner . run_with_paths ( ) <EOL> expected_parse_file_args = [ ( ( x . upper ( ) , ) , { "<STR_LIT>" : "<STR_LIT>" } ) for x in feature_locations ] <EOL> eq_ ( parse_file . call_args_list , expected_parse_file_args ) <EOL> eq_ ( self . runner . features , [ feature ] * <NUM_LIT:3> ) <EOL> class FsMock ( object ) : <EOL> def __init__ ( self , * paths ) : <EOL> self . base = os . path . abspath ( "<STR_LIT:.>" ) <EOL> self . sep = os . path . sep <EOL> def full_split ( path ) : <EOL> bits = [ ] <EOL> while path : <EOL> path , bit = os . path . split ( path ) <EOL> bits . insert ( <NUM_LIT:0> , bit ) <EOL> return bits <EOL> paths = [ os . path . join ( self . base , * full_split ( path ) ) for path in paths ] <EOL> print ( repr ( paths ) ) <EOL> self . paths = paths <EOL> self . files = set ( ) <EOL> self . dirs = defaultdict ( list ) <EOL> separators = [ sep for sep in ( os . path . sep , os . path . altsep ) if sep ] <EOL> for path in paths : <EOL> if path [ - <NUM_LIT:1> ] in separators : <EOL> self . dirs [ path [ : - <NUM_LIT:1> ] ] = [ ] <EOL> d , p = os . path . split ( path [ : - <NUM_LIT:1> ] ) <EOL> while d and p : <EOL> self . dirs [ d ] . append ( p ) <EOL> d , p = os . path . split ( d ) <EOL> else : <EOL> self . files . add ( path ) <EOL> d , f = os . path . split ( path ) <EOL> self . dirs [ d ] . append ( f ) <EOL> self . calls = [ ] <EOL> def listdir ( self , dir ) : <EOL> self . calls . append ( ( "<STR_LIT>" , dir ) ) <EOL> return self . dirs . get ( dir , [ ] ) <EOL> def isfile ( self , path ) : <EOL> self . calls . append ( ( "<STR_LIT>" , path ) ) <EOL> return path in self . files <EOL> def isdir ( self , path ) : <EOL> self . calls . append ( ( "<STR_LIT>" , path ) ) <EOL> return path in self . dirs <EOL> def exists ( self , path ) : <EOL> self . calls . append ( ( "<STR_LIT>" , path ) ) <EOL> return path in self . dirs or path in self . files <EOL> def walk ( self , path , locations = None ) : <EOL> if locations is None : <EOL> assert path in self . dirs , "<STR_LIT>" % ( path , self . dirs ) <EOL> locations = [ ] <EOL> dirnames = [ ] <EOL> filenames = [ ] <EOL> for e in self . dirs [ path ] : <EOL> if os . path . join ( path , e ) in self . dirs : <EOL> dirnames . append ( e ) <EOL> self . walk ( os . path . join ( path , e ) , locations ) <EOL> else : <EOL> filenames . append ( e ) <EOL> locations . append ( ( path , dirnames , filenames ) ) <EOL> return locations <EOL> def dirname ( self , path , orig = os . path . dirname ) : <EOL> return orig ( path ) <EOL> def abspath ( self , path , orig = os . path . abspath ) : <EOL> return orig ( path ) <EOL> def join ( self , x , y , orig = os . path . join ) : <EOL> return orig ( x , y ) <EOL> def split ( self , path , orig = os . path . split ) : <EOL> return orig ( path ) <EOL> def splitdrive ( self , path , orig = os . path . splitdrive ) : <EOL> return orig ( path ) <EOL> class TestFeatureDirectory ( object ) : <EOL> def test_default_path_no_steps ( self ) : <EOL> config = create_mock_config ( ) <EOL> config . paths = [ ] <EOL> config . verbose = True <EOL> r = runner . Runner ( config ) <EOL> fs = FsMock ( ) <EOL> with patch ( "<STR_LIT>" , fs ) : <EOL> assert_raises ( ConfigError , r . setup_paths ) <EOL> ok_ ( ( "<STR_LIT>" , os . path . join ( fs . base , "<STR_LIT>" , "<STR_LIT>" ) ) in fs . calls ) <EOL> def test_default_path_no_features ( self ) : <EOL> config = create_mock_config ( ) <EOL> config . paths = [ ] <EOL> config . verbose = True <EOL> r = runner . Runner ( config ) <EOL> fs = FsMock ( "<STR_LIT>" ) <EOL> with patch ( "<STR_LIT>" , fs ) : <EOL> with patch ( "<STR_LIT>" , fs . walk ) : <EOL> assert_raises ( ConfigError , r . setup_paths ) <EOL> def test_default_path ( self ) : <EOL> config = create_mock_config ( ) <EOL> config . paths = [ ] <EOL> config . verbose = True <EOL> r = runner . Runner ( config ) <EOL> fs = FsMock ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> with patch ( "<STR_LIT>" , fs ) : <EOL> with patch ( "<STR_LIT>" , fs . walk ) : <EOL> with r . path_manager : <EOL> r . setup_paths ( ) <EOL> eq_ ( r . base_dir , os . path . abspath ( "<STR_LIT>" ) ) <EOL> def test_supplied_feature_file ( self ) : <EOL> config = create_mock_config ( ) <EOL> config . paths = [ "<STR_LIT>" ] <EOL> config . verbose = True <EOL> r = runner . Runner ( config ) <EOL> r . context = Mock ( ) <EOL> fs = FsMock ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> with patch ( "<STR_LIT>" , fs ) : <EOL> with patch ( "<STR_LIT>" , fs . walk ) : <EOL> with r . path_manager : <EOL> r . setup_paths ( ) <EOL> ok_ ( ( "<STR_LIT>" , os . path . join ( fs . base , "<STR_LIT>" ) ) in fs . calls ) <EOL> ok_ ( ( "<STR_LIT>" , os . path . join ( fs . base , "<STR_LIT>" ) ) in fs . calls ) <EOL> eq_ ( r . base_dir , fs . base ) <EOL> def test_supplied_feature_file_no_steps ( self ) : <EOL> config = create_mock_config ( ) <EOL> config . paths = [ "<STR_LIT>" ] <EOL> config . verbose = True <EOL> r = runner . Runner ( config ) <EOL> fs = FsMock ( "<STR_LIT>" ) <EOL> with patch ( "<STR_LIT>" , fs ) : <EOL> with patch ( "<STR_LIT>" , fs . walk ) : <EOL> with r . path_manager : <EOL> assert_raises ( ConfigError , r . setup_paths ) <EOL> def test_supplied_feature_directory ( self ) : <EOL> config = create_mock_config ( ) <EOL> config . paths = [ "<STR_LIT>" ] <EOL> config . verbose = True <EOL> r = runner . Runner ( config ) <EOL> fs = FsMock ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> with patch ( "<STR_LIT>" , fs ) : <EOL> with patch ( "<STR_LIT>" , fs . walk ) : <EOL> with r . path_manager : <EOL> r . setup_paths ( ) <EOL> ok_ ( ( "<STR_LIT>" , os . path . join ( fs . base , "<STR_LIT>" , "<STR_LIT>" ) ) in fs . calls ) <EOL> eq_ ( r . base_dir , os . path . join ( fs . base , "<STR_LIT>" ) ) <EOL> def test_supplied_feature_directory_no_steps ( self ) : <EOL> config = create_mock_config ( ) <EOL> config . paths = [ "<STR_LIT>" ] <EOL> config . verbose = True <EOL> r = runner . Runner ( config ) <EOL> fs = FsMock ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> with patch ( "<STR_LIT>" , fs ) : <EOL> with patch ( "<STR_LIT>" , fs . walk ) : <EOL> assert_raises ( ConfigError , r . setup_paths ) <EOL> ok_ ( ( "<STR_LIT>" , os . path . join ( fs . base , "<STR_LIT>" , "<STR_LIT>" ) ) in fs . calls ) <EOL> def test_supplied_feature_directory_missing ( self ) : <EOL> config = create_mock_config ( ) <EOL> config . paths = [ "<STR_LIT>" ] <EOL> config . verbose = True <EOL> r = runner . Runner ( config ) <EOL> fs = FsMock ( ) <EOL> with patch ( "<STR_LIT>" , fs ) : <EOL> with patch ( "<STR_LIT>" , fs . walk ) : <EOL> assert_raises ( ConfigError , r . setup_paths ) <EOL> class TestFeatureDirectoryLayout2 ( object ) : <EOL> def test_default_path ( self ) : <EOL> config = create_mock_config ( ) <EOL> config . paths = [ ] <EOL> config . verbose = True <EOL> r = runner . Runner ( config ) <EOL> fs = FsMock ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> with patch ( "<STR_LIT>" , fs ) : <EOL> with patch ( "<STR_LIT>" , fs . walk ) : <EOL> with r . path_manager : <EOL> r . setup_paths ( ) <EOL> eq_ ( r . base_dir , os . path . abspath ( "<STR_LIT>" ) ) <EOL> def test_supplied_root_directory ( self ) : <EOL> config = create_mock_config ( ) <EOL> config . paths = [ "<STR_LIT>" ] <EOL> config . verbose = True <EOL> r = runner . Runner ( config ) <EOL> fs = FsMock ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> with patch ( "<STR_LIT>" , fs ) : <EOL> with patch ( "<STR_LIT>" , fs . walk ) : <EOL> with r . path_manager : <EOL> r . setup_paths ( ) <EOL> ok_ ( ( "<STR_LIT>" , os . path . join ( fs . base , "<STR_LIT>" , "<STR_LIT>" ) ) in fs . calls ) <EOL> eq_ ( r . base_dir , os . path . join ( fs . base , "<STR_LIT>" ) ) <EOL> def test_supplied_root_directory_no_steps ( self ) : <EOL> config = create_mock_config ( ) <EOL> config . paths = [ "<STR_LIT>" ] <EOL> config . verbose = True <EOL> r = runner . Runner ( config ) <EOL> fs = FsMock ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> with patch ( "<STR_LIT>" , fs ) : <EOL> with patch ( "<STR_LIT>" , fs . walk ) : <EOL> with r . path_manager : <EOL> assert_raises ( ConfigError , r . setup_paths ) <EOL> ok_ ( ( "<STR_LIT>" , os . path . join ( fs . base , "<STR_LIT>" , "<STR_LIT>" ) ) in fs . calls ) <EOL> eq_ ( r . base_dir , None ) <EOL> def test_supplied_feature_file ( self ) : <EOL> config = create_mock_config ( ) <EOL> config . paths = [ "<STR_LIT>" ] <EOL> config . verbose = True <EOL> r = runner . Runner ( config ) <EOL> r . context = Mock ( ) <EOL> fs = FsMock ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> with patch ( "<STR_LIT>" , fs ) : <EOL> with patch ( "<STR_LIT>" , fs . walk ) : <EOL> with r . path_manager : <EOL> r . setup_paths ( ) <EOL> ok_ ( ( "<STR_LIT>" , os . path . join ( fs . base , "<STR_LIT>" , "<STR_LIT>" ) ) in fs . calls ) <EOL> ok_ ( ( "<STR_LIT>" , os . path . join ( fs . base , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) ) in fs . calls ) <EOL> eq_ ( r . base_dir , fs . join ( fs . base , "<STR_LIT>" ) ) <EOL> def test_supplied_feature_file_no_steps ( self ) : <EOL> config = create_mock_config ( ) <EOL> config . paths = [ "<STR_LIT>" ] <EOL> config . verbose = True <EOL> r = runner . Runner ( config ) <EOL> fs = FsMock ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> with patch ( "<STR_LIT>" , fs ) : <EOL> with patch ( "<STR_LIT>" , fs . walk ) : <EOL> with r . path_manager : <EOL> assert_raises ( ConfigError , r . setup_paths ) <EOL> def test_supplied_feature_directory ( self ) : <EOL> config = create_mock_config ( ) <EOL> config . paths = [ "<STR_LIT>" ] <EOL> config . verbose = True <EOL> r = runner . Runner ( config ) <EOL> fs = FsMock ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> with patch ( "<STR_LIT>" , fs ) : <EOL> with patch ( "<STR_LIT>" , fs . walk ) : <EOL> with r . path_manager : <EOL> r . setup_paths ( ) <EOL> ok_ ( ( "<STR_LIT>" , os . path . join ( fs . base , "<STR_LIT>" , "<STR_LIT>" ) ) in fs . calls ) <EOL> eq_ ( r . base_dir , os . path . join ( fs . base , "<STR_LIT>" ) ) <EOL> def test_supplied_feature_directory_no_steps ( self ) : <EOL> config = create_mock_config ( ) <EOL> config . paths = [ "<STR_LIT>" ] <EOL> config . verbose = True <EOL> r = runner . Runner ( config ) <EOL> fs = FsMock ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> with patch ( "<STR_LIT>" , fs ) : <EOL> with patch ( "<STR_LIT>" , fs . walk ) : <EOL> assert_raises ( ConfigError , r . setup_paths ) <EOL> ok_ ( ( "<STR_LIT>" , os . path . join ( fs . base , "<STR_LIT>" , "<STR_LIT>" ) ) in fs . calls ) </s>
