Repository: python-lsp/python-lsp-server
Pull request #319 (2022-12-26 17:59:40)

Fix Pycodestyle linting with line endings other than LF 
- See spyder-ide/spyder#19565 for context.
- Also, cancel runs in progress and make pytest color its output on CIs.


Patch:
diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml
index af235b1..7a398c0 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/pylsp/plugins/pycodestyle_lint.py b/pylsp/plugins/pycodestyle_lint.py
index 3702fdb..8cdcee5 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()


Test patch:
diff --git a/.github/workflows/test-linux.yml b/.github/workflows/test-linux.yml
index 5441d69..af6079b 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 d00d6da..e111ff5 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 20eb7ea..eb393a9 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/
diff --git a/test/plugins/test_pycodestyle_lint.py b/test/plugins/test_pycodestyle_lint.py
index e238147..0323356 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,22 @@ 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 = f"try:{newline}    1/0{newline}except Exception:{newline}    pass{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