Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements-dev-minimal.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
coverage
flake8==7.1.1
mockey
gibberish-detector>=0.1.1
monotonic
mypy
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mockey
attrs==23.2.0
backports.entry-points-selectable==1.3.0
certifi==2024.7.4
Expand Down
6 changes: 2 additions & 4 deletions testing/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,8 @@ def disable_gibberish_filter() -> Iterator[None]:

However, since this only happens in test environments, we can just mock it out.
"""
with mock.patch(
'detect_secrets.filters.gibberish.is_feature_enabled',
return_value=False,
):
with mock.patch('detect_secrets.filters.gibberish.is_feature_enabled') as m:
m.return_value = False
yield


Expand Down
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from mockey.fixture import patch_mock_module
patch_mock_module()
9 changes: 4 additions & 5 deletions tests/audit/audit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_ensure_file_transformers_are_used(printer):
assert lines[line_number - 1] in printer.message


def test_fails_if_no_line_numbers_found(printer):
def test_fails_if_no_line_numbers_found(printer, prevent_clear_screen):
with transient_settings({
'plugins_used': [
{'name': 'Base64HighEntropyString'},
Expand All @@ -152,9 +152,8 @@ def test_fails_if_no_line_numbers_found(printer):
# Remove line numbers
secrets = baseline.load(baseline.format_for_output(secrets, is_slim_mode=True))

with mock.patch('detect_secrets.audit.io.clear_screen') as m:
run_logic(secrets)
assert not m.called
run_logic(secrets)
assert not prevent_clear_screen.called

assert 'No line numbers found in baseline' in printer.message

Expand All @@ -171,7 +170,7 @@ def run_logic(
baseline.save_to_file(secrets, f.name)
f.seek(0)

with mock.patch('detect_secrets.audit.io.input') as m:
with mock.patch('detect_secrets.audit.io.input', create=True) as m:
if input is not None:
m.side_effect = list(input) + ['q']

Expand Down
13 changes: 11 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest
import responses
from mockey.fixture import MockAutospecFixture

import detect_secrets
from detect_secrets import filters
Expand All @@ -13,6 +14,14 @@
from testing.mocks import MockLogWrapper


@pytest.fixture(autouse=True)
def mock_autospec_fixture():
fixture = MockAutospecFixture()
fixture.setUp()
yield
fixture.cleanUp()


@pytest.fixture(autouse=True)
def clear_cache():
# This is also probably too aggressive, but test pollution is tough to debug.
Expand Down Expand Up @@ -82,5 +91,5 @@ def mocked_requests():

@pytest.fixture(autouse=True)
def prevent_clear_screen():
with mock.patch('detect_secrets.audit.io.clear_screen'):
yield
with mock.patch('detect_secrets.audit.io.clear_screen') as mock_clear_screen:
yield mock_clear_screen
1 change: 1 addition & 0 deletions tests/core/secrets_collection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def test_error_reading_file(mock_log_warning):
with mock.patch(
'detect_secrets.core.scan.open',
side_effect=IOError,
create=True,
):
SecretsCollection().scan_file('test_data/config.env')

Expand Down
14 changes: 5 additions & 9 deletions tests/plugins/aws_key_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class TestAWSKeyDetector:

def setup(self):
def setup_method(self):
self.example_key = 'AKIAZZZZZZZZZZZZZZZZ'

@pytest.mark.parametrize(
Expand Down Expand Up @@ -82,20 +82,16 @@ def test_verify_no_secret(self):
) == VerifiedResult.UNVERIFIED

def test_verify_valid_secret(self):
with mock.patch(
'detect_secrets.plugins.aws.verify_aws_secret_access_key',
return_value=True,
):
with mock.patch('detect_secrets.plugins.aws.verify_aws_secret_access_key') as m:
m.return_value = True
assert AWSKeyDetector().verify(
self.example_key,
get_code_snippet(['={}'.format(EXAMPLE_SECRET)], 1),
) == VerifiedResult.VERIFIED_TRUE

def test_verify_invalid_secret(self):
with mock.patch(
'detect_secrets.plugins.aws.verify_aws_secret_access_key',
return_value=False,
):
with mock.patch('detect_secrets.plugins.aws.verify_aws_secret_access_key') as m:
m.return_value = False
assert AWSKeyDetector().verify(
self.example_key,
get_code_snippet(['={}'.format(EXAMPLE_SECRET)], 1),
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def verify(self, secret: str, context: CodeSnippet):


class TestAnalyzeLine():
def setup(self):
def setup_method(self):
self.line = 'some-secret'
self.filename = 'secrets.py'
self.context = get_code_snippet(lines=[self.line], line_number=1)
Expand Down