示例#1
0
    def __init__(self, params=None):
        if params is None:
            params = {}
        self.workspace = self.get_workspace()
        config.init_config(self.workspace)  # Initialize runtime config
        self.github_workspace = config.get("GITHUB_WORKSPACE", self.workspace)
        self.report_folder = config.get(
            "REPORT_OUTPUT_FOLDER",
            config.get("OUTPUT_FOLDER", self.github_workspace + os.path.sep + "report"),
        )
        self.initialize_logger()
        self.display_header()
        # Mega-Linter default rules location
        self.default_rules_location = (
            "/action/lib/.automation"
            if os.path.isdir("/action/lib/.automation")
            else os.path.relpath(
                os.path.relpath(
                    os.path.dirname(os.path.abspath(__file__)) + "/../TEMPLATES"
                )
            )
        )
        # User-defined rules location
        self.linter_rules_path = self.github_workspace + os.path.sep + ".github/linters"

        self.validate_all_code_base = True
        self.filter_regex_include = None
        self.filter_regex_exclude = None
        self.cli = params["cli"] if "cli" in params else False
        self.default_linter_activation = True

        # Get enable / disable vars
        self.enable_descriptors = config.get_list("ENABLE", [])
        self.enable_linters = config.get_list("ENABLE_LINTERS", [])
        self.disable_descriptors = config.get_list("DISABLE", [])
        self.disable_linters = config.get_list("DISABLE_LINTERS", [])
        self.manage_default_linter_activation()
        self.apply_fixes = config.get_list("APPLY_FIXES", "none")
        self.show_elapsed_time = (
            config.get("SHOW_ELAPSED_TIME", "false") == "true"
            or config.get("LOG_LEVEL", "DEBUG") == "DEBUG"
        )
        # Load optional configuration
        self.load_config_vars()
        # Runtime properties
        self.reporters = []
        self.linters = []
        self.file_extensions = []
        self.file_names_regex = []
        self.status = "success"
        self.return_code = 0
        self.has_updated_sources = 0
        self.flavor_suggestions = None
        # Initialize plugins
        plugin_factory.initialize_plugins()
        # Initialize linters and gather criteria to browse files
        self.load_linters()
        self.compute_file_extensions()
        # Load Mega-Linter reporters
        self.load_reporters()
示例#2
0
def linter_test_setup(params=None):
    config.init_config(None)
    if params is None:
        params = {}
    # Root to lint
    sub_lint_root = (params["sub_lint_root"] if "sub_lint_root" in params else
                     f"{os.path.sep}.automation{os.path.sep}test")
    # Ignore report folder
    config.set_value("FILTER_REGEX_EXCLUDE", "\\/(report)\\/")
    # TAP Output deactivated by default
    config.set_value("OUTPUT_FORMAT", "text")
    config.set_value("OUTPUT_DETAIL", "detailed")
    # Root path of default rules
    root_dir = ("/tmp/lint" if os.path.isdir("/tmp/lint") else os.path.relpath(
        os.path.relpath(os.path.dirname(os.path.abspath(__file__))) +
        "/../../../.."))

    config.set_value("VALIDATE_ALL_CODEBASE", "true")
    # Root path of files to lint
    config.set_value(
        "DEFAULT_WORKSPACE",
        (config.get("DEFAULT_WORKSPACE") +
         sub_lint_root if config.exists("DEFAULT_WORKSPACE")
         and os.path.isdir(config.get("DEFAULT_WORKSPACE") + sub_lint_root)
         else root_dir + sub_lint_root),
    )
    assert os.path.isdir(
        config.get("DEFAULT_WORKSPACE")), ("DEFAULT_WORKSPACE " +
                                           config.get("DEFAULT_WORKSPACE") +
                                           " is not a valid folder")
示例#3
0
 def test_remote_config_extends_success_2(self):
     remote_config = self.test_folder + "base2.mega-linter.yml"
     os.environ["MEGALINTER_CONFIG"] = remote_config
     config.init_config()
     self.assertEqual("(base)", config.get("FILTER_REGEX_INCLUDE"))
     self.assertEqual("(extension2)", config.get("FILTER_REGEX_EXCLUDE"))
     self.assertEqual("true", config.get("SHOW_ELAPSED_TIME"))
示例#4
0
 def test_remote_config_error(self):
     remote_config = self.test_folder + "custom.mega-linter-not-existing.yml"
     try:
         os.environ["MEGALINTER_CONFIG"] = remote_config
         config.init_config()
     except Exception as e:
         self.assertIn("http", str(e))
示例#5
0
 def test_remote_config_extends_error(self):
     remote_config = self.test_folder + "base-error.mega-linter.yml"
     os.environ["MEGALINTER_CONFIG"] = remote_config
     try:
         os.environ["MEGALINTER_CONFIG"] = remote_config
         config.init_config()
     except Exception as e:
         self.assertIn("http", str(e))
示例#6
0
def linter_test_setup(params=None):
    for key in [
        "MEGALINTER_CONFIG",
        "EXTENDS",
        "FILTER_REGEX_INCLUDE",
        "FILTER_REGEX_EXCLUDE",
        "SHOW_ELAPSED_TIME",
    ]:
        if key in os.environ:
            del os.environ[key]
    config.delete()
    if params is None:
        params = {}
    # Root to lint
    sub_lint_root = (
        params["sub_lint_root"]
        if "sub_lint_root" in params
        else f"{os.path.sep}.automation{os.path.sep}test"
    )
    # Root path of default rules
    root_dir = (
        "/tmp/lint"
        if os.path.isdir("/tmp/lint")
        else os.path.relpath(
            os.path.relpath(os.path.dirname(os.path.abspath(__file__))) + "/../../../.."
        )
    )
    workspace = None
    config_file_path = root_dir + sub_lint_root + os.path.sep + ".mega-linter.yml"
    if os.path.isfile(config_file_path):
        workspace = root_dir + sub_lint_root
    elif params.get("required_config_file", False) is True:
        raise Exception(
            f"[test] There should be a .mega-linter.yml file in test folder {config_file_path}"
        )
    config.init_config(workspace)
    # Ignore report folder
    config.set_value("FILTER_REGEX_EXCLUDE", r"\/report\/")
    # TAP Output deactivated by default
    config.set_value("OUTPUT_FORMAT", "text")
    config.set_value("OUTPUT_DETAIL", "detailed")
    config.set_value("PLUGINS", "")
    config.set_value("VALIDATE_ALL_CODEBASE", "true")
    # Root path of files to lint
    config.set_value(
        "DEFAULT_WORKSPACE",
        (
            config.get("DEFAULT_WORKSPACE") + sub_lint_root
            if config.exists("DEFAULT_WORKSPACE")
            and os.path.isdir(config.get("DEFAULT_WORKSPACE") + sub_lint_root)
            else root_dir + sub_lint_root
        ),
    )
    assert os.path.isdir(config.get("DEFAULT_WORKSPACE")), (
        "DEFAULT_WORKSPACE "
        + config.get("DEFAULT_WORKSPACE")
        + " is not a valid folder"
    )
示例#7
0
 def test_remote_config_success(self):
     remote_config = self.test_folder + "custom.mega-linter.yml"
     os.environ["MEGALINTER_CONFIG"] = remote_config
     config.init_config()
     self.assertEqual("(custom)", config.get("FILTER_REGEX_INCLUDE"))