Skip to content

Commit a83c53f

Browse files
alipqbsigmavirus24
andauthored
fix reading initial values from .bandit (#722)
Pass the default CLI arg into the helper function so we can discern between a value passed by CLI and a default Co-authored-by: Ian Stapleton Cordasco <[email protected]>
1 parent aac3f16 commit a83c53f

File tree

2 files changed

+69
-13
lines changed

2 files changed

+69
-13
lines changed

bandit/cli/main.py

+34-8
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,24 @@ def _init_extensions():
8080
return ext_loader.MANAGER
8181

8282

83-
def _log_option_source(arg_val, ini_val, option_name):
83+
def _log_option_source(default_val, arg_val, ini_val, option_name):
8484
"""It's useful to show the source of each option."""
85-
if arg_val:
86-
LOG.info("Using command line arg for %s", option_name)
87-
return arg_val
88-
elif ini_val:
89-
LOG.info("Using ini file for %s", option_name)
90-
return ini_val
85+
# When default value is not defined, arg_val and ini_val is deterministic
86+
if default_val is None:
87+
if arg_val:
88+
LOG.info("Using command line arg for %s", option_name)
89+
return arg_val
90+
elif ini_val:
91+
LOG.info("Using ini file for %s", option_name)
92+
return ini_val
93+
else:
94+
return None
95+
# No value passed to commad line and default value is used
96+
elif default_val == arg_val:
97+
return ini_val if ini_val else arg_val
98+
# Certainly a value is passed to commad line
9199
else:
92-
return None
100+
return arg_val
93101

94102

95103
def _running_under_virtualenv():
@@ -354,16 +362,19 @@ def main():
354362
if ini_options:
355363
# prefer command line, then ini file
356364
args.excluded_paths = _log_option_source(
365+
parser.get_default('excluded_paths'),
357366
args.excluded_paths,
358367
ini_options.get('exclude'),
359368
'excluded paths')
360369

361370
args.skips = _log_option_source(
371+
parser.get_default('skips'),
362372
args.skips,
363373
ini_options.get('skips'),
364374
'skipped tests')
365375

366376
args.tests = _log_option_source(
377+
parser.get_default('tests'),
367378
args.tests,
368379
ini_options.get('tests'),
369380
'selected tests')
@@ -373,78 +384,93 @@ def main():
373384
ini_targets = ini_targets.split(',')
374385

375386
args.targets = _log_option_source(
387+
parser.get_default('targets'),
376388
args.targets,
377389
ini_targets,
378390
'selected targets')
379391

380392
# TODO(tmcpeak): any other useful options to pass from .bandit?
381393

382394
args.recursive = _log_option_source(
395+
parser.get_default('recursive'),
383396
args.recursive,
384397
ini_options.get('recursive'),
385398
'recursive scan')
386399

387400
args.agg_type = _log_option_source(
401+
parser.get_default('agg_type'),
388402
args.agg_type,
389403
ini_options.get('aggregate'),
390404
'aggregate output type')
391405

392406
args.context_lines = _log_option_source(
407+
parser.get_default('context_lines'),
393408
args.context_lines,
394409
ini_options.get('number'),
395410
'max code lines output for issue')
396411

397412
args.profile = _log_option_source(
413+
parser.get_default('profile'),
398414
args.profile,
399415
ini_options.get('profile'),
400416
'profile')
401417

402418
args.severity = _log_option_source(
419+
parser.get_default('severity'),
403420
args.severity,
404421
ini_options.get('level'),
405422
'severity level')
406423

407424
args.confidence = _log_option_source(
425+
parser.get_default('confidence'),
408426
args.confidence,
409427
ini_options.get('confidence'),
410428
'confidence level')
411429

412430
args.output_format = _log_option_source(
431+
parser.get_default('output_format'),
413432
args.output_format,
414433
ini_options.get('format'),
415434
'output format')
416435

417436
args.msg_template = _log_option_source(
437+
parser.get_default('msg_template'),
418438
args.msg_template,
419439
ini_options.get('msg-template'),
420440
'output message template')
421441

422442
args.output_file = _log_option_source(
443+
parser.get_default('output_file'),
423444
args.output_file,
424445
ini_options.get('output'),
425446
'output file')
426447

427448
args.verbose = _log_option_source(
449+
parser.get_default('verbose'),
428450
args.verbose,
429451
ini_options.get('verbose'),
430452
'output extra information')
431453

432454
args.debug = _log_option_source(
455+
parser.get_default('debug'),
433456
args.debug,
434457
ini_options.get('debug'),
435458
'debug mode')
436459

437460
args.quiet = _log_option_source(
461+
parser.get_default('quiet'),
438462
args.quiet,
439463
ini_options.get('quiet'),
440464
'silent mode')
441465

442466
args.ignore_nosec = _log_option_source(
467+
parser.get_default('ignore_nosec'),
443468
args.ignore_nosec,
444469
ini_options.get('ignore-nosec'),
445470
'do not skip lines with # nosec')
446471

447472
args.baseline = _log_option_source(
473+
parser.get_default('baseline'),
448474
args.baseline,
449475
ini_options.get('baseline'),
450476
'path of a baseline report')

tests/unit/cli/test_main.py

+35-5
Original file line numberDiff line numberDiff line change
@@ -126,25 +126,55 @@ def test_init_extensions(self):
126126

127127
def test_log_option_source_arg_val(self):
128128
# Test that the command argument value is returned when provided
129+
# with None or a string default value
129130
arg_val = 'file'
130131
ini_val = 'vuln'
131132
option_name = 'aggregate'
132-
self.assertEqual(arg_val, bandit._log_option_source(arg_val, ini_val,
133-
option_name))
133+
for default_val in (None, 'default'):
134+
self.assertEqual(arg_val, bandit._log_option_source(
135+
default_val,
136+
arg_val,
137+
ini_val,
138+
option_name
139+
))
134140

135141
def test_log_option_source_ini_value(self):
136142
# Test that the ini value is returned when no command argument is
137143
# provided
144+
default_val = None
138145
ini_val = 'vuln'
139146
option_name = 'aggregate'
140-
self.assertEqual(ini_val, bandit._log_option_source(None, ini_val,
141-
option_name))
147+
self.assertEqual(ini_val, bandit._log_option_source(
148+
default_val,
149+
None,
150+
ini_val,
151+
option_name
152+
))
153+
154+
def test_log_option_source_ini_val_with_str_default_and_no_arg_val(self):
155+
# Test that the ini value is returned when no command argument is
156+
# provided
157+
default_val = "file"
158+
arg_val = 'file'
159+
ini_val = 'vuln'
160+
option_name = 'aggregate'
161+
self.assertEqual(ini_val, bandit._log_option_source(
162+
default_val,
163+
arg_val,
164+
ini_val,
165+
option_name
166+
))
142167

143168
def test_log_option_source_no_values(self):
144169
# Test that None is returned when no command argument or ini value are
145170
# provided
146171
option_name = 'aggregate'
147-
self.assertIsNone(bandit._log_option_source(None, None, option_name))
172+
self.assertIsNone(bandit._log_option_source(
173+
None,
174+
None,
175+
None,
176+
option_name
177+
))
148178

149179
@mock.patch('sys.argv', ['bandit', '-c', 'bandit.yaml', 'test'])
150180
def test_main_config_unopenable(self):

0 commit comments

Comments
 (0)