Skip to content

Commit

Permalink
🐍 Improve test coverage
Browse files Browse the repository at this point in the history
Trim uncovered code
Change tox to ensure tests are covered 100%
  • Loading branch information
KevinHock committed Dec 14, 2018
1 parent d8f4e29 commit ed6a374
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 35 deletions.
4 changes: 1 addition & 3 deletions detect_secrets/plugins/keyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ def secret_generator(self, string, is_php_file):
if match:
lowered_secret = match.group(group_number)

if not lowered_secret:
continue

# ([^\s]+) guarantees lowered_secret is not ''
if not probably_false_positive(lowered_secret, is_php_file):
yield lowered_secret

Expand Down
10 changes: 3 additions & 7 deletions tests/core/audit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_making_decisions(self, mock_printer):
def test_quit_half_way(self, mock_printer):
modified_baseline = deepcopy(self.baseline)

for secrets in modified_baseline['results'].values():
for secrets in modified_baseline['results'].values(): # pragma: no cover
secrets[0]['is_secret'] = False
break

Expand Down Expand Up @@ -147,8 +147,7 @@ def test_go_back_several_steps(self, mock_printer):
for secrets in modified_baseline['results'].values():
for secret in secrets:
value = values_to_inject.pop(0)
if value is not None:
secret['is_secret'] = value
secret['is_secret'] = value

self.run_logic(
['s', 'y', 'b', 's', 'b', 'b', 'n', 'n', 'n'],
Expand All @@ -174,10 +173,7 @@ def run_logic(self, inputs, modified_baseline=None, input_baseline=None):
) as m:
audit.audit_baseline('will_be_mocked')

if not modified_baseline:
assert m.call_args[0][1] == self.baseline
else:
assert m.call_args[0][1] == modified_baseline
assert m.call_args[0][1] == modified_baseline

@contextmanager
def mock_env(self, user_inputs=None, baseline=None):
Expand Down
2 changes: 1 addition & 1 deletion tests/core/secrets_collection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def assert_loaded_collection_is_original_collection(self, original, new):
assert original[key] == new[key]


class MockBasePlugin(BasePlugin):
class MockBasePlugin(BasePlugin): # pragma: no cover
"""Abstract testing class, to implement abstract methods."""

def analyze_string(self, value):
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 @@ -6,7 +6,7 @@


def test_fails_if_no_secret_type_defined():
class MockPlugin(BasePlugin):
class MockPlugin(BasePlugin): # pragma: no cover
def analyze_string(self, *args, **kwargs):
pass

Expand Down
44 changes: 22 additions & 22 deletions tests/plugins/keyword_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,69 +15,69 @@ class TestKeywordDetector(object):
[
# FOLLOWED_BY_COLON_RE
(
"'theapikey': 'hopenobodyfinds>-_$#thisone'"
"'theapikey': 'hope]nobody[finds>-_$#thisone'"
),
(
'"theapikey": "hopenobodyfinds>-_$#thisone"'
'"theapikey": "hope]nobody[finds>-_$#thisone"'
),
(
'apikey: hopenobodyfinds>-_$#thisone'
'apikey: hope]nobody[finds>-_$#thisone'
),
(
'apikey:hopenobodyfinds>-_$#thisone'
'apikey:hope]nobody[finds>-_$#thisone'
),
(
'theapikey:hopenobodyfinds>-_$#thisone'
'theapikey:hope]nobody[finds>-_$#thisone'
),
(
'apikey: "hopenobodyfinds>-_$#thisone"'
'apikey: "hope]nobody[finds>-_$#thisone"'
),
(
"apikey: 'hopenobodyfinds>-_$#thisone'"
"apikey: 'hope]nobody[finds>-_$#thisone'"
),
# FOLLOWED_BY_EQUAL_SIGNS_RE
(
'my_password=hopenobodyfinds>-_$#thisone'
'my_password=hope]nobody[finds>-_$#thisone'
),
(
'my_password= hopenobodyfinds>-_$#thisone'
'my_password= hope]nobody[finds>-_$#thisone'
),
(
'my_password =hopenobodyfinds>-_$#thisone'
'my_password =hope]nobody[finds>-_$#thisone'
),
(
'my_password_for_stuff = hopenobodyfinds>-_$#thisone'
'my_password_for_stuff = hope]nobody[finds>-_$#thisone'
),
(
'my_password_for_stuff =hopenobodyfinds>-_$#thisone'
'my_password_for_stuff =hope]nobody[finds>-_$#thisone'
),
(
'passwordone=hopenobodyfinds>-_$#thisone\n'
'passwordone=hope]nobody[finds>-_$#thisone\n'
),
(
'passwordone= "hopenobodyfinds>-_$#thisone"\n'
'passwordone= "hope]nobody[finds>-_$#thisone"\n'
),
(
'passwordone=\'hopenobodyfinds>-_$#thisone\'\n'
'passwordone=\'hope]nobody[finds>-_$#thisone\'\n'
),
# FOLLOWED_BY_QUOTES_AND_SEMICOLON_RE
(
'apikey "hopenobodyfinds>-_$#thisone";' # Double-quotes
'apikey "hope]nobody[finds>-_$#thisone";' # Double-quotes
),
(
'fooapikeyfoo "hopenobodyfinds>-_$#thisone";' # Double-quotes
'fooapikeyfoo "hope]nobody[finds>-_$#thisone";' # Double-quotes
),
(
'fooapikeyfoo"hopenobodyfinds>-_$#thisone";' # Double-quotes
'fooapikeyfoo"hope]nobody[finds>-_$#thisone";' # Double-quotes
),
(
'private_key \'hopenobodyfinds>-_$#thisone\';' # Single-quotes
'private_key \'hope]nobody[finds>-_$#thisone\';' # Single-quotes
),
(
'fooprivate_keyfoo\'hopenobodyfinds>-_$#thisone\';' # Single-quotes
'fooprivate_keyfoo\'hope]nobody[finds>-_$#thisone\';' # Single-quotes
),
(
'fooprivate_key\'hopenobodyfinds>-_$#thisone\';' # Single-quotes
'fooprivate_key\'hope]nobody[finds>-_$#thisone\';' # Single-quotes
),
],
)
Expand All @@ -91,7 +91,7 @@ def test_analyze_positives(self, file_content):
assert 'mock_filename' == potential_secret.filename
assert (
potential_secret.secret_hash
== PotentialSecret.hash_secret('hopenobodyfinds>-_$#thisone')
== PotentialSecret.hash_secret('hope]nobody[finds>-_$#thisone')
)

@pytest.mark.parametrize(
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ deps = -rrequirements-dev.txt
commands =
coverage erase
coverage run -m pytest tests
coverage report --show-missing --fail-under 98
coverage report --show-missing --include=tests/* --fail-under 100
coverage report --show-missing --include=detect_secrets/* --fail-under 96
pre-commit run --all-files

[testenv:venv]
Expand Down

0 comments on commit ed6a374

Please sign in to comment.