@@ -85,3 +85,77 @@ def test_flake8_executable_param(workspace):
8585
8686 (call_args ,) = popen_mock .call_args [0 ]
8787 assert flake8_executable in call_args
88+
89+
90+ def get_flake8_cfg_settings (workspace , config_str ):
91+ """Write a ``setup.cfg``, load it in the workspace, and return the flake8 settings.
92+
93+ This function creates a ``setup.cfg``; you'll have to delete it yourself.
94+ """
95+
96+ with open (os .path .join (workspace .root_path , "setup.cfg" ), "w+" ) as f :
97+ f .write (config_str )
98+
99+ workspace .update_config ({"pylsp" : {"configurationSources" : ["flake8" ]}})
100+
101+ return workspace ._config .plugin_settings ("flake8" )
102+
103+
104+ def test_flake8_multiline (workspace ):
105+ config_str = r"""[flake8]
106+ exclude =
107+ blah/,
108+ file_2.py
109+ """
110+
111+ doc_str = "print('hi')\n import os\n "
112+
113+ doc_uri = uris .from_fs_path (os .path .join (workspace .root_path , "blah/__init__.py" ))
114+ workspace .put_document (doc_uri , doc_str )
115+
116+ flake8_settings = get_flake8_cfg_settings (workspace , config_str )
117+
118+ assert "exclude" in flake8_settings
119+ assert len (flake8_settings ["exclude" ]) == 2
120+
121+ with patch ('pylsp.plugins.flake8_lint.Popen' ) as popen_mock :
122+ mock_instance = popen_mock .return_value
123+ mock_instance .communicate .return_value = [bytes (), bytes ()]
124+
125+ doc = workspace .get_document (doc_uri )
126+ flake8_lint .pylsp_lint (workspace , doc )
127+
128+ call_args = popen_mock .call_args [0 ][0 ]
129+ assert call_args == ["flake8" , "-" , "--exclude=blah/,file_2.py" ]
130+
131+ os .unlink (os .path .join (workspace .root_path , "setup.cfg" ))
132+
133+
134+ def test_flake8_per_file_ignores (workspace ):
135+ config_str = r"""[flake8]
136+ ignores = F403
137+ per-file-ignores =
138+ **/__init__.py:F401,E402
139+ test_something.py:E402,
140+ exclude =
141+ file_1.py
142+ file_2.py
143+ """
144+
145+ doc_str = "print('hi')\n import os\n "
146+
147+ doc_uri = uris .from_fs_path (os .path .join (workspace .root_path , "blah/__init__.py" ))
148+ workspace .put_document (doc_uri , doc_str )
149+
150+ flake8_settings = get_flake8_cfg_settings (workspace , config_str )
151+
152+ assert "perFileIgnores" in flake8_settings
153+ assert len (flake8_settings ["perFileIgnores" ]) == 2
154+ assert "exclude" in flake8_settings
155+ assert len (flake8_settings ["exclude" ]) == 2
156+
157+ doc = workspace .get_document (doc_uri )
158+ res = flake8_lint .pylsp_lint (workspace , doc )
159+ assert not res
160+
161+ os .unlink (os .path .join (workspace .root_path , "setup.cfg" ))
0 commit comments