presubmit_diff: add support for -U

This is a reland of https://crrev.com/c/6168707 with fixes.
The CL adds a new param, unified, to git_diff() and create_diffs()
as a required param.

The functions themselves are tested in presubmit_diffs_test.py, but
their usages in presubmit_support.py were not, and the missing param
caused a failure in CiderG (b/389876151)

This CL basically carries the same patch, but makes the params
optional with a default value and adds unit tests for the usage
in presubmit_support.py

Tested with CiderG: https://screenshot.googleplex.com/PYEDZ3NGn5cYGtV


Change-Id: Ic747c6a133c016dbcd80034e521c76a3db3a3f60
Bug: 389876151, 379902295
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6182703
Commit-Queue: Scott Lee <ddoman@chromium.org>
Reviewed-by: Gary Tong <gatong@chromium.org>
This commit is contained in:
Scott Lee
2025-01-17 14:51:19 -08:00
committed by LUCI CQ
parent a1fd773224
commit 80d1969422
3 changed files with 115 additions and 23 deletions

View File

@@ -89,6 +89,22 @@ class PresubmitDiffTest(unittest.TestCase):
{"unchanged.txt": ""},
)
@mock.patch('subprocess2.capture', return_value="".encode("utf-8"))
def test_create_diffs_executes_git_diff_with_unified(self, capture):
create_diffs = presubmit_diff.create_diffs
# None => no -U
create_diffs("host", "repo", "ref", self.root, ["unchanged.txt"], None)
capture.assert_called_with(
["git", "diff", "--no-index", "--", mock.ANY, mock.ANY])
# 0 => -U0
create_diffs("host", "repo", "ref", self.root, ["unchanged.txt"], 0)
capture.assert_called_with(
["git", "diff", "--no-index", "-U0", "--", mock.ANY, mock.ANY])
# 3 => -U3
create_diffs("host", "repo", "ref", self.root, ["unchanged.txt"], 3)
capture.assert_called_with(
["git", "diff", "--no-index", "-U3", "--", mock.ANY, mock.ANY])
def test_create_diffs_with_added_file(self):
expected_diff = """diff --git a/added.txt b/added.txt
new file mode 100644

View File

@@ -98,19 +98,69 @@ class ProvidedDiffChangeTest(fake_repos.FakeReposTestBase):
self._test('somewhere/else', ['not a top level file!'],
['still not a top level file!'])
def test_old_contents_of_bad_diff_raises_runtimeerror(self):
diff = """
diff --git a/foo b/foo
new file mode 100644
index 0000000..9daeafb
--- /dev/null
+++ b/foo
@@ -0,0 +1 @@
+add
"""
change = self._create_change(diff)
with self.assertRaises(RuntimeError):
change._affected_files[0].OldContents()
class TestGenerateDiff(fake_repos.FakeReposTestBase):
""" Tests for --generate_diff.
The option is used to generate diffs of given files against the upstream
server as base.
"""
FAKE_REPOS_CLASS = ProvidedDiffChangeFakeRepo
def setUp(self):
super().setUp()
self.repo = os.path.join(self.FAKE_REPOS.git_base, 'repo_1')
self.parser = mock.Mock()
self.parser.error.side_effect = SystemExit
def test_with_diff_file(self):
"""Tests that only either --generate_diff or --diff_file is allowed."""
options = mock.Mock(root=self.repo,
all_files=False,
generate_diff=True,
description='description',
files=None,
diff_file="patch.diff")
with self.assertRaises(SystemExit):
presubmit_support._parse_change(self.parser, options)
self.parser.error.assert_called_once_with(
'<diff_file> cannot be specified when <generate_diff> is set.', )
@mock.patch('presubmit_diff.create_diffs')
def test_with_all_files(self, create_diffs):
"""Ensures --generate_diff is noop if --all_files is specified."""
options = mock.Mock(root=self.repo,
all_files=True,
generate_diff=True,
description='description',
files=None,
source_controlled_only=False,
diff_file=None)
changes = presubmit_support._parse_change(self.parser, options)
self.assertEqual(changes.AllFiles(),
['added', 'somewhere/else', 'to_be_modified'])
create_diffs.assert_not_called()
@mock.patch('presubmit_diff.fetch_content')
def test_with_files(self, fetch_content):
"""Tests --generate_diff with files, which should call create_diffs()."""
# fetch_content would return the old content of a given file.
# In this test case, the mocked file is a newly added file.
# hence, empty content.
fetch_content.side_effect = ['']
options = mock.Mock(root=self.repo,
all_files=False,
gerrit_url='https://chromium.googlesource.com',
generate_diff=True,
description='description',
files=['added'],
source_controlled_only=False,
diff_file=None)
change = presubmit_support._parse_change(self.parser, options)
affected_files = change.AffectedFiles()
self.assertEqual(len(affected_files), 1)
self.assertEqual(affected_files[0].LocalPath(), 'added')
class TestParseDiff(unittest.TestCase):