summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Gräsman <kim.grasman@gmail.com>2023-05-21 10:50:33 +0200
committerKim Gräsman <kim.grasman@gmail.com>2023-05-21 11:08:51 +0200
commitbd9c2c8c3e7bdf5c7cc6a06a27081794b35eeb27 (patch)
tree5e3fbc5dbd1c80a8825841f5f3bf723c29b6e9de
parent613b05b866680b8c015c95ffa1a0e9cb7633f6f6 (diff)
Don't run include-what-you-use through shell in tests
Instead of formatting a haphazard string, build a list for the command-line. Use shlex to split IWYU_ARGS (which implies these arguments must be Unix-style quoted if quoting is necessary). This makes it easier to add new arguments. No functional change intended.
-rwxr-xr-xiwyu_test_util.py36
1 files changed, 13 insertions, 23 deletions
diff --git a/iwyu_test_util.py b/iwyu_test_util.py
index d748cf2..c4b0d5e 100755
--- a/iwyu_test_util.py
+++ b/iwyu_test_util.py
@@ -18,6 +18,7 @@ import difflib
import operator
import os
import re
+import shlex
import subprocess
import sys
@@ -100,15 +101,8 @@ def _GetIwyuPath():
return _IWYU_PATH
-def _ShellQuote(arg):
- if ' ' in arg:
- arg = '"' + arg + '"'
- return arg
-
-
def _GetCommandOutput(command):
p = subprocess.Popen(command,
- shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout, _ = p.communicate()
@@ -422,7 +416,7 @@ def _GetLaunchArguments(cc_file):
raise SyntaxError('%s:%s syntax error in multiline IWYU_ARGS' %
(cc_file, lineno))
- return args
+ return shlex.split(args)
def TestIwyuOnRelativeFile(cc_file, cpp_files_to_check, verbose=False):
@@ -434,24 +428,20 @@ def TestIwyuOnRelativeFile(cc_file, cpp_files_to_check, verbose=False):
to check the diagnostics on, relative to the current dir.
verbose: Whether to display verbose output.
"""
- verbosity_flags = []
+ cmd = [_GetIwyuPath()]
+ # Require verbose level 3 so that we can verify the individual diagnostics.
+ # We allow the level to be overriden by
+ # * IWYU_ARGS comment in a test file
+ # * IWYU_VERBOSE environment variable
+ cmd += ['-Xiwyu', '--verbose=3']
+ cmd += _GetLaunchArguments(cc_file)
env_verbose_level = os.getenv('IWYU_VERBOSE')
if env_verbose_level:
- verbosity_flags = ['-Xiwyu', '--verbose=' + env_verbose_level]
-
- cmd = '%s %s %s %s %s' % (
- _ShellQuote(_GetIwyuPath()),
- # Require verbose level 3 so that we can verify the individual diagnostics.
- # We allow the level to be overriden by
- # * IWYU_ARGS comment in a test file;
- # * iwyu_flags;
- # * IWYU_VERBOSE environment variable;
- '-Xiwyu --verbose=3',
- _GetLaunchArguments(cc_file),
- ' '.join(verbosity_flags),
- cc_file)
+ cmd += ['-Xiwyu', '--verbose=' + env_verbose_level]
+ cmd += [cc_file]
+
if verbose:
- print('>>> Running %s' % cmd)
+ print('>>> Running %s' % shlex.join(cmd))
exit_code, output = _GetCommandOutput(cmd)
print(''.join(output))
sys.stdout.flush() # don't commingle this output with the failure output