def test_main_no_config(self): # Test that bandit exits when a config file cannot be found, raising a # NoConfigFileFound error with patch('bandit.cli.main._find_config') as mock_find_config: mock_find_config.side_effect = utils.NoConfigFileFound('') # assert a SystemExit with code 2 self.assertRaisesRegex(SystemExit, '2', bandit.main)
def _find_config(): # prefer config file in the following order: # 1) current directory, 2) user home directory, 3) bundled config config_dirs = ( ['.'] + [appdirs.user_config_dir("bandit")] + appdirs.site_config_dir("bandit", multipath=True).split(':')) if _running_under_virtualenv(): config_dirs.append(os.path.join(sys.prefix, 'etc', 'bandit')) config_dirs.append( os.path.join(sysconfig.get_paths().get('purelib', ''), 'bandit', 'config')) config_locations = [os.path.join(s, BASE_CONFIG) for s in config_dirs] # pip on Mac installs to the following path, but appdirs expects to # follow Mac's BPFileSystem spec which doesn't include this path so # we'll insert it. Issue raised as http://git.io/vOreU mac_pip_cfg_path = "/usr/local/etc/bandit/bandit.yaml" if mac_pip_cfg_path not in config_locations: config_locations.append(mac_pip_cfg_path) for config_file in config_locations: if os.path.isfile(config_file): return config_file # Found a valid config else: # Failed to find any config, raise an error. raise utils.NoConfigFileFound(config_locations)