From 42d581c35b8d387dd7da50e60ad18e62b6cb4864 Mon Sep 17 00:00:00 2001 From: George Pickering <9803299+bigpick@users.noreply.github.com> Date: Fri, 23 Aug 2024 11:29:56 -0400 Subject: [PATCH] fix: tests for plugins reuse excludes CLI arg Signed-off-by: George Pickering <9803299+bigpick@users.noreply.github.com> --- detect_secrets/core/baseline.py | 8 ++++---- detect_secrets/core/secrets_collection.py | 6 +++--- detect_secrets/core/usage.py | 10 +++++----- detect_secrets/main.py | 11 ++++++----- requirements-dev.txt | 1 - tests/main_test.py | 10 +++++----- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/detect_secrets/core/baseline.py b/detect_secrets/core/baseline.py index fea77c2ee..0c688cc1c 100644 --- a/detect_secrets/core/baseline.py +++ b/detect_secrets/core/baseline.py @@ -16,7 +16,7 @@ def initialize( path, plugins, - plugins_reuse_exclude=None, + plugins_reuse_excludes=None, exclude_files_regex=None, exclude_lines_regex=None, word_list_file=None, @@ -34,8 +34,8 @@ def initialize( :type plugins: tuple of detect_secrets.plugins.base.BasePlugin :param plugins: rules to initialize the SecretsCollection with. - :type plugins_reuse_exclude: bool|None - :param plugins_reuse_exclude optional bool indicating whether plugins were forced to reuse excludes. + :type plugins_reuse_excludes: bool|None + :param plugins_reuse_excludes optional bool indicating whether plugins were forced to reuse excludes. :type exclude_files_regex: str|None :type exclude_lines_regex: str|None @@ -57,7 +57,7 @@ def initialize( """ output = SecretsCollection( plugins, - plugins_reuse_exclude=plugins_reuse_exclude, + plugins_reuse_excludes=plugins_reuse_excludes, exclude_files=exclude_files_regex, exclude_lines=exclude_lines_regex, word_list_file=word_list_file, diff --git a/detect_secrets/core/secrets_collection.py b/detect_secrets/core/secrets_collection.py index e03ef1726..e488d3f43 100644 --- a/detect_secrets/core/secrets_collection.py +++ b/detect_secrets/core/secrets_collection.py @@ -21,7 +21,7 @@ class SecretsCollection: def __init__( self, plugins=(), - plugins_reuse_exclude=None, + plugins_reuse_excludes=None, exclude_files=None, exclude_lines=None, word_list_file=None, @@ -47,7 +47,7 @@ def __init__( """ self.data = {} self.plugins = plugins - self.plugins_reuse_exclude = plugins_reuse_exclude + self.plugins_reuse_excludes = plugins_reuse_excludes self.exclude_files = exclude_files self.exclude_lines = exclude_lines self.word_list_file = word_list_file @@ -344,7 +344,7 @@ def format_for_baseline_output(self): plugins_used = sorted(plugins_used, key=lambda x: x['name']) return { - **({"plugins_reuse_excludes": True} if self.plugins_reuse_exclude else {}), + **({"plugins_reuse_excludes": True} if self.plugins_reuse_excludes else {}), 'generated_at': strftime('%Y-%m-%dT%H:%M:%SZ', gmtime()), 'exclude': { 'files': self.exclude_files, diff --git a/detect_secrets/core/usage.py b/detect_secrets/core/usage.py index c174a6d81..f27e4ba28 100644 --- a/detect_secrets/core/usage.py +++ b/detect_secrets/core/usage.py @@ -5,7 +5,7 @@ from detect_secrets.constants import DEFAULT_GHE_INSTANCE -def add_plugins_reuse_exclude_flag(parser): +def add_plugins_reuse_excludes_flag(parser): parser.add_argument( '--plugins-reuse-excludes', action='store_true', @@ -97,7 +97,7 @@ def add_default_arguments(self): def add_pre_commit_arguments(self): self._add_filenames_argument()\ - ._add_plugins_reuse_exclude_flag()\ + ._add_plugins_reuse_excludes_flag()\ ._add_set_baseline_argument()\ ._add_exclude_lines_argument()\ ._add_word_list_argument()\ @@ -163,8 +163,8 @@ def _add_set_baseline_argument(self): ) return self - def _add_plugins_reuse_exclude_flag(self): - add_plugins_reuse_exclude_flag(self.parser) + def _add_plugins_reuse_excludes_flag(self): + add_plugins_reuse_excludes_flag(self.parser) return self def _add_exclude_lines_argument(self): @@ -237,7 +237,7 @@ def _add_initialize_baseline_argument(self): # Pairing `--plugins-reuse-excludes` to # both pre-commit and `--scan` because it can be used for both. - add_plugins_reuse_exclude_flag(self.parser) + add_plugins_reuse_excludes_flag(self.parser) # Pairing `--exclude-lines` and `--word-list` to # both pre-commit and `--scan` because it can be used for both. diff --git a/detect_secrets/main.py b/detect_secrets/main.py index 67354044f..54978a004 100644 --- a/detect_secrets/main.py +++ b/detect_secrets/main.py @@ -49,7 +49,7 @@ def main(argv=None): if args.word_list_file: automaton, word_list_hash = build_automaton(args.word_list_file) - _baseline = _get_existing_baseline(args.import_filename) + _baseline = _get_existing_baseline(args.import_filename, args.string) if args.plugins_reuse_excludes or (_baseline and _baseline.get("plugins_reuse_excludes", False)): args.exclude_files, args.exclude_lines = maybe_get_existing_exclude(args.exclude_files, args.exclude_lines, _baseline) @@ -170,7 +170,7 @@ def _perform_scan(args, plugins, automaton, word_list_hash): :rtype: dict """ - old_baseline = _get_existing_baseline(args.import_filename) + old_baseline = _get_existing_baseline(args.import_filename, args.string) if old_baseline: plugins = initialize.merge_plugins_from_baseline( _get_plugins_from_baseline(old_baseline, tuple(args.plugin_filenames)), @@ -200,7 +200,7 @@ def _perform_scan(args, plugins, automaton, word_list_hash): new_baseline = baseline.initialize( plugins=plugins, - plugins_reuse_exclude=args.plugins_reuse_excludes, + plugins_reuse_excludes=args.plugins_reuse_excludes, exclude_files_regex=args.exclude_files, exclude_lines_regex=args.exclude_lines, word_list_file=args.word_list_file, @@ -221,7 +221,7 @@ def _perform_scan(args, plugins, automaton, word_list_hash): return new_baseline -def _get_existing_baseline(import_filename): +def _get_existing_baseline(import_filename, args_string): # Favors --update argument over stdin. if import_filename: try: @@ -235,7 +235,8 @@ def _get_existing_baseline(import_filename): file=sys.stderr, ) raise fnf_error - if not sys.stdin.isatty(): + + if not sys.stdin.isatty() and not args_string: stdin = sys.stdin.read().strip() if stdin: return json.loads(stdin) diff --git a/requirements-dev.txt b/requirements-dev.txt index 1e84c2f71..3b8f65cc0 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -9,7 +9,6 @@ pre-commit pytest pyyaml responses -tox-pip-extensions tox>=3.8 unidiff ibm_db; platform_machine == 'x86_64' diff --git a/tests/main_test.py b/tests/main_test.py index a20ef1ddc..1bf4c4aa6 100644 --- a/tests/main_test.py +++ b/tests/main_test.py @@ -88,7 +88,7 @@ def test_scan_basic(self, mock_baseline_initialize): mock_baseline_initialize.assert_called_once_with( plugins=Any(tuple), - plugins_reuse_excludes=None, + plugins_reuse_excludes=False, exclude_files_regex=None, exclude_lines_regex=None, path='.', @@ -106,7 +106,7 @@ def test_scan_with_rootdir(self, mock_baseline_initialize): mock_baseline_initialize.assert_called_once_with( plugins=Any(tuple), - plugins_reuse_excludes=None, + plugins_reuse_excludes=False, exclude_files_regex=None, exclude_lines_regex=None, path=['test_data'], @@ -126,7 +126,7 @@ def test_scan_with_exclude_args(self, mock_baseline_initialize): mock_baseline_initialize.assert_called_once_with( plugins=Any(tuple), - plugins_reuse_excludes=None, + plugins_reuse_excludes=False, exclude_files_regex='some_pattern_here', exclude_lines_regex='other_patt', path='.', @@ -216,7 +216,7 @@ def test_scan_with_all_files_flag(self, mock_baseline_initialize): mock_baseline_initialize.assert_called_once_with( plugins=Any(tuple), - plugins_reuse_excludes=None, + plugins_reuse_excludes=False, exclude_files_regex=None, exclude_lines_regex=None, path='.', @@ -274,7 +274,7 @@ def test_reads_non_existed_baseline_from_file( mock_baseline_initialize.assert_called_once_with( plugins=Any(tuple), - plugins_reuse_excludes=None, + plugins_reuse_excludes=False, exclude_files_regex='^non_existed_baseline_file$', exclude_lines_regex=None, path='.',