55import stat
66import sys
77import tempfile
8- from unittest .mock import Mock , patch
8+ from unittest .mock import patch
99
10- import pytest
1110from pylsp import lsp , uris
12- from pylsp .config . config import Config
13- from pylsp . workspace import Document , Workspace
11+ from pylsp .workspace import Document
12+ from conftest import temp_document
1413
1514import pylsp_ruff .plugin as ruff_lint
1615
@@ -29,24 +28,6 @@ def using_const():
2928"""
3029
3130
32- @pytest .fixture ()
33- def workspace (tmp_path ):
34- """Return a workspace."""
35- ws = Workspace (tmp_path .absolute ().as_uri (), Mock ())
36- ws ._config = Config (ws .root_uri , {}, 0 , {})
37- return ws
38-
39-
40- def temp_document (doc_text , workspace ):
41- with tempfile .NamedTemporaryFile (
42- mode = "w" , dir = workspace .root_path , delete = False
43- ) as temp_file :
44- name = temp_file .name
45- temp_file .write (doc_text )
46- doc = Document (uris .from_fs_path (name ), workspace )
47- return name , doc
48-
49-
5031def test_ruff_unsaved (workspace ):
5132 doc = Document ("" , workspace , DOC )
5233 diags = ruff_lint .pylsp_lint (workspace , doc )
@@ -276,6 +257,38 @@ def f():
276257 os .unlink (os .path .join (workspace .root_path , "pyproject.toml" ))
277258
278259
260+ def test_strip_virtual_documents_path_no_match ():
261+ path = "/work/foo/bar.py"
262+ assert ruff_lint .strip_virtual_documents_path (path , ".virtual_documents" ) == path
263+
264+
265+ def test_strip_virtual_documents_path_empty ():
266+ path = "/work/.virtual_documents/foo/bar.ipynb"
267+ assert ruff_lint .strip_virtual_documents_path (path , None ) == path
268+ assert ruff_lint .strip_virtual_documents_path (path , "" ) == path
269+
270+
271+ def test_ruff_lint_strips_virtual_documents_path (notebook_workspace ):
272+ virtual_path = os .path .join (
273+ notebook_workspace .root_path , ".virtual_documents" , "foo" , "bar.ipynb"
274+ )
275+ expected_stripped = os .path .join (notebook_workspace .root_path , "foo" , "bar.ipynb" )
276+ doc_uri = uris .from_fs_path (virtual_path )
277+ notebook_workspace .put_document (doc_uri , "import os\n " )
278+ doc = notebook_workspace .get_document (doc_uri )
279+
280+ with patch ("pylsp_ruff.plugin.Popen" ) as popen_mock :
281+ mock_instance = popen_mock .return_value
282+ mock_instance .communicate .return_value = [bytes (), bytes ()]
283+ ruff_lint .pylsp_lint (notebook_workspace , doc )
284+
285+ (call_args ,) = popen_mock .call_args [0 ]
286+ assert f"--stdin-filename={ expected_stripped } " in call_args
287+ assert (
288+ f"--stdin-filename={ virtual_path } " not in call_args
289+ ), "virtual_documents prefix was not stripped"
290+
291+
279292def test_notebook_input (workspace ):
280293 doc_str = r"""
281294print('hi')
0 commit comments