Skip to content

Commit 8fd3289

Browse files
committed
Add multiple file support
Signed-off-by: Daniel González Lopes <[email protected]> Improve multiple file support Signed-off-by: Daniel González Lopes <[email protected]> Fixing multiple files logic Signed-off-by: dgzlopes <[email protected]> Fixing baseline tests Signed-off-by: dgzlopes <[email protected]>
1 parent 1389360 commit 8fd3289

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ $ detect-secrets scan --update .secrets.baseline
7373

7474
### Command Line
7575

76-
`detect-secrets` is designed to be used as a git pre-commit hook, but you can also invoke `detect-secrets scan [path]` directly (`path` defaults to `.` if not specified).
76+
`detect-secrets` is designed to be used as a git pre-commit hook, but you can also invoke `detect-secrets scan [path]` directly being `path` the file(s) and/or directory(ies) to scan (`path` defaults to `.` if not specified).
7777

7878
It should be noted that by default, `detect-secrets scan` only operates on files that are tracked by git. So if you intend to scan files outside of a git repository, you will need to pass the `--all-files` flag.
7979

detect_secrets/core/baseline.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
import re
66
import subprocess
77

8+
from detect_secrets.core.log import get_logger
89
from detect_secrets.core.secrets_collection import SecretsCollection
910

11+
log = get_logger(format_string='%(message)s')
12+
1013

1114
def initialize(
1215
plugins,
@@ -23,7 +26,7 @@ def initialize(
2326
2427
:type exclude_files_regex: str|None
2528
:type exclude_lines_regex: str|None
26-
:type path: str
29+
:type path: str|list
2730
:type scan_all_files: bool
2831
2932
:rtype: SecretsCollection
@@ -34,13 +37,20 @@ def initialize(
3437
exclude_lines=exclude_lines_regex,
3538
)
3639

37-
if os.path.isfile(path):
38-
# This option allows for much easier adhoc usage.
39-
files_to_scan = [path]
40-
elif scan_all_files:
41-
files_to_scan = _get_files_recursively(path)
42-
else:
43-
files_to_scan = _get_git_tracked_files(path)
40+
if not isinstance(path, list):
41+
path = [path]
42+
43+
files_to_scan = list()
44+
for element in path:
45+
if os.path.isdir(element):
46+
if scan_all_files:
47+
files_to_scan.extend(_get_files_recursively(element))
48+
else:
49+
files_to_scan.extend(_get_git_tracked_files(element))
50+
elif os.path.isfile(element):
51+
files_to_scan.append(element)
52+
else:
53+
log.error("detect-secrets: " + element + ": No such file or directory")
4454

4555
if not files_to_scan:
4656
return output

detect_secrets/core/usage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def add_arguments(self):
120120
def _add_initialize_baseline_argument(self):
121121
self.parser.add_argument(
122122
'path',
123-
nargs='?',
123+
nargs='*',
124124
default='.',
125125
help=(
126126
'Scans the entire codebase and outputs a snapshot of '

tests/core/baseline_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ def test_basic_usage(self, path):
5858
assert len(results['test_data/files/file_with_secrets.py']) == 1
5959
assert len(results['test_data/files/tmp/file_with_secrets.py']) == 2
6060

61+
def test_with_multiple_files(self):
62+
results = self.get_results(path=[
63+
'test_data/files/file_with_secrets.py',
64+
'test_data/files/tmp/file_with_secrets.py',
65+
])
66+
67+
assert len(results['test_data/files/file_with_secrets.py']) == 1
68+
assert len(results['test_data/files/tmp/file_with_secrets.py']) == 2
69+
assert 'test_data/files/file_with_secrets.py' in results
70+
assert 'test_data/files/tmp/file_with_secrets.py' in results
71+
72+
def test_with_multiple_non_existent_files(self):
73+
results = self.get_results(path=['non-existent-file.A', 'non-existent-file.B'])
74+
75+
# No expected results, because files don't exist
76+
assert not results
77+
78+
def test_with_folders_and_files(self):
79+
results = self.get_results(path=['test_data/', 'non-existent-file.B'])
80+
81+
assert 'test_data/files/file_with_secrets.py' in results
82+
assert 'test_data/files/tmp/file_with_secrets.py' in results
83+
assert 'test_data/files/file_with_no_secrets.py' not in results
84+
assert 'non-existent-file.B' not in results
85+
6186
def test_exclude_regex(self):
6287
results = self.get_results(exclude_files_regex='tmp*')
6388

tests/main_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_scan_with_rootdir(self, mock_baseline_initialize):
4141
plugins=Any(tuple),
4242
exclude_files_regex=None,
4343
exclude_lines_regex=None,
44-
path='test_data',
44+
path=['test_data'],
4545
scan_all_files=False,
4646
)
4747

0 commit comments

Comments
 (0)