Skip to content

Commit 20d1921

Browse files
committed
💯 Coverage for baseline.py initialize.py usage.py
And normalize main_test.py comments
1 parent 9cabbe0 commit 20d1921

File tree

6 files changed

+55
-21
lines changed

6 files changed

+55
-21
lines changed

detect_secrets/core/usage.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,7 @@ def consolidate_args(args):
419419
# Consolidate related args
420420
related_args = {}
421421
for related_arg_tuple in plugin.related_args:
422-
try:
423-
flag_name, default_value = related_arg_tuple
424-
except ValueError:
425-
flag_name = related_arg_tuple
426-
default_value = None
422+
flag_name, default_value = related_arg_tuple
427423

428424
arg_name = PluginOptions._convert_flag_text_to_argument_name(
429425
flag_name,

detect_secrets/plugins/common/initialize.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ def _remove_key(d, key):
109109
):
110110
try:
111111
plugins_dict[plugin_name][param_name] = param_value
112-
except KeyError:
112+
except KeyError: # pragma: no cover
113113
log.warning(
114-
'Baseline contain plugin {} which is not in all plugins! Ignoring...',
114+
'Baseline contains plugin {} which is not in all plugins! Ignoring...',
115115
plugin_name,
116116
)
117117

tests/core/baseline_test.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,29 @@ def test_basic_usage(self, path):
6060
assert len(results['test_data/files/file_with_secrets.py']) == 1
6161
assert len(results['test_data/files/tmp/file_with_secrets.py']) == 2
6262

63+
@pytest.mark.parametrize(
64+
'path',
65+
[
66+
[
67+
'./test_data/files',
68+
],
69+
],
70+
)
71+
def test_error_when_getting_git_tracked_files(self, path):
72+
with mock_git_calls(
73+
'detect_secrets.core.baseline.subprocess.check_output',
74+
(
75+
SubprocessMock(
76+
expected_input='git -C ./test_data/files ls-files',
77+
should_throw_exception=True,
78+
mocked_output='',
79+
),
80+
),
81+
):
82+
results = self.get_results(path=['./test_data/files'])
83+
84+
assert not results
85+
6386
def test_with_multiple_files(self):
6487
results = self.get_results(
6588
path=[
@@ -80,7 +103,13 @@ def test_with_multiple_non_existent_files(self):
80103
assert not results
81104

82105
def test_with_folders_and_files(self):
83-
results = self.get_results(path=['test_data/', 'non-existent-file.B'])
106+
results = self.get_results(
107+
path=[
108+
'non-existent-file.B',
109+
'test_data/',
110+
'test_data/empty_folder',
111+
],
112+
)
84113

85114
assert 'test_data/files/file_with_secrets.py' in results
86115
assert 'test_data/files/tmp/file_with_secrets.py' in results

tests/main_test.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def test_old_baseline_ignored_with_update_flag(
221221
@pytest.mark.parametrize(
222222
'plugins_used, plugins_overwriten, plugins_wrote',
223223
[
224-
( # remove some plugins from baseline
224+
( # Remove some plugins from baseline
225225
[
226226
{
227227
'base64_limit': 4.5,
@@ -238,7 +238,7 @@ def test_old_baseline_ignored_with_update_flag(
238238
},
239239
],
240240
),
241-
( # all plugins
241+
( # All plugins
242242
[
243243
{
244244
'base64_limit': 1.5,
@@ -284,7 +284,7 @@ def test_old_baseline_ignored_with_update_flag(
284284
},
285285
],
286286
),
287-
( # remove some plugins from all plugins
287+
( # Remove some plugins from all plugins
288288
[
289289
{
290290
'base64_limit': 4.5,
@@ -324,7 +324,7 @@ def test_old_baseline_ignored_with_update_flag(
324324
},
325325
],
326326
),
327-
( # use same plugin list from baseline
327+
( # Use same plugin list from baseline
328328
[
329329
{
330330
'base64_limit': 3.5,
@@ -345,7 +345,7 @@ def test_old_baseline_ignored_with_update_flag(
345345
},
346346
],
347347
),
348-
( # overwrite base limit from CLI
348+
( # Overwrite base limit from CLI
349349
[
350350
{
351351
'base64_limit': 3.5,
@@ -365,7 +365,7 @@ def test_old_baseline_ignored_with_update_flag(
365365
},
366366
],
367367
),
368-
( # does not overwrite base limit from CLI if baseline not using the plugin
368+
( # Does not overwrite base limit from CLI if baseline not using the plugin
369369
[
370370
{
371371
'name': 'PrivateKeyDetector',
@@ -378,7 +378,7 @@ def test_old_baseline_ignored_with_update_flag(
378378
},
379379
],
380380
),
381-
( # use overwriten option from CLI only when using --use-all-plugins
381+
( # Use overwriten option from CLI only when using --use-all-plugins
382382
[
383383
{
384384
'base64_limit': 3.5,
@@ -420,7 +420,7 @@ def test_old_baseline_ignored_with_update_flag(
420420
},
421421
],
422422
),
423-
( # use plugin limit from baseline when using --use-all-plugins and no input limit
423+
( # Use plugin limit from baseline when using --use-all-plugins and no input limit
424424
[
425425
{
426426
'base64_limit': 2.5,
@@ -492,8 +492,11 @@ def test_plugin_from_old_baseline_respected_with_update_flag(
492492
),
493493
) == 0
494494

495-
assert file_writer.call_args[1]['data']['plugins_used'] == \
495+
assert (
496+
file_writer.call_args[1]['data']['plugins_used']
497+
==
496498
plugins_wrote
499+
)
497500

498501
@pytest.mark.parametrize(
499502
'filename, expected_output',

tests/plugins/common/initialize_test.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def setup(self):
5555
def test_success(self):
5656
plugin = initialize.from_secret_type(
5757
'Base64 High Entropy String',
58-
self.settings,
58+
settings=self.settings,
5959
)
6060

6161
assert isinstance(plugin, Base64HighEntropyString)
@@ -64,5 +64,11 @@ def test_success(self):
6464
def test_failure(self):
6565
assert not initialize.from_secret_type(
6666
'some random secret_type',
67-
self.settings,
67+
settings=self.settings,
68+
)
69+
70+
def test_secret_type_not_in_settings(self):
71+
assert not initialize.from_secret_type(
72+
'Base64 High Entropy String',
73+
settings=[],
6874
)

tox.ini

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ commands =
1414
coverage run -m pytest tests -v
1515
coverage report --show-missing --include=tests/* --fail-under 100
1616
# This is so that we do not regress unintentionally
17-
coverage report --show-missing --include=detect_secrets/* --omit=detect_secrets/core/audit.py,detect_secrets/core/baseline.py,detect_secrets/core/secrets_collection.py,detect_secrets/core/usage.py,detect_secrets/main.py,detect_secrets/plugins/common/ini_file_parser.py,detect_secrets/plugins/common/initialize.py --fail-under 100
18-
coverage report --show-missing --include=detect_secrets/core/audit.py,detect_secrets/core/baseline.py,detect_secrets/core/secrets_collection.py,detect_secrets/core/usage.py,detect_secrets/main.py,detect_secrets/plugins/common/ini_file_parser.py,detect_secrets/plugins/common/initialize.py --fail-under 96
17+
coverage report --show-missing --include=detect_secrets/* --omit=detect_secrets/core/audit.py,detect_secrets/core/secrets_collection.py,detect_secrets/main.py,detect_secrets/plugins/common/ini_file_parser.py --fail-under 100
18+
coverage report --show-missing --include=detect_secrets/core/audit.py,detect_secrets/core/secrets_collection.py,detect_secrets/main.py,detect_secrets/plugins/common/ini_file_parser.py --fail-under 96
1919
pre-commit run --all-files --show-diff-on-failure
2020

2121
[testenv:venv]

0 commit comments

Comments
 (0)