Skip to content

Commit

Permalink
fix git diff command syntax, tests
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Jones <[email protected]>
  • Loading branch information
nickrayjones committed Dec 15, 2023
1 parent 3044e03 commit b6f22b7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
6 changes: 2 additions & 4 deletions detect_secrets/core/baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,18 +414,16 @@ def _get_git_tracked_diff_files(rootdir='.',diff_branch=None):
git_files = subprocess.check_output(
[
'git',
'-C', rootdir,
'diff',
'--name-only',
'--diff-filter=ACMRTUX',
diff_branch,
'--', rootdir,
],
stderr=fnull,
)
for filename in git_files.decode('utf-8').split():
relative_path = util.get_relative_path_if_in_cwd(rootdir, filename)
if relative_path:
output.append(relative_path)
output.append(filename)
except subprocess.CalledProcessError:
pass
return output
Expand Down
28 changes: 21 additions & 7 deletions tests/core/baseline_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def test_scan_all_files_with_bad_symlinks(self):
assert len(results.keys()) == 0

def test_diff_branch_nodiff(self):
results = self.get_results(path=['test_data/files'],diff_branch="staging")
results = self.get_results(path=['./test_data/files'],diff_branch="origin/master")

# No expected results, because differences
assert not results
Expand All @@ -197,16 +197,30 @@ def test_diff_branch_diff(self):
'detect_secrets.core.baseline.subprocess.check_output',
(
SubprocessMock(
expected_input='git -C test_data/files diff --name-only --diff-filter=ACMRTUX origin/master',
should_throw_exception=True,
mocked_output='',
expected_input='git diff --name-only --diff-filter=ACMRTUX origin/master -- ./test_data/files',
mocked_output=b'test_data/files/file_with_secrets.py\n',
),
),
):
results = self.get_results(path=['test_data/files'],diff_branch="origin/master")
results = self.get_results(path=['./test_data/files'],diff_branch="origin/master")
assert len(results.keys()) == 1
assert len(results['test_data/files/file_with_secrets.py']) == 1

assert not results
assert len(results['test_data/files/tmp/file_with_secrets.py']) == 3

def test_diff_branch_diff2(self):
with mock_git_calls(
'detect_secrets.core.baseline.subprocess.check_output',
(
SubprocessMock(
expected_input='git diff --name-only --diff-filter=ACMRTUX origin/master -- ./test_data/files',
mocked_output=b'test_data/files/file_with_secrets.py\ntest_data/files/tmp/file_with_secrets.py\n',
),
),
):
results = self.get_results(path=['./test_data/files'],diff_branch="origin/master")
assert len(results.keys()) == 2
assert len(results['test_data/files/file_with_secrets.py']) == 1
assert len(results['test_data/files/tmp/file_with_secrets.py']) == 2


class TestGetSecretsNotInBaseline:
Expand Down

0 comments on commit b6f22b7

Please sign in to comment.