fix: lint command with nested spaced strings

This commit is contained in:
Aaron Weisberg 2024-12-29 21:45:25 +01:00
parent f292e01980
commit acf654c984
3 changed files with 10 additions and 13 deletions

View file

@ -11,6 +11,7 @@ from grep_ast import TreeContext, filename_to_lang
from tree_sitter_languages import get_parser # noqa: E402 from tree_sitter_languages import get_parser # noqa: E402
from aider.dump import dump # noqa: F401 from aider.dump import dump # noqa: F401
from aider.run_cmd import run_cmd_subprocess # noqa: F401
# tree_sitter is throwing a FutureWarning # tree_sitter is throwing a FutureWarning
warnings.simplefilter("ignore", category=FutureWarning) warnings.simplefilter("ignore", category=FutureWarning)
@ -44,26 +45,22 @@ class Linter:
def run_cmd(self, cmd, rel_fname, code): def run_cmd(self, cmd, rel_fname, code):
cmd += " " + rel_fname cmd += " " + rel_fname
cmd = cmd.split()
returncode = 0
stdout = ""
try: try:
process = subprocess.Popen( returncode, stdout = run_cmd_subprocess(
cmd, cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding=self.encoding,
errors="replace",
cwd=self.root, cwd=self.root,
encoding=self.encoding,
) )
except OSError as err: except OSError as err:
print(f"Unable to execute lint command: {err}") print(f"Unable to execute lint command: {err}")
return return
stdout, _ = process.communicate()
errors = stdout errors = stdout
if process.returncode == 0: if returncode == 0:
return # zero exit status return # zero exit status
cmd = " ".join(cmd)
res = f"## Running: {cmd}\n\n" res = f"## Running: {cmd}\n\n"
res += errors res += errors

View file

@ -39,7 +39,7 @@ def get_windows_parent_process_name():
return None 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: if verbose:
print("Using run_cmd_subprocess:", command) print("Using run_cmd_subprocess:", command)
@ -65,7 +65,7 @@ def run_cmd_subprocess(command, verbose=False, cwd=None):
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
text=True, text=True,
shell=True, shell=True,
encoding=sys.stdout.encoding, encoding=encoding,
errors="replace", errors="replace",
bufsize=0, # Set bufsize to 0 for unbuffered output bufsize=0, # Set bufsize to 0 for unbuffered output
universal_newlines=True, universal_newlines=True,

View file

@ -30,7 +30,7 @@ class TestLinter(unittest.TestCase):
def test_run_cmd(self, mock_popen): def test_run_cmd(self, mock_popen):
mock_process = MagicMock() mock_process = MagicMock()
mock_process.returncode = 0 mock_process.returncode = 0
mock_process.communicate.return_value = ("", None) mock_process.stdout.read.side_effect = ("", None)
mock_popen.return_value = mock_process mock_popen.return_value = mock_process
result = self.linter.run_cmd("test_cmd", "test_file.py", "code") 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): def test_run_cmd_with_errors(self, mock_popen):
mock_process = MagicMock() mock_process = MagicMock()
mock_process.returncode = 1 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 mock_popen.return_value = mock_process
result = self.linter.run_cmd("test_cmd", "test_file.py", "code") result = self.linter.run_cmd("test_cmd", "test_file.py", "code")