From acf654c984cc0b43f858b59492c987c0a1747907 Mon Sep 17 00:00:00 2001 From: Aaron Weisberg Date: Sun, 29 Dec 2024 21:45:25 +0100 Subject: [PATCH] fix: lint command with nested spaced strings --- aider/linter.py | 15 ++++++--------- aider/run_cmd.py | 4 ++-- tests/basic/test_linter.py | 4 ++-- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/aider/linter.py b/aider/linter.py index 529cacb9d..77d9b5eb2 100644 --- a/aider/linter.py +++ b/aider/linter.py @@ -11,6 +11,7 @@ from grep_ast import TreeContext, filename_to_lang from tree_sitter_languages import get_parser # noqa: E402 from aider.dump import dump # noqa: F401 +from aider.run_cmd import run_cmd_subprocess # noqa: F401 # tree_sitter is throwing a FutureWarning warnings.simplefilter("ignore", category=FutureWarning) @@ -44,26 +45,22 @@ class Linter: def run_cmd(self, cmd, rel_fname, code): cmd += " " + rel_fname - cmd = cmd.split() + returncode = 0 + stdout = "" try: - process = subprocess.Popen( + returncode, stdout = run_cmd_subprocess( cmd, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - encoding=self.encoding, - errors="replace", cwd=self.root, + encoding=self.encoding, ) except OSError as err: print(f"Unable to execute lint command: {err}") return - stdout, _ = process.communicate() errors = stdout - if process.returncode == 0: + if returncode == 0: return # zero exit status - cmd = " ".join(cmd) res = f"## Running: {cmd}\n\n" res += errors diff --git a/aider/run_cmd.py b/aider/run_cmd.py index 4d8e0d4fe..f201b41dc 100644 --- a/aider/run_cmd.py +++ b/aider/run_cmd.py @@ -39,7 +39,7 @@ def get_windows_parent_process_name(): return None -def run_cmd_subprocess(command, verbose=False, cwd=None): +def run_cmd_subprocess(command, verbose=False, cwd=None, encoding=sys.stdout.encoding): if verbose: print("Using run_cmd_subprocess:", command) @@ -65,7 +65,7 @@ def run_cmd_subprocess(command, verbose=False, cwd=None): stderr=subprocess.STDOUT, text=True, shell=True, - encoding=sys.stdout.encoding, + encoding=encoding, errors="replace", bufsize=0, # Set bufsize to 0 for unbuffered output universal_newlines=True, diff --git a/tests/basic/test_linter.py b/tests/basic/test_linter.py index 9310557c5..9dce56355 100644 --- a/tests/basic/test_linter.py +++ b/tests/basic/test_linter.py @@ -30,7 +30,7 @@ class TestLinter(unittest.TestCase): def test_run_cmd(self, mock_popen): mock_process = MagicMock() mock_process.returncode = 0 - mock_process.communicate.return_value = ("", None) + mock_process.stdout.read.side_effect = ("", None) mock_popen.return_value = mock_process result = self.linter.run_cmd("test_cmd", "test_file.py", "code") @@ -40,7 +40,7 @@ class TestLinter(unittest.TestCase): def test_run_cmd_with_errors(self, mock_popen): mock_process = MagicMock() mock_process.returncode = 1 - mock_process.communicate.return_value = ("Error message", None) + mock_process.stdout.read.side_effect = ("Error message", None) mock_popen.return_value = mock_process result = self.linter.run_cmd("test_cmd", "test_file.py", "code")