<s> import mock <EOL> import subprocess <EOL> from anvil import exceptions as exc <EOL> from anvil import shell as sh <EOL> from anvil import test <EOL> class TestShell ( test . MockTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestShell , self ) . setUp ( ) <EOL> self . cmd = [ '<STR_LIT:test>' , '<STR_LIT>' ] <EOL> self . str_cmd = '<STR_LIT:U+0020>' . join ( self . cmd ) <EOL> self . result = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . popen_mock , self . popen_inst_mock = self . _patch_class ( <EOL> sh . subprocess , '<STR_LIT>' ) <EOL> self . popen_inst_mock . returncode = <NUM_LIT:0> <EOL> self . popen_inst_mock . communicate . return_value = self . result <EOL> def test_execute_default_params ( self ) : <EOL> result = sh . execute ( self . cmd ) <EOL> master_mock_calls = [ <EOL> mock . call . Popen ( self . cmd , <EOL> stdin = subprocess . PIPE , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE , <EOL> close_fds = True , <EOL> shell = False , <EOL> cwd = None , <EOL> env = None ) , <EOL> mock . call . popen . communicate ( None ) <EOL> ] <EOL> self . assertEqual ( self . master_mock . mock_calls , master_mock_calls ) <EOL> self . assertEqual ( result , self . result ) <EOL> @ mock . patch . object ( sh . env , '<STR_LIT>' ) <EOL> def test_execute_custom_params ( self , mocked_env_get ) : <EOL> mocked_env_get . return_value = { '<STR_LIT:a>' : '<STR_LIT:a>' } <EOL> env = { '<STR_LIT:b>' : '<STR_LIT:b>' } <EOL> sh . execute ( self . cmd , <EOL> process_input = '<STR_LIT:input>' , <EOL> cwd = '<STR_LIT>' , <EOL> shell = True , <EOL> env_overrides = env , <EOL> stdout_fh = '<STR_LIT>' , <EOL> stderr_fh = '<STR_LIT>' ) <EOL> env . update ( { '<STR_LIT:a>' : '<STR_LIT:a>' } ) <EOL> self . assertEqual ( self . master_mock . mock_calls , [ <EOL> mock . call . Popen ( self . str_cmd , <EOL> stdin = subprocess . PIPE , <EOL> stdout = '<STR_LIT>' , <EOL> stderr = '<STR_LIT>' , <EOL> close_fds = True , <EOL> shell = True , <EOL> cwd = '<STR_LIT>' , <EOL> env = env ) , <EOL> mock . call . popen . communicate ( '<STR_LIT:input>' ) <EOL> ] ) <EOL> def test_execute_with_result_none ( self ) : <EOL> self . popen_inst_mock . communicate . return_value = ( None , None ) <EOL> self . assertEqual ( sh . execute ( self . cmd ) , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_execute_popen_raises ( self ) : <EOL> self . popen_mock . side_effect = OSError ( '<STR_LIT>' ) <EOL> self . assertRaises ( exc . ProcessExecutionError , sh . execute , self . cmd ) <EOL> def test_execute_communicate_raises ( self ) : <EOL> self . popen_inst_mock . communicate . side_effect = OSError ( '<STR_LIT>' ) <EOL> self . assertRaises ( exc . ProcessExecutionError , sh . execute , self . cmd ) <EOL> def test_execute_bad_return_code_no_check ( self ) : <EOL> self . popen_inst_mock . returncode = <NUM_LIT:1> <EOL> self . assertEqual ( sh . execute ( self . cmd , check_exit_code = False ) , <EOL> self . result ) <EOL> def test_execute_bad_return_code_with_check ( self ) : <EOL> self . popen_inst_mock . returncode = <NUM_LIT:1> <EOL> self . assertRaisesRegexp ( exc . ProcessExecutionError , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . str_cmd , <EOL> sh . execute , self . cmd ) <EOL> def test_execute_bad_return_code_with_tail ( self ) : <EOL> self . popen_inst_mock . returncode = <NUM_LIT:1> <EOL> self . popen_inst_mock . communicate . return_value = ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> stdout = ( '<STR_LIT>' ) <EOL> expected = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( self . str_cmd , stdout ) <EOL> ) <EOL> self . assertRaisesRegexp ( exc . ProcessExecutionError , <EOL> expected , sh . execute , self . cmd ) <EOL> @ mock . patch . object ( sh , '<STR_LIT>' ) <EOL> def test_execute_save_output ( self , mocked_mkdirslist ) : <EOL> self . popen_inst_mock . returncode = <NUM_LIT:1> <EOL> file_name = '<STR_LIT>' <EOL> with mock . patch . object ( sh , '<STR_LIT>' , mock . mock_open ( ) , <EOL> create = True ) as fh_mock : <EOL> fh_mock . return_value . name = file_name <EOL> self . assertRaisesRegexp ( <EOL> exc . ProcessExecutionError , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( self . str_cmd , file_name , <EOL> file_name ) , <EOL> sh . execute_save_output , self . cmd , file_name <EOL> ) </s>
