Skip to content

Commit

Permalink
Define PYTHON_DEFAULT_STYLE & custom test cases :)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvuillam committed Jan 6, 2025
1 parent 3176865 commit 8c7481d
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .automation/test/python_ruff_format/python_bad_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def f () :
return 1 is 1
4 changes: 4 additions & 0 deletions .automation/test/python_ruff_format/python_bad_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def foo():
x=1
y = 2
return x
2 changes: 2 additions & 0 deletions .automation/test/python_ruff_format/python_fix_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def f () :
return 1 is 1
2 changes: 2 additions & 0 deletions .automation/test/python_ruff_format/python_good_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def f():
return 1 == 1
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,7 @@ description: List of common variables that you can use to customize MegaLinter b
| [**PRE_COMMANDS**](https://github.com/oxsecurity/megalinter/tree/main/docs/config-precommands.md) | \[\] | Custom bash commands to run before linters |
| **PRINT_ALPACA** | `true` | Enable printing alpaca image to console |
| **PRINT_ALL_FILES** | `false` | Display all files analyzed by the linter instead of only the number |
| **PYTHON_DEFAULT_STYLE** | `black` | Python default style to check/apply. `black`,`ruff` |
| **REPORT_OUTPUT_FOLDER** | `${GITHUB_WORKSPACE}/megalinter-reports` | Directory for generating report files. Set to `none` to not generate reports |
| [**SECURED_ENV_VARIABLES**](https://github.com/oxsecurity/megalinter/tree/main/docs/config-variables-security.md) | \[\] | Additional list of secured environment variables to hide when calling linters. |
| [**SECURED_ENV_VARIABLES_DEFAULT**](https://github.com/oxsecurity/megalinter/tree/main/docs/config-variables-security.md) | MegaLinter & CI platforms sensitive variables | List of secured environment variables to hide when calling linters. [Default list](https://github.com/oxsecurity/megalinter/tree/main/docs/config-variables-security.md). This is not recommended to override this variable, use SECURED_ENV_VARIABLES |
Expand Down
13 changes: 11 additions & 2 deletions megalinter/descriptors/python.megalinter-descriptor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ linters:
- linter_name: black
name: PYTHON_BLACK
is_formatter: true
activation_rules:
- type: variable
variable: PYTHON_DEFAULT_STYLE
expected_value: black
default_value: black
linter_url: https://black.readthedocs.io/en/stable/
linter_repo: https://github.com/psf/black
linter_speed: 3
Expand Down Expand Up @@ -384,10 +389,15 @@ linters:
- name: Ruff
url: https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff

# ruff
# ruff fmt
- linter_name: ruff-format
name: PYTHON_RUFF_FORMAT
is_formatter: true
activation_rules:
- type: variable
variable: PYTHON_DEFAULT_STYLE
expected_value: ruff
default_value: black
can_output_sarif: true
linter_text: |
An extremely fast Python linter, written in Rust. Configured for formatting only.
Expand Down Expand Up @@ -416,7 +426,6 @@ linters:
- sarif
- --output-file
- "{{SARIF_OUTPUT_FILE}}"
test_folder: python_ruff
examples:
- "ruff format --check myfile.py myfile2.py"
- "ruff format --config .ruff.toml myfile.py myfile2.py"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14485,6 +14485,20 @@
"title": "PYTHON_BLACK: Unsecured env variables",
"type": "array"
},
"PYTHON_DEFAULT_STYLE": {
"$id": "#/properties/PYTHON_DEFAULT_STYLE",
"description": "Style of python formatting to apply",
"enum": [
"black",
"ruff"
],
"examples": [
"black",
"ruff"
],
"title": "Python default style",
"type": "string"
},
"PYTHON_FILTER_REGEX_EXCLUDE": {
"$id": "#/properties/PYTHON_FILTER_REGEX_EXCLUDE",
"title": "Excluding regex filter for PYTHON descriptor",
Expand Down
12 changes: 12 additions & 0 deletions megalinter/linters/RuffFormatLinter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python3
"""
Use Ruff to to format python files
https://github.com/astral-sh/ruff
"""

from megalinter import Linter, config


class RuffFormatLinter(Linter):
def pre_test(self):
config.set_value(self.request_id, "PYTHON_DEFAULT_STYLE", "ruff")
7 changes: 5 additions & 2 deletions megalinter/utilstest.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ def linter_test_setup(params=None):
# Workarounds to avoid wrong test classes to be called
test_name = os.environ.get("PYTEST_CURRENT_TEST", "")
test_keywords = os.environ.get("TEST_KEYWORDS", "")
if (test_keywords == "api_spectral" and "openapi_spectral" in test_name) or (
test_keywords == "php_phpcs" and "php_phpcsfixer" in test_name
# Special cases with names resembling each other
if (
(test_keywords == "api_spectral" and "openapi_spectral" in test_name)
or (test_keywords == "php_phpcs" and "php_phpcsfixer" in test_name)
or (test_keywords == "python_ruff" and "python_ruff_format" in test_name)
):
raise unittest.SkipTest("This test class should not be run in this campaign")
if params is None:
Expand Down

0 comments on commit 8c7481d

Please sign in to comment.