From a3ffc10687467d50921327d2a6b00c25d799a6da Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Mon, 26 Dec 2022 12:50:51 -0500 Subject: [PATCH 1/4] Fix Pycodestyle linting with line endings other than LF Also add a test to check that. --- pylsp/plugins/pycodestyle_lint.py | 15 ++++++++++++++- test/plugins/test_pycodestyle_lint.py | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/pylsp/plugins/pycodestyle_lint.py b/pylsp/plugins/pycodestyle_lint.py index 3702fdb9..8cdcee5c 100644 --- a/pylsp/plugins/pycodestyle_lint.py +++ b/pylsp/plugins/pycodestyle_lint.py @@ -2,8 +2,11 @@ # Copyright 2021- Python Language Server Contributors. import logging + import pycodestyle + from pylsp import hookimpl, lsp +from pylsp._utils import get_eol_chars try: from autopep8 import continued_indentation as autopep8_c_i @@ -39,8 +42,18 @@ def pylsp_lint(workspace, document): kwargs = {k: v for k, v in opts.items() if v} styleguide = pycodestyle.StyleGuide(kwargs) + # Use LF to lint file because other line endings can give false positives. + # See spyder-ide/spyder#19565 for context. + source = document.source + eol_chars = get_eol_chars(source) + if eol_chars in ['\r', '\r\n']: + source = source.replace(eol_chars, '\n') + lines = source.splitlines(keepends=True) + else: + lines = document.lines + c = pycodestyle.Checker( - filename=document.uri, lines=document.lines, options=styleguide.options, + filename=document.path, lines=lines, options=styleguide.options, report=PyCodeStyleDiagnosticReport(styleguide.options) ) c.check_all() diff --git a/test/plugins/test_pycodestyle_lint.py b/test/plugins/test_pycodestyle_lint.py index e2381472..e23c2178 100644 --- a/test/plugins/test_pycodestyle_lint.py +++ b/test/plugins/test_pycodestyle_lint.py @@ -2,6 +2,9 @@ # Copyright 2021- Python Language Server Contributors. import os + +import pytest + from pylsp import lsp, uris from pylsp.workspace import Document from pylsp.plugins import pycodestyle_lint @@ -110,3 +113,23 @@ def test_pycodestyle_config(workspace): assert not [d for d in diags if d['code'] == 'W191'] assert not [d for d in diags if d['code'] == 'E201'] assert [d for d in diags if d['code'] == 'W391'] + + +@pytest.mark.parametrize('newline', ['\r\n', '\r']) +def test_line_endings(workspace, newline): + """ + Check that Pycodestyle doesn't generate false positives with line endings + other than LF. + """ + # Create simple source that should give false positives + source = "try:\n 1/0\nexcept:\n pass\n" + source = source.replace('\n', newline) + + # Create document + doc = Document(DOC_URI, workspace, source) + + # Get diagnostics + diags = pycodestyle_lint.pylsp_lint(workspace, doc) + + # Assert no diagnostics were given + assert len(diags) == 0 From cf1013ed2d98e76b811568a3646fb024e1883365 Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Mon, 26 Dec 2022 12:57:02 -0500 Subject: [PATCH 2/4] Cancel runs in progress and make pytest color its output on CIs --- .github/workflows/static.yml | 4 ++++ .github/workflows/test-linux.yml | 6 +++++- .github/workflows/test-mac.yml | 6 +++++- .github/workflows/test-win.yml | 6 +++++- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index af235b1e..7a398c0b 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -9,6 +9,10 @@ on: branches: - '*' +concurrency: + group: static-${{ github.ref }} + cancel-in-progress: true + jobs: build: name: Static code analysis diff --git a/.github/workflows/test-linux.yml b/.github/workflows/test-linux.yml index 5441d69a..af6079ba 100644 --- a/.github/workflows/test-linux.yml +++ b/.github/workflows/test-linux.yml @@ -9,6 +9,10 @@ on: branches: - '*' +concurrency: + group: test-linux-${{ github.ref }} + cancel-in-progress: true + jobs: build: name: Linux Py${{ matrix.PYTHON_VERSION }} @@ -41,7 +45,7 @@ jobs: - run: pip install -e .[all,test] - name: Show test environment run: pip list - - run: pytest -v test/ + - run: pytest --color=yes -v test/ # Enable this if SSH debugging is required # - name: Setup tmate session # uses: mxschmitt/action-tmate@v3 diff --git a/.github/workflows/test-mac.yml b/.github/workflows/test-mac.yml index d00d6da8..e111ff5c 100644 --- a/.github/workflows/test-mac.yml +++ b/.github/workflows/test-mac.yml @@ -9,6 +9,10 @@ on: branches: - '*' +concurrency: + group: test-mac-${{ github.ref }} + cancel-in-progress: true + jobs: build: name: Mac Py${{ matrix.PYTHON_VERSION }} @@ -41,7 +45,7 @@ jobs: - run: pip install -e .[all,test] - name: Show test environment run: pip list - - run: pytest -v test/ + - run: pytest --color=yes -v test/ # Enable this if SSH debugging is required # - name: Setup tmate session # uses: mxschmitt/action-tmate@v3 diff --git a/.github/workflows/test-win.yml b/.github/workflows/test-win.yml index 20eb7ea6..eb393a9f 100644 --- a/.github/workflows/test-win.yml +++ b/.github/workflows/test-win.yml @@ -9,6 +9,10 @@ on: branches: - '*' +concurrency: + group: test-win-${{ github.ref }} + cancel-in-progress: true + jobs: build: name: Win Py${{ matrix.PYTHON_VERSION }} @@ -37,4 +41,4 @@ jobs: - run: pip install -e .[all,test] - name: Show test environment run: pip list - - run: pytest -v test/ + - run: pytest --color=yes -v test/ From 7a4fdfdcad906bbd98fc5ffbf98203f8afe0488c Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Mon, 26 Dec 2022 13:05:15 -0500 Subject: [PATCH 3/4] Fix new test on Windows --- test/plugins/test_pycodestyle_lint.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/plugins/test_pycodestyle_lint.py b/test/plugins/test_pycodestyle_lint.py index e23c2178..2408b689 100644 --- a/test/plugins/test_pycodestyle_lint.py +++ b/test/plugins/test_pycodestyle_lint.py @@ -122,7 +122,7 @@ def test_line_endings(workspace, newline): other than LF. """ # Create simple source that should give false positives - source = "try:\n 1/0\nexcept:\n pass\n" + source = "try:\n 1/0\nexcept Exception:\n pass\n" source = source.replace('\n', newline) # Create document From 54b030eb17561ca028fefa1a49c32d9a3e96287e Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Mon, 26 Dec 2022 15:48:23 -0500 Subject: [PATCH 4/4] Simplify PyCodestyle test for line endings0 --- test/plugins/test_pycodestyle_lint.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/plugins/test_pycodestyle_lint.py b/test/plugins/test_pycodestyle_lint.py index 2408b689..03233569 100644 --- a/test/plugins/test_pycodestyle_lint.py +++ b/test/plugins/test_pycodestyle_lint.py @@ -122,8 +122,7 @@ def test_line_endings(workspace, newline): other than LF. """ # Create simple source that should give false positives - source = "try:\n 1/0\nexcept Exception:\n pass\n" - source = source.replace('\n', newline) + source = f"try:{newline} 1/0{newline}except Exception:{newline} pass{newline}" # Create document doc = Document(DOC_URI, workspace, source)