0

Fix CheckDanglingUntriaged in PRESUBMIT.py

Due to not having type check in python, both the implementation and the
test were assuming `OldContents()` and `NewContents()` to be string.
They are list of strings instead.

The function `count(str)` being defined on both, it was passing locally,
when I was using DanglingUntriaged on its own line.

Bug: chromium:1395297
Change-Id: I2b20e92e2dfd0054bb4b2c3630f35af20661bbdc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5012813
Commit-Queue: Arthur Sonzogni <arthursonzogni@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1222807}
This commit is contained in:
Arthur Sonzogni
2023-11-10 08:50:39 +00:00
committed by Chromium LUCI CQ
parent 9c26bb7ec4
commit 9eafd22a23
2 changed files with 34 additions and 40 deletions

@ -7160,7 +7160,7 @@ def CheckDanglingUntriaged(input_api, output_api):
# `win-presubmit` are particularly sensitive to reading the files. Adding # `win-presubmit` are particularly sensitive to reading the files. Adding
# this check caused the bot to run 2x longer. See https://crbug.com/1486612. # this check caused the bot to run 2x longer. See https://crbug.com/1486612.
if input_api.no_diffs: if input_api.no_diffs:
return [] return []
def FilterFile(file): def FilterFile(file):
return input_api.FilterSourceFile( return input_api.FilterSourceFile(
@ -7171,8 +7171,8 @@ def CheckDanglingUntriaged(input_api, output_api):
count = 0 count = 0
for f in input_api.AffectedSourceFiles(FilterFile): for f in input_api.AffectedSourceFiles(FilterFile):
count -= f.OldContents().count("DanglingUntriaged") count -= sum([l.count("DanglingUntriaged") for l in f.OldContents()])
count += f.NewContents().count("DanglingUntriaged") count += sum([l.count("DanglingUntriaged") for l in f.NewContents()])
# Most likely, nothing changed: # Most likely, nothing changed:
if count == 0: if count == 0:
@ -7180,10 +7180,7 @@ def CheckDanglingUntriaged(input_api, output_api):
# Congrats developers for improving it: # Congrats developers for improving it:
if count < 0: if count < 0:
message = ( message = f"DanglingUntriaged pointers removed: {-count}\nThank you!"
f"DanglingUntriaged pointers removed: {-count}",
f"Thank you!",
)
return [output_api.PresubmitNotifyResult(message)] return [output_api.PresubmitNotifyResult(message)]
# Check for 'DanglingUntriaged-notes' in the description: # Check for 'DanglingUntriaged-notes' in the description:
@ -7199,18 +7196,18 @@ def CheckDanglingUntriaged(input_api, output_api):
return [] return []
message = ( message = (
"Unexpected new occurrences of `DanglingUntriaged` detected. Please", "Unexpected new occurrences of `DanglingUntriaged` detected. Please\n" +
"avoid adding new ones", "avoid adding new ones\n" +
"", "\n" +
"See documentation:", "See documentation:\n" +
"https://chromium.googlesource.com/chromium/src/+/main/docs/dangling_ptr.md", "https://chromium.googlesource.com/chromium/src/+/main/docs/dangling_ptr.md\n" +
"", "\n" +
"See also the guide to fix dangling pointers:", "See also the guide to fix dangling pointers:\n" +
"https://chromium.googlesource.com/chromium/src/+/main/docs/dangling_ptr_guide.md", "https://chromium.googlesource.com/chromium/src/+/main/docs/dangling_ptr_guide.md\n" +
"", "\n" +
"To disable this warning, please add in the commit description:", "To disable this warning, please add in the commit description:\n" +
"DanglingUntriaged-notes: <rational for new untriaged dangling " "DanglingUntriaged-notes: <rational for new untriaged dangling " +
"pointers>", "pointers>"
) )
return [output_api.PresubmitPromptWarning(message)] return [output_api.PresubmitPromptWarning(message)]

@ -5106,8 +5106,8 @@ class CheckDanglingUntriagedTest(unittest.TestCase):
mock_input_api.files = [ mock_input_api.files = [
MockAffectedFile( MockAffectedFile(
local_path="foo/foo.cc", local_path="foo/foo.cc",
old_contents="raw_ptr<T>", old_contents=["raw_ptr<T>"],
new_contents="raw_ptr<T, DanglingUntriaged>", new_contents=["raw_ptr<T, DanglingUntriaged>"],
) )
] ]
msgs = PRESUBMIT.CheckDanglingUntriaged(mock_input_api, mock_output_api) msgs = PRESUBMIT.CheckDanglingUntriaged(mock_input_api, mock_output_api)
@ -5128,18 +5128,15 @@ class CheckDanglingUntriagedTest(unittest.TestCase):
mock_input_api.files = [ mock_input_api.files = [
MockAffectedFile( MockAffectedFile(
local_path="foo/foo.cc", local_path="foo/foo.cc",
old_contents="raw_ptr<T>", old_contents=["raw_ptr<T>"],
new_contents="raw_ptr<T, DanglingUntriaged>", new_contents=["raw_ptr<T, DanglingUntriaged>"],
) )
] ]
msgs = PRESUBMIT.CheckDanglingUntriaged(mock_input_api, msgs = PRESUBMIT.CheckDanglingUntriaged(mock_input_api,
mock_output_api) mock_output_api)
self.assertEqual(len(msgs), 1) self.assertEqual(len(msgs), 1)
self.assertEqual(len(msgs[0].message), 11) self.assertTrue(("Unexpected new occurrences of `DanglingUntriaged` detected"
self.assertEqual( in msgs[0].message))
msgs[0].message[0],
"Unexpected new occurrences of `DanglingUntriaged` detected. Please",
)
def testNonCppFile(self): def testNonCppFile(self):
"""Test patch adding dangling pointers are not reported in non C++ files""" """Test patch adding dangling pointers are not reported in non C++ files"""
@ -5150,8 +5147,8 @@ class CheckDanglingUntriagedTest(unittest.TestCase):
mock_input_api.files = [ mock_input_api.files = [
MockAffectedFile( MockAffectedFile(
local_path="foo/README.md", local_path="foo/README.md",
old_contents="", old_contents=[""],
new_contents="The DanglingUntriaged annotation means", new_contents=["The DanglingUntriaged annotation means"],
) )
] ]
msgs = PRESUBMIT.CheckDanglingUntriaged(mock_input_api, msgs = PRESUBMIT.CheckDanglingUntriaged(mock_input_api,
@ -5167,8 +5164,8 @@ class CheckDanglingUntriagedTest(unittest.TestCase):
mock_input_api.files = [ mock_input_api.files = [
MockAffectedFile( MockAffectedFile(
local_path="foo/foo.cc", local_path="foo/foo.cc",
old_contents="raw_ptr<T>", old_contents=["raw_ptr<T>"],
new_contents="raw_ptr<T, DanglingUntriaged>", new_contents=["raw_ptr<T, DanglingUntriaged>"],
) )
] ]
mock_input_api.change.DescriptionText = lambda: ( mock_input_api.change.DescriptionText = lambda: (
@ -5186,8 +5183,8 @@ class CheckDanglingUntriagedTest(unittest.TestCase):
mock_input_api.files = [ mock_input_api.files = [
MockAffectedFile( MockAffectedFile(
local_path="foo/foo.cc", local_path="foo/foo.cc",
old_contents="raw_ptr<T>", old_contents=["raw_ptr<T>"],
new_contents="raw_ptr<T, DanglingUntriaged>", new_contents=["raw_ptr<T, DanglingUntriaged>"],
) )
] ]
mock_input_api.change.DescriptionText = lambda: "description" mock_input_api.change.DescriptionText = lambda: "description"
@ -5204,8 +5201,8 @@ class CheckDanglingUntriagedTest(unittest.TestCase):
mock_input_api.files = [ mock_input_api.files = [
MockAffectedFile( MockAffectedFile(
local_path="foo/foo.cc", local_path="foo/foo.cc",
old_contents="raw_ptr<T, DanglingUntriaged>", old_contents=["raw_ptr<T, DanglingUntriaged>"],
new_contents="raw_ptr<T>", new_contents=["raw_ptr<T>"],
) )
] ]
mock_input_api.change.DescriptionText = lambda: ( mock_input_api.change.DescriptionText = lambda: (
@ -5225,14 +5222,14 @@ class CheckDanglingUntriagedTest(unittest.TestCase):
mock_input_api.files = [ mock_input_api.files = [
MockAffectedFile( MockAffectedFile(
local_path="foo/foo.cc", local_path="foo/foo.cc",
old_contents="raw_ptr<T, DanglingUntriaged>", old_contents=["raw_ptr<T, DanglingUntriaged>"],
new_contents="", new_contents=[""],
action="D", action="D",
), ),
MockAffectedFile( MockAffectedFile(
local_path="foo/foo.cc", local_path="foo/foo.cc",
old_contents="", old_contents=[""],
new_contents="raw_ptr<T, DanglingUntriaged>", new_contents=["raw_ptr<T, DanglingUntriaged>"],
action="A", action="A",
), ),
] ]