<s> from __future__ import unicode_literals <EOL> import os <EOL> import shutil <EOL> import tempfile <EOL> from mock import Mock , call , patch <EOL> from nose import tools <EOL> from nose . tools import eq_ , raises , ok_ <EOL> from sprinter . testtools import ( MockEnvironment , <EOL> create_mock_formulabase ) <EOL> from sprinter . exceptions import SprinterException , FormulaException <EOL> from sprinter . environment import Environment <EOL> from sprinter . core . templates import source_template <EOL> from sprinter . core . globals import create_default_config <EOL> source_config = """<STR_LIT>""" <EOL> target_config = """<STR_LIT>""" <EOL> class TestEnvironment ( object ) : <EOL> """<STR_LIT>""" <EOL> def test_grab_inputs_existing_source ( self ) : <EOL> """<STR_LIT>""" <EOL> with MockEnvironment ( source_config , target_config ) as environment : <EOL> environment . target . grab_inputs = Mock ( ) <EOL> environment . grab_inputs ( ) <EOL> eq_ ( environment . target . inputs . get_unset_inputs ( ) , set ( [ '<STR_LIT:password>' , '<STR_LIT>' ] ) ) <EOL> @ tools . raises ( SprinterException ) <EOL> def test_running_missing_formula ( self ) : <EOL> """<STR_LIT>""" <EOL> with MockEnvironment ( target_config = missing_formula_config ) as environment : <EOL> environment . install ( ) <EOL> def test_catch_exception_in_feature ( self ) : <EOL> """<STR_LIT>""" <EOL> def test_feature_run_order_install ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch ( '<STR_LIT>' , new = create_mock_formulabase ( ) ) as formulabase : <EOL> with MockEnvironment ( test_source , test_target , mock_formulabase = formulabase ) as environment : <EOL> environment . install ( ) <EOL> eq_ ( formulabase ( ) . method_calls , [ call . should_run ( ) , <EOL> call . validate ( ) , <EOL> call . resolve ( ) , <EOL> call . prompt ( ) , <EOL> call . sync ( ) ] ) <EOL> def test_feature_run_order_update ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch ( '<STR_LIT>' , new = create_mock_formulabase ( ) ) as formulabase : <EOL> with MockEnvironment ( test_source , test_target , mock_formulabase = formulabase ) as environment : <EOL> environment . directory = Mock ( spec = environment . directory ) <EOL> environment . directory . root_dir = "<STR_LIT>" <EOL> environment . directory . new = False <EOL> environment . update ( ) <EOL> eq_ ( formulabase ( ) . method_calls , [ call . should_run ( ) , <EOL> call . validate ( ) , <EOL> call . resolve ( ) , <EOL> call . prompt ( ) , <EOL> call . sync ( ) ] ) <EOL> def test_feature_run_order_remove ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch ( '<STR_LIT>' , new = create_mock_formulabase ( ) ) as formulabase : <EOL> with MockEnvironment ( test_source , test_target , mock_formulabase = formulabase ) as environment : <EOL> environment . directory = Mock ( spec = environment . directory ) <EOL> environment . directory . new = False <EOL> environment . remove ( ) <EOL> eq_ ( formulabase ( ) . method_calls , [ call . should_run ( ) , <EOL> call . validate ( ) , <EOL> call . resolve ( ) , <EOL> call . prompt ( ) , <EOL> call . sync ( ) ] ) <EOL> def test_feature_run_order_deactivate ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch ( '<STR_LIT>' , new = create_mock_formulabase ( ) ) as formulabase : <EOL> with MockEnvironment ( test_source , test_target , mock_formulabase = formulabase ) as environment : <EOL> environment . directory = Mock ( spec = environment . directory ) <EOL> environment . directory . new = False <EOL> environment . deactivate ( ) <EOL> eq_ ( formulabase ( ) . method_calls , [ call . should_run ( ) , <EOL> call . validate ( ) , <EOL> call . resolve ( ) , <EOL> call . prompt ( ) , <EOL> call . deactivate ( ) ] ) <EOL> def test_feature_run_order_activate ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch ( '<STR_LIT>' , new = create_mock_formulabase ( ) ) as formulabase : <EOL> with MockEnvironment ( test_source , test_target , mock_formulabase = formulabase ) as environment : <EOL> environment . directory = Mock ( spec = environment . directory ) <EOL> environment . directory . root_dir = "<STR_LIT>" <EOL> environment . directory . new = False <EOL> environment . activate ( ) <EOL> eq_ ( formulabase ( ) . method_calls , [ call . should_run ( ) , <EOL> call . validate ( ) , <EOL> call . resolve ( ) , <EOL> call . prompt ( ) , <EOL> call . activate ( ) ] ) <EOL> def test_global_shell_configuration_bash ( self ) : <EOL> """<STR_LIT>""" <EOL> global_config = create_default_config ( ) <EOL> global_config . set ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:true>' ) <EOL> global_config . set ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:false>' ) <EOL> global_config . set ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:true>' ) <EOL> with MockEnvironment ( test_source , test_target , global_config = global_config ) as environment : <EOL> environment . install ( ) <EOL> assert [ x for x in environment . injections . inject_dict . keys ( ) if x . endswith ( '<STR_LIT>' ) ] <EOL> env_injected = False <EOL> for profile in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> env_injected = env_injected or filter ( lambda x : x . endswith ( profile ) , environment . injections . inject_dict . keys ( ) ) <EOL> assert env_injected <EOL> assert not [ x for x in environment . injections . inject_dict . keys ( ) if x . endswith ( '<STR_LIT>' ) ] <EOL> for profile in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> assert not [ x for x in environment . injections . inject_dict . keys ( ) if x . endswith ( profile ) ] <EOL> def test_env_to_rc_injection ( self ) : <EOL> """<STR_LIT>""" <EOL> global_config = create_default_config ( ) <EOL> global_config . set ( '<STR_LIT>' , '<STR_LIT>' , True ) <EOL> with MockEnvironment ( test_source , test_target , global_config = global_config ) as environment : <EOL> environment . install ( ) <EOL> env_injected = False <EOL> full_rc_path = os . path . expanduser ( os . path . join ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> for profile in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> full_profile_path = os . path . expanduser ( os . path . join ( "<STR_LIT>" , profile ) ) <EOL> specific_env_injected = full_profile_path in environment . global_injections . inject_dict <EOL> if specific_env_injected : <EOL> env_injected = True <EOL> assert ( source_template % ( full_rc_path , full_rc_path ) in <EOL> environment . global_injections . inject_dict [ full_profile_path ] ) <EOL> assert env_injected <EOL> env_injected = False <EOL> full_rc_path = os . path . expanduser ( os . path . join ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> for profile in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> full_profile_path = os . path . expanduser ( os . path . join ( "<STR_LIT>" , profile ) ) <EOL> specific_env_injected = full_profile_path in environment . global_injections . inject_dict <EOL> if specific_env_injected : <EOL> env_injected = True <EOL> assert ( source_template % ( full_rc_path , full_rc_path ) in <EOL> environment . global_injections . inject_dict [ full_profile_path ] ) <EOL> assert env_injected <EOL> def test_global_shell_configuration_zshell ( self ) : <EOL> """<STR_LIT>""" <EOL> global_config = create_default_config ( ) <EOL> global_config . set ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:false>' ) <EOL> global_config . set ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:true>' ) <EOL> global_config . set ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:false>' ) <EOL> with MockEnvironment ( target_config = test_target , global_config = global_config ) as environment : <EOL> environment . install ( ) <EOL> assert [ x for x in environment . injections . inject_dict . keys ( ) if x . endswith ( '<STR_LIT>' ) ] <EOL> env_injected = False <EOL> for profile in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> env_injected = env_injected or filter ( lambda x : x . endswith ( profile ) , environment . injections . inject_dict . keys ( ) ) <EOL> assert env_injected <EOL> assert not [ x for x in environment . injections . inject_dict . keys ( ) if x . endswith ( '<STR_LIT>' ) ] <EOL> for profile in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> assert not [ x for x in environment . injections . inject_dict . keys ( ) if x . endswith ( profile ) ] <EOL> def test_global_config ( self ) : <EOL> """<STR_LIT>""" <EOL> temp_dir = tempfile . mkdtemp ( ) <EOL> os . makedirs ( os . path . join ( temp_dir , "<STR_LIT>" ) ) <EOL> with open ( os . path . join ( temp_dir , "<STR_LIT>" , "<STR_LIT>" ) , '<STR_LIT>' ) as fh : <EOL> fh . write ( """<STR_LIT>""" ) <EOL> try : <EOL> env = Environment ( root = temp_dir ) <EOL> assert env . global_config . get ( '<STR_LIT>' , '<STR_LIT>' ) == "<STR_LIT:true>" <EOL> finally : <EOL> shutil . rmtree ( temp_dir ) <EOL> def test_utilssh_file_written ( self ) : <EOL> """<STR_LIT>""" <EOL> with MockEnvironment ( target_config = test_target ) as environment : <EOL> environment . install ( ) <EOL> assert os . path . exists ( os . path . join ( environment . global_path , '<STR_LIT>' ) ) <EOL> def test_message_failure_bad_manifest ( self ) : <EOL> "<STR_LIT>" "<STR_LIT>" <EOL> with MockEnvironment ( target_config = test_target ) as environment : <EOL> environment . target = "<STR_LIT>" <EOL> assert environment . message_failure ( ) is None <EOL> @ raises ( SprinterException ) <EOL> def test_no_namespace ( self ) : <EOL> """<STR_LIT>""" <EOL> with MockEnvironment ( target_config = "<STR_LIT>" ) as environment : <EOL> del ( environment . namespace ) <EOL> environment . warmup ( ) <EOL> def test_grab_inputs_in_install ( self ) : <EOL> """<STR_LIT>""" <EOL> with MockEnvironment ( test_source , test_target ) as environment : <EOL> environment . grab_inputs = Mock ( ) <EOL> environment . install ( ) <EOL> ok_ ( environment . grab_inputs . called ) <EOL> def test_source_to_target_config ( self ) : <EOL> """<STR_LIT>""" <EOL> with MockEnvironment ( test_input_source , test_input_target ) as environment : <EOL> environment . directory = Mock ( spec = environment . directory ) <EOL> environment . directory . root_dir = "<STR_LIT>" <EOL> environment . directory . new = False <EOL> environment . update ( ) <EOL> eq_ ( environment . target . get ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT:foo>' ) <EOL> eq_ ( environment . target . get ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> @ raises ( SprinterException ) <EOL> def test_errors_fail_out_immediately ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch ( '<STR_LIT>' , new = create_mock_formulabase ( ) ) as formulabase : <EOL> formulabase . install . side_effect = Exception <EOL> with MockEnvironment ( None , test_target , mock_formulabase = formulabase ) as environment : <EOL> environment . run_feature ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_feature_run_remove_failure ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch ( '<STR_LIT>' , new = create_mock_formulabase ( ) ) as formulabase : <EOL> formulabase . sync . side_effect = Exception <EOL> with MockEnvironment ( test_source , test_target , mock_formulabase = formulabase ) as environment : <EOL> environment . directory = Mock ( spec = environment . directory ) <EOL> environment . directory . new = False <EOL> environment . remove ( ) <EOL> missing_formula_config = """<STR_LIT>""" <EOL> test_source = """<STR_LIT>""" <EOL> test_target = """<STR_LIT>""" <EOL> test_input_source = """<STR_LIT>""" <EOL> test_input_target = """<STR_LIT>""" </s>
