Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve runtime perfs when FLAVOR_SUGGESTIONS: false #1073

Merged
merged 4 commits into from
Dec 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python : CurrentFile",
"type": "python",
"request": "test",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Note: Can be used with `megalinter/megalinter@beta` in your GitHub Action mega-l
- Docker run -- clean-up containers when exits (#1033)
- Add missing Bandit config file and rules path options (#679)
- Fix getting linter version of npm plugin. (#845)
- Improve runtime performances when using a flavor and defining `FLAVORS_SUGGESTION: false`

- Linters
- New linter `phplint` to speed-up linting of php files (#1031)
Expand Down
13 changes: 12 additions & 1 deletion megalinter/MegaLinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, params=None):
self.workspace = self.get_workspace()
config.init_config(self.workspace) # Initialize runtime config
self.github_workspace = config.get("GITHUB_WORKSPACE", self.workspace)
self.megalinter_flavor = config.get("MEGALINTER_FLAVOR", "all")
self.report_folder = config.get(
"REPORT_OUTPUT_FOLDER",
config.get("OUTPUT_FOLDER", self.github_workspace + os.path.sep + "report"),
Expand Down Expand Up @@ -350,7 +351,17 @@ def load_linters(self):
}

# Build linters from descriptor files
all_linters = linter_factory.list_all_linters(linter_init_params)
# if flavor selected and no flavor suggestion, ignore linters that are not in current flavor)
if (
self.megalinter_flavor != "all"
and config.get("FLAVOR_SUGGESTIONS", "true") != "true"
):
all_linters = linter_factory.list_flavor_linters(
linter_init_params, self.megalinter_flavor
)
else:
all_linters = linter_factory.list_all_linters(linter_init_params)

skipped_linters = []
for linter in all_linters:
linter.master = self
Expand Down
8 changes: 7 additions & 1 deletion megalinter/flavor_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ def get_all_flavors():
return all_flavors


def list_flavor_linters(flavor_id):
all_flavors = get_all_flavors()
flavor_definition = all_flavors[flavor_id]
return flavor_definition["linters"]


def list_megalinter_flavors():
flavors = {
"all": {"label": "MegaLinter for any type of project"},
Expand Down Expand Up @@ -59,7 +65,7 @@ def list_megalinter_flavors():


def get_image_flavor():
return os.environ.get("MEGALINTER_FLAVOR", "all")
return config.get("MEGALINTER_FLAVOR", "all")


# Compare linters active for the current repo, and linters available in the current MegaLinter image flavor
Expand Down
15 changes: 14 additions & 1 deletion megalinter/linter_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os

import yaml
from megalinter import Linter
from megalinter import Linter, flavor_factory


# Returns directory where all .yml language descriptors are defined
Expand Down Expand Up @@ -34,6 +34,19 @@ def list_all_linters(linters_init_params=None):
return linters


# List flavor linters
def list_flavor_linters(linters_init_params=None, flavor_id="all"):
all_linters = list_all_linters(linters_init_params)
flavor_linter_ids = flavor_factory.list_flavor_linters(flavor_id)
linters = []
for linter in all_linters:
if linter.name in flavor_linter_ids:
linters += [linter]
else:
del linter
return linters


# List all descriptor files (one by language)
def list_descriptor_files():
descriptors_dir = get_descriptor_dir()
Expand Down
2 changes: 2 additions & 0 deletions megalinter/tests/test_megalinter/helpers/utilstest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def linter_test_setup(params=None):
"IGNORE_GITIGNORED_FILES",
"IGNORE_GENERATED_FILES",
"SHOW_ELAPSED_TIME",
"MEGALINTER_FLAVOR",
"FLAVOR_SUGGESTIONS",
]:
if key in os.environ:
del os.environ[key]
Expand Down
9 changes: 7 additions & 2 deletions megalinter/tests/test_megalinter/mega_linter_1_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,14 @@ def test_override_cli_executable(self):
"PHP_BUILTIN should have been processed with cli_executable = /usr/bin/php8",
)

def test_print_all_files_false(self):
def test_print_all_files_false_and_no_flavor_suggestion(self):
mega_linter, output = utilstest.call_mega_linter(
{"ENABLE_LINTERS": "JAVASCRIPT_ES", "PRINT_ALL_FILES": "false"}
{
"ENABLE_LINTERS": "JAVASCRIPT_ES",
"PRINT_ALL_FILES": "false",
"MEGALINTER_FLAVOR": "javascript",
"FLAVOR_SUGGESTIONS": "false",
}
)
self.assertTrue(
len(mega_linter.linters) > 0, "Linters have been created and run"
Expand Down