Skip to content

Commit

Permalink
fix: tests for plugins reuse excludes CLI arg
Browse files Browse the repository at this point in the history
Signed-off-by: George Pickering <[email protected]>
  • Loading branch information
bigpick committed Aug 23, 2024
1 parent 7d0d6af commit 42d581c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
8 changes: 4 additions & 4 deletions detect_secrets/core/baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions detect_secrets/core/secrets_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions detect_secrets/core/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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()\
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 6 additions & 5 deletions detect_secrets/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pre-commit
pytest
pyyaml
responses
tox-pip-extensions
tox>=3.8
unidiff
ibm_db; platform_machine == 'x86_64'
Expand Down
10 changes: 5 additions & 5 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='.',
Expand All @@ -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'],
Expand All @@ -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='.',
Expand Down Expand Up @@ -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='.',
Expand Down Expand Up @@ -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='.',
Expand Down

0 comments on commit 42d581c

Please sign in to comment.