Skip to content

Commit 3fa00ac

Browse files
authored
Merge pull request #349 from akaihola/gh-action-linter-versions
Allow constraining linter versions in GitHub Action configuration
2 parents 0d83595 + 064c1bf commit 3fa00ac

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

CHANGES.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Added
77
-----
88
- The ``--jobs`` option now specifies how many Darker jobs are used to process files in
99
parallel to complete reformatting/linting faster.
10-
- Linters can now be run in the GitHub Action using the ``lint:`` option.
10+
- Linters can now be installed and run in the GitHub Action using the ``lint:`` option.
1111

1212
Fixed
1313
-----

README.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ Create a file named ``.github/workflows/darker.yml`` inside your repository with
575575
revision: "master..."
576576
src: "./src"
577577
version: "1.4.2"
578-
lint: "flake8,pylint"
578+
lint: "flake8,pylint==2.13.1"
579579
580580
There needs to be a working Python environment, set up using ``actions/setup-python``
581581
in the above example. Darker will be installed in an isolated virtualenv to prevent
@@ -603,6 +603,7 @@ You can e.g. add ``"--isort"`` to sort imports, or ``"--verbose"`` for debug log
603603

604604
To run linters through Darker, you can provide a comma separated list of linters using
605605
the ``lint:`` option. Only ``flake8``, ``pylint`` and ``mypy`` are supported.
606+
Versions can be constrained using ``pip`` syntax, e.g. ``"flake8>=3.9.2"``.
606607

607608
*New in version 1.1.0:*
608609
GitHub Actions integration. Modeled after how Black_ does it,

action.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ inputs:
2525
lint:
2626
description: >-
2727
Comma-separated list of linters to `pip install` and run from Darker.
28-
Example: flake8,pylint
28+
Optionally, version constraints (using pip syntax) can be specified.
29+
Example: flake8,pylint==2.13.1
2930
required: false
3031
default: ''
3132
branding:

action/main.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
from pathlib import Path
55
from subprocess import PIPE, STDOUT, run # nosec
66

7+
from pkg_resources import parse_requirements
8+
79
LINTER_WHITELIST = {"flake8", "pylint", "mypy"}
810
ACTION_PATH = Path(os.environ["GITHUB_ACTION_PATH"])
911
ENV_PATH = ACTION_PATH / ".darker-env"
1012
ENV_BIN = ENV_PATH / ("Scripts" if sys.platform == "win32" else "bin")
1113
OPTIONS = os.getenv("INPUT_OPTIONS", default="")
1214
SRC = os.getenv("INPUT_SRC", default="")
1315
VERSION = os.getenv("INPUT_VERSION", default="")
16+
LINT = os.getenv("INPUT_LINT", default="")
1417
REVISION = os.getenv(
1518
"INPUT_REVISION", default=os.getenv("INPUT_COMMIT_RANGE", default="HEAD^")
1619
)
@@ -21,15 +24,13 @@
2124
if VERSION:
2225
req[0] += f"=={VERSION}"
2326
linter_options = []
24-
for linter_ in os.getenv("INPUT_LINT", default="").split(","):
25-
linter = linter_.strip()
26-
if not linter:
27-
continue
27+
for linter_requirement in parse_requirements(LINT.replace(",", "\n")):
28+
linter = linter_requirement.name
2829
if linter not in LINTER_WHITELIST:
2930
raise RuntimeError(
3031
f"{linter!r} is not supported as a linter by the GitHub Action"
3132
)
32-
req.append(linter)
33+
req.append(str(linter_requirement))
3334
linter_options.extend(["--lint", linter])
3435

3536
pip_proc = run( # nosec

action/tests/test_main.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ def test_creates_virtualenv(tmp_path, main_patch):
103103
run_main_env={"INPUT_LINT": " flake8 , pylint "},
104104
expect=["darker[isort]", "flake8", "pylint"],
105105
),
106+
dict(
107+
run_main_env={"INPUT_LINT": " flake8 >= 3.9.2 , pylint == 2.13.1 "},
108+
expect=["darker[isort]", "flake8>=3.9.2", "pylint==2.13.1"],
109+
),
106110
)
107111
def test_installs_packages(tmp_path, main_patch, run_main_env, expect):
108112
"""Darker, isort and linters are installed in the virtualenv using pip"""
@@ -126,7 +130,7 @@ def test_installs_packages(tmp_path, main_patch, run_main_env, expect):
126130

127131

128132
@pytest.mark.parametrize(
129-
"linters", ["foo", " foo ", "foo,bar", " foo , bar ", "pylint,foo"]
133+
"linters", ["foo", " foo ", "foo==2.0,bar", " foo>1.0 , bar ", "pylint,foo"]
130134
)
131135
def test_wont_install_unknown_packages(tmp_path, linters):
132136
"""Non-whitelisted linters raise an exception"""
@@ -173,6 +177,10 @@ def test_wont_install_unknown_packages(tmp_path, linters):
173177
env={"INPUT_SRC": ".", "INPUT_LINT": "pylint,flake8"},
174178
expect=["--lint", "pylint", "--lint", "flake8", "--revision", "HEAD^", "."],
175179
),
180+
dict(
181+
env={"INPUT_SRC": ".", "INPUT_LINT": "pylint == 2.13.1,flake8>=3.9.2"},
182+
expect=["--lint", "pylint", "--lint", "flake8", "--revision", "HEAD^", "."],
183+
),
176184
dict(
177185
env={
178186
"INPUT_SRC": "here.py there/too",

0 commit comments

Comments
 (0)