diff --git a/requirements-dev-minimal.txt b/requirements-dev-minimal.txt index 28cd8eec7..2afc37d51 100644 --- a/requirements-dev-minimal.txt +++ b/requirements-dev-minimal.txt @@ -1,5 +1,6 @@ coverage flake8==7.1.1 +mockey gibberish-detector>=0.1.1 monotonic mypy diff --git a/requirements-dev.txt b/requirements-dev.txt index 5f27fd029..c14115f9e 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,3 +1,4 @@ +mockey attrs==23.2.0 backports.entry-points-selectable==1.3.0 certifi==2024.7.4 diff --git a/testing/mocks.py b/testing/mocks.py index a85325651..32d103284 100644 --- a/testing/mocks.py +++ b/testing/mocks.py @@ -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 diff --git a/tests/__init__.py b/tests/__init__.py index e69de29bb..95e96744f 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1,2 @@ +from mockey.fixture import patch_mock_module +patch_mock_module() diff --git a/tests/audit/audit_test.py b/tests/audit/audit_test.py index 5de9d0636..c74666a97 100644 --- a/tests/audit/audit_test.py +++ b/tests/audit/audit_test.py @@ -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'}, @@ -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 @@ -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'] diff --git a/tests/conftest.py b/tests/conftest.py index 9535d33e8..58637064e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,6 +4,7 @@ import pytest import responses +from mockey.fixture import MockAutospecFixture import detect_secrets from detect_secrets import filters @@ -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. @@ -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 diff --git a/tests/core/secrets_collection_test.py b/tests/core/secrets_collection_test.py index 8e7b9a6b7..e7ce11ee3 100644 --- a/tests/core/secrets_collection_test.py +++ b/tests/core/secrets_collection_test.py @@ -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') diff --git a/tests/plugins/aws_key_test.py b/tests/plugins/aws_key_test.py index 166269aab..0735c71fa 100644 --- a/tests/plugins/aws_key_test.py +++ b/tests/plugins/aws_key_test.py @@ -14,7 +14,7 @@ class TestAWSKeyDetector: - def setup(self): + def setup_method(self): self.example_key = 'AKIAZZZZZZZZZZZZZZZZ' @pytest.mark.parametrize( @@ -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), diff --git a/tests/plugins/base_test.py b/tests/plugins/base_test.py index 518cb3346..d8509d80c 100644 --- a/tests/plugins/base_test.py +++ b/tests/plugins/base_test.py @@ -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)