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 5, 2020
1 parent 0a7617b commit 20ad663
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 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
6 changes: 5 additions & 1 deletion bin/CodeChecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ def signal_handler(signum, frame):
if 'func_process_config_file' in args:
cfg_args = args.func_process_config_file(args)
if cfg_args:
sys.argv.extend(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:]
args = parser.parse_args()

try:
Expand Down

0 comments on commit 20ad663

Please sign in to comment.