Re-land "git-cl: Fix format --dry-run not working with --full."
Re-land fixes format always saying the folder was dirty.
The --full option would skip setting the return value of the format
command when used with --dry-run. This in turn would cause the
presubmit check to always pass when --full was enabled in a repo by
default.
Was discovered in the ANGLE repository.
Bug: angleproject:4241
Change-Id: I986646336d1857af2826863ef4becbf2b9ee3049
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1993910
Commit-Queue: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py
index e3f6f57..67792a9 100755
--- a/tests/git_cl_test.py
+++ b/tests/git_cl_test.py
@@ -32,6 +32,7 @@
# We have to disable monitoring before importing git_cl.
metrics.DISABLE_METRICS_COLLECTION = True
+import clang_format
import gerrit_util
import git_cl
import git_common
@@ -3441,20 +3442,62 @@
def setUp(self):
super(CMDFormatTestCase, self).setUp()
+ mock.patch('git_cl.RunCommand').start()
+ mock.patch('clang_format.FindClangFormatToolInChromiumTree').start()
+ mock.patch('clang_format.FindClangFormatScriptInChromiumTree').start()
+ mock.patch('git_cl.settings').start()
self._top_dir = tempfile.mkdtemp()
+ self.addCleanup(mock.patch.stopall)
def tearDown(self):
shutil.rmtree(self._top_dir)
super(CMDFormatTestCase, self).tearDown()
+ def _make_temp_file(self, fname, contents):
+ with open(os.path.join(self._top_dir, fname), 'w') as tf:
+ tf.write('\n'.join(contents))
+
def _make_yapfignore(self, contents):
- with open(os.path.join(self._top_dir, '.yapfignore'), 'w') as yapfignore:
- yapfignore.write('\n'.join(contents))
+ self._make_temp_file('.yapfignore', contents)
def _check_yapf_filtering(self, files, expected):
self.assertEqual(expected, git_cl._FilterYapfIgnoredFiles(
files, git_cl._GetYapfIgnorePatterns(self._top_dir)))
+ def testClangFormatDiffFull(self):
+ self._make_temp_file('test.cc', ['// test'])
+ git_cl.settings.GetFormatFullByDefault.return_value = False
+ diff_file = [os.path.join(self._top_dir, 'test.cc')]
+ mock_opts = mock.Mock(full=True, dry_run=True, diff=False)
+
+ # Diff
+ git_cl.RunCommand.return_value = ' // test'
+ return_value = git_cl._RunClangFormatDiff(mock_opts, diff_file,
+ self._top_dir, 'HEAD')
+ self.assertEqual(2, return_value)
+
+ # No diff
+ git_cl.RunCommand.return_value = '// test'
+ return_value = git_cl._RunClangFormatDiff(mock_opts, diff_file,
+ self._top_dir, 'HEAD')
+ self.assertEqual(0, return_value)
+
+ def testClangFormatDiff(self):
+ git_cl.settings.GetFormatFullByDefault.return_value = False
+ mock_opts = mock.Mock(full=False, dry_run=True, diff=False)
+
+ # Diff
+ git_cl.RunCommand.return_value = 'error'
+ return_value = git_cl._RunClangFormatDiff(mock_opts, ['.'], self._top_dir,
+ 'HEAD')
+ self.assertEqual(2, return_value)
+
+ # No diff
+ git_cl.RunCommand.return_value = ''
+ return_value = git_cl._RunClangFormatDiff(mock_opts, ['.'], self._top_dir,
+ 'HEAD')
+ self.assertEqual(0, return_value)
+
def testYapfignoreExplicit(self):
self._make_yapfignore(['foo/bar.py', 'foo/bar/baz.py'])
files = [