Skip to content

Commit

Permalink
💯 Coverage for baseline.py initialize.py usage.py
Browse files Browse the repository at this point in the history
And normalize main_test.py comments
  • Loading branch information
KevinHock committed Sep 21, 2019
1 parent 676e391 commit 739f3bf
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 21 deletions.
6 changes: 1 addition & 5 deletions detect_secrets/core/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,7 @@ def consolidate_args(args):
# Consolidate related args
related_args = {}
for related_arg_tuple in plugin.related_args:
try:
flag_name, default_value = related_arg_tuple
except ValueError:
flag_name = related_arg_tuple
default_value = None
flag_name, default_value = related_arg_tuple

arg_name = PluginOptions._convert_flag_text_to_argument_name(
flag_name,
Expand Down
4 changes: 2 additions & 2 deletions detect_secrets/plugins/common/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ def _remove_key(d, key):
):
try:
plugins_dict[plugin_name][param_name] = param_value
except KeyError:
except KeyError: # pragma: no cover
log.warning(
'Baseline contain plugin {} which is not in all plugins! Ignoring...',
'Baseline contains plugin {} which is not in all plugins! Ignoring...',
plugin_name,
)

Expand Down
31 changes: 30 additions & 1 deletion tests/core/baseline_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ def test_basic_usage(self, path):
assert len(results['test_data/files/file_with_secrets.py']) == 1
assert len(results['test_data/files/tmp/file_with_secrets.py']) == 2

@pytest.mark.parametrize(
'path',
[
[
'./test_data/files',
],
],
)
def test_error_when_getting_git_tracked_files(self, path):
with mock_git_calls(
'detect_secrets.core.baseline.subprocess.check_output',
(
SubprocessMock(
expected_input='git -C ./test_data/files ls-files',
should_throw_exception=True,
mocked_output='',
),
),
):
results = self.get_results(path=['./test_data/files'])

assert not results

def test_with_multiple_files(self):
results = self.get_results(
path=[
Expand All @@ -80,7 +103,13 @@ def test_with_multiple_non_existent_files(self):
assert not results

def test_with_folders_and_files(self):
results = self.get_results(path=['test_data/', 'non-existent-file.B'])
results = self.get_results(
path=[
'non-existent-file.B',
'test_data/',
'test_data/empty_folder',
],
)

assert 'test_data/files/file_with_secrets.py' in results
assert 'test_data/files/tmp/file_with_secrets.py' in results
Expand Down
21 changes: 12 additions & 9 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def test_old_baseline_ignored_with_update_flag(
@pytest.mark.parametrize(
'plugins_used, plugins_overwriten, plugins_wrote',
[
( # remove some plugins from baseline
( # Remove some plugins from baseline
[
{
'base64_limit': 4.5,
Expand All @@ -238,7 +238,7 @@ def test_old_baseline_ignored_with_update_flag(
},
],
),
( # all plugins
( # All plugins
[
{
'base64_limit': 1.5,
Expand Down Expand Up @@ -284,7 +284,7 @@ def test_old_baseline_ignored_with_update_flag(
},
],
),
( # remove some plugins from all plugins
( # Remove some plugins from all plugins
[
{
'base64_limit': 4.5,
Expand Down Expand Up @@ -324,7 +324,7 @@ def test_old_baseline_ignored_with_update_flag(
},
],
),
( # use same plugin list from baseline
( # Use same plugin list from baseline
[
{
'base64_limit': 3.5,
Expand All @@ -345,7 +345,7 @@ def test_old_baseline_ignored_with_update_flag(
},
],
),
( # overwrite base limit from CLI
( # Overwrite base limit from CLI
[
{
'base64_limit': 3.5,
Expand All @@ -365,7 +365,7 @@ def test_old_baseline_ignored_with_update_flag(
},
],
),
( # does not overwrite base limit from CLI if baseline not using the plugin
( # Does not overwrite base limit from CLI if baseline not using the plugin
[
{
'name': 'PrivateKeyDetector',
Expand All @@ -378,7 +378,7 @@ def test_old_baseline_ignored_with_update_flag(
},
],
),
( # use overwriten option from CLI only when using --use-all-plugins
( # Use overwriten option from CLI only when using --use-all-plugins
[
{
'base64_limit': 3.5,
Expand Down Expand Up @@ -420,7 +420,7 @@ def test_old_baseline_ignored_with_update_flag(
},
],
),
( # use plugin limit from baseline when using --use-all-plugins and no input limit
( # Use plugin limit from baseline when using --use-all-plugins and no input limit
[
{
'base64_limit': 2.5,
Expand Down Expand Up @@ -492,8 +492,11 @@ def test_plugin_from_old_baseline_respected_with_update_flag(
),
) == 0

assert file_writer.call_args[1]['data']['plugins_used'] == \
assert (
file_writer.call_args[1]['data']['plugins_used']
==
plugins_wrote
)

@pytest.mark.parametrize(
'filename, expected_output',
Expand Down
10 changes: 8 additions & 2 deletions tests/plugins/common/initialize_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def setup(self):
def test_success(self):
plugin = initialize.from_secret_type(
'Base64 High Entropy String',
self.settings,
settings=self.settings,
)

assert isinstance(plugin, Base64HighEntropyString)
Expand All @@ -64,5 +64,11 @@ def test_success(self):
def test_failure(self):
assert not initialize.from_secret_type(
'some random secret_type',
self.settings,
settings=self.settings,
)

def test_secret_type_not_in_settings(self):
assert not initialize.from_secret_type(
'Base64 High Entropy String',
settings=[],
)
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ commands =
coverage run -m pytest tests -v
coverage report --show-missing --include=tests/* --fail-under 100
# This is so that we do not regress unintentionally
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
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
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
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
pre-commit run --all-files --show-diff-on-failure

[testenv:venv]
Expand Down

0 comments on commit 739f3bf

Please sign in to comment.