Skip to content

Commit 1604463

Browse files
freebird-2Richardk2n
authored andcommitted
Handles Windows line endings properly. Added two unit tests.
1 parent 6a9ca1d commit 1604463

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

pylsp_mypy/plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,9 @@ def get_diagnostics(
295295
global tmpFile
296296
if live_mode and not is_saved:
297297
if tmpFile:
298-
tmpFile = open(tmpFile.name, "w", encoding="utf-8")
298+
tmpFile = open(tmpFile.name, "w", encoding="utf-8", newline="")
299299
else:
300-
tmpFile = tempfile.NamedTemporaryFile("w", delete=False, encoding="utf-8")
300+
tmpFile = tempfile.NamedTemporaryFile("w", delete=False, encoding="utf-8", newline="")
301301
log.info("live_mode tmpFile = %s", tmpFile.name)
302302
tmpFile.write(document.source)
303303
tmpFile.close()

test/test_plugin.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ def test_plugin(workspace, last_diagnostics_monkeypatch):
8383
assert diag["code"] == "attr-defined"
8484

8585

86+
@pytest.mark.parametrize("doc_source", ["a = 1\nb\n", "a = 1\r\nb\r\n"])
87+
def test_handling_of_line_endings(workspace, last_diagnostics_monkeypatch, doc_source):
88+
# setup
89+
doc = Document(DOC_URI, workspace, doc_source)
90+
plugin.pylsp_settings(workspace._config)
91+
92+
# run
93+
diags = plugin.pylsp_lint(workspace._config, workspace, doc, is_saved=False)
94+
95+
# assert
96+
undefined_name_diags = list(filter(lambda diag: diag["code"] == "name-defined", diags))
97+
assert len(undefined_name_diags) == 1
98+
diag = undefined_name_diags[0]
99+
assert diag["message"] == 'Name "b" is not defined'
100+
assert diag["range"]["start"] == {"line": 1, "character": 0}
101+
assert diag["range"]["end"] == {"line": 1, "character": 1}
102+
103+
86104
def test_parse_full_line(workspace):
87105
diag = plugin.parse_line(TEST_LINE) # TODO parse a document here
88106
assert diag["message"] == '"Request" has no attribute "id"'

0 commit comments

Comments
 (0)