示例#1
0
def test_get_style_guide():
    """Verify the methods called on our internal Application."""
    prelim_opts = argparse.Namespace(
        append_config=[],
        config=None,
        isolated=False,
        output_file=None,
        verbose=0,
    )
    mockedapp = mock.Mock()
    mockedapp.parse_preliminary_options.return_value = (prelim_opts, [])
    mockedapp.program = 'flake8'
    with mock.patch('flake8.api.legacy.config.ConfigFileFinder'
                    ) as mock_config_finder:  # noqa: E501
        config_finder = ConfigFileFinder(mockedapp.program)
        mock_config_finder.return_value = config_finder

        with mock.patch('flake8.main.application.Application') as application:
            application.return_value = mockedapp
            style_guide = api.get_style_guide()

    application.assert_called_once_with()
    mockedapp.parse_preliminary_options.assert_called_once_with([])
    mockedapp.find_plugins.assert_called_once_with(config_finder)
    mockedapp.register_plugin_options.assert_called_once_with()
    mockedapp.parse_configuration_and_cli.assert_called_once_with(
        config_finder, [])
    mockedapp.make_formatter.assert_called_once_with()
    mockedapp.make_guide.assert_called_once_with()
    mockedapp.make_file_checker_manager.assert_called_once_with()
    assert isinstance(style_guide, api.StyleGuide)
示例#2
0
    def from_config_file(cls) -> "Flake8BanditConfig":
        # set defaults
        profile = {}
        target_paths = set()
        excluded_paths = set()

        # populate config from `.bandit` configuration file
        ini_file = ConfigFileFinder("bandit", None, None).local_config_files()
        config = configparser.ConfigParser()
        try:
            config.read(ini_file)
            bandit_config = {k: v for k, v in config.items("bandit")}

            # test-set profile
            if bandit_config.get("skips"):
                profile["exclude"] = (bandit_config.get("skips").replace(
                    "S", "B").split(","))
            if bandit_config.get("tests"):
                profile["include"] = (bandit_config.get("tests").replace(
                    "S", "B").split(","))

            # file include/exclude
            if bandit_config.get("targets"):
                # paths = bandit_config.get("targets").split(",")
                paths = [
                    elem.strip()
                    for elem in bandit_config.get("targets").split(",")
                ]
                for path in paths:
                    # convert absolute to relative
                    if path.startswith("/"):
                        path = Path(Path.cwd(), path[1:])
                    target_paths.add(Path(path))

            if bandit_config.get("exclude"):
                # paths = bandit_config.get("exclude").split(",")
                paths = [
                    elem.strip()
                    for elem in bandit_config.get("exclude").split(",")
                ]
                for path in paths:
                    # convert absolute to relative
                    if path.startswith("/"):
                        path = Path(Path.cwd(), path[1:])
                    excluded_paths.add(Path(path))

        except (configparser.Error, KeyError, TypeError) as e:
            profile = {}
            if str(e) != "No section: 'bandit'":
                sys.stderr.write(f"Unable to parse config file: {e}")

        return cls(profile, target_paths, excluded_paths)
示例#3
0
 def _check_source(self):
     ini_file = ConfigFileFinder("bandit", None, None).local_config_files()
     config = configparser.ConfigParser()
     try:
         config.read(ini_file)
         profile = {
             k: v.replace("S", "B")
             for k, v in config.items("bandit")
         }
         if profile.get("skips"):
             profile["exclude"] = profile.get("skips").split(",")
         if profile.get("tests"):
             profile["include"] = profile.get("tests").split(",")
     except (configparser.Error, KeyError, TypeError) as e:
         if str(e) != "No section: 'bandit'":
             import sys
             err = "Unable to parse config file: %s\n" % e
             sys.stderr.write(err)
         profile = {}
     bnv = BanditNodeVisitor(
         self.filename,
         BanditMetaAst(),
         BanditTestSet(BanditConfig(), profile=profile),
         False,
         [],
         Metrics(),
     )
     bnv.generic_visit(self.tree)
     return [
         {
             # flake8-bugbear uses bandit default prefix 'B'
             # so this plugin replaces the 'B' with an 'S' for Security
             # See https://github.com/PyCQA/flake8-bugbear/issues/37
             "test_id": item.test_id.replace("B", "S"),
             "issue_text": item.text,
             "line_number": item.lineno,
         } for item in bnv.tester.results
     ]