<s> import logging <EOL> import os <EOL> import fixtures <EOL> from mock import patch <EOL> import testtools <EOL> from bandit . cli import main as bandit <EOL> from bandit . core import extension_loader as ext_loader <EOL> from bandit . core import utils <EOL> bandit_config_content = """<STR_LIT>""" <EOL> bandit_baseline_content = """<STR_LIT>""" <EOL> class BanditCLIMainLoggerTests ( testtools . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( BanditCLIMainLoggerTests , self ) . setUp ( ) <EOL> self . logger = logging . getLogger ( ) <EOL> self . original_logger_handlers = self . logger . handlers <EOL> self . original_logger_level = self . logger . level <EOL> self . logger . handlers = [ ] <EOL> def tearDown ( self ) : <EOL> super ( BanditCLIMainLoggerTests , self ) . tearDown ( ) <EOL> self . logger . handlers = self . original_logger_handlers <EOL> self . logger . level = self . original_logger_level <EOL> def test_init_logger ( self ) : <EOL> bandit . _init_logger ( False ) <EOL> self . assertIsNotNone ( self . logger ) <EOL> self . assertNotEqual ( self . logger . handlers , [ ] ) <EOL> self . assertEqual ( logging . INFO , self . logger . level ) <EOL> def test_init_logger_debug_mode ( self ) : <EOL> bandit . _init_logger ( True ) <EOL> self . assertEqual ( logging . DEBUG , self . logger . level ) <EOL> class BanditCLIMainTests ( testtools . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( BanditCLIMainTests , self ) . setUp ( ) <EOL> self . current_directory = os . getcwd ( ) <EOL> def tearDown ( self ) : <EOL> super ( BanditCLIMainTests , self ) . tearDown ( ) <EOL> os . chdir ( self . current_directory ) <EOL> def test_get_options_from_ini_no_ini_path_no_target ( self ) : <EOL> self . assertIsNone ( bandit . _get_options_from_ini ( None , [ ] ) ) <EOL> def test_get_options_from_ini_empty_directory_no_target ( self ) : <EOL> ini_directory = self . useFixture ( fixtures . TempDir ( ) ) . path <EOL> self . assertIsNone ( bandit . _get_options_from_ini ( ini_directory , [ ] ) ) <EOL> def test_get_options_from_ini_no_ini_path_no_bandit_files ( self ) : <EOL> target_directory = self . useFixture ( fixtures . TempDir ( ) ) . path <EOL> self . assertIsNone ( bandit . _get_options_from_ini ( None , <EOL> [ target_directory ] ) ) <EOL> def test_get_options_from_ini_no_ini_path_multi_bandit_files ( self ) : <EOL> target_directory = self . useFixture ( fixtures . TempDir ( ) ) . path <EOL> second_config = '<STR_LIT>' <EOL> os . mkdir ( os . path . join ( target_directory , second_config ) ) <EOL> bandit_config_one = os . path . join ( target_directory , '<STR_LIT>' ) <EOL> bandit_config_two = os . path . join ( target_directory , second_config , <EOL> '<STR_LIT>' ) <EOL> bandit_files = [ bandit_config_one , bandit_config_two ] <EOL> for bandit_file in bandit_files : <EOL> with open ( bandit_file , '<STR_LIT>' ) as fd : <EOL> fd . write ( bandit_config_content ) <EOL> self . assertRaisesRegex ( SystemExit , '<STR_LIT:2>' , bandit . _get_options_from_ini , <EOL> None , [ target_directory ] ) <EOL> def test_init_extensions ( self ) : <EOL> self . assertEqual ( ext_loader . MANAGER , bandit . _init_extensions ( ) ) <EOL> def test_log_option_source_arg_val ( self ) : <EOL> arg_val = '<STR_LIT:file>' <EOL> ini_val = '<STR_LIT>' <EOL> option_name = '<STR_LIT>' <EOL> self . assertEqual ( arg_val , bandit . _log_option_source ( arg_val , ini_val , <EOL> option_name ) ) <EOL> def test_log_option_source_ini_value ( self ) : <EOL> ini_val = '<STR_LIT>' <EOL> option_name = '<STR_LIT>' <EOL> self . assertEqual ( ini_val , bandit . _log_option_source ( None , ini_val , <EOL> option_name ) ) <EOL> def test_log_option_source_no_values ( self ) : <EOL> option_name = '<STR_LIT>' <EOL> self . assertIsNone ( bandit . _log_option_source ( None , None , option_name ) ) <EOL> @ patch ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT:-c>' , '<STR_LIT>' , '<STR_LIT:test>' ] ) <EOL> def test_main_config_unopenable ( self ) : <EOL> with patch ( '<STR_LIT>' ) as mock_bandit_config : <EOL> mock_bandit_config . side_effect = utils . ConfigError ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertRaisesRegex ( SystemExit , '<STR_LIT:2>' , bandit . main ) <EOL> @ patch ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT:-c>' , '<STR_LIT>' , '<STR_LIT:test>' ] ) <EOL> def test_main_invalid_config ( self ) : <EOL> with patch ( '<STR_LIT>' <EOL> ) as mock_bandit_config : <EOL> mock_bandit_config . side_effect = utils . ConfigError ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertRaisesRegex ( SystemExit , '<STR_LIT:2>' , bandit . main ) <EOL> @ patch ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT:-c>' , '<STR_LIT>' , '<STR_LIT:test>' ] ) <EOL> def test_main_handle_ini_options ( self ) : <EOL> temp_directory = self . useFixture ( fixtures . TempDir ( ) ) . path <EOL> os . chdir ( temp_directory ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT>' ) as fd : <EOL> fd . write ( bandit_config_content ) <EOL> with patch ( '<STR_LIT>' ) as mock_get_opts : <EOL> mock_get_opts . return_value = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> with patch ( '<STR_LIT>' ) as err_mock : <EOL> self . assertRaisesRegex ( SystemExit , '<STR_LIT:2>' , bandit . main ) <EOL> self . assertEqual ( str ( err_mock . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) , <EOL> '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT:-c>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:test>' ] ) <EOL> def test_main_unknown_tests ( self ) : <EOL> temp_directory = self . useFixture ( fixtures . TempDir ( ) ) . path <EOL> os . chdir ( temp_directory ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT>' ) as fd : <EOL> fd . write ( bandit_config_content ) <EOL> self . assertRaisesRegex ( SystemExit , '<STR_LIT:2>' , bandit . main ) <EOL> @ patch ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT:-c>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:test>' ] ) <EOL> def test_main_unknown_skip_tests ( self ) : <EOL> temp_directory = self . useFixture ( fixtures . TempDir ( ) ) . path <EOL> os . chdir ( temp_directory ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT>' ) as fd : <EOL> fd . write ( bandit_config_content ) <EOL> self . assertRaisesRegex ( SystemExit , '<STR_LIT:2>' , bandit . main ) <EOL> @ patch ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT:-c>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:test>' ] ) <EOL> def test_main_profile_not_found ( self ) : <EOL> temp_directory = self . useFixture ( fixtures . TempDir ( ) ) . path <EOL> os . chdir ( temp_directory ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT>' ) as fd : <EOL> fd . write ( bandit_config_content ) <EOL> with patch ( '<STR_LIT>' ) as err_mock : <EOL> self . assertRaisesRegex ( SystemExit , '<STR_LIT:2>' , bandit . main ) <EOL> self . assertEqual ( <EOL> str ( err_mock . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) , <EOL> '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT:-c>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:test>' ] ) <EOL> def test_main_baseline_ioerror ( self ) : <EOL> temp_directory = self . useFixture ( fixtures . TempDir ( ) ) . path <EOL> os . chdir ( temp_directory ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT>' ) as fd : <EOL> fd . write ( bandit_config_content ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT>' ) as fd : <EOL> fd . write ( bandit_baseline_content ) <EOL> with patch ( '<STR_LIT>' <EOL> ) as mock_mgr_pop_bl : <EOL> mock_mgr_pop_bl . side_effect = IOError <EOL> self . assertRaisesRegex ( SystemExit , '<STR_LIT:2>' , bandit . main ) <EOL> @ patch ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT:-c>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:test>' ] ) <EOL> def test_main_invalid_output_format ( self ) : <EOL> temp_directory = self . useFixture ( fixtures . TempDir ( ) ) . path <EOL> os . chdir ( temp_directory ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT>' ) as fd : <EOL> fd . write ( bandit_config_content ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT>' ) as fd : <EOL> fd . write ( bandit_baseline_content ) <EOL> self . assertRaisesRegex ( SystemExit , '<STR_LIT:2>' , bandit . main ) <EOL> @ patch ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT:-c>' , '<STR_LIT>' , '<STR_LIT:test>' ] ) <EOL> def test_main_exit_with_results ( self ) : <EOL> temp_directory = self . useFixture ( fixtures . TempDir ( ) ) . path <EOL> os . chdir ( temp_directory ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT>' ) as fd : <EOL> fd . write ( bandit_config_content ) <EOL> with patch ( '<STR_LIT>' <EOL> ) as mock_mgr_results_ct : <EOL> mock_mgr_results_ct . return_value = <NUM_LIT:1> <EOL> self . assertRaisesRegex ( SystemExit , '<STR_LIT:1>' , bandit . main ) <EOL> @ patch ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT:-c>' , '<STR_LIT>' , '<STR_LIT:test>' ] ) <EOL> def test_main_exit_with_no_results ( self ) : <EOL> temp_directory = self . useFixture ( fixtures . TempDir ( ) ) . path <EOL> os . chdir ( temp_directory ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT>' ) as fd : <EOL> fd . write ( bandit_config_content ) <EOL> with patch ( '<STR_LIT>' <EOL> ) as mock_mgr_results_ct : <EOL> mock_mgr_results_ct . return_value = <NUM_LIT:0> <EOL> self . assertRaisesRegex ( SystemExit , '<STR_LIT:0>' , bandit . main ) </s>
