Visitar URL original
Refactor tests and metadata by aaronliu0130 · Pull Request #317 · cpplint/cpplint · GitHub
Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: revert Win32 slash replacement changes, use subprocess.run()
Yep, I forgot the purpose of something I coded myself.

subprocess.run() is Python's recommended replacement to popen that doesn't need context management
  • Loading branch information
aaronliu0130 committed Mar 11, 2025
commit 3a5eaeef53d02a13a0e5aa3b0d17f513e321daab
25 changes: 10 additions & 15 deletions cpplint_clitest.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,19 @@ def run_shell_command(cmd: str, args: str, cwd='.'):
args: A string with arguments to the command.
cwd: from which folder to run.
"""

stdout_target = subprocess.PIPE
stderr_target = subprocess.PIPE

with subprocess.Popen(cmd + ' ' + args,
shell=True,
cwd=cwd,
stdout=stdout_target,
stderr=stderr_target) as proc:
out, err = proc.communicate()
cmd, args = cmd.split(), args.split()
proc = subprocess.run(cmd + args, cwd=cwd, capture_output=True)
out, err = proc.stdout, proc.stderr

# Make output system-agnostic, aka support Windows
if os.sep == '\\':
args_paths = args.split(' ')
for path in args_paths:
win_path = (os.path.dirname(path) + '\\').encode()
good_path = win_path.replace(b'\\', b'/')
out, err = out.replace(win_path, good_path), err.replace(win_path, good_path)
# TODO: Support scenario with multiple input names
# We currently only support the last arguments as the input name
# to prevent accidentally replacing sed tests.
# Fixing would likely need coding an internal "replace slashes" option for cpplint itself.
win_path = (os.path.dirname(args[-1]) + '\\').encode()
good_path = win_path.replace(b'\\', b'/')
out, err = out.replace(win_path, good_path), err.replace(win_path, good_path)
if os.linesep == '\r\n':
out, err = out.replace(b'\r\n', b'\n'), err.replace(b'\r\n', b'\n')

Expand Down