Skip to content

Commit

Permalink
Merge pull request #2753 from aweis89/fix-linting-cmd
Browse files Browse the repository at this point in the history
fix: lint command with nested spaced strings
  • Loading branch information
paul-gauthier authored Jan 4, 2025
2 parents 591edbb + acf654c commit 98ad513
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
15 changes: 6 additions & 9 deletions aider/linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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)
Expand Down Expand Up @@ -44,26 +45,22 @@ def get_rel_fname(self, fname):

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

Expand Down
4 changes: 2 additions & 2 deletions aider/run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/basic/test_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_get_rel_fname(self):
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")
Expand All @@ -40,7 +40,7 @@ def test_run_cmd(self, mock_popen):
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")
Expand Down

0 comments on commit 98ad513

Please sign in to comment.