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

🐛 Fix format_baseline_for_output TypeError #116

Merged
merged 1 commit into from
Jan 7, 2019
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
4 changes: 2 additions & 2 deletions detect_secrets/core/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def audit_baseline(baseline_filename):
dict(results),
)
write_baseline_to_file(
baseline_filename,
original_baseline,
filename=baseline_filename,
data=original_baseline,
)


Expand Down
2 changes: 2 additions & 0 deletions detect_secrets/core/baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ def merge_baseline(old_baseline, new_baseline):

:type new_baseline: dict
:param new_baseline: most recent scan

:rtype: dict
"""
new_baseline['results'] = merge_results(
old_baseline['results'],
Expand Down
7 changes: 6 additions & 1 deletion detect_secrets/core/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from .baseline import format_baseline_for_output


def write_baseline_to_file(filename, data): # pragma: no cover
def write_baseline_to_file(filename, data):
"""
:type filename: str
:type data: dict
:rtype: None
"""
with open(filename, 'w') as f:
f.write(format_baseline_for_output(data) + '\n')
21 changes: 16 additions & 5 deletions detect_secrets/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,22 @@ def main(argv=None):
_scan_string(line, plugins)

else:
output = baseline.format_baseline_for_output(
_perform_scan(args, plugins),
baseline_dict = _perform_scan(
args,
plugins,
)

if args.import_filename:
write_baseline_to_file(
args.import_filename[0],
output,
filename=args.import_filename[0],
data=baseline_dict,
)
else:
print(output)
print(
baseline.format_baseline_for_output(
baseline_dict,
),
)

elif args.action == 'audit':
if not args.diff:
Expand Down Expand Up @@ -94,6 +99,12 @@ def _scan_string(line, plugins):


def _perform_scan(args, plugins):
"""
:param args: output of `argparse.ArgumentParser.parse_args`
:param plugins: tuple of initialized plugins

:rtype: dict
"""
old_baseline = _get_existing_baseline(args.import_filename)

# Favors --exclude argument over existing baseline's regex (if exists)
Expand Down
4 changes: 2 additions & 2 deletions detect_secrets/pre_commit_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def main(argv=None):

if baseline_modified:
write_baseline_to_file(
args.baseline[0],
baseline_collection.format_for_baseline_output(),
filename=args.baseline[0],
data=baseline_collection.format_for_baseline_output(),
KevinHock marked this conversation as resolved.
Show resolved Hide resolved
)

log.error(
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ipdb
coverage
flake8==3.5.0
mock
Expand Down
44 changes: 33 additions & 11 deletions tests/core/audit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_quit_before_making_decision(self, mock_printer):
with self.mock_env(['q']) as m:
audit.audit_baseline('will_be_mocked')

assert m.call_args[0][1] == self.baseline
assert m.call_args[1]['data'] == self.baseline

assert mock_printer.message == (
'Quitting...\n'
Expand Down Expand Up @@ -56,7 +56,10 @@ def test_making_decisions(self, mock_printer):
for secret in secrets:
secret['is_secret'] = values_to_inject.pop(0)

self.run_logic(['y', 'n', 'n'], modified_baseline)
self.run_logic(
inputs=['y', 'n', 'n'],
modified_baseline=modified_baseline,
)

assert mock_printer.message == (
'Saving progress...\n'
Expand All @@ -69,7 +72,10 @@ def test_quit_half_way(self, mock_printer):
secrets[0]['is_secret'] = False
break

self.run_logic(['n', 'q'], modified_baseline)
self.run_logic(
inputs=['n', 'q'],
modified_baseline=modified_baseline,
)

assert mock_printer.message == (
'Quitting...\n'
Expand All @@ -86,7 +92,10 @@ def test_skip_decision(self, mock_printer):
if value:
secret['is_secret'] = value

self.run_logic(['s', 'y', 'y'], modified_baseline)
self.run_logic(
inputs=['s', 'y', 'y'],
modified_baseline=modified_baseline,
)

assert mock_printer.message == (
'Saving progress...\n'
Expand All @@ -102,7 +111,10 @@ def test_go_back_and_change_yes_to_no(self, mock_printer):
if value is not None:
secret['is_secret'] = value

self.run_logic(['s', 'y', 'b', 'n', 'y'], modified_baseline)
self.run_logic(
inputs=['s', 'y', 'b', 'n', 'y'],
modified_baseline=modified_baseline,
)

assert mock_printer.message == (
'Saving progress...\n'
Expand All @@ -118,7 +130,10 @@ def test_go_back_and_change_no_to_yes(self, mock_printer):
if value is not None:
secret['is_secret'] = value

self.run_logic(['s', 'n', 'b', 'y', 'y'], modified_baseline)
self.run_logic(
inputs=['s', 'n', 'b', 'y', 'y'],
modified_baseline=modified_baseline,
)

assert mock_printer.message == (
'Saving progress...\n'
Expand All @@ -134,7 +149,10 @@ def test_go_back_and_change_yes_to_skip(self, mock_printer):
if value is not None:
secret['is_secret'] = value

self.run_logic(['s', 'y', 'b', 's', 'y'], modified_baseline)
self.run_logic(
inputs=['s', 'y', 'b', 's', 'y'],
modified_baseline=modified_baseline,
)

assert mock_printer.message == (
'Saving progress...\n'
Expand All @@ -150,8 +168,8 @@ def test_go_back_several_steps(self, mock_printer):
secret['is_secret'] = value

self.run_logic(
['s', 'y', 'b', 's', 'b', 'b', 'n', 'n', 'n'],
modified_baseline,
inputs=['s', 'y', 'b', 's', 'b', 'b', 'n', 'n', 'n'],
modified_baseline=modified_baseline,
)

assert mock_printer.message == (
Expand All @@ -163,7 +181,11 @@ def test_leapfrog_decision(self, mock_printer):
modified_baseline['results']['filenameA'][1]['is_secret'] = True
modified_baseline['results']['filenameA'][3]['is_secret'] = True

self.run_logic(['y', 'y'], modified_baseline, self.leapfrog_baseline)
self.run_logic(
inputs=['y', 'y'],
modified_baseline=modified_baseline,
input_baseline=self.leapfrog_baseline,
)

@contextmanager
def run_logic(self, inputs, modified_baseline=None, input_baseline=None):
Expand All @@ -173,7 +195,7 @@ def run_logic(self, inputs, modified_baseline=None, input_baseline=None):
) as m:
audit.audit_baseline('will_be_mocked')

assert m.call_args[0][1] == modified_baseline
assert m.call_args[1]['data'] == modified_baseline

@contextmanager
def mock_env(self, user_inputs=None, baseline=None):
Expand Down
7 changes: 5 additions & 2 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def test_reads_old_baseline_from_file(self, mock_merge_baseline):
) as m_write:
assert main('scan --update old_baseline_file'.split()) == 0
assert m_read.call_args[0][0] == 'old_baseline_file'
assert m_write.call_args[0] == ('old_baseline_file', Any(str))
assert m_write.call_args[1]['filename'] == 'old_baseline_file'
assert m_write.call_args[1]['data'] == Any(dict)

mock_merge_baseline.assert_called_once_with(
{'key': 'value'},
Expand Down Expand Up @@ -161,8 +162,10 @@ def test_old_baseline_ignored_with_update_flag(
),
) == 0

assert json.loads(file_writer.call_args[0][1])['exclude_regex'] == \
assert (
file_writer.call_args[1]['data']['exclude_regex'] ==
expected_regex
)

@pytest.mark.parametrize(
'filename, expected_output',
Expand Down
4 changes: 2 additions & 2 deletions tests/pre_commit_hook_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_that_baseline_gets_updated(
'--baseline will_be_mocked test_data/files/file_with_secrets.py',
)

baseline_written = m.call_args[0][1]
baseline_written = m.call_args[1]['data']

original_baseline = json.loads(baseline_string)
assert original_baseline['exclude_regex'] == baseline_written['exclude_regex']
Expand Down Expand Up @@ -168,7 +168,7 @@ def test_writes_new_baseline_if_modified(self):
'--baseline will_be_mocked test_data/files/file_with_secrets.py',
)

baseline_written = m.call_args[0][1]
baseline_written = m.call_args[1]['data']

original_baseline = json.loads(baseline_string)
assert original_baseline['exclude_regex'] == baseline_written['exclude_regex']
Expand Down