From df62b14364dbf02c6f608c72dae5e48c5d516692 Mon Sep 17 00:00:00 2001 From: Kiran Kumar Manku Date: Tue, 8 Aug 2023 15:50:20 -0700 Subject: [PATCH 1/9] Added workaround and early returning if file name matches excluded arg --- bundled/tool/lsp_server.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/bundled/tool/lsp_server.py b/bundled/tool/lsp_server.py index a21c3e2..7230118 100644 --- a/bundled/tool/lsp_server.py +++ b/bundled/tool/lsp_server.py @@ -394,6 +394,19 @@ def _run_tool_on_document( argv += TOOL_ARGS + settings["args"] + extra_args if use_stdin: + arg = _parse_autopep_exclude_arg(argv) + + if arg.exclude is not None: + exclude_patterns = _split_comma_separated(arg.exclude) + + import fnmatch + for pattern in exclude_patterns: + if fnmatch.fnmatch(document.path, pattern): + log_to_output(f"Excluded file: {document.path} because it matches patterh: {pattern}") + return None + + + argv += ["-"] if use_path: @@ -545,6 +558,29 @@ def _to_run_result_with_logging(rpc_result: jsonrpc.RpcRunResult) -> utils.RunRe return utils.RunResult(rpc_result.stdout, error) +def _parse_autopep_exclude_arg( + argv: list(str) +): + import argparse + parser = argparse.ArgumentParser( + description="Exclude Argument Parser" + ) + + parser.add_argument( + "--exclude", + metavar='globs', + required=False + ) + + exclude_argument, _ = parser.parse_known_args(argv) + + return exclude_argument + +def _split_comma_separated(string: str): + """Return a set of strings.""" + return {text.strip() for text in string.split(',') if text.strip()} + + # ***************************************************** # Logging and notification. # ***************************************************** From 84b21d17a5dc95e0f6f0a9d667afb546924fb53a Mon Sep 17 00:00:00 2001 From: Kiran Kumar Manku Date: Tue, 8 Aug 2023 18:14:42 -0700 Subject: [PATCH 2/9] Added test cases for exclude argument --- .../test_data/sample3/sample.unformatted | 2 + .../sample4/sample.excluded.unformatted. | 1 + .../sample4/sample.included.unformatted | 2 + .../test_data/sample4/sample_formatted.py | 3 + src/test/python_tests/test_formatting.py | 82 ++++++++++++++++++- 5 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/test/python_tests/test_data/sample3/sample.unformatted create mode 100644 src/test/python_tests/test_data/sample4/sample.excluded.unformatted. create mode 100644 src/test/python_tests/test_data/sample4/sample.included.unformatted create mode 100644 src/test/python_tests/test_data/sample4/sample_formatted.py diff --git a/src/test/python_tests/test_data/sample3/sample.unformatted b/src/test/python_tests/test_data/sample3/sample.unformatted new file mode 100644 index 0000000..3055777 --- /dev/null +++ b/src/test/python_tests/test_data/sample3/sample.unformatted @@ -0,0 +1,2 @@ +import sys;print(sys.executable) +# There is intentionally no last empty line. \ No newline at end of file diff --git a/src/test/python_tests/test_data/sample4/sample.excluded.unformatted. b/src/test/python_tests/test_data/sample4/sample.excluded.unformatted. new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/test/python_tests/test_data/sample4/sample.excluded.unformatted. @@ -0,0 +1 @@ + diff --git a/src/test/python_tests/test_data/sample4/sample.included.unformatted b/src/test/python_tests/test_data/sample4/sample.included.unformatted new file mode 100644 index 0000000..3055777 --- /dev/null +++ b/src/test/python_tests/test_data/sample4/sample.included.unformatted @@ -0,0 +1,2 @@ +import sys;print(sys.executable) +# There is intentionally no last empty line. \ No newline at end of file diff --git a/src/test/python_tests/test_data/sample4/sample_formatted.py b/src/test/python_tests/test_data/sample4/sample_formatted.py new file mode 100644 index 0000000..f99f90f --- /dev/null +++ b/src/test/python_tests/test_data/sample4/sample_formatted.py @@ -0,0 +1,3 @@ +import sys +print(sys.executable) +# There is intentionally no last empty line. \ No newline at end of file diff --git a/src/test/python_tests/test_formatting.py b/src/test/python_tests/test_formatting.py index 9dd93ee..2b10f81 100644 --- a/src/test/python_tests/test_formatting.py +++ b/src/test/python_tests/test_formatting.py @@ -7,7 +7,7 @@ from hamcrest import assert_that, is_ -from .lsp_test_client import constants, session, utils +from .lsp_test_client import constants, defaults, session, utils FORMATTER = utils.get_server_info_defaults() TIMEOUT = 10000 # 10 seconds @@ -138,3 +138,83 @@ def test_skipping_site_packages_files(): expected = None assert_that(actual, is_(expected)) + + +def test_skipping_excluded_files(): + """Test skipping formatting when the file is in excluded pattern""" + + UNFORMATTED_EXCLUDED_FILE_PATH = constants.TEST_DATA / "sample3" / "sample.unformatted" + with session.LspSession() as ls_session: + # Use any stdlib path here + uri = utils.as_uri(pathlib.__file__) + + init_options = defaults.VSCODE_DEFAULT_INITIALIZE["initializationOptions"] + init_options["settings"][0]["args"] = ["--exclude='**/*.py'"] + ls_session.initialize(defaults.VSCODE_DEFAULT_INITIALIZE) + ls_session.initialize() + + ls_session.notify_did_open( + { + "textDocument": { + "uri": uri, + "languageId": "python", + "version": 1, + "text": UNFORMATTED_EXCLUDED_FILE_PATH.read_text(encoding="utf-8"), + } + } + ) + + actual = ls_session.text_document_formatting( + { + "textDocument": {"uri": uri}, + # `options` is not used by black + "options": {"tabSize": 4, "insertSpaces": True}, + } + ) + + expected = None + assert_that(actual, is_(expected)) + + def test_formatting_file_not_in_excluded_files(): + """Test formatting when the file is not in excluded pattern""" + FORMATTED_TEST_FILE_PATH = constants.TEST_DATA / "sample4" / "sample.py" + UNFORMATTED_INCLUDED_FILE_PATH = constants.TEST_DATA / "sample4" / "sample.included.unformatted" + + with session.LspSession() as ls_session: + # Use any stdlib path here + uri = utils.as_uri(pathlib.__file__) + + init_options = defaults.VSCODE_DEFAULT_INITIALIZE["initializationOptions"] + init_options["settings"][0]["args"] = ["--exclude='**/*exclude'"] + ls_session.initialize(defaults.VSCODE_DEFAULT_INITIALIZE) + ls_session.initialize() + + ls_session.notify_did_open( + { + "textDocument": { + "uri": uri, + "languageId": "python", + "version": 1, + "text": UNFORMATTED_INCLUDED_FILE_PATH.read_text(encoding="utf-8"), + } + } + ) + + actual = ls_session.text_document_formatting( + { + "textDocument": {"uri": uri}, + # `options` is not used by black + "options": {"tabSize": 4, "insertSpaces": True}, + } + ) + + expected = [ + { + "range": { + "start": {"line": 0, "character": 0}, + "end": {"line": len(lines), "character": 0}, + }, + "newText": FORMATTED_TEST_FILE_PATH.read_text(), + } + ] + assert_that(actual, is_(expected)) From ba15c543c5dd0609f69c01e9445611292ba0561a Mon Sep 17 00:00:00 2001 From: Kiran Kumar Manku Date: Tue, 8 Aug 2023 18:26:44 -0700 Subject: [PATCH 3/9] Added one more test case --- src/test/python_tests/test_formatting.py | 44 ++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/test/python_tests/test_formatting.py b/src/test/python_tests/test_formatting.py index 2b10f81..cf1e530 100644 --- a/src/test/python_tests/test_formatting.py +++ b/src/test/python_tests/test_formatting.py @@ -218,3 +218,47 @@ def test_formatting_file_not_in_excluded_files(): } ] assert_that(actual, is_(expected)) + + def test_formatting_file_with_excluded_and_other_args(): + """Test formatting when we have more arguments specified""" + FORMATTED_TEST_FILE_PATH = constants.TEST_DATA / "sample4" / "sample.py" + UNFORMATTED_INCLUDED_FILE_PATH = constants.TEST_DATA / "sample4" / "sample.included.unformatted" + + with session.LspSession() as ls_session: + # Use any stdlib path here + uri = utils.as_uri(pathlib.__file__) + + init_options = defaults.VSCODE_DEFAULT_INITIALIZE["initializationOptions"] + init_options["settings"][0]["args"] = ["--exclude='**/*exclude'", "--aggressive"] + ls_session.initialize(defaults.VSCODE_DEFAULT_INITIALIZE) + ls_session.initialize() + + ls_session.notify_did_open( + { + "textDocument": { + "uri": uri, + "languageId": "python", + "version": 1, + "text": UNFORMATTED_INCLUDED_FILE_PATH.read_text(encoding="utf-8"), + } + } + ) + + actual = ls_session.text_document_formatting( + { + "textDocument": {"uri": uri}, + # `options` is not used by black + "options": {"tabSize": 4, "insertSpaces": True}, + } + ) + + expected = [ + { + "range": { + "start": {"line": 0, "character": 0}, + "end": {"line": len(lines), "character": 0}, + }, + "newText": FORMATTED_TEST_FILE_PATH.read_text(), + } + ] + assert_that(actual, is_(expected)) From da497200936546cd33e2af662ed82c710c96c7f4 Mon Sep 17 00:00:00 2001 From: Kiran Kumar Manku Date: Tue, 8 Aug 2023 18:54:49 -0700 Subject: [PATCH 4/9] Addressed the code review comments. --- src/test/python_tests/test_formatting.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/test/python_tests/test_formatting.py b/src/test/python_tests/test_formatting.py index cf1e530..e27c224 100644 --- a/src/test/python_tests/test_formatting.py +++ b/src/test/python_tests/test_formatting.py @@ -3,6 +3,7 @@ """ Test for formatting over LSP. """ +import copy import pathlib from hamcrest import assert_that, is_ @@ -148,10 +149,10 @@ def test_skipping_excluded_files(): # Use any stdlib path here uri = utils.as_uri(pathlib.__file__) - init_options = defaults.VSCODE_DEFAULT_INITIALIZE["initializationOptions"] + init_args = copy.deepcopy(defaults.VSCODE_DEFAULT_INITIALIZE) + init_options = init_args["initializationOptions"] init_options["settings"][0]["args"] = ["--exclude='**/*.py'"] - ls_session.initialize(defaults.VSCODE_DEFAULT_INITIALIZE) - ls_session.initialize() + ls_session.initialize(init_args) ls_session.notify_did_open( { @@ -184,10 +185,10 @@ def test_formatting_file_not_in_excluded_files(): # Use any stdlib path here uri = utils.as_uri(pathlib.__file__) - init_options = defaults.VSCODE_DEFAULT_INITIALIZE["initializationOptions"] + init_args = copy.deepcopy(defaults.VSCODE_DEFAULT_INITIALIZE) + init_options = init_args["initializationOptions"] init_options["settings"][0]["args"] = ["--exclude='**/*exclude'"] - ls_session.initialize(defaults.VSCODE_DEFAULT_INITIALIZE) - ls_session.initialize() + ls_session.initialize(init_args) ls_session.notify_did_open( { @@ -228,10 +229,10 @@ def test_formatting_file_with_excluded_and_other_args(): # Use any stdlib path here uri = utils.as_uri(pathlib.__file__) - init_options = defaults.VSCODE_DEFAULT_INITIALIZE["initializationOptions"] + init_args = copy.deepcopy(defaults.VSCODE_DEFAULT_INITIALIZE) + init_options = init_args["initializationOptions"] init_options["settings"][0]["args"] = ["--exclude='**/*exclude'", "--aggressive"] - ls_session.initialize(defaults.VSCODE_DEFAULT_INITIALIZE) - ls_session.initialize() + ls_session.initialize(init_args) ls_session.notify_did_open( { From 166887048aa2a3e0bf64447ab9694a3047f0e15c Mon Sep 17 00:00:00 2001 From: Kiran Kumar Manku Date: Tue, 8 Aug 2023 20:05:37 -0700 Subject: [PATCH 5/9] Updated test cases --- bundled/tool/lsp_server.py | 4 +- src/test/python_tests/test_formatting.py | 132 ++++++++++++----------- 2 files changed, 74 insertions(+), 62 deletions(-) diff --git a/bundled/tool/lsp_server.py b/bundled/tool/lsp_server.py index 7230118..b4483b3 100644 --- a/bundled/tool/lsp_server.py +++ b/bundled/tool/lsp_server.py @@ -3,8 +3,10 @@ """Implementation of tool support over LSP.""" from __future__ import annotations +import argparse import ast import copy +import fnmatch import json import os import pathlib @@ -399,7 +401,6 @@ def _run_tool_on_document( if arg.exclude is not None: exclude_patterns = _split_comma_separated(arg.exclude) - import fnmatch for pattern in exclude_patterns: if fnmatch.fnmatch(document.path, pattern): log_to_output(f"Excluded file: {document.path} because it matches patterh: {pattern}") @@ -561,7 +562,6 @@ def _to_run_result_with_logging(rpc_result: jsonrpc.RpcRunResult) -> utils.RunRe def _parse_autopep_exclude_arg( argv: list(str) ): - import argparse parser = argparse.ArgumentParser( description="Exclude Argument Parser" ) diff --git a/src/test/python_tests/test_formatting.py b/src/test/python_tests/test_formatting.py index e27c224..79a57fe 100644 --- a/src/test/python_tests/test_formatting.py +++ b/src/test/python_tests/test_formatting.py @@ -145,50 +145,19 @@ def test_skipping_excluded_files(): """Test skipping formatting when the file is in excluded pattern""" UNFORMATTED_EXCLUDED_FILE_PATH = constants.TEST_DATA / "sample3" / "sample.unformatted" - with session.LspSession() as ls_session: - # Use any stdlib path here - uri = utils.as_uri(pathlib.__file__) - - init_args = copy.deepcopy(defaults.VSCODE_DEFAULT_INITIALIZE) - init_options = init_args["initializationOptions"] - init_options["settings"][0]["args"] = ["--exclude='**/*.py'"] - ls_session.initialize(init_args) - - ls_session.notify_did_open( - { - "textDocument": { - "uri": uri, - "languageId": "python", - "version": 1, - "text": UNFORMATTED_EXCLUDED_FILE_PATH.read_text(encoding="utf-8"), - } - } - ) - - actual = ls_session.text_document_formatting( - { - "textDocument": {"uri": uri}, - # `options` is not used by black - "options": {"tabSize": 4, "insertSpaces": True}, - } - ) - - expected = None - assert_that(actual, is_(expected)) - def test_formatting_file_not_in_excluded_files(): - """Test formatting when the file is not in excluded pattern""" - FORMATTED_TEST_FILE_PATH = constants.TEST_DATA / "sample4" / "sample.py" - UNFORMATTED_INCLUDED_FILE_PATH = constants.TEST_DATA / "sample4" / "sample.included.unformatted" + contents = UNFORMATTED_EXCLUDED_FILE_PATH.read_text() + with utils.python_file(contents, UNFORMATTED_EXCLUDED_FILE_PATH.parent) as pf: with session.LspSession() as ls_session: # Use any stdlib path here - uri = utils.as_uri(pathlib.__file__) + uri = utils.as_uri(str(pf)) init_args = copy.deepcopy(defaults.VSCODE_DEFAULT_INITIALIZE) init_options = init_args["initializationOptions"] - init_options["settings"][0]["args"] = ["--exclude='**/*exclude'"] + init_options["settings"][0]["args"] = ["--exclude=**/*.py"] ls_session.initialize(init_args) + # ls_session.initialize() ls_session.notify_did_open( { @@ -196,7 +165,7 @@ def test_formatting_file_not_in_excluded_files(): "uri": uri, "languageId": "python", "version": 1, - "text": UNFORMATTED_INCLUDED_FILE_PATH.read_text(encoding="utf-8"), + "text": UNFORMATTED_EXCLUDED_FILE_PATH.read_text(encoding="utf-8"), } } ) @@ -209,6 +178,45 @@ def test_formatting_file_not_in_excluded_files(): } ) + expected = None + assert_that(actual, is_(expected)) + + def test_formatting_file_not_in_excluded_files(): + """Test formatting when the file is not in excluded pattern""" + FORMATTED_TEST_FILE_PATH = constants.TEST_DATA / "sample4" / "sample.py" + UNFORMATTED_INCLUDED_FILE_PATH = constants.TEST_DATA / "sample4" / "sample.included.unformatted" + + contents = UNFORMATTED_INCLUDED_FILE_PATH.read_text() + + with utils.python_file(contents, UNFORMATTED_INCLUDED_FILE_PATH.parent) as pf: + with session.LspSession() as ls_session: + # Use any stdlib path here + uri = utils.as_uri(str(pf)) + + init_args = copy.deepcopy(defaults.VSCODE_DEFAULT_INITIALIZE) + init_options = init_args["initializationOptions"] + init_options["settings"][0]["args"] = ["--exclude='**/*exclude'"] + ls_session.initialize(init_args) + + ls_session.notify_did_open( + { + "textDocument": { + "uri": uri, + "languageId": "python", + "version": 1, + "text": UNFORMATTED_INCLUDED_FILE_PATH.read_text(encoding="utf-8"), + } + } + ) + + actual = ls_session.text_document_formatting( + { + "textDocument": {"uri": uri}, + # `options` is not used by black + "options": {"tabSize": 4, "insertSpaces": True}, + } + ) + expected = [ { "range": { @@ -225,33 +233,37 @@ def test_formatting_file_with_excluded_and_other_args(): FORMATTED_TEST_FILE_PATH = constants.TEST_DATA / "sample4" / "sample.py" UNFORMATTED_INCLUDED_FILE_PATH = constants.TEST_DATA / "sample4" / "sample.included.unformatted" - with session.LspSession() as ls_session: - # Use any stdlib path here - uri = utils.as_uri(pathlib.__file__) + contents = UNFORMATTED_INCLUDED_FILE_PATH.read_text() - init_args = copy.deepcopy(defaults.VSCODE_DEFAULT_INITIALIZE) - init_options = init_args["initializationOptions"] - init_options["settings"][0]["args"] = ["--exclude='**/*exclude'", "--aggressive"] - ls_session.initialize(init_args) + with utils.python_file(contents, UNFORMATTED_INCLUDED_FILE_PATH.parent) as pf: - ls_session.notify_did_open( - { - "textDocument": { - "uri": uri, - "languageId": "python", - "version": 1, - "text": UNFORMATTED_INCLUDED_FILE_PATH.read_text(encoding="utf-8"), + with session.LspSession() as ls_session: + # Use any stdlib path here + uri = utils.as_uri(str(pf)) + + init_args = copy.deepcopy(defaults.VSCODE_DEFAULT_INITIALIZE) + init_options = init_args["initializationOptions"] + init_options["settings"][0]["args"] = ["--exclude='**/*exclude'", "--aggressive"] + ls_session.initialize(init_args) + + ls_session.notify_did_open( + { + "textDocument": { + "uri": uri, + "languageId": "python", + "version": 1, + "text": UNFORMATTED_INCLUDED_FILE_PATH.read_text(encoding="utf-8"), + } } - } - ) + ) - actual = ls_session.text_document_formatting( - { - "textDocument": {"uri": uri}, - # `options` is not used by black - "options": {"tabSize": 4, "insertSpaces": True}, - } - ) + actual = ls_session.text_document_formatting( + { + "textDocument": {"uri": uri}, + # `options` is not used by black + "options": {"tabSize": 4, "insertSpaces": True}, + } + ) expected = [ { From b9f41e6deb3d66f6a9ff5cd131b682a054acad66 Mon Sep 17 00:00:00 2001 From: Kiran Kumar Manku Date: Tue, 8 Aug 2023 20:08:50 -0700 Subject: [PATCH 6/9] Updated test case. --- src/test/python_tests/test_formatting.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/python_tests/test_formatting.py b/src/test/python_tests/test_formatting.py index 79a57fe..38da7ae 100644 --- a/src/test/python_tests/test_formatting.py +++ b/src/test/python_tests/test_formatting.py @@ -195,7 +195,7 @@ def test_formatting_file_not_in_excluded_files(): init_args = copy.deepcopy(defaults.VSCODE_DEFAULT_INITIALIZE) init_options = init_args["initializationOptions"] - init_options["settings"][0]["args"] = ["--exclude='**/*exclude'"] + init_options["settings"][0]["args"] = ["--exclude=**/*exclude"] ls_session.initialize(init_args) ls_session.notify_did_open( @@ -243,7 +243,7 @@ def test_formatting_file_with_excluded_and_other_args(): init_args = copy.deepcopy(defaults.VSCODE_DEFAULT_INITIALIZE) init_options = init_args["initializationOptions"] - init_options["settings"][0]["args"] = ["--exclude='**/*exclude'", "--aggressive"] + init_options["settings"][0]["args"] = ["--exclude=**/*exclude", "--aggressive"] ls_session.initialize(init_args) ls_session.notify_did_open( From 02472964d47aee1fa85db745170d0505e1743322 Mon Sep 17 00:00:00 2001 From: Kiran Kumar Manku Date: Tue, 8 Aug 2023 20:38:07 -0700 Subject: [PATCH 7/9] Fixed test cases --- ...mple.excluded.unformatted. => sample.excluded.unformatted} | 0 src/test/python_tests/test_formatting.py | 4 ++++ 2 files changed, 4 insertions(+) rename src/test/python_tests/test_data/sample4/{sample.excluded.unformatted. => sample.excluded.unformatted} (100%) diff --git a/src/test/python_tests/test_data/sample4/sample.excluded.unformatted. b/src/test/python_tests/test_data/sample4/sample.excluded.unformatted similarity index 100% rename from src/test/python_tests/test_data/sample4/sample.excluded.unformatted. rename to src/test/python_tests/test_data/sample4/sample.excluded.unformatted diff --git a/src/test/python_tests/test_formatting.py b/src/test/python_tests/test_formatting.py index 38da7ae..31a4095 100644 --- a/src/test/python_tests/test_formatting.py +++ b/src/test/python_tests/test_formatting.py @@ -147,6 +147,8 @@ def test_skipping_excluded_files(): UNFORMATTED_EXCLUDED_FILE_PATH = constants.TEST_DATA / "sample3" / "sample.unformatted" contents = UNFORMATTED_EXCLUDED_FILE_PATH.read_text() + lines = contents.splitlines(keepends=False) + with utils.python_file(contents, UNFORMATTED_EXCLUDED_FILE_PATH.parent) as pf: with session.LspSession() as ls_session: @@ -187,6 +189,7 @@ def test_formatting_file_not_in_excluded_files(): UNFORMATTED_INCLUDED_FILE_PATH = constants.TEST_DATA / "sample4" / "sample.included.unformatted" contents = UNFORMATTED_INCLUDED_FILE_PATH.read_text() + lines = contents.splitlines(keepends=False) with utils.python_file(contents, UNFORMATTED_INCLUDED_FILE_PATH.parent) as pf: with session.LspSession() as ls_session: @@ -234,6 +237,7 @@ def test_formatting_file_with_excluded_and_other_args(): UNFORMATTED_INCLUDED_FILE_PATH = constants.TEST_DATA / "sample4" / "sample.included.unformatted" contents = UNFORMATTED_INCLUDED_FILE_PATH.read_text() + lines = contents.splitlines(keepends=False) with utils.python_file(contents, UNFORMATTED_INCLUDED_FILE_PATH.parent) as pf: From 1f3a33c809199982a0dfc005ab13f7db53fbab75 Mon Sep 17 00:00:00 2001 From: Kiran Kumar Manku Date: Wed, 9 Aug 2023 10:53:53 -0700 Subject: [PATCH 8/9] Refactored the code to be more readable and fixed lint errors --- bundled/tool/lsp_server.py | 26 +++++++++++++++--------- src/test/python_tests/test_formatting.py | 2 -- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/bundled/tool/lsp_server.py b/bundled/tool/lsp_server.py index b4483b3..127f742 100644 --- a/bundled/tool/lsp_server.py +++ b/bundled/tool/lsp_server.py @@ -396,17 +396,10 @@ def _run_tool_on_document( argv += TOOL_ARGS + settings["args"] + extra_args if use_stdin: - arg = _parse_autopep_exclude_arg(argv) - - if arg.exclude is not None: - exclude_patterns = _split_comma_separated(arg.exclude) - - for pattern in exclude_patterns: - if fnmatch.fnmatch(document.path, pattern): - log_to_output(f"Excluded file: {document.path} because it matches patterh: {pattern}") - return None - + if _is_file_in_excluded_pattern(document.path, argv): + log_to_output(f"Excluded file: {document.path} because it matches pattern in args") + return None argv += ["-"] @@ -559,6 +552,19 @@ def _to_run_result_with_logging(rpc_result: jsonrpc.RpcRunResult) -> utils.RunRe return utils.RunResult(rpc_result.stdout, error) +def _is_file_in_excluded_pattern(file_path: str, argv: list(str)) -> bool: + arg = _parse_autopep_exclude_arg(argv) + + if arg.exclude is not None: + exclude_patterns = _split_comma_separated(arg.exclude) + + for pattern in exclude_patterns: + if fnmatch.fnmatch(file_path, pattern): + return True + + return False + + def _parse_autopep_exclude_arg( argv: list(str) ): diff --git a/src/test/python_tests/test_formatting.py b/src/test/python_tests/test_formatting.py index 31a4095..7b02a2c 100644 --- a/src/test/python_tests/test_formatting.py +++ b/src/test/python_tests/test_formatting.py @@ -147,8 +147,6 @@ def test_skipping_excluded_files(): UNFORMATTED_EXCLUDED_FILE_PATH = constants.TEST_DATA / "sample3" / "sample.unformatted" contents = UNFORMATTED_EXCLUDED_FILE_PATH.read_text() - lines = contents.splitlines(keepends=False) - with utils.python_file(contents, UNFORMATTED_EXCLUDED_FILE_PATH.parent) as pf: with session.LspSession() as ls_session: From 0890bc04e358b433576ac9684e7b8927478df0c3 Mon Sep 17 00:00:00 2001 From: Kiran Kumar Manku Date: Wed, 9 Aug 2023 11:24:43 -0700 Subject: [PATCH 9/9] Fixed linting errors. --- noxfile.py | 2 +- src/test/python_tests/test_formatting.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/noxfile.py b/noxfile.py index b3f2b57..883f2db 100644 --- a/noxfile.py +++ b/noxfile.py @@ -140,7 +140,7 @@ def lint(session: nox.Session) -> None: # check import sorting using isort session.install("isort") session.run("isort", "--check", "./bundled/tool") - session.run("isort", "--check", "--skip", "sample.py", "./src/test/python_tests") + session.run("isort", "--check", "--skip", "sample.py", "--skip", "sample_formatted.py", "./src/test/python_tests") session.run("isort", "--check", "noxfile.py") # check formatting using autopep8 diff --git a/src/test/python_tests/test_formatting.py b/src/test/python_tests/test_formatting.py index 7b02a2c..792287e 100644 --- a/src/test/python_tests/test_formatting.py +++ b/src/test/python_tests/test_formatting.py @@ -157,7 +157,6 @@ def test_skipping_excluded_files(): init_options = init_args["initializationOptions"] init_options["settings"][0]["args"] = ["--exclude=**/*.py"] ls_session.initialize(init_args) - # ls_session.initialize() ls_session.notify_did_open( {