Exemple #1
0
def test_get_config_from_missing_file():
    """Test that None is returned when the configuration file does not exist."""
    base_config_file = os.path.join(os.path.dirname(__file__), "rsc", "config.yaml")
    config = Config(base_config_file)
    config_from_file = config.get_config_from_file("")

    assert config_from_file is None
Exemple #2
0
def test_config_inherits_from_multiple_levels():
    """
    Test that the Config module supports a level that inherits from multiple child levels.

    Expected result: plugins listed in combined config file are returned
    """
    config_file = os.path.join(os.path.dirname(__file__), "rsc",
                               "config-list.yaml")
    config = Config(config_file)

    plugins = config.get_enabled_plugins("combined", "discovery")
    assert "C" in plugins
    assert "python" in plugins

    plugins = config.get_enabled_plugins("combined", "tool")
    assert "black" in plugins
    assert "catkin_lint" in plugins
    assert "cppcheck" in plugins
    assert "cpplint" in plugins
    assert "docformatter" in plugins
    assert "isort" in plugins
    assert "mypy" in plugins
    assert "pydocstyle" in plugins
    assert "xmllint" in plugins
    assert "yamllint" in plugins

    plugins = config.get_enabled_plugins("combined", "reporting")
    assert "print_to_console" in plugins
    assert "write_jenkins_warnings_ng" in plugins
Exemple #3
0
    def __init__(self, user_paths):
        """Initialize Statick."""
        self.resources = Resources(user_paths)

        self.manager = PluginManager()
        self.manager.setPluginPlaces(self.resources.get_plugin_paths())
        self.manager.setCategoriesFilter({
            "Discovery": DiscoveryPlugin,
            "Tool": ToolPlugin,
            "Reporting": ReportingPlugin
        })
        self.manager.collectPlugins()

        self.discovery_plugins = {}
        for plugin_info in self.manager.getPluginsOfCategory("Discovery"):
            self.discovery_plugins[plugin_info.plugin_object.get_name()] = \
                plugin_info.plugin_object

        self.tool_plugins = {}
        for plugin_info in self.manager.getPluginsOfCategory("Tool"):
            self.tool_plugins[plugin_info.plugin_object.get_name()] = \
                plugin_info.plugin_object

        self.reporting_plugins = {}
        for plugin_info in self.manager.getPluginsOfCategory("Reporting"):
            self.reporting_plugins[plugin_info.plugin_object.get_name()] = \
                    plugin_info.plugin_object

        self.config = Config(self.resources.get_file("config.yaml"))

        self.exceptions = Exceptions(
            self.resources.get_file("exceptions.yaml"))
Exemple #4
0
 def get_config(self, args: argparse.Namespace) -> None:
     """Get Statick configuration."""
     base_config_filename = "config.yaml"
     user_config_filename = ""
     if args.config is not None:
         user_config_filename = args.config
     try:
         self.config = Config(
             self.resources.get_file(base_config_filename),
             self.resources.get_file(user_config_filename),
         )
     except OSError as ex:
         logging.error(
             "Failed to access configuration file %s or %s: %s",
             base_config_filename,
             user_config_filename,
             ex,
         )
     except ValueError as ex:
         logging.error(
             "Configuration file %s or %s has errors: %s",
             base_config_filename,
             user_config_filename,
             ex,
         )
Exemple #5
0
def test_get_user_levels_value_error(mock_open):
    """Test the behavior when Config user file throws a YAMLError."""
    user_config_file = os.path.join(os.path.dirname(__file__), "rsc", "user.yaml")

    config = Config("")
    mock_open.side_effect = yaml.YAMLError("error")
    with pytest.raises(yaml.YAMLError):
        config.get_user_levels(user_config_file)
Exemple #6
0
def test_config_get_tool_config():
    """
    Test that the Config module gives correct config for tools.

    Expected result: tool plugin configuration matches config file
    """
    config_file = os.path.join(os.path.dirname(__file__), "rsc", "config.yaml")
    config = Config(config_file)

    tool_config = config.get_tool_config("make", "example", "flags")
    assert "-Wall" in tool_config
Exemple #7
0
def test_config_enabled_discovery_plugins():
    """
    Test that the Config module identifies enabled discovery plugins for a given level.

    Expected result: plugins listed in example config file are returned
    """
    config_file = os.path.join(os.path.dirname(__file__), 'rsc', 'config.yaml')
    config = Config(config_file)

    plugins = config.get_enabled_discovery_plugins("example")
    assert "cmake" in plugins
Exemple #8
0
def test_config_get_discovery_config():
    """
    Test that the Config module gives correct config for discovery.

    Expected result: discovery plugin configuration matches config file
    """
    config_file = os.path.join(os.path.dirname(__file__), "rsc", "config.yaml")
    config = Config(config_file)

    discovery_config = config.get_discovery_config("cmake", "example", "flags")
    assert not discovery_config
Exemple #9
0
def test_config_get_reporintg_config():
    """
    Test that the Config module gives correct config for reporting.

    Expected result: reporting plugin configuration matches config file
    """
    config_file = os.path.join(os.path.dirname(__file__), "rsc", "config.yaml")
    config = Config(config_file)

    reporting_config = config.get_reporting_config("write_to_file", "example", "flags")
    assert not reporting_config
Exemple #10
0
def test_config_enabled_reporting_plugins():
    """
    Test that the Config module identifies enabled reporting plugins for a given level.

    Expected result: plugins listed in example config file are returned
    """
    config_file = os.path.join(os.path.dirname(__file__), "rsc", "config.yaml")
    config = Config(config_file)

    plugins = config.get_enabled_reporting_plugins("example")
    assert "write_to_file" in plugins
Exemple #11
0
def test_add_user_config():
    """Test that the Config module adds user levels that inherit from base levels."""
    base_config_file = os.path.join(os.path.dirname(__file__), "rsc", "config.yaml")
    user_config_file = os.path.join(os.path.dirname(__file__), "rsc", "user.yaml")

    config = Config(base_config_file, user_config_file)

    flags = config.get_tool_config("catkin_lint", "custom", "flags")
    assert flags == "--unit_test"

    flags = config.get_tool_config("make", "custom", "flags")
    assert "-Wall" in flags
Exemple #12
0
def test_user_level_override_base_level_with_same_name():
    """Test that user level that overrides a base level with same name finds flags.."""
    base_config_file = os.path.join(os.path.dirname(__file__), "rsc", "config.yaml")
    user_config_file = os.path.join(
        os.path.dirname(__file__), "rsc", "user-level-same-name.yaml"
    )

    config = Config(base_config_file, user_config_file)

    flags = config.get_tool_config("pylint", "threshold", "flags")
    assert flags == "--user-override"

    flags = config.get_tool_config("make", "threshold", "flags")
    assert flags is None
Exemple #13
0
def test_user_level_extends_override_level():
    """Test that user level extends a level that overrides a base level."""
    base_config_file = os.path.join(os.path.dirname(__file__), "rsc", "config.yaml")
    user_config_file = os.path.join(
        os.path.dirname(__file__), "rsc", "user-extend.yaml"
    )

    config = Config(base_config_file, user_config_file)

    flags = config.get_tool_config("pylint", "sei_cert", "flags")
    assert flags == "--user-override"

    flags = config.get_tool_config("make", "sei_cert", "flags")
    assert "-Wall" in flags
Exemple #14
0
def test_user_level_overrides_base_level():
    """Test that user level overrides base level in configuration."""
    base_config_file = os.path.join(os.path.dirname(__file__), "rsc", "config.yaml")
    user_config_file = os.path.join(
        os.path.dirname(__file__), "rsc", "user-override.yaml"
    )

    config = Config(base_config_file, user_config_file)

    flags = config.get_tool_config("pylint", "sei_cert", "flags")
    assert flags == "--user-override"

    flags = config.get_tool_config("make", "sei_cert", "flags")
    assert flags is None
Exemple #15
0
def test_config_init():
    """
    Test that the Config module initializes correctly.

    Expected result: parser and pre_parser are initialized
    """
    config = Config(None)
    assert not config.config

    config = Config("not_a_file.yaml")
    assert not config.config

    config_file = os.path.join(os.path.dirname(__file__), "rsc", "config.yaml")
    config = Config(config_file)
    assert config.config
Exemple #16
0
def setup_clang_format_tool_plugin(use_plugin_context=True,
                                   binary=None,
                                   do_raise=False):
    """Initialize and return an instance of the clang-format plugin."""
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument("--clang-format-bin", dest="clang_format_bin")
    if do_raise:
        arg_parser.add_argument(
            "--clang-format-raise-exception",
            dest="clang_format_raise_exception",
            action="store_false",
        )
    else:
        arg_parser.add_argument(
            "--clang-format-raise-exception",
            dest="clang_format_raise_exception",
            action="store_true",
        )

    resources = Resources(
        [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")])
    config = Config(resources.get_file("config.yaml"))
    plugin_context = PluginContext(arg_parser.parse_args([]), resources,
                                   config)
    plugin_context.args.output_directory = os.path.dirname(__file__)
    cftp = ClangFormatToolPlugin()
    if binary is not None:
        plugin_context.args.clang_format_bin = binary
    if use_plugin_context:
        cftp.set_plugin_context(plugin_context)
    return cftp
def setup_bandit_tool_plugin(binary=None):
    """Create an instance of the bandit plugin."""
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument(
        "--show-tool-output",
        dest="show_tool_output",
        action="store_false",
        help="Show tool output",
    )
    arg_parser.add_argument("--bandit-bin", dest="bandit_bin")
    arg_parser.add_argument(
        "--mapping-file-suffix", dest="mapping_file_suffix", type=str
    )

    resources = Resources(
        [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")]
    )
    config = Config(resources.get_file("config.yaml"))
    plugin_context = PluginContext(arg_parser.parse_args([]), resources, config)
    plugin_context.args.output_directory = os.path.dirname(__file__)
    btp = BanditToolPlugin()
    if binary:
        plugin_context.args.bandit_bin = binary
    btp.set_plugin_context(plugin_context)
    return btp
def setup_uncrustify_tool_plugin(extra_path=None,
                                 use_plugin_context=True,
                                 binary=None):
    """Initialize and return an instance of the uncrustify plugin."""
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument("--uncrustify-bin", dest="uncrustify_bin")
    arg_parser.add_argument(
        "--show-tool-output",
        dest="show_tool_output",
        action="store_false",
        help="Show tool output",
    )

    paths = []
    if extra_path:
        paths.append(extra_path)
    paths.append(
        os.path.join(os.path.dirname(statick_tool.__file__), "plugins"))
    resources = Resources(paths)
    config = Config(resources.get_file("config.yaml"))
    plugin_context = PluginContext(arg_parser.parse_args([]), resources,
                                   config)
    plugin_context.args.output_directory = os.path.dirname(__file__)
    utp = UncrustifyToolPlugin()
    if binary:
        plugin_context.args.uncrustify_bin = binary
    if use_plugin_context:
        utp.set_plugin_context(plugin_context)
    return utp
def setup_clang_format_tool_plugin_non_default():
    """Initialize and return an instance of the clang-format plugin."""
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument(
        "--show-tool-output",
        dest="show_tool_output",
        action="store_false",
        help="Show tool output",
    )
    arg_parser.add_argument("--clang-format-bin", dest="clang_format_bin")
    arg_parser.add_argument(
        "--clang-format-raise-exception",
        dest="clang_format_raise_exception",
        action="store_true",
        default=False,
    )
    arg_parser.add_argument("--output-directory", dest="output_directory")

    resources = Resources(
        [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")])
    config = Config(resources.get_file("config.yaml"))
    plugin_context = PluginContext(arg_parser.parse_args([]), resources,
                                   config)
    cftp = ClangFormatToolPlugin()
    cftp.set_plugin_context(plugin_context)
    return cftp
def setup_cppcheck_tool_plugin(use_plugin_context=True, binary=None):
    """Initialize and return an instance of the cppcheck plugin."""
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument(
        "--show-tool-output",
        dest="show_tool_output",
        action="store_false",
        help="Show tool output",
    )
    arg_parser.add_argument("--cppcheck-bin", dest="cppcheck_bin")
    arg_parser.add_argument("--mapping-file-suffix",
                            dest="mapping_file_suffix",
                            type=str)

    resources = Resources(
        [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")])
    config = Config(resources.get_file("config.yaml"))
    plugin_context = PluginContext(arg_parser.parse_args([]), resources,
                                   config)
    plugin_context.args.output_directory = os.path.dirname(__file__)
    cctp = CppcheckToolPlugin()
    if binary:
        plugin_context.args.cppcheck_bin = binary
    if use_plugin_context:
        cctp.set_plugin_context(plugin_context)
    return cctp
Exemple #21
0
def test_user_config_value_error(mock_open):
    """Test the behavior when Config base file throws a YAMLError."""
    config_file = os.path.join(os.path.dirname(__file__), "rsc", "config.yaml")

    mock_open.side_effect = yaml.YAMLError("error")
    with pytest.raises(yaml.YAMLError):
        Config(config_file)
Exemple #22
0
def fortify_tool_plugin():
    """Create an instance of the fortify plugin."""
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument("--show-tool-output",
                            dest="show_tool_output",
                            action="store_true",
                            help="Show tool output")
    arg_parser.add_argument("--fortify-python",
                            dest="fortify_python",
                            type=int)
    arg_parser.add_argument("--mapping-file-suffix",
                            dest="mapping_file_suffix",
                            default=None)
    arg_parser.add_argument("--fortify-version",
                            dest="fortify_version",
                            default="18.20")

    resources = Resources(
        [os.path.join(os.path.dirname(statick_tool.__file__), 'plugins')])
    config = Config(resources.get_file("config.yaml"))
    plugin_context = PluginContext(arg_parser.parse_args([]), resources,
                                   config)
    ftp = FortifyToolPlugin()
    ftp.set_plugin_context(plugin_context)
    return ftp
Exemple #23
0
def test_config_enabled_tool_plugins():
    """
    Test that the Config module identifies enabled tool plugins for a given level.

    Expected result: plugins listed in example config file are returned
    """
    config_file = os.path.join(os.path.dirname(__file__), "rsc", "config.yaml")
    config = Config(config_file)

    plugins = config.get_enabled_tool_plugins("sei_cert")
    assert "bandit" in plugins
    assert "clang-tidy" in plugins
    assert "cppcheck" in plugins
    assert "flawfinder" in plugins
    assert "make" in plugins
    assert "perlcritic" in plugins
    assert "spotbugs" in plugins
Exemple #24
0
def test_config_file_invalid_yaml():
    """
    Test for when a Config is initialized with an invalid yaml file.

    Expected result: ValueError is thrown
    """
    with pytest.raises(ValueError):
        Config(os.path.join(os.path.dirname(__file__), "rsc", "bad.yaml"))
Exemple #25
0
def test_tool_dependencies():
    """Verify that dependencies are reported correctly."""
    arg_parser = argparse.ArgumentParser()
    resources = Resources([os.path.join(os.path.dirname(__file__), 'user_flags_config')])
    config = Config(resources.get_file("config.yaml"))
    plugin_context = PluginContext(arg_parser.parse_args([]), resources, config)
    tp = ToolPlugin()
    tp.set_plugin_context(plugin_context)
    assert tp.get_tool_dependencies() == []
Exemple #26
0
def test_config_enabled_plugins_inherits():
    """
    Test that the Config module identifies enabled plugins for a given level with inheritance.

    Expected result: plugins listed in example config file are returned
    """
    config_file = os.path.join(os.path.dirname(__file__), "rsc", "config.yaml")
    config = Config(config_file)

    plugins = config.get_enabled_plugins("objective_minus_pylint", "tool")
    assert "catkin_lint" in plugins
    assert "clang-tidy" in plugins
    assert "cmakelint" in plugins
    assert "cppcheck" in plugins
    assert "make" in plugins
    assert "pylint" in plugins
    assert "xmllint" in plugins
    assert "yamllint" in plugins
Exemple #27
0
def test_tool_plugin_get_user_flags_invalid_level():
    """Test that we return an empty list for invalid levels."""
    arg_parser = argparse.ArgumentParser()
    resources = Resources([os.path.join(os.path.dirname(__file__), 'user_flags_config')])
    config = Config(resources.get_file("config.yaml"))
    plugin_context = PluginContext(arg_parser.parse_args([]), resources, config)
    tp = ToolPlugin()
    tp.set_plugin_context(plugin_context)
    flags = tp.get_user_flags('level2', name='test')
    assert flags == []
Exemple #28
0
def test_tool_plugin_get_user_flags_valid_flags():
    """Test that we return a list of user flags."""
    arg_parser = argparse.ArgumentParser()
    resources = Resources(
        [os.path.join(os.path.dirname(__file__), "user_flags_config")])
    config = Config(resources.get_file("config.yaml"))
    plugin_context = PluginContext(arg_parser.parse_args([]), resources,
                                   config)
    tp = ToolPlugin()
    tp.set_plugin_context(plugin_context)
    flags = tp.get_user_flags("level", name="test")
    assert flags == ["look", "a", "flag"]
def setup_cpplint_tool_plugin():
    """Initialize and return an instance of the cpplint plugin."""
    arg_parser = argparse.ArgumentParser()

    resources = Resources(
        [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")]
    )
    config = Config(resources.get_file("config.yaml"))
    plugin_context = PluginContext(arg_parser.parse_args([]), resources, config)
    plugin_context.args.output_directory = os.path.dirname(__file__)
    ctp = CpplintToolPlugin()
    ctp.set_plugin_context(plugin_context)
    return ctp
def setup_mypy_tool_plugin():
    """Create and return an instance of the Mypy plugin."""
    arg_parser = argparse.ArgumentParser()

    resources = Resources(
        [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")]
    )
    config = Config(resources.get_file("config.yaml"))
    plugin_context = PluginContext(arg_parser.parse_args([]), resources, config)
    plugin_context.args.output_directory = os.path.dirname(__file__)
    mtp = MypyToolPlugin()
    mtp.set_plugin_context(plugin_context)
    return mtp