diff --git a/.gitignore b/.gitignore index b8bd4fab2..3b56902d6 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,7 @@ /.coverage /.pytest_cache /.tox -/venv +/venv** /tmp .*ignore diff --git a/detect_secrets/core/audit.py b/detect_secrets/core/audit.py index e79b33880..f45cedeba 100644 --- a/detect_secrets/core/audit.py +++ b/detect_secrets/core/audit.py @@ -216,6 +216,7 @@ def _save_baseline_to_file(filename, data): # pragma: no cover data, indent=2, sort_keys=True, + separators=(',', ': '), )) diff --git a/detect_secrets/main.py b/detect_secrets/main.py index 1010a34bd..64e76513b 100644 --- a/detect_secrets/main.py +++ b/detect_secrets/main.py @@ -42,6 +42,7 @@ def main(argv=None): _perform_scan(args, plugins), indent=2, sort_keys=True, + separators=(',', ': '), ) if args.import_filename: @@ -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, diff --git a/detect_secrets/pre_commit_hook.py b/detect_secrets/pre_commit_hook.py index b8642806d..db77db99d 100644 --- a/detect_secrets/pre_commit_hook.py +++ b/detect_secrets/pre_commit_hook.py @@ -77,6 +77,7 @@ def _write_to_baseline_file(filename, payload): # pragma: no cover payload, indent=2, sort_keys=True, + separators=(',', ': '), ), ) diff --git a/tests/main_test.py b/tests/main_test.py index 8f936a4c6..c66ac0163 100644 --- a/tests/main_test.py +++ b/tests/main_test.py @@ -1,4 +1,5 @@ import json +import shlex import textwrap from contextlib import contextmanager @@ -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 @@ -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( @@ -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', [