Skip to content

Commit

Permalink
[cmd] Override --config file options from command line
Browse files Browse the repository at this point in the history
With this change options specified on the command line after the --config option
will override options specified in the config file.
  • Loading branch information
csordasmarton committed Aug 7, 2020
1 parent c938960 commit bc84194
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
22 changes: 21 additions & 1 deletion analyzer/tests/functional/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def setUp(self):
encoding="utf-8", errors="ignore") as source:
source.write(simple_file_content)

def __run_analyze(self):
def __run_analyze(self, extra_options=None):
"""
Run the CodeChecker analyze command with a configuration file.
"""
Expand All @@ -68,6 +68,9 @@ def __run_analyze(self):
"-o", self.reports_dir,
"--config", self.config_file]

if extra_options:
analyze_cmd.extend(extra_options)

# Run analyze.
process = subprocess.Popen(
analyze_cmd,
Expand All @@ -94,6 +97,23 @@ def test_only_clangsa_config(self):
self.assertIn("clangsa analyzed simple.cpp", out)
self.assertNotIn("clang-tidy analyzed simple.cpp", out)

def test_override_config_file(self):
"""
Run analyze command with a config file which enables the clang-tidy
analyzer only and override this option from the command line and enable
only clangsa analyze.
"""
with open(self.config_file, 'w+',
encoding="utf-8", errors="ignore") as config_f:
json.dump({
'analyzer': ['--analyzers', 'clang-tidy']}, config_f)

out, returncode = self.__run_analyze(['--analyzers', 'clangsa'])

self.assertEqual(returncode, 0)
self.assertIn("clangsa analyzed simple.cpp", out)
self.assertNotIn("clang-tidy analyzed simple.cpp", out)

def test_empty_config(self):
"""
Run analyze with an empty config file.
Expand Down
9 changes: 7 additions & 2 deletions bin/CodeChecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,14 @@ def signal_handler(signum, frame):
cfg_args = args.func_process_config_file(args)
if cfg_args:
# Expand environment variables in the arguments.
cfg_args = map(lambda cfg: os.path.expandvars(cfg), cfg_args)
cfg_args = [os.path.expandvars(cfg) for cfg in cfg_args]

# Replace --config option with the options inside the config
# file.
cfg_idx = sys.argv.index("--config")
sys.argv = sys.argv[:cfg_idx] + cfg_args + \
sys.argv[cfg_idx + 2:]

sys.argv.extend(cfg_args)
args = parser.parse_args()

try:
Expand Down

0 comments on commit bc84194

Please sign in to comment.