Skip to content

Commit

Permalink
Merge pull request #78 from Yelp/nicer-json-output
Browse files Browse the repository at this point in the history
Nicer JSON output + exclude regex bug fix
  • Loading branch information
domanchi committed Sep 28, 2018
2 parents 1198523 + 0a6f767 commit 9445982
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/.coverage
/.pytest_cache
/.tox
/venv
/venv**
/tmp

.*ignore
Expand Down
1 change: 1 addition & 0 deletions detect_secrets/core/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ def _save_baseline_to_file(filename, data): # pragma: no cover
data,
indent=2,
sort_keys=True,
separators=(',', ': '),
))


Expand Down
9 changes: 7 additions & 2 deletions detect_secrets/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def main(argv=None):
_perform_scan(args, plugins),
indent=2,
sort_keys=True,
separators=(',', ': '),
)

if args.import_filename:
Expand Down Expand Up @@ -85,8 +86,12 @@ def _perform_scan(args, plugins):

# If we have knowledge of an existing baseline file, we should use
# that knowledge and *not* scan that file.
if args.import_filename and args.exclude:
args.exclude += r'|^{}$'.format(args.import_filename[0])
if args.import_filename:
payload = '^{}$'.format(args.import_filename[0])
if args.exclude and payload not in args.exclude:
args.exclude += r'|{}'.format(payload)
elif not args.exclude:
args.exclude = payload

new_baseline = baseline.initialize(
plugins,
Expand Down
1 change: 1 addition & 0 deletions detect_secrets/pre_commit_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def _write_to_baseline_file(filename, payload): # pragma: no cover
payload,
indent=2,
sort_keys=True,
separators=(',', ': '),
),
)

Expand Down
26 changes: 20 additions & 6 deletions tests/main_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import shlex
import textwrap
from contextlib import contextmanager

Expand All @@ -16,11 +17,15 @@

@pytest.fixture
def mock_baseline_initialize():
secrets = secrets_collection_factory()
def mock_initialize_function(plugins, exclude_regex, *args, **kwargs):
return secrets_collection_factory(
plugins=plugins,
exclude_regex=exclude_regex,
)

with mock.patch(
'detect_secrets.main.baseline.initialize',
return_value=secrets,
side_effect=mock_initialize_function,
) as mock_initialize:
yield mock_initialize

Expand Down Expand Up @@ -154,6 +159,10 @@ def test_reads_old_baseline_from_file(self, mock_merge_baseline):
'--exclude "secrets/.*"',
'secrets/.*|^old_baseline_file$',
),
(
'--exclude "^old_baseline_file$"',
'^old_baseline_file$',
),
],
)
def test_old_baseline_ignored_with_update_flag(
Expand All @@ -168,13 +177,18 @@ def test_old_baseline_ignored_with_update_flag(
), mock.patch(
# We don't want to be creating a file during test
'detect_secrets.main._write_to_file',
):
) as file_writer:
assert main(
'scan --update old_baseline_file {}'.format(
exclude_param,
).split(),
shlex.split(
'scan --update old_baseline_file {}'.format(
exclude_param,
),
),
) == 0

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

@pytest.mark.parametrize(
'filename, expected_output',
[
Expand Down

0 comments on commit 9445982

Please sign in to comment.