Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmd] Override --config file options from command line #2883

Merged
merged 1 commit into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
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