Skip to content

Commit

Permalink
ordered hashes, for nicer diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Loo committed Oct 19, 2018
1 parent 0321548 commit ad1de5d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
6 changes: 6 additions & 0 deletions detect_secrets/core/baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ def format_baseline_for_output(baseline):
:type baseline: dict
:rtype: str
"""
for filename, secret_list in baseline['results'].items():
baseline['results'][filename] = sorted(
secret_list,
key=lambda x: (x['line_number'], x['hashed_secret'],),
)

return json.dumps(
baseline,
indent=2,
Expand Down
32 changes: 32 additions & 0 deletions tests/core/baseline_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import absolute_import

import json
import random

import mock
import pytest

from detect_secrets.core import baseline
from detect_secrets.core.baseline import format_baseline_for_output
from detect_secrets.core.baseline import get_secrets_not_in_baseline
from detect_secrets.core.baseline import merge_baseline
from detect_secrets.core.baseline import merge_results
Expand Down Expand Up @@ -512,3 +514,33 @@ def get_secret():
'line_number': random_number,
'type': 'Test Type',
}


class TestFormatBaselineForOutput(object):

def test_sorts_by_line_number_then_hash(self):
output_string = format_baseline_for_output({
'results': {
'filename': [
{
'hashed_secret': 'a',
'line_number': 3,
},
{
'hashed_secret': 'z',
'line_number': 2,
},
{
'hashed_secret': 'f',
'line_number': 3,
},
],
},
})

ordered_hashes = list(map(
lambda x: x['hashed_secret'],
json.loads(output_string)['results']['filename'],
))

assert ordered_hashes == ['z', 'a', 'f']
7 changes: 3 additions & 4 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ def mock_merge_baseline():
with mock.patch(
'detect_secrets.main.baseline.merge_baseline',
) as m:
# This return value doesn't matter, because we're not testing
# for it. It just needs to be a dictionary, so it can be properly
# JSON dumped.
m.return_value = {}
# This return value needs to have the `results` key, so that it can
# formatted appropriately for output.
m.return_value = {'results': {}}
yield m


Expand Down

0 comments on commit ad1de5d

Please sign in to comment.