Skip to content

Commit

Permalink
Merge pull request #444 from Yelp/bug-fix/clear-filter-caches
Browse files Browse the repository at this point in the history
bug fix: clearing filter caches upon loading new settings
  • Loading branch information
domanchi committed Apr 9, 2021
2 parents 194f4c1 + 8a5fab4 commit a7dfa01
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
34 changes: 32 additions & 2 deletions detect_secrets/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,40 @@ def transient_settings(config: Dict[str, Any]) -> Generator['Settings', None, No


def cache_bust() -> None:
get_settings.cache_clear()
get_filters.cache_clear()
get_plugins.cache_clear()

get_filters.cache_clear()
for path, config in get_settings().filters.items():
# Need to also clear the individual caches (e.g. cached regex patterns).
parts = urlparse(path)
if not parts.scheme:
module_path, _ = path.rsplit('.', 1)
try:
module = import_module(module_path)
except ModuleNotFoundError:
continue
elif parts.scheme == 'file':
file_path = path[len('file://'):].split('::')[0]
try:
module = import_file_as_module(file_path)
except (FileNotFoundError, InvalidFile):
continue
else:
continue

for item_key in dir(module):
item = getattr(module, item_key)
try:
if item.__module__ != module_path:
# Make sure we only clear the cache specific to the module.
raise AttributeError

item.cache_clear()
except AttributeError:
pass

get_settings.cache_clear()


class Settings:
DEFAULT_FILTERS = {
Expand Down
16 changes: 16 additions & 0 deletions tests/filters/regex_filter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from detect_secrets import filters
from detect_secrets.core.usage import ParserBuilder
from detect_secrets.settings import default_settings
from detect_secrets.settings import get_settings


Expand Down Expand Up @@ -86,3 +87,18 @@ def test_should_exclude_secret(parser):
],
},
]


def test_cache_should_be_cleared_with_different_settings(parser):
with default_settings():
parser.parse_args([
'--exclude-lines', 'abcde',
])

assert filters.regex.should_exclude_line('abcde') is True

# Since the regex isn't cached anymore, it needs to be regenerated. However,
# we didn't configure the regex in the settings object, so it will raise a KeyError
# when trying to obtain the patterns.
with pytest.raises(KeyError):
assert filters.regex.should_exclude_line('abcde')

0 comments on commit a7dfa01

Please sign in to comment.