Skip to content

Commit ad1de5d

Browse files
author
Aaron Loo
committed
ordered hashes, for nicer diffs
1 parent 0321548 commit ad1de5d

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

Diff for: detect_secrets/core/baseline.py

+6
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ def format_baseline_for_output(baseline):
235235
:type baseline: dict
236236
:rtype: str
237237
"""
238+
for filename, secret_list in baseline['results'].items():
239+
baseline['results'][filename] = sorted(
240+
secret_list,
241+
key=lambda x: (x['line_number'], x['hashed_secret'],),
242+
)
243+
238244
return json.dumps(
239245
baseline,
240246
indent=2,

Diff for: tests/core/baseline_test.py

+32
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from __future__ import absolute_import
22

3+
import json
34
import random
45

56
import mock
67
import pytest
78

89
from detect_secrets.core import baseline
10+
from detect_secrets.core.baseline import format_baseline_for_output
911
from detect_secrets.core.baseline import get_secrets_not_in_baseline
1012
from detect_secrets.core.baseline import merge_baseline
1113
from detect_secrets.core.baseline import merge_results
@@ -512,3 +514,33 @@ def get_secret():
512514
'line_number': random_number,
513515
'type': 'Test Type',
514516
}
517+
518+
519+
class TestFormatBaselineForOutput(object):
520+
521+
def test_sorts_by_line_number_then_hash(self):
522+
output_string = format_baseline_for_output({
523+
'results': {
524+
'filename': [
525+
{
526+
'hashed_secret': 'a',
527+
'line_number': 3,
528+
},
529+
{
530+
'hashed_secret': 'z',
531+
'line_number': 2,
532+
},
533+
{
534+
'hashed_secret': 'f',
535+
'line_number': 3,
536+
},
537+
],
538+
},
539+
})
540+
541+
ordered_hashes = list(map(
542+
lambda x: x['hashed_secret'],
543+
json.loads(output_string)['results']['filename'],
544+
))
545+
546+
assert ordered_hashes == ['z', 'a', 'f']

Diff for: tests/main_test.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ def mock_merge_baseline():
3535
with mock.patch(
3636
'detect_secrets.main.baseline.merge_baseline',
3737
) as m:
38-
# This return value doesn't matter, because we're not testing
39-
# for it. It just needs to be a dictionary, so it can be properly
40-
# JSON dumped.
41-
m.return_value = {}
38+
# This return value needs to have the `results` key, so that it can
39+
# formatted appropriately for output.
40+
m.return_value = {'results': {}}
4241
yield m
4342

4443

0 commit comments

Comments
 (0)