From 76f4e5f3fc3d05beb154a3379c861248320e1e1d Mon Sep 17 00:00:00 2001 From: poppyschmo Date: Tue, 12 May 2020 19:26:42 -0700 Subject: [PATCH] Add option for prepending arbitrary cmd-line args --- README.rst | 4 +++- pyls_mypy/plugin.py | 4 ++++ test/test_plugin.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 50e6ba5..416214d 100644 --- a/README.rst +++ b/README.rst @@ -21,6 +21,7 @@ Install into the same virtualenv as pyls itself. Configuration ------------- +``prepend`` (default is ``[]``) list of additional command-line options to prepend ``live_mode`` (default is True) provides type checking as you type. @@ -41,7 +42,8 @@ Depending on your editor, the configuration should be roughly like this: "pyls_mypy": { "enabled": true, - "live_mode": false + "live_mode": false, + "prepend": ["--python-executable", "/tmp/foo/bin/python"] } } } diff --git a/pyls_mypy/plugin.py b/pyls_mypy/plugin.py index 530dea9..e06d515 100644 --- a/pyls_mypy/plugin.py +++ b/pyls_mypy/plugin.py @@ -72,6 +72,10 @@ def pyls_lint(config, workspace, document, is_saved): if settings.get('strict', False): args.append('--strict') + prepend = settings.get('prepend') + if prepend: + args = prepend + args + report, errors, _ = mypy_api.run(args) diagnostics = [] diff --git a/test/test_plugin.py b/test/test_plugin.py index 6e13ae3..bbefbc7 100644 --- a/test/test_plugin.py +++ b/test/test_plugin.py @@ -65,3 +65,38 @@ def test_parse_line_with_context(monkeypatch, word, bounds): assert diag['message'] == '"Request" has no attribute "id"' assert diag['range']['start'] == {'line': 278, 'character': bounds[0]} assert diag['range']['end'] == {'line': 278, 'character': bounds[1]} + + +def test_option_prepend(tmpdir, monkeypatch): + import sys + from textwrap import dedent + + sentinel = tmpdir / 'ran' + + source = dedent( + """\ + #!/bin/sh + touch {} + exec {} "$@" + """ + ).format(sentinel, sys.executable) + + wrapper = tmpdir / 'bin/wrapper' + wrapper.write(source, ensure=True) + wrapper.chmod(0o700) + + monkeypatch.setattr( + FakeConfig, + 'plugin_settings', + lambda *_: {'prepend': ['--python-executable', wrapper.strpath]} + ) + + assert not sentinel.exists() + diags = plugin.pyls_lint( + config=FakeConfig(), + workspace=None, + document=Document(DOC_URI, DOC_TYPE_ERR), + is_saved=False, + ) + assert len(diags) == 1 + assert sentinel.exists()