1 Star 0 Fork 0

Holt/chromium

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
PRESUBMIT_test.py 106.48 KB
一键复制 编辑 原始数据 按行查看 历史
Pengcheng Ding 提交于 2020-03-17 23:08 . Migrate chromium components
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os.path
import subprocess
import unittest
import PRESUBMIT
from PRESUBMIT_test_mocks import MockFile, MockAffectedFile
from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi
_TEST_DATA_DIR = 'base/test/data/presubmit'
class VersionControlConflictsTest(unittest.TestCase):
def testTypicalConflict(self):
lines = ['<<<<<<< HEAD',
' base::ScopedTempDir temp_dir_;',
'=======',
' ScopedTempDir temp_dir_;',
'>>>>>>> master']
errors = PRESUBMIT._CheckForVersionControlConflictsInFile(
MockInputApi(), MockFile('some/path/foo_platform.cc', lines))
self.assertEqual(3, len(errors))
self.assertTrue('1' in errors[0])
self.assertTrue('3' in errors[1])
self.assertTrue('5' in errors[2])
def testIgnoresReadmes(self):
lines = ['A First Level Header',
'====================',
'',
'A Second Level Header',
'---------------------']
errors = PRESUBMIT._CheckForVersionControlConflictsInFile(
MockInputApi(), MockFile('some/polymer/README.md', lines))
self.assertEqual(0, len(errors))
class UmaHistogramChangeMatchedOrNotTest(unittest.TestCase):
def testTypicalCorrectlyMatchedChange(self):
diff_cc = ['UMA_HISTOGRAM_BOOL("Bla.Foo.Dummy", true)']
diff_java = [
'RecordHistogram.recordBooleanHistogram("Bla.Foo.Dummy", true)']
diff_xml = ['<histogram name="Bla.Foo.Dummy"> </histogram>']
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('some/path/foo.cc', diff_cc),
MockFile('some/path/foo.java', diff_java),
MockFile('tools/metrics/histograms/histograms.xml', diff_xml),
]
warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api,
MockOutputApi())
self.assertEqual(0, len(warnings))
def testTypicalNotMatchedChange(self):
diff_cc = ['UMA_HISTOGRAM_BOOL("Bla.Foo.Dummy", true)']
diff_java = [
'RecordHistogram.recordBooleanHistogram("Bla.Foo.Dummy", true)']
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('some/path/foo.cc', diff_cc),
MockFile('some/path/foo.java', diff_java),
]
warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api,
MockOutputApi())
self.assertEqual(1, len(warnings))
self.assertEqual('warning', warnings[0].type)
self.assertTrue('foo.cc' in warnings[0].items[0])
self.assertTrue('foo.java' in warnings[0].items[1])
def testTypicalNotMatchedChangeViaSuffixes(self):
diff_cc = ['UMA_HISTOGRAM_BOOL("Bla.Foo.Dummy", true)']
diff_java = [
'RecordHistogram.recordBooleanHistogram("Bla.Foo.Dummy", true)']
diff_xml = ['<histogram_suffixes name="SuperHistogram">',
' <suffix name="Dummy"/>',
' <affected-histogram name="Snafu.Dummy"/>',
'</histogram>']
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('some/path/foo.cc', diff_cc),
MockFile('some/path/foo.java', diff_java),
MockFile('tools/metrics/histograms/histograms.xml', diff_xml),
]
warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api,
MockOutputApi())
self.assertEqual(1, len(warnings))
self.assertEqual('warning', warnings[0].type)
self.assertTrue('foo.cc' in warnings[0].items[0])
self.assertTrue('foo.java' in warnings[0].items[1])
def testTypicalCorrectlyMatchedChangeViaSuffixes(self):
diff_cc = ['UMA_HISTOGRAM_BOOL("Bla.Foo.Dummy", true)']
diff_java = [
'RecordHistogram.recordBooleanHistogram("Bla.Foo.Dummy", true)']
diff_xml = ['<histogram_suffixes name="SuperHistogram">',
' <suffix name="Dummy"/>',
' <affected-histogram name="Bla.Foo"/>',
'</histogram>']
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('some/path/foo.cc', diff_cc),
MockFile('some/path/foo.java', diff_java),
MockFile('tools/metrics/histograms/histograms.xml', diff_xml),
]
warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api,
MockOutputApi())
self.assertEqual(0, len(warnings))
def testTypicalCorrectlyMatchedChangeViaSuffixesWithSeparator(self):
diff_cc = ['UMA_HISTOGRAM_BOOL("Snafu_Dummy", true)']
diff_java = ['RecordHistogram.recordBooleanHistogram("Snafu_Dummy", true)']
diff_xml = ['<histogram_suffixes name="SuperHistogram" separator="_">',
' <suffix name="Dummy"/>',
' <affected-histogram name="Snafu"/>',
'</histogram>']
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('some/path/foo.cc', diff_cc),
MockFile('some/path/foo.java', diff_java),
MockFile('tools/metrics/histograms/histograms.xml', diff_xml),
]
warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api,
MockOutputApi())
self.assertEqual(0, len(warnings))
def testCorrectlyMatchedChangeViaSuffixesWithLineWrapping(self):
diff_cc = [
'UMA_HISTOGRAM_BOOL("LongHistogramNameNeedsLineWrapping.Dummy", true)']
diff_java = ['RecordHistogram.recordBooleanHistogram(' +
'"LongHistogramNameNeedsLineWrapping.Dummy", true)']
diff_xml = ['<histogram_suffixes',
' name="LongHistogramNameNeedsLineWrapping"',
' separator=".">',
' <suffix name="Dummy"/>',
' <affected-histogram',
' name="LongHistogramNameNeedsLineWrapping"/>',
'</histogram>']
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('some/path/foo.cc', diff_cc),
MockFile('some/path/foo.java', diff_java),
MockFile('tools/metrics/histograms/histograms.xml', diff_xml),
]
warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api,
MockOutputApi())
self.assertEqual(0, len(warnings))
def testNameMatch(self):
# Check that the detected histogram name is "Dummy" and not, e.g.,
# "Dummy\", true); // The \"correct"
diff_cc = ['UMA_HISTOGRAM_BOOL("Dummy", true); // The "correct" histogram']
diff_java = [
'RecordHistogram.recordBooleanHistogram("Dummy", true);' +
' // The "correct" histogram']
diff_xml = ['<histogram name="Dummy"> </histogram>']
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('some/path/foo.cc', diff_cc),
MockFile('some/path/foo.java', diff_java),
MockFile('tools/metrics/histograms/histograms.xml', diff_xml),
]
warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api,
MockOutputApi())
self.assertEqual(0, len(warnings))
def testSimilarMacroNames(self):
diff_cc = ['PUMA_HISTOGRAM_COOL("Mountain Lion", 42)']
diff_java = [
'FakeRecordHistogram.recordFakeHistogram("Mountain Lion", 42)']
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('some/path/foo.cc', diff_cc),
MockFile('some/path/foo.java', diff_java),
]
warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api,
MockOutputApi())
self.assertEqual(0, len(warnings))
def testMultiLine(self):
diff_cc = ['UMA_HISTOGRAM_BOOLEAN(', ' "Multi.Line", true)']
diff_cc2 = ['UMA_HISTOGRAM_BOOLEAN(', ' "Multi.Line"', ' , true)']
diff_java = [
'RecordHistogram.recordBooleanHistogram(',
' "Multi.Line", true);',
]
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('some/path/foo.cc', diff_cc),
MockFile('some/path/foo2.cc', diff_cc2),
MockFile('some/path/foo.java', diff_java),
]
warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api,
MockOutputApi())
self.assertEqual(1, len(warnings))
self.assertEqual('warning', warnings[0].type)
self.assertTrue('foo.cc' in warnings[0].items[0])
self.assertTrue('foo2.cc' in warnings[0].items[1])
class BadExtensionsTest(unittest.TestCase):
def testBadRejFile(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('some/path/foo.cc', ''),
MockFile('some/path/foo.cc.rej', ''),
MockFile('some/path2/bar.h.rej', ''),
]
results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi())
self.assertEqual(1, len(results))
self.assertEqual(2, len(results[0].items))
self.assertTrue('foo.cc.rej' in results[0].items[0])
self.assertTrue('bar.h.rej' in results[0].items[1])
def testBadOrigFile(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('other/path/qux.h.orig', ''),
MockFile('other/path/qux.h', ''),
MockFile('other/path/qux.cc', ''),
]
results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi())
self.assertEqual(1, len(results))
self.assertEqual(1, len(results[0].items))
self.assertTrue('qux.h.orig' in results[0].items[0])
def testGoodFiles(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('other/path/qux.h', ''),
MockFile('other/path/qux.cc', ''),
]
results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi())
self.assertEqual(0, len(results))
class CheckSingletonInHeadersTest(unittest.TestCase):
def testSingletonInArbitraryHeader(self):
diff_singleton_h = ['base::subtle::AtomicWord '
'base::Singleton<Type, Traits, DifferentiatingType>::']
diff_foo_h = ['// base::Singleton<Foo> in comment.',
'friend class base::Singleton<Foo>']
diff_foo2_h = [' //Foo* bar = base::Singleton<Foo>::get();']
diff_bad_h = ['Foo* foo = base::Singleton<Foo>::get();']
mock_input_api = MockInputApi()
mock_input_api.files = [MockAffectedFile('base/memory/singleton.h',
diff_singleton_h),
MockAffectedFile('foo.h', diff_foo_h),
MockAffectedFile('foo2.h', diff_foo2_h),
MockAffectedFile('bad.h', diff_bad_h)]
warnings = PRESUBMIT._CheckSingletonInHeaders(mock_input_api,
MockOutputApi())
self.assertEqual(1, len(warnings))
self.assertEqual(1, len(warnings[0].items))
self.assertEqual('error', warnings[0].type)
self.assertTrue('Found base::Singleton<T>' in warnings[0].message)
def testSingletonInCC(self):
diff_cc = ['Foo* foo = base::Singleton<Foo>::get();']
mock_input_api = MockInputApi()
mock_input_api.files = [MockAffectedFile('some/path/foo.cc', diff_cc)]
warnings = PRESUBMIT._CheckSingletonInHeaders(mock_input_api,
MockOutputApi())
self.assertEqual(0, len(warnings))
class InvalidOSMacroNamesTest(unittest.TestCase):
def testInvalidOSMacroNames(self):
lines = ['#if defined(OS_WINDOWS)',
' #elif defined(OS_WINDOW)',
' # if defined(OS_MACOSX) || defined(OS_CHROME)',
'# else // defined(OS_MAC)',
'#endif // defined(OS_MACOS)']
errors = PRESUBMIT._CheckForInvalidOSMacrosInFile(
MockInputApi(), MockFile('some/path/foo_platform.cc', lines))
self.assertEqual(len(lines), len(errors))
self.assertTrue(':1 OS_WINDOWS' in errors[0])
self.assertTrue('(did you mean OS_WIN?)' in errors[0])
def testValidOSMacroNames(self):
lines = ['#if defined(%s)' % m for m in PRESUBMIT._VALID_OS_MACROS]
errors = PRESUBMIT._CheckForInvalidOSMacrosInFile(
MockInputApi(), MockFile('some/path/foo_platform.cc', lines))
self.assertEqual(0, len(errors))
class InvalidIfDefinedMacroNamesTest(unittest.TestCase):
def testInvalidIfDefinedMacroNames(self):
lines = ['#if defined(TARGET_IPHONE_SIMULATOR)',
'#if !defined(TARGET_IPHONE_SIMULATOR)',
'#elif defined(TARGET_IPHONE_SIMULATOR)',
'#ifdef TARGET_IPHONE_SIMULATOR',
' # ifdef TARGET_IPHONE_SIMULATOR',
'# if defined(VALID) || defined(TARGET_IPHONE_SIMULATOR)',
'# else // defined(TARGET_IPHONE_SIMULATOR)',
'#endif // defined(TARGET_IPHONE_SIMULATOR)']
errors = PRESUBMIT._CheckForInvalidIfDefinedMacrosInFile(
MockInputApi(), MockFile('some/path/source.mm', lines))
self.assertEqual(len(lines), len(errors))
def testValidIfDefinedMacroNames(self):
lines = ['#if defined(FOO)',
'#ifdef BAR']
errors = PRESUBMIT._CheckForInvalidIfDefinedMacrosInFile(
MockInputApi(), MockFile('some/path/source.cc', lines))
self.assertEqual(0, len(errors))
class CheckAddedDepsHaveTestApprovalsTest(unittest.TestCase):
def calculate(self, old_include_rules, old_specific_include_rules,
new_include_rules, new_specific_include_rules):
return PRESUBMIT._CalculateAddedDeps(
os.path, 'include_rules = %r\nspecific_include_rules = %r' % (
old_include_rules, old_specific_include_rules),
'include_rules = %r\nspecific_include_rules = %r' % (
new_include_rules, new_specific_include_rules))
def testCalculateAddedDeps(self):
old_include_rules = [
'+base',
'-chrome',
'+content',
'-grit',
'-grit/",',
'+jni/fooblat.h',
'!sandbox',
]
old_specific_include_rules = {
'compositor\.*': {
'+cc',
},
}
new_include_rules = [
'-ash',
'+base',
'+chrome',
'+components',
'+content',
'+grit',
'+grit/generated_resources.h",',
'+grit/",',
'+jni/fooblat.h',
'+policy',
'+' + os.path.join('third_party', 'WebKit'),
]
new_specific_include_rules = {
'compositor\.*': {
'+cc',
},
'widget\.*': {
'+gpu',
},
}
expected = set([
os.path.join('chrome', 'DEPS'),
os.path.join('gpu', 'DEPS'),
os.path.join('components', 'DEPS'),
os.path.join('policy', 'DEPS'),
os.path.join('third_party', 'WebKit', 'DEPS'),
])
self.assertEqual(
expected,
self.calculate(old_include_rules, old_specific_include_rules,
new_include_rules, new_specific_include_rules))
def testCalculateAddedDepsIgnoresPermutations(self):
old_include_rules = [
'+base',
'+chrome',
]
new_include_rules = [
'+chrome',
'+base',
]
self.assertEqual(set(),
self.calculate(old_include_rules, {}, new_include_rules,
{}))
class JSONParsingTest(unittest.TestCase):
def testSuccess(self):
input_api = MockInputApi()
filename = 'valid_json.json'
contents = ['// This is a comment.',
'{',
' "key1": ["value1", "value2"],',
' "key2": 3 // This is an inline comment.',
'}'
]
input_api.files = [MockFile(filename, contents)]
self.assertEqual(None,
PRESUBMIT._GetJSONParseError(input_api, filename))
def testFailure(self):
input_api = MockInputApi()
test_data = [
('invalid_json_1.json',
['{ x }'],
'Expecting property name:'),
('invalid_json_2.json',
['// Hello world!',
'{ "hello": "world }'],
'Unterminated string starting at:'),
('invalid_json_3.json',
['{ "a": "b", "c": "d", }'],
'Expecting property name:'),
('invalid_json_4.json',
['{ "a": "b" "c": "d" }'],
'Expecting , delimiter:'),
]
input_api.files = [MockFile(filename, contents)
for (filename, contents, _) in test_data]
for (filename, _, expected_error) in test_data:
actual_error = PRESUBMIT._GetJSONParseError(input_api, filename)
self.assertTrue(expected_error in str(actual_error),
"'%s' not found in '%s'" % (expected_error, actual_error))
def testNoEatComments(self):
input_api = MockInputApi()
file_with_comments = 'file_with_comments.json'
contents_with_comments = ['// This is a comment.',
'{',
' "key1": ["value1", "value2"],',
' "key2": 3 // This is an inline comment.',
'}'
]
file_without_comments = 'file_without_comments.json'
contents_without_comments = ['{',
' "key1": ["value1", "value2"],',
' "key2": 3',
'}'
]
input_api.files = [MockFile(file_with_comments, contents_with_comments),
MockFile(file_without_comments,
contents_without_comments)]
self.assertEqual('No JSON object could be decoded',
str(PRESUBMIT._GetJSONParseError(input_api,
file_with_comments,
eat_comments=False)))
self.assertEqual(None,
PRESUBMIT._GetJSONParseError(input_api,
file_without_comments,
eat_comments=False))
class IDLParsingTest(unittest.TestCase):
def testSuccess(self):
input_api = MockInputApi()
filename = 'valid_idl_basics.idl'
contents = ['// Tests a valid IDL file.',
'namespace idl_basics {',
' enum EnumType {',
' name1,',
' name2',
' };',
'',
' dictionary MyType1 {',
' DOMString a;',
' };',
'',
' callback Callback1 = void();',
' callback Callback2 = void(long x);',
' callback Callback3 = void(MyType1 arg);',
' callback Callback4 = void(EnumType type);',
'',
' interface Functions {',
' static void function1();',
' static void function2(long x);',
' static void function3(MyType1 arg);',
' static void function4(Callback1 cb);',
' static void function5(Callback2 cb);',
' static void function6(Callback3 cb);',
' static void function7(Callback4 cb);',
' };',
'',
' interface Events {',
' static void onFoo1();',
' static void onFoo2(long x);',
' static void onFoo2(MyType1 arg);',
' static void onFoo3(EnumType type);',
' };',
'};'
]
input_api.files = [MockFile(filename, contents)]
self.assertEqual(None,
PRESUBMIT._GetIDLParseError(input_api, filename))
def testFailure(self):
input_api = MockInputApi()
test_data = [
('invalid_idl_1.idl',
['//',
'namespace test {',
' dictionary {',
' DOMString s;',
' };',
'};'],
'Unexpected "{" after keyword "dictionary".\n'),
# TODO(yoz): Disabled because it causes the IDL parser to hang.
# See crbug.com/363830.
# ('invalid_idl_2.idl',
# (['namespace test {',
# ' dictionary MissingSemicolon {',
# ' DOMString a',
# ' DOMString b;',
# ' };',
# '};'],
# 'Unexpected symbol DOMString after symbol a.'),
('invalid_idl_3.idl',
['//',
'namespace test {',
' enum MissingComma {',
' name1',
' name2',
' };',
'};'],
'Unexpected symbol name2 after symbol name1.'),
('invalid_idl_4.idl',
['//',
'namespace test {',
' enum TrailingComma {',
' name1,',
' name2,',
' };',
'};'],
'Trailing comma in block.'),
('invalid_idl_5.idl',
['//',
'namespace test {',
' callback Callback1 = void(;',
'};'],
'Unexpected ";" after "(".'),
('invalid_idl_6.idl',
['//',
'namespace test {',
' callback Callback1 = void(long );',
'};'],
'Unexpected ")" after symbol long.'),
('invalid_idl_7.idl',
['//',
'namespace test {',
' interace Events {',
' static void onFoo1();',
' };',
'};'],
'Unexpected symbol Events after symbol interace.'),
('invalid_idl_8.idl',
['//',
'namespace test {',
' interface NotEvent {',
' static void onFoo1();',
' };',
'};'],
'Did not process Interface Interface(NotEvent)'),
('invalid_idl_9.idl',
['//',
'namespace test {',
' interface {',
' static void function1();',
' };',
'};'],
'Interface missing name.'),
]
input_api.files = [MockFile(filename, contents)
for (filename, contents, _) in test_data]
for (filename, _, expected_error) in test_data:
actual_error = PRESUBMIT._GetIDLParseError(input_api, filename)
self.assertTrue(expected_error in str(actual_error),
"'%s' not found in '%s'" % (expected_error, actual_error))
class TryServerMasterTest(unittest.TestCase):
def testTryServerMasters(self):
bots = {
'master.tryserver.chromium.android': [
'android_archive_rel_ng',
'android_arm64_dbg_recipe',
'android_blink_rel',
'android_clang_dbg_recipe',
'android_compile_dbg',
'android_compile_x64_dbg',
'android_compile_x86_dbg',
'android_coverage',
'android_cronet_tester'
'android_swarming_rel',
'cast_shell_android',
'linux_android_dbg_ng',
'linux_android_rel_ng',
],
'master.tryserver.chromium.mac': [
'ios_dbg_simulator',
'ios_rel_device',
'ios_rel_device_ninja',
'mac_asan',
'mac_asan_64',
'mac_chromium_compile_dbg',
'mac_chromium_compile_rel',
'mac_chromium_dbg',
'mac_chromium_rel',
'mac_nacl_sdk',
'mac_nacl_sdk_build',
'mac_rel_naclmore',
'mac_x64_rel',
'mac_xcodebuild',
],
'master.tryserver.chromium.linux': [
'chromium_presubmit',
'linux_arm_cross_compile',
'linux_arm_tester',
'linux_chromeos_asan',
'linux_chromeos_browser_asan',
'linux_chromeos_valgrind',
'linux_chromium_chromeos_dbg',
'linux_chromium_chromeos_rel',
'linux_chromium_compile_dbg',
'linux_chromium_compile_rel',
'linux_chromium_dbg',
'linux_chromium_gn_dbg',
'linux_chromium_gn_rel',
'linux_chromium_rel',
'linux_chromium_trusty32_dbg',
'linux_chromium_trusty32_rel',
'linux_chromium_trusty_dbg',
'linux_chromium_trusty_rel',
'linux_clang_tsan',
'linux_ecs_ozone',
'linux_layout',
'linux_layout_asan',
'linux_layout_rel',
'linux_layout_rel_32',
'linux_nacl_sdk',
'linux_nacl_sdk_bionic',
'linux_nacl_sdk_bionic_build',
'linux_nacl_sdk_build',
'linux_redux',
'linux_rel_naclmore',
'linux_rel_precise32',
'linux_valgrind',
'tools_build_presubmit',
],
'master.tryserver.chromium.win': [
'win8_aura',
'win8_chromium_dbg',
'win8_chromium_rel',
'win_chromium_compile_dbg',
'win_chromium_compile_rel',
'win_chromium_dbg',
'win_chromium_rel',
'win_chromium_rel',
'win_chromium_x64_dbg',
'win_chromium_x64_rel',
'win_nacl_sdk',
'win_nacl_sdk_build',
'win_rel_naclmore',
],
}
for master, bots in bots.iteritems():
for bot in bots:
self.assertEqual(master, PRESUBMIT.GetTryServerMasterForBot(bot),
'bot=%s: expected %s, computed %s' % (
bot, master, PRESUBMIT.GetTryServerMasterForBot(bot)))
class UserMetricsActionTest(unittest.TestCase):
def testUserMetricsActionInActions(self):
input_api = MockInputApi()
file_with_user_action = 'file_with_user_action.cc'
contents_with_user_action = [
'base::UserMetricsAction("AboutChrome")'
]
input_api.files = [MockFile(file_with_user_action,
contents_with_user_action)]
self.assertEqual(
[], PRESUBMIT._CheckUserActionUpdate(input_api, MockOutputApi()))
def testUserMetricsActionNotAddedToActions(self):
input_api = MockInputApi()
file_with_user_action = 'file_with_user_action.cc'
contents_with_user_action = [
'base::UserMetricsAction("NotInActionsXml")'
]
input_api.files = [MockFile(file_with_user_action,
contents_with_user_action)]
output = PRESUBMIT._CheckUserActionUpdate(input_api, MockOutputApi())
self.assertEqual(
('File %s line %d: %s is missing in '
'tools/metrics/actions/actions.xml. Please run '
'tools/metrics/actions/extract_actions.py to update.'
% (file_with_user_action, 1, 'NotInActionsXml')),
output[0].message)
class PydepsNeedsUpdatingTest(unittest.TestCase):
class MockSubprocess(object):
CalledProcessError = subprocess.CalledProcessError
def setUp(self):
mock_all_pydeps = ['A.pydeps', 'B.pydeps']
self.old_ALL_PYDEPS_FILES = PRESUBMIT._ALL_PYDEPS_FILES
PRESUBMIT._ALL_PYDEPS_FILES = mock_all_pydeps
self.mock_input_api = MockInputApi()
self.mock_output_api = MockOutputApi()
self.mock_input_api.subprocess = PydepsNeedsUpdatingTest.MockSubprocess()
self.checker = PRESUBMIT.PydepsChecker(self.mock_input_api, mock_all_pydeps)
self.checker._file_cache = {
'A.pydeps': '# Generated by:\n# CMD A\nA.py\nC.py\n',
'B.pydeps': '# Generated by:\n# CMD B\nB.py\nC.py\n',
}
def tearDown(self):
PRESUBMIT._ALL_PYDEPS_FILES = self.old_ALL_PYDEPS_FILES
def _RunCheck(self):
return PRESUBMIT._CheckPydepsNeedsUpdating(self.mock_input_api,
self.mock_output_api,
checker_for_tests=self.checker)
def testAddedPydep(self):
# PRESUBMIT._CheckPydepsNeedsUpdating is only implemented for Android.
if self.mock_input_api.platform != 'linux2':
return []
self.mock_input_api.files = [
MockAffectedFile('new.pydeps', [], action='A'),
]
self.mock_input_api.CreateMockFileInPath(
[x.LocalPath() for x in self.mock_input_api.AffectedFiles(
include_deletes=True)])
results = self._RunCheck()
self.assertEqual(1, len(results))
self.assertTrue('PYDEPS_FILES' in str(results[0]))
def testPydepNotInSrc(self):
self.mock_input_api.files = [
MockAffectedFile('new.pydeps', [], action='A'),
]
self.mock_input_api.CreateMockFileInPath([])
results = self._RunCheck()
self.assertEqual(0, len(results))
def testRemovedPydep(self):
# PRESUBMIT._CheckPydepsNeedsUpdating is only implemented for Android.
if self.mock_input_api.platform != 'linux2':
return []
self.mock_input_api.files = [
MockAffectedFile(PRESUBMIT._ALL_PYDEPS_FILES[0], [], action='D'),
]
self.mock_input_api.CreateMockFileInPath(
[x.LocalPath() for x in self.mock_input_api.AffectedFiles(
include_deletes=True)])
results = self._RunCheck()
self.assertEqual(1, len(results))
self.assertTrue('PYDEPS_FILES' in str(results[0]))
def testRandomPyIgnored(self):
# PRESUBMIT._CheckPydepsNeedsUpdating is only implemented for Android.
if self.mock_input_api.platform != 'linux2':
return []
self.mock_input_api.files = [
MockAffectedFile('random.py', []),
]
results = self._RunCheck()
self.assertEqual(0, len(results), 'Unexpected results: %r' % results)
def testRelevantPyNoChange(self):
# PRESUBMIT._CheckPydepsNeedsUpdating is only implemented for Android.
if self.mock_input_api.platform != 'linux2':
return []
self.mock_input_api.files = [
MockAffectedFile('A.py', []),
]
def mock_check_output(cmd, shell=False, env=None):
self.assertEqual('CMD A --output ""', cmd)
return self.checker._file_cache['A.pydeps']
self.mock_input_api.subprocess.check_output = mock_check_output
results = self._RunCheck()
self.assertEqual(0, len(results), 'Unexpected results: %r' % results)
def testRelevantPyOneChange(self):
# PRESUBMIT._CheckPydepsNeedsUpdating is only implemented for Android.
if self.mock_input_api.platform != 'linux2':
return []
self.mock_input_api.files = [
MockAffectedFile('A.py', []),
]
def mock_check_output(cmd, shell=False, env=None):
self.assertEqual('CMD A --output ""', cmd)
return 'changed data'
self.mock_input_api.subprocess.check_output = mock_check_output
results = self._RunCheck()
self.assertEqual(1, len(results))
self.assertTrue('File is stale' in str(results[0]))
def testRelevantPyTwoChanges(self):
# PRESUBMIT._CheckPydepsNeedsUpdating is only implemented for Android.
if self.mock_input_api.platform != 'linux2':
return []
self.mock_input_api.files = [
MockAffectedFile('C.py', []),
]
def mock_check_output(cmd, shell=False, env=None):
return 'changed data'
self.mock_input_api.subprocess.check_output = mock_check_output
results = self._RunCheck()
self.assertEqual(2, len(results))
self.assertTrue('File is stale' in str(results[0]))
self.assertTrue('File is stale' in str(results[1]))
class IncludeGuardTest(unittest.TestCase):
def testIncludeGuardChecks(self):
mock_input_api = MockInputApi()
mock_output_api = MockOutputApi()
mock_input_api.files = [
MockAffectedFile('content/browser/thing/foo.h', [
'// Comment',
'#ifndef CONTENT_BROWSER_THING_FOO_H_',
'#define CONTENT_BROWSER_THING_FOO_H_',
'struct McBoatFace;',
'#endif // CONTENT_BROWSER_THING_FOO_H_',
]),
MockAffectedFile('content/browser/thing/bar.h', [
'#ifndef CONTENT_BROWSER_THING_BAR_H_',
'#define CONTENT_BROWSER_THING_BAR_H_',
'namespace content {',
'#endif // CONTENT_BROWSER_THING_BAR_H_',
'} // namespace content',
]),
MockAffectedFile('content/browser/test1.h', [
'namespace content {',
'} // namespace content',
]),
MockAffectedFile('content\\browser\\win.h', [
'#ifndef CONTENT_BROWSER_WIN_H_',
'#define CONTENT_BROWSER_WIN_H_',
'struct McBoatFace;',
'#endif // CONTENT_BROWSER_WIN_H_',
]),
MockAffectedFile('content/browser/test2.h', [
'// Comment',
'#ifndef CONTENT_BROWSER_TEST2_H_',
'struct McBoatFace;',
'#endif // CONTENT_BROWSER_TEST2_H_',
]),
MockAffectedFile('content/browser/internal.h', [
'// Comment',
'#ifndef CONTENT_BROWSER_INTERNAL_H_',
'#define CONTENT_BROWSER_INTERNAL_H_',
'// Comment',
'#ifndef INTERNAL_CONTENT_BROWSER_INTERNAL_H_',
'#define INTERNAL_CONTENT_BROWSER_INTERNAL_H_',
'namespace internal {',
'} // namespace internal',
'#endif // INTERNAL_CONTENT_BROWSER_THING_BAR_H_',
'namespace content {',
'} // namespace content',
'#endif // CONTENT_BROWSER_THING_BAR_H_',
]),
MockAffectedFile('content/browser/thing/foo.cc', [
'// This is a non-header.',
]),
MockAffectedFile('content/browser/disabled.h', [
'// no-include-guard-because-multiply-included',
'struct McBoatFace;',
]),
# New files don't allow misspelled include guards.
MockAffectedFile('content/browser/spleling.h', [
'#ifndef CONTENT_BROWSER_SPLLEING_H_',
'#define CONTENT_BROWSER_SPLLEING_H_',
'struct McBoatFace;',
'#endif // CONTENT_BROWSER_SPLLEING_H_',
]),
# New files don't allow + in include guards.
MockAffectedFile('content/browser/foo+bar.h', [
'#ifndef CONTENT_BROWSER_FOO+BAR_H_',
'#define CONTENT_BROWSER_FOO+BAR_H_',
'struct McBoatFace;',
'#endif // CONTENT_BROWSER_FOO+BAR_H_',
]),
# Old files allow misspelled include guards (for now).
MockAffectedFile('chrome/old.h', [
'// New contents',
'#ifndef CHROME_ODL_H_',
'#define CHROME_ODL_H_',
'#endif // CHROME_ODL_H_',
], [
'// Old contents',
'#ifndef CHROME_ODL_H_',
'#define CHROME_ODL_H_',
'#endif // CHROME_ODL_H_',
]),
# Using a Blink style include guard outside Blink is wrong.
MockAffectedFile('content/NotInBlink.h', [
'#ifndef NotInBlink_h',
'#define NotInBlink_h',
'struct McBoatFace;',
'#endif // NotInBlink_h',
]),
# Using a Blink style include guard in Blink is no longer ok.
MockAffectedFile('third_party/blink/InBlink.h', [
'#ifndef InBlink_h',
'#define InBlink_h',
'struct McBoatFace;',
'#endif // InBlink_h',
]),
# Using a bad include guard in Blink is not ok.
MockAffectedFile('third_party/blink/AlsoInBlink.h', [
'#ifndef WrongInBlink_h',
'#define WrongInBlink_h',
'struct McBoatFace;',
'#endif // WrongInBlink_h',
]),
# Using a bad include guard in Blink is not accepted even if
# it's an old file.
MockAffectedFile('third_party/blink/StillInBlink.h', [
'// New contents',
'#ifndef AcceptedInBlink_h',
'#define AcceptedInBlink_h',
'struct McBoatFace;',
'#endif // AcceptedInBlink_h',
], [
'// Old contents',
'#ifndef AcceptedInBlink_h',
'#define AcceptedInBlink_h',
'struct McBoatFace;',
'#endif // AcceptedInBlink_h',
]),
# Using a non-Chromium include guard in third_party
# (outside blink) is accepted.
MockAffectedFile('third_party/foo/some_file.h', [
'#ifndef REQUIRED_RPCNDR_H_',
'#define REQUIRED_RPCNDR_H_',
'struct SomeFileFoo;',
'#endif // REQUIRED_RPCNDR_H_',
]),
# Not having proper include guard in *_message_generator.h
# for old IPC messages is allowed.
MockAffectedFile('content/common/content_message_generator.h', [
'#undef CONTENT_COMMON_FOO_MESSAGES_H_',
'#include "content/common/foo_messages.h"',
'#ifndef CONTENT_COMMON_FOO_MESSAGES_H_',
'#error "Failed to include content/common/foo_messages.h"',
'#endif',
]),
]
msgs = PRESUBMIT._CheckForIncludeGuards(
mock_input_api, mock_output_api)
expected_fail_count = 8
self.assertEqual(expected_fail_count, len(msgs),
'Expected %d items, found %d: %s'
% (expected_fail_count, len(msgs), msgs))
self.assertEqual(msgs[0].items, ['content/browser/thing/bar.h'])
self.assertEqual(msgs[0].message,
'Include guard CONTENT_BROWSER_THING_BAR_H_ '
'not covering the whole file')
self.assertEqual(msgs[1].items, ['content/browser/test1.h'])
self.assertEqual(msgs[1].message,
'Missing include guard CONTENT_BROWSER_TEST1_H_')
self.assertEqual(msgs[2].items, ['content/browser/test2.h:3'])
self.assertEqual(msgs[2].message,
'Missing "#define CONTENT_BROWSER_TEST2_H_" for '
'include guard')
self.assertEqual(msgs[3].items, ['content/browser/spleling.h:1'])
self.assertEqual(msgs[3].message,
'Header using the wrong include guard name '
'CONTENT_BROWSER_SPLLEING_H_')
self.assertEqual(msgs[4].items, ['content/browser/foo+bar.h'])
self.assertEqual(msgs[4].message,
'Missing include guard CONTENT_BROWSER_FOO_BAR_H_')
self.assertEqual(msgs[5].items, ['content/NotInBlink.h:1'])
self.assertEqual(msgs[5].message,
'Header using the wrong include guard name '
'NotInBlink_h')
self.assertEqual(msgs[6].items, ['third_party/blink/InBlink.h:1'])
self.assertEqual(msgs[6].message,
'Header using the wrong include guard name '
'InBlink_h')
self.assertEqual(msgs[7].items, ['third_party/blink/AlsoInBlink.h:1'])
self.assertEqual(msgs[7].message,
'Header using the wrong include guard name '
'WrongInBlink_h')
class AndroidDeprecatedTestAnnotationTest(unittest.TestCase):
def testCheckAndroidTestAnnotationUsage(self):
mock_input_api = MockInputApi()
mock_output_api = MockOutputApi()
mock_input_api.files = [
MockAffectedFile('LalaLand.java', [
'random stuff'
]),
MockAffectedFile('CorrectUsage.java', [
'import android.support.test.filters.LargeTest;',
'import android.support.test.filters.MediumTest;',
'import android.support.test.filters.SmallTest;',
]),
MockAffectedFile('UsedDeprecatedLargeTestAnnotation.java', [
'import android.test.suitebuilder.annotation.LargeTest;',
]),
MockAffectedFile('UsedDeprecatedMediumTestAnnotation.java', [
'import android.test.suitebuilder.annotation.MediumTest;',
]),
MockAffectedFile('UsedDeprecatedSmallTestAnnotation.java', [
'import android.test.suitebuilder.annotation.SmallTest;',
]),
MockAffectedFile('UsedDeprecatedSmokeAnnotation.java', [
'import android.test.suitebuilder.annotation.Smoke;',
])
]
msgs = PRESUBMIT._CheckAndroidTestAnnotationUsage(
mock_input_api, mock_output_api)
self.assertEqual(1, len(msgs),
'Expected %d items, found %d: %s'
% (1, len(msgs), msgs))
self.assertEqual(4, len(msgs[0].items),
'Expected %d items, found %d: %s'
% (4, len(msgs[0].items), msgs[0].items))
self.assertTrue('UsedDeprecatedLargeTestAnnotation.java:1' in msgs[0].items,
'UsedDeprecatedLargeTestAnnotation not found in errors')
self.assertTrue('UsedDeprecatedMediumTestAnnotation.java:1'
in msgs[0].items,
'UsedDeprecatedMediumTestAnnotation not found in errors')
self.assertTrue('UsedDeprecatedSmallTestAnnotation.java:1' in msgs[0].items,
'UsedDeprecatedSmallTestAnnotation not found in errors')
self.assertTrue('UsedDeprecatedSmokeAnnotation.java:1' in msgs[0].items,
'UsedDeprecatedSmokeAnnotation not found in errors')
class AndroidDeprecatedJUnitFrameworkTest(unittest.TestCase):
def testCheckAndroidTestJUnitFramework(self):
mock_input_api = MockInputApi()
mock_output_api = MockOutputApi()
mock_input_api.files = [
MockAffectedFile('LalaLand.java', [
'random stuff'
]),
MockAffectedFile('CorrectUsage.java', [
'import org.junit.ABC',
'import org.junit.XYZ;',
]),
MockAffectedFile('UsedDeprecatedJUnit.java', [
'import junit.framework.*;',
]),
MockAffectedFile('UsedDeprecatedJUnitAssert.java', [
'import junit.framework.Assert;',
]),
]
msgs = PRESUBMIT._CheckAndroidTestJUnitFrameworkImport(
mock_input_api, mock_output_api)
self.assertEqual(1, len(msgs),
'Expected %d items, found %d: %s'
% (1, len(msgs), msgs))
self.assertEqual(2, len(msgs[0].items),
'Expected %d items, found %d: %s'
% (2, len(msgs[0].items), msgs[0].items))
self.assertTrue('UsedDeprecatedJUnit.java:1' in msgs[0].items,
'UsedDeprecatedJUnit.java not found in errors')
self.assertTrue('UsedDeprecatedJUnitAssert.java:1'
in msgs[0].items,
'UsedDeprecatedJUnitAssert not found in errors')
class AndroidJUnitBaseClassTest(unittest.TestCase):
def testCheckAndroidTestJUnitBaseClass(self):
mock_input_api = MockInputApi()
mock_output_api = MockOutputApi()
mock_input_api.files = [
MockAffectedFile('LalaLand.java', [
'random stuff'
]),
MockAffectedFile('CorrectTest.java', [
'@RunWith(ABC.class);'
'public class CorrectTest {',
'}',
]),
MockAffectedFile('HistoricallyIncorrectTest.java', [
'public class Test extends BaseCaseA {',
'}',
], old_contents=[
'public class Test extends BaseCaseB {',
'}',
]),
MockAffectedFile('CorrectTestWithInterface.java', [
'@RunWith(ABC.class);'
'public class CorrectTest implement Interface {',
'}',
]),
MockAffectedFile('IncorrectTest.java', [
'public class IncorrectTest extends TestCase {',
'}',
]),
MockAffectedFile('IncorrectWithInterfaceTest.java', [
'public class Test implements X extends BaseClass {',
'}',
]),
MockAffectedFile('IncorrectMultiLineTest.java', [
'public class Test implements X, Y, Z',
' extends TestBase {',
'}',
]),
]
msgs = PRESUBMIT._CheckAndroidTestJUnitInheritance(
mock_input_api, mock_output_api)
self.assertEqual(1, len(msgs),
'Expected %d items, found %d: %s'
% (1, len(msgs), msgs))
self.assertEqual(3, len(msgs[0].items),
'Expected %d items, found %d: %s'
% (3, len(msgs[0].items), msgs[0].items))
self.assertTrue('IncorrectTest.java:1' in msgs[0].items,
'IncorrectTest not found in errors')
self.assertTrue('IncorrectWithInterfaceTest.java:1'
in msgs[0].items,
'IncorrectWithInterfaceTest not found in errors')
self.assertTrue('IncorrectMultiLineTest.java:2' in msgs[0].items,
'IncorrectMultiLineTest not found in errors')
class AndroidDebuggableBuildTest(unittest.TestCase):
def testCheckAndroidDebuggableBuild(self):
mock_input_api = MockInputApi()
mock_output_api = MockOutputApi()
mock_input_api.files = [
MockAffectedFile('RandomStuff.java', [
'random stuff'
]),
MockAffectedFile('CorrectUsage.java', [
'import org.chromium.base.BuildInfo;',
'some random stuff',
'boolean isOsDebuggable = BuildInfo.isDebugAndroid();',
]),
MockAffectedFile('JustCheckUserdebugBuild.java', [
'import android.os.Build;',
'some random stuff',
'boolean isOsDebuggable = Build.TYPE.equals("userdebug")',
]),
MockAffectedFile('JustCheckEngineeringBuild.java', [
'import android.os.Build;',
'some random stuff',
'boolean isOsDebuggable = "eng".equals(Build.TYPE)',
]),
MockAffectedFile('UsedBuildType.java', [
'import android.os.Build;',
'some random stuff',
'boolean isOsDebuggable = Build.TYPE.equals("userdebug")'
'|| "eng".equals(Build.TYPE)',
]),
MockAffectedFile('UsedExplicitBuildType.java', [
'some random stuff',
'boolean isOsDebuggable = android.os.Build.TYPE.equals("userdebug")'
'|| "eng".equals(android.os.Build.TYPE)',
]),
]
msgs = PRESUBMIT._CheckAndroidDebuggableBuild(
mock_input_api, mock_output_api)
self.assertEqual(1, len(msgs),
'Expected %d items, found %d: %s'
% (1, len(msgs), msgs))
self.assertEqual(4, len(msgs[0].items),
'Expected %d items, found %d: %s'
% (4, len(msgs[0].items), msgs[0].items))
self.assertTrue('JustCheckUserdebugBuild.java:3' in msgs[0].items)
self.assertTrue('JustCheckEngineeringBuild.java:3' in msgs[0].items)
self.assertTrue('UsedBuildType.java:3' in msgs[0].items)
self.assertTrue('UsedExplicitBuildType.java:2' in msgs[0].items)
class LogUsageTest(unittest.TestCase):
def testCheckAndroidCrLogUsage(self):
mock_input_api = MockInputApi()
mock_output_api = MockOutputApi()
mock_input_api.files = [
MockAffectedFile('RandomStuff.java', [
'random stuff'
]),
MockAffectedFile('HasAndroidLog.java', [
'import android.util.Log;',
'some random stuff',
'Log.d("TAG", "foo");',
]),
MockAffectedFile('HasExplicitUtilLog.java', [
'some random stuff',
'android.util.Log.d("TAG", "foo");',
]),
MockAffectedFile('IsInBasePackage.java', [
'package org.chromium.base;',
'private static final String TAG = "cr_Foo";',
'Log.d(TAG, "foo");',
]),
MockAffectedFile('IsInBasePackageButImportsLog.java', [
'package org.chromium.base;',
'import android.util.Log;',
'private static final String TAG = "cr_Foo";',
'Log.d(TAG, "foo");',
]),
MockAffectedFile('HasBothLog.java', [
'import org.chromium.base.Log;',
'some random stuff',
'private static final String TAG = "cr_Foo";',
'Log.d(TAG, "foo");',
'android.util.Log.d("TAG", "foo");',
]),
MockAffectedFile('HasCorrectTag.java', [
'import org.chromium.base.Log;',
'some random stuff',
'private static final String TAG = "cr_Foo";',
'Log.d(TAG, "foo");',
]),
MockAffectedFile('HasOldTag.java', [
'import org.chromium.base.Log;',
'some random stuff',
'private static final String TAG = "cr.Foo";',
'Log.d(TAG, "foo");',
]),
MockAffectedFile('HasDottedTag.java', [
'import org.chromium.base.Log;',
'some random stuff',
'private static final String TAG = "cr_foo.bar";',
'Log.d(TAG, "foo");',
]),
MockAffectedFile('HasDottedTagPublic.java', [
'import org.chromium.base.Log;',
'some random stuff',
'public static final String TAG = "cr_foo.bar";',
'Log.d(TAG, "foo");',
]),
MockAffectedFile('HasNoTagDecl.java', [
'import org.chromium.base.Log;',
'some random stuff',
'Log.d(TAG, "foo");',
]),
MockAffectedFile('HasIncorrectTagDecl.java', [
'import org.chromium.base.Log;',
'private static final String TAHG = "cr_Foo";',
'some random stuff',
'Log.d(TAG, "foo");',
]),
MockAffectedFile('HasInlineTag.java', [
'import org.chromium.base.Log;',
'some random stuff',
'private static final String TAG = "cr_Foo";',
'Log.d("TAG", "foo");',
]),
MockAffectedFile('HasUnprefixedTag.java', [
'import org.chromium.base.Log;',
'some random stuff',
'private static final String TAG = "rubbish";',
'Log.d(TAG, "foo");',
]),
MockAffectedFile('HasTooLongTag.java', [
'import org.chromium.base.Log;',
'some random stuff',
'private static final String TAG = "21_charachers_long___";',
'Log.d(TAG, "foo");',
]),
]
msgs = PRESUBMIT._CheckAndroidCrLogUsage(
mock_input_api, mock_output_api)
self.assertEqual(5, len(msgs),
'Expected %d items, found %d: %s' % (5, len(msgs), msgs))
# Declaration format
nb = len(msgs[0].items)
self.assertEqual(2, nb,
'Expected %d items, found %d: %s' % (2, nb, msgs[0].items))
self.assertTrue('HasNoTagDecl.java' in msgs[0].items)
self.assertTrue('HasIncorrectTagDecl.java' in msgs[0].items)
# Tag length
nb = len(msgs[1].items)
self.assertEqual(1, nb,
'Expected %d items, found %d: %s' % (1, nb, msgs[1].items))
self.assertTrue('HasTooLongTag.java' in msgs[1].items)
# Tag must be a variable named TAG
nb = len(msgs[2].items)
self.assertEqual(1, nb,
'Expected %d items, found %d: %s' % (1, nb, msgs[2].items))
self.assertTrue('HasInlineTag.java:4' in msgs[2].items)
# Util Log usage
nb = len(msgs[3].items)
self.assertEqual(2, nb,
'Expected %d items, found %d: %s' % (2, nb, msgs[3].items))
self.assertTrue('HasAndroidLog.java:3' in msgs[3].items)
self.assertTrue('IsInBasePackageButImportsLog.java:4' in msgs[3].items)
# Tag must not contain
nb = len(msgs[4].items)
self.assertEqual(3, nb,
'Expected %d items, found %d: %s' % (2, nb, msgs[4].items))
self.assertTrue('HasDottedTag.java' in msgs[4].items)
self.assertTrue('HasDottedTagPublic.java' in msgs[4].items)
self.assertTrue('HasOldTag.java' in msgs[4].items)
class GoogleAnswerUrlFormatTest(unittest.TestCase):
def testCatchAnswerUrlId(self):
input_api = MockInputApi()
input_api.files = [
MockFile('somewhere/file.cc',
['char* host = '
' "https://support.google.com/chrome/answer/123456";']),
MockFile('somewhere_else/file.cc',
['char* host = '
' "https://support.google.com/chrome/a/answer/123456";']),
]
warnings = PRESUBMIT._CheckGoogleSupportAnswerUrl(
input_api, MockOutputApi())
self.assertEqual(1, len(warnings))
self.assertEqual(2, len(warnings[0].items))
def testAllowAnswerUrlParam(self):
input_api = MockInputApi()
input_api.files = [
MockFile('somewhere/file.cc',
['char* host = '
' "https://support.google.com/chrome/?p=cpn_crash_reports";']),
]
warnings = PRESUBMIT._CheckGoogleSupportAnswerUrl(
input_api, MockOutputApi())
self.assertEqual(0, len(warnings))
class HardcodedGoogleHostsTest(unittest.TestCase):
def testWarnOnAssignedLiterals(self):
input_api = MockInputApi()
input_api.files = [
MockFile('content/file.cc',
['char* host = "https://www.google.com";']),
MockFile('content/file.cc',
['char* host = "https://www.googleapis.com";']),
MockFile('content/file.cc',
['char* host = "https://clients1.google.com";']),
]
warnings = PRESUBMIT._CheckHardcodedGoogleHostsInLowerLayers(
input_api, MockOutputApi())
self.assertEqual(1, len(warnings))
self.assertEqual(3, len(warnings[0].items))
def testAllowInComment(self):
input_api = MockInputApi()
input_api.files = [
MockFile('content/file.cc',
['char* host = "https://www.aol.com"; // google.com'])
]
warnings = PRESUBMIT._CheckHardcodedGoogleHostsInLowerLayers(
input_api, MockOutputApi())
self.assertEqual(0, len(warnings))
class ChromeOsSyncedPrefRegistrationTest(unittest.TestCase):
def testWarnsOnChromeOsDirectories(self):
input_api = MockInputApi()
input_api.files = [
MockFile('ash/file.cc',
['PrefRegistrySyncable::SYNCABLE_PREF']),
MockFile('chrome/browser/chromeos/file.cc',
['PrefRegistrySyncable::SYNCABLE_PREF']),
MockFile('chromeos/file.cc',
['PrefRegistrySyncable::SYNCABLE_PREF']),
MockFile('components/arc/file.cc',
['PrefRegistrySyncable::SYNCABLE_PREF']),
MockFile('components/exo/file.cc',
['PrefRegistrySyncable::SYNCABLE_PREF']),
]
warnings = PRESUBMIT._CheckChromeOsSyncedPrefRegistration(
input_api, MockOutputApi())
self.assertEqual(1, len(warnings))
def testDoesNotWarnOnSyncOsPref(self):
input_api = MockInputApi()
input_api.files = [
MockFile('chromeos/file.cc',
['PrefRegistrySyncable::SYNCABLE_OS_PREF']),
]
warnings = PRESUBMIT._CheckChromeOsSyncedPrefRegistration(
input_api, MockOutputApi())
self.assertEqual(0, len(warnings))
def testDoesNotWarnOnCrossPlatformDirectories(self):
input_api = MockInputApi()
input_api.files = [
MockFile('chrome/browser/ui/file.cc',
['PrefRegistrySyncable::SYNCABLE_PREF']),
MockFile('components/sync/file.cc',
['PrefRegistrySyncable::SYNCABLE_PREF']),
MockFile('content/browser/file.cc',
['PrefRegistrySyncable::SYNCABLE_PREF']),
]
warnings = PRESUBMIT._CheckChromeOsSyncedPrefRegistration(
input_api, MockOutputApi())
self.assertEqual(0, len(warnings))
def testSeparateWarningForPriorityPrefs(self):
input_api = MockInputApi()
input_api.files = [
MockFile('chromeos/file.cc',
['PrefRegistrySyncable::SYNCABLE_PREF',
'PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF']),
]
warnings = PRESUBMIT._CheckChromeOsSyncedPrefRegistration(
input_api, MockOutputApi())
self.assertEqual(2, len(warnings))
class ForwardDeclarationTest(unittest.TestCase):
def testCheckHeadersOnlyOutsideThirdParty(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('somewhere/file.cc', [
'class DummyClass;'
]),
MockAffectedFile('third_party/header.h', [
'class DummyClass;'
])
]
warnings = PRESUBMIT._CheckUselessForwardDeclarations(mock_input_api,
MockOutputApi())
self.assertEqual(0, len(warnings))
def testNoNestedDeclaration(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('somewhere/header.h', [
'class SomeClass {',
' protected:',
' class NotAMatch;',
'};'
])
]
warnings = PRESUBMIT._CheckUselessForwardDeclarations(mock_input_api,
MockOutputApi())
self.assertEqual(0, len(warnings))
def testSubStrings(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('somewhere/header.h', [
'class NotUsefulClass;',
'struct SomeStruct;',
'UsefulClass *p1;',
'SomeStructPtr *p2;'
])
]
warnings = PRESUBMIT._CheckUselessForwardDeclarations(mock_input_api,
MockOutputApi())
self.assertEqual(2, len(warnings))
def testUselessForwardDeclaration(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('somewhere/header.h', [
'class DummyClass;',
'struct DummyStruct;',
'class UsefulClass;',
'std::unique_ptr<UsefulClass> p;'
])
]
warnings = PRESUBMIT._CheckUselessForwardDeclarations(mock_input_api,
MockOutputApi())
self.assertEqual(2, len(warnings))
def testBlinkHeaders(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('third_party/blink/header.h', [
'class DummyClass;',
'struct DummyStruct;',
]),
MockAffectedFile('third_party\\blink\\header.h', [
'class DummyClass;',
'struct DummyStruct;',
])
]
warnings = PRESUBMIT._CheckUselessForwardDeclarations(mock_input_api,
MockOutputApi())
self.assertEqual(4, len(warnings))
class RelativeIncludesTest(unittest.TestCase):
def testThirdPartyNotWebKitIgnored(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('third_party/test.cpp', '#include "../header.h"'),
MockAffectedFile('third_party/test/test.cpp', '#include "../header.h"'),
]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckForRelativeIncludes(
mock_input_api, mock_output_api)
self.assertEqual(0, len(errors))
def testNonCppFileIgnored(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('test.py', '#include "../header.h"'),
]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckForRelativeIncludes(
mock_input_api, mock_output_api)
self.assertEqual(0, len(errors))
def testInnocuousChangesAllowed(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('test.cpp', '#include "header.h"'),
MockAffectedFile('test2.cpp', '../'),
]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckForRelativeIncludes(
mock_input_api, mock_output_api)
self.assertEqual(0, len(errors))
def testRelativeIncludeNonWebKitProducesError(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('test.cpp', ['#include "../header.h"']),
]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckForRelativeIncludes(
mock_input_api, mock_output_api)
self.assertEqual(1, len(errors))
def testRelativeIncludeWebKitProducesError(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('third_party/blink/test.cpp',
['#include "../header.h']),
]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckForRelativeIncludes(
mock_input_api, mock_output_api)
self.assertEqual(1, len(errors))
class CCIncludeTest(unittest.TestCase):
def testThirdPartyNotBlinkIgnored(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('third_party/test.cpp', '#include "file.cc"'),
]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckForCcIncludes(
mock_input_api, mock_output_api)
self.assertEqual(0, len(errors))
def testPythonFileIgnored(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('test.py', '#include "file.cc"'),
]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckForCcIncludes(
mock_input_api, mock_output_api)
self.assertEqual(0, len(errors))
def testIncFilesAccepted(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('test.py', '#include "file.inc"'),
]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckForCcIncludes(
mock_input_api, mock_output_api)
self.assertEqual(0, len(errors))
def testInnocuousChangesAllowed(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('test.cpp', '#include "header.h"'),
MockAffectedFile('test2.cpp', 'Something "file.cc"'),
]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckForCcIncludes(
mock_input_api, mock_output_api)
self.assertEqual(0, len(errors))
def testCcIncludeNonBlinkProducesError(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('test.cpp', ['#include "file.cc"']),
]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckForCcIncludes(
mock_input_api, mock_output_api)
self.assertEqual(1, len(errors))
def testCppIncludeBlinkProducesError(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('third_party/blink/test.cpp',
['#include "foo/file.cpp"']),
]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckForCcIncludes(
mock_input_api, mock_output_api)
self.assertEqual(1, len(errors))
class NewHeaderWithoutGnChangeTest(unittest.TestCase):
def testAddHeaderWithoutGn(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('base/stuff.h', ''),
]
warnings = PRESUBMIT._CheckNewHeaderWithoutGnChange(
mock_input_api, MockOutputApi())
self.assertEqual(1, len(warnings))
self.assertTrue('base/stuff.h' in warnings[0].items)
def testModifyHeader(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('base/stuff.h', '', action='M'),
]
warnings = PRESUBMIT._CheckNewHeaderWithoutGnChange(
mock_input_api, MockOutputApi())
self.assertEqual(0, len(warnings))
def testDeleteHeader(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('base/stuff.h', '', action='D'),
]
warnings = PRESUBMIT._CheckNewHeaderWithoutGnChange(
mock_input_api, MockOutputApi())
self.assertEqual(0, len(warnings))
def testAddHeaderWithGn(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('base/stuff.h', ''),
MockAffectedFile('base/BUILD.gn', 'stuff.h'),
]
warnings = PRESUBMIT._CheckNewHeaderWithoutGnChange(
mock_input_api, MockOutputApi())
self.assertEqual(0, len(warnings))
def testAddHeaderWithGni(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('base/stuff.h', ''),
MockAffectedFile('base/files.gni', 'stuff.h'),
]
warnings = PRESUBMIT._CheckNewHeaderWithoutGnChange(
mock_input_api, MockOutputApi())
self.assertEqual(0, len(warnings))
def testAddHeaderWithOther(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('base/stuff.h', ''),
MockAffectedFile('base/stuff.cc', 'stuff.h'),
]
warnings = PRESUBMIT._CheckNewHeaderWithoutGnChange(
mock_input_api, MockOutputApi())
self.assertEqual(1, len(warnings))
def testAddHeaderWithWrongGn(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('base/stuff.h', ''),
MockAffectedFile('base/BUILD.gn', 'stuff_h'),
]
warnings = PRESUBMIT._CheckNewHeaderWithoutGnChange(
mock_input_api, MockOutputApi())
self.assertEqual(1, len(warnings))
def testAddHeadersWithGn(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('base/stuff.h', ''),
MockAffectedFile('base/another.h', ''),
MockAffectedFile('base/BUILD.gn', 'another.h\nstuff.h'),
]
warnings = PRESUBMIT._CheckNewHeaderWithoutGnChange(
mock_input_api, MockOutputApi())
self.assertEqual(0, len(warnings))
def testAddHeadersWithWrongGn(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('base/stuff.h', ''),
MockAffectedFile('base/another.h', ''),
MockAffectedFile('base/BUILD.gn', 'another_h\nstuff.h'),
]
warnings = PRESUBMIT._CheckNewHeaderWithoutGnChange(
mock_input_api, MockOutputApi())
self.assertEqual(1, len(warnings))
self.assertFalse('base/stuff.h' in warnings[0].items)
self.assertTrue('base/another.h' in warnings[0].items)
def testAddHeadersWithWrongGn2(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('base/stuff.h', ''),
MockAffectedFile('base/another.h', ''),
MockAffectedFile('base/BUILD.gn', 'another_h\nstuff_h'),
]
warnings = PRESUBMIT._CheckNewHeaderWithoutGnChange(
mock_input_api, MockOutputApi())
self.assertEqual(1, len(warnings))
self.assertTrue('base/stuff.h' in warnings[0].items)
self.assertTrue('base/another.h' in warnings[0].items)
class CorrectProductNameInMessagesTest(unittest.TestCase):
def testProductNameInDesc(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('chrome/app/google_chrome_strings.grd', [
'<message name="Foo" desc="Welcome to Chrome">',
' Welcome to Chrome!',
'</message>',
]),
MockAffectedFile('chrome/app/chromium_strings.grd', [
'<message name="Bar" desc="Welcome to Chrome">',
' Welcome to Chromium!',
'</message>',
]),
]
warnings = PRESUBMIT._CheckCorrectProductNameInMessages(
mock_input_api, MockOutputApi())
self.assertEqual(0, len(warnings))
def testChromeInChromium(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('chrome/app/google_chrome_strings.grd', [
'<message name="Foo" desc="Welcome to Chrome">',
' Welcome to Chrome!',
'</message>',
]),
MockAffectedFile('chrome/app/chromium_strings.grd', [
'<message name="Bar" desc="Welcome to Chrome">',
' Welcome to Chrome!',
'</message>',
]),
]
warnings = PRESUBMIT._CheckCorrectProductNameInMessages(
mock_input_api, MockOutputApi())
self.assertEqual(1, len(warnings))
self.assertTrue('chrome/app/chromium_strings.grd' in warnings[0].items[0])
def testChromiumInChrome(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('chrome/app/google_chrome_strings.grd', [
'<message name="Foo" desc="Welcome to Chrome">',
' Welcome to Chromium!',
'</message>',
]),
MockAffectedFile('chrome/app/chromium_strings.grd', [
'<message name="Bar" desc="Welcome to Chrome">',
' Welcome to Chromium!',
'</message>',
]),
]
warnings = PRESUBMIT._CheckCorrectProductNameInMessages(
mock_input_api, MockOutputApi())
self.assertEqual(1, len(warnings))
self.assertTrue(
'chrome/app/google_chrome_strings.grd:2' in warnings[0].items[0])
def testMultipleInstances(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('chrome/app/chromium_strings.grd', [
'<message name="Bar" desc="Welcome to Chrome">',
' Welcome to Chrome!',
'</message>',
'<message name="Baz" desc="A correct message">',
' Chromium is the software you are using.',
'</message>',
'<message name="Bat" desc="An incorrect message">',
' Google Chrome is the software you are using.',
'</message>',
]),
]
warnings = PRESUBMIT._CheckCorrectProductNameInMessages(
mock_input_api, MockOutputApi())
self.assertEqual(1, len(warnings))
self.assertTrue(
'chrome/app/chromium_strings.grd:2' in warnings[0].items[0])
self.assertTrue(
'chrome/app/chromium_strings.grd:8' in warnings[0].items[1])
def testMultipleWarnings(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('chrome/app/chromium_strings.grd', [
'<message name="Bar" desc="Welcome to Chrome">',
' Welcome to Chrome!',
'</message>',
'<message name="Baz" desc="A correct message">',
' Chromium is the software you are using.',
'</message>',
'<message name="Bat" desc="An incorrect message">',
' Google Chrome is the software you are using.',
'</message>',
]),
MockAffectedFile('components/components_google_chrome_strings.grd', [
'<message name="Bar" desc="Welcome to Chrome">',
' Welcome to Chrome!',
'</message>',
'<message name="Baz" desc="A correct message">',
' Chromium is the software you are using.',
'</message>',
'<message name="Bat" desc="An incorrect message">',
' Google Chrome is the software you are using.',
'</message>',
]),
]
warnings = PRESUBMIT._CheckCorrectProductNameInMessages(
mock_input_api, MockOutputApi())
self.assertEqual(2, len(warnings))
self.assertTrue(
'components/components_google_chrome_strings.grd:5'
in warnings[0].items[0])
self.assertTrue(
'chrome/app/chromium_strings.grd:2' in warnings[1].items[0])
self.assertTrue(
'chrome/app/chromium_strings.grd:8' in warnings[1].items[1])
class ServiceManifestOwnerTest(unittest.TestCase):
def testServiceManifestJsonChangeNeedsSecurityOwner(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('services/goat/manifest.json',
[
'{',
' "name": "teleporter",',
' "display_name": "Goat Teleporter",'
' "interface_provider_specs": {',
' }',
'}',
])
]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckIpcOwners(
mock_input_api, mock_output_api)
self.assertEqual(1, len(errors))
self.assertEqual(
'Found OWNERS files that need to be updated for IPC security review ' +
'coverage.\nPlease update the OWNERS files below:', errors[0].message)
# No warning if already covered by an OWNERS rule.
def testNonManifestJsonChangesDoNotRequireSecurityOwner(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('services/goat/species.json',
[
'[',
' "anglo-nubian",',
' "angora"',
']',
])
]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckIpcOwners(
mock_input_api, mock_output_api)
self.assertEqual([], errors)
def testServiceManifestChangeNeedsSecurityOwner(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('services/goat/public/cpp/manifest.cc',
[
'#include "services/goat/public/cpp/manifest.h"',
'const service_manager::Manifest& GetManifest() {}',
])]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckIpcOwners(
mock_input_api, mock_output_api)
self.assertEqual(1, len(errors))
self.assertEqual(
'Found OWNERS files that need to be updated for IPC security review ' +
'coverage.\nPlease update the OWNERS files below:', errors[0].message)
def testNonServiceManifestSourceChangesDoNotRequireSecurityOwner(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('some/non/service/thing/foo_manifest.cc',
[
'const char kNoEnforcement[] = "not a manifest!";',
])]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckIpcOwners(
mock_input_api, mock_output_api)
self.assertEqual([], errors)
class BannedTypeCheckTest(unittest.TestCase):
def testBannedCppFunctions(self):
input_api = MockInputApi()
input_api.files = [
MockFile('some/cpp/problematic/file.cc',
['using namespace std;']),
MockFile('third_party/blink/problematic/file.cc',
['GetInterfaceProvider()']),
MockFile('some/cpp/ok/file.cc',
['using std::string;']),
]
results = PRESUBMIT._CheckNoBannedFunctions(input_api, MockOutputApi())
# warnings are results[0], errors are results[1]
self.assertEqual(2, len(results))
self.assertTrue('some/cpp/problematic/file.cc' in results[1].message)
self.assertTrue(
'third_party/blink/problematic/file.cc' in results[0].message)
self.assertTrue('some/cpp/ok/file.cc' not in results[1].message)
def testBannedBlinkDowncastHelpers(self):
input_api = MockInputApi()
input_api.files = [
MockFile('some/cpp/problematic/file1.cc',
['DEFINE_TYPE_CASTS(ToType, FromType, from_argument,'
'PointerPredicate(), ReferencePredicate());']),
MockFile('some/cpp/problematic/file2.cc',
['bool is_test_ele = IsHTMLTestElement(n);']),
MockFile('some/cpp/problematic/file3.cc',
['auto* html_test_ele = ToHTMLTestElement(n);']),
MockFile('some/cpp/problematic/file4.cc',
['auto* html_test_ele_or_null = ToHTMLTestElementOrNull(n);']),
MockFile('some/cpp/ok/file1.cc',
['bool is_test_ele = IsA<HTMLTestElement>(n);']),
MockFile('some/cpp/ok/file2.cc',
['auto* html_test_ele = To<HTMLTestElement>(n);']),
MockFile('some/cpp/ok/file3.cc',
['auto* html_test_ele_or_null = ',
'DynamicTo<HTMLTestElement>(n);']),
]
# warnings are errors[0], errors are errors[1]
errors = PRESUBMIT._CheckNoBannedFunctions(input_api, MockOutputApi())
self.assertEqual(2, len(errors))
self.assertTrue('some/cpp/problematic/file1.cc' in errors[1].message)
self.assertTrue('some/cpp/problematic/file2.cc' in errors[0].message)
self.assertTrue('some/cpp/problematic/file3.cc' in errors[0].message)
self.assertTrue('some/cpp/problematic/file4.cc' in errors[0].message)
self.assertTrue('some/cpp/ok/file1.cc' not in errors[0].message)
self.assertTrue('some/cpp/ok/file2.cc' not in errors[0].message)
self.assertTrue('some/cpp/ok/file3.cc' not in errors[0].message)
def testBannedIosObjcFunctions(self):
input_api = MockInputApi()
input_api.files = [
MockFile('some/ios/file.mm',
['TEST(SomeClassTest, SomeInteraction) {',
'}']),
MockFile('some/mac/file.mm',
['TEST(SomeClassTest, SomeInteraction) {',
'}']),
MockFile('another/ios_file.mm',
['class SomeTest : public testing::Test {};']),
MockFile('some/ios/file_egtest.mm',
['- (void)testSomething { EXPECT_OCMOCK_VERIFY(aMock); }']),
MockFile('some/ios/file_unittest.mm',
['TEST_F(SomeTest, TestThis) { EXPECT_OCMOCK_VERIFY(aMock); }']),
]
errors = PRESUBMIT._CheckNoBannedFunctions(input_api, MockOutputApi())
self.assertEqual(1, len(errors))
self.assertTrue('some/ios/file.mm' in errors[0].message)
self.assertTrue('another/ios_file.mm' in errors[0].message)
self.assertTrue('some/mac/file.mm' not in errors[0].message)
self.assertTrue('some/ios/file_egtest.mm' in errors[0].message)
self.assertTrue('some/ios/file_unittest.mm' not in errors[0].message)
def testBannedMojoFunctions(self):
input_api = MockInputApi()
input_api.files = [
MockFile('some/cpp/problematic/file.cc',
['mojo::DataPipe();']),
MockFile('some/cpp/problematic/file2.cc',
['mojo::ConvertTo<>']),
MockFile('some/cpp/ok/file.cc',
['CreateDataPipe();']),
MockFile('some/cpp/ok/file2.cc',
['mojo::DataPipeDrainer();']),
MockFile('third_party/blink/ok/file3.cc',
['mojo::ConvertTo<>']),
MockFile('content/renderer/ok/file3.cc',
['mojo::ConvertTo<>']),
]
results = PRESUBMIT._CheckNoBannedFunctions(input_api, MockOutputApi())
# warnings are results[0], errors are results[1]
self.assertEqual(2, len(results))
self.assertTrue('some/cpp/problematic/file.cc' in results[1].message)
self.assertTrue('some/cpp/problematic/file2.cc' in results[0].message)
self.assertTrue('some/cpp/ok/file.cc' not in results[1].message)
self.assertTrue('some/cpp/ok/file2.cc' not in results[1].message)
self.assertTrue('third_party/blink/ok/file3.cc' not in results[0].message)
self.assertTrue('content/renderer/ok/file3.cc' not in results[0].message)
def testDeprecatedMojoTypes(self):
ok_paths = ['components/arc']
warning_paths = ['some/cpp']
error_paths = ['third_party/blink', 'content']
test_cases = [
{
'type': 'mojo::AssociatedBinding<>;',
'file': 'file1.c'
},
{
'type': 'mojo::AssociatedBindingSet<>;',
'file': 'file2.c'
},
{
'type': 'mojo::AssociatedInterfacePtr<>',
'file': 'file3.cc'
},
{
'type': 'mojo::AssociatedInterfacePtrInfo<>',
'file': 'file4.cc'
},
{
'type': 'mojo::AssociatedInterfaceRequest<>',
'file': 'file5.cc'
},
{
'type': 'mojo::Binding<>',
'file': 'file6.cc'
},
{
'type': 'mojo::BindingSet<>',
'file': 'file7.cc'
},
{
'type': 'mojo::InterfacePtr<>',
'file': 'file8.cc'
},
{
'type': 'mojo::InterfacePtrInfo<>',
'file': 'file9.cc'
},
{
'type': 'mojo::InterfaceRequest<>',
'file': 'file10.cc'
},
{
'type': 'mojo::MakeRequest()',
'file': 'file11.cc'
},
{
'type': 'mojo::MakeRequestAssociatedWithDedicatedPipe()',
'file': 'file12.cc'
},
{
'type': 'mojo::MakeStrongBinding()<>',
'file': 'file13.cc'
},
{
'type': 'mojo::MakeStrongAssociatedBinding()<>',
'file': 'file14.cc'
},
{
'type': 'mojo::StrongAssociatedBindingSet<>',
'file': 'file15.cc'
},
{
'type': 'mojo::StrongBindingSet<>',
'file': 'file16.cc'
},
]
# Build the list of MockFiles considering paths that should trigger warnings
# as well as paths that should trigger errors.
input_api = MockInputApi()
input_api.files = []
for test_case in test_cases:
for path in ok_paths:
input_api.files.append(MockFile(os.path.join(path, test_case['file']),
[test_case['type']]))
for path in warning_paths:
input_api.files.append(MockFile(os.path.join(path, test_case['file']),
[test_case['type']]))
for path in error_paths:
input_api.files.append(MockFile(os.path.join(path, test_case['file']),
[test_case['type']]))
results = PRESUBMIT._CheckNoDeprecatedMojoTypes(input_api, MockOutputApi())
# warnings are results[0], errors are results[1]
self.assertEqual(2, len(results))
for test_case in test_cases:
# Check that no warnings nor errors have been triggered for these paths.
for path in ok_paths:
self.assertFalse(path in results[0].message)
self.assertFalse(path in results[1].message)
# Check warnings have been triggered for these paths.
for path in warning_paths:
self.assertTrue(path in results[0].message)
self.assertFalse(path in results[1].message)
# Check errors have been triggered for these paths.
for path in error_paths:
self.assertFalse(path in results[0].message)
self.assertTrue(path in results[1].message)
class NoProductionCodeUsingTestOnlyFunctionsTest(unittest.TestCase):
def testTruePositives(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('some/path/foo.cc', ['foo_for_testing();']),
MockFile('some/path/foo.mm', ['FooForTesting();']),
MockFile('some/path/foo.cxx', ['FooForTests();']),
MockFile('some/path/foo.cpp', ['foo_for_test();']),
]
results = PRESUBMIT._CheckNoProductionCodeUsingTestOnlyFunctions(
mock_input_api, MockOutputApi())
self.assertEqual(1, len(results))
self.assertEqual(4, len(results[0].items))
self.assertTrue('foo.cc' in results[0].items[0])
self.assertTrue('foo.mm' in results[0].items[1])
self.assertTrue('foo.cxx' in results[0].items[2])
self.assertTrue('foo.cpp' in results[0].items[3])
def testFalsePositives(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('some/path/foo.h', ['foo_for_testing();']),
MockFile('some/path/foo.mm', ['FooForTesting() {']),
MockFile('some/path/foo.cc', ['::FooForTests();']),
MockFile('some/path/foo.cpp', ['// foo_for_test();']),
]
results = PRESUBMIT._CheckNoProductionCodeUsingTestOnlyFunctions(
mock_input_api, MockOutputApi())
self.assertEqual(0, len(results))
class NoProductionJavaCodeUsingTestOnlyFunctionsTest(unittest.TestCase):
def testTruePositives(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('dir/java/src/foo.java', ['FooForTesting();']),
MockFile('dir/java/src/bar.java', ['FooForTests(x);']),
MockFile('dir/java/src/baz.java', ['FooForTest(', 'y', ');']),
MockFile('dir/java/src/mult.java', [
'int x = SomethingLongHere()',
' * SomethingLongHereForTesting();'
])
]
results = PRESUBMIT._CheckNoProductionCodeUsingTestOnlyFunctionsJava(
mock_input_api, MockOutputApi())
self.assertEqual(1, len(results))
self.assertEqual(4, len(results[0].items))
self.assertTrue('foo.java' in results[0].items[0])
self.assertTrue('bar.java' in results[0].items[1])
self.assertTrue('baz.java' in results[0].items[2])
self.assertTrue('mult.java' in results[0].items[3])
def testFalsePositives(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('dir/java/src/foo.xml', ['FooForTesting();']),
MockFile('dir/java/src/foo.java', ['FooForTests() {']),
MockFile('dir/java/src/bar.java', ['// FooForTest();']),
MockFile('dir/java/src/bar2.java', ['x = 1; // FooForTest();']),
MockFile('dir/javatests/src/baz.java', ['FooForTest(', 'y', ');']),
MockFile('dir/junit/src/baz.java', ['FooForTest(', 'y', ');']),
MockFile('dir/junit/src/javadoc.java', [
'/** Use FooForTest(); to obtain foo in tests.'
' */'
]),
MockFile('dir/junit/src/javadoc2.java', [
'/** ',
' * Use FooForTest(); to obtain foo in tests.'
' */'
]),
]
results = PRESUBMIT._CheckNoProductionCodeUsingTestOnlyFunctionsJava(
mock_input_api, MockOutputApi())
self.assertEqual(0, len(results))
class NewImagesWarningTest(unittest.TestCase):
def testTruePositives(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('dir/android/res/drawable/foo.png', []),
MockFile('dir/android/res/drawable-v21/bar.svg', []),
MockFile('dir/android/res/mipmap-v21-en/baz.webp', []),
MockFile('dir/android/res_gshoe/drawable-mdpi/foobar.png', []),
]
results = PRESUBMIT._CheckNewImagesWarning(mock_input_api, MockOutputApi())
self.assertEqual(1, len(results))
self.assertEqual(4, len(results[0].items))
self.assertTrue('foo.png' in results[0].items[0].LocalPath())
self.assertTrue('bar.svg' in results[0].items[1].LocalPath())
self.assertTrue('baz.webp' in results[0].items[2].LocalPath())
self.assertTrue('foobar.png' in results[0].items[3].LocalPath())
def testFalsePositives(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('dir/pngs/README.md', []),
MockFile('java/test/res/drawable/foo.png', []),
MockFile('third_party/blink/foo.png', []),
MockFile('dir/third_party/libpng/src/foo.cc', ['foobar']),
MockFile('dir/resources.webp/.gitignore', ['foo.png']),
]
results = PRESUBMIT._CheckNewImagesWarning(mock_input_api, MockOutputApi())
self.assertEqual(0, len(results))
class CheckUniquePtrTest(unittest.TestCase):
def testTruePositivesNullptr(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('dir/baz.cc', ['std::unique_ptr<T>()']),
MockFile('dir/baz-p.cc', ['std::unique_ptr<T<P>>()']),
]
results = PRESUBMIT._CheckUniquePtr(mock_input_api, MockOutputApi())
self.assertEqual(1, len(results))
self.assertTrue('nullptr' in results[0].message)
self.assertEqual(2, len(results[0].items))
self.assertTrue('baz.cc' in results[0].items[0])
self.assertTrue('baz-p.cc' in results[0].items[1])
def testTruePositivesConstructor(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('dir/foo.cc', ['return std::unique_ptr<T>(foo);']),
MockFile('dir/bar.mm', ['bar = std::unique_ptr<T>(foo)']),
MockFile('dir/mult.cc', [
'return',
' std::unique_ptr<T>(barVeryVeryLongFooSoThatItWouldNotFitAbove);'
]),
MockFile('dir/mult2.cc', [
'barVeryVeryLongLongBaaaaaarSoThatTheLineLimitIsAlmostReached =',
' std::unique_ptr<T>(foo);'
]),
MockFile('dir/mult3.cc', [
'bar = std::unique_ptr<T>(',
' fooVeryVeryVeryLongStillGoingWellThisWillTakeAWhileFinallyThere);'
]),
MockFile('dir/multi_arg.cc', [
'auto p = std::unique_ptr<std::pair<T, D>>(new std::pair(T, D));']),
]
results = PRESUBMIT._CheckUniquePtr(mock_input_api, MockOutputApi())
self.assertEqual(1, len(results))
self.assertTrue('std::make_unique' in results[0].message)
self.assertEqual(6, len(results[0].items))
self.assertTrue('foo.cc' in results[0].items[0])
self.assertTrue('bar.mm' in results[0].items[1])
self.assertTrue('mult.cc' in results[0].items[2])
self.assertTrue('mult2.cc' in results[0].items[3])
self.assertTrue('mult3.cc' in results[0].items[4])
self.assertTrue('multi_arg.cc' in results[0].items[5])
def testFalsePositives(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('dir/foo.cc', ['return std::unique_ptr<T[]>(foo);']),
MockFile('dir/bar.mm', ['bar = std::unique_ptr<T[]>(foo)']),
MockFile('dir/file.cc', ['std::unique_ptr<T> p = Foo();']),
MockFile('dir/baz.cc', [
'std::unique_ptr<T> result = std::make_unique<T>();'
]),
MockFile('dir/baz2.cc', [
'std::unique_ptr<T> result = std::make_unique<T>('
]),
MockFile('dir/nested.cc', ['set<std::unique_ptr<T>>();']),
MockFile('dir/nested2.cc', ['map<U, std::unique_ptr<T>>();']),
# Two-argument invocation of std::unique_ptr is exempt because there is
# no equivalent using std::make_unique.
MockFile('dir/multi_arg.cc', [
'auto p = std::unique_ptr<T, D>(new T(), D());']),
]
results = PRESUBMIT._CheckUniquePtr(mock_input_api, MockOutputApi())
self.assertEqual(0, len(results))
class CheckNoDirectIncludesHeadersWhichRedefineStrCat(unittest.TestCase):
def testBlocksDirectIncludes(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('dir/foo_win.cc', ['#include "shlwapi.h"']),
MockFile('dir/bar.h', ['#include <propvarutil.h>']),
MockFile('dir/baz.h', ['#include <atlbase.h>']),
MockFile('dir/jumbo.h', ['#include "sphelper.h"']),
]
results = PRESUBMIT._CheckNoStrCatRedefines(mock_input_api, MockOutputApi())
self.assertEquals(1, len(results))
self.assertEquals(4, len(results[0].items))
self.assertTrue('StrCat' in results[0].message)
self.assertTrue('foo_win.cc' in results[0].items[0])
self.assertTrue('bar.h' in results[0].items[1])
self.assertTrue('baz.h' in results[0].items[2])
self.assertTrue('jumbo.h' in results[0].items[3])
def testAllowsToIncludeWrapper(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('dir/baz_win.cc', ['#include "base/win/shlwapi.h"']),
MockFile('dir/baz-win.h', ['#include "base/win/atl.h"']),
]
results = PRESUBMIT._CheckNoStrCatRedefines(mock_input_api, MockOutputApi())
self.assertEquals(0, len(results))
def testAllowsToCreateWrapper(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('base/win/shlwapi.h', [
'#include <shlwapi.h>',
'#include "base/win/windows_defines.inc"']),
]
results = PRESUBMIT._CheckNoStrCatRedefines(mock_input_api, MockOutputApi())
self.assertEquals(0, len(results))
class TranslationScreenshotsTest(unittest.TestCase):
# An empty grd file.
OLD_GRD_CONTENTS = """<?xml version="1.0" encoding="UTF-8"?>
<grit latest_public_release="1" current_release="1">
<release seq="1">
<messages></messages>
</release>
</grit>
""".splitlines()
# A grd file with a single message.
NEW_GRD_CONTENTS1 = """<?xml version="1.0" encoding="UTF-8"?>
<grit latest_public_release="1" current_release="1">
<release seq="1">
<messages>
<message name="IDS_TEST1">
Test string 1
</message>
</messages>
</release>
</grit>
""".splitlines()
# A grd file with two messages.
NEW_GRD_CONTENTS2 = """<?xml version="1.0" encoding="UTF-8"?>
<grit latest_public_release="1" current_release="1">
<release seq="1">
<messages>
<message name="IDS_TEST1">
Test string 1
</message>
<message name="IDS_TEST2">
Test string 2
</message>
</messages>
</release>
</grit>
""".splitlines()
OLD_GRDP_CONTENTS = (
'<?xml version="1.0" encoding="utf-8"?>',
'<grit-part>',
'</grit-part>'
)
NEW_GRDP_CONTENTS1 = (
'<?xml version="1.0" encoding="utf-8"?>',
'<grit-part>',
'<message name="IDS_PART_TEST1">',
'Part string 1',
'</message>',
'</grit-part>')
NEW_GRDP_CONTENTS2 = (
'<?xml version="1.0" encoding="utf-8"?>',
'<grit-part>',
'<message name="IDS_PART_TEST1">',
'Part string 1',
'</message>',
'<message name="IDS_PART_TEST2">',
'Part string 2',
'</message>',
'</grit-part>')
DO_NOT_UPLOAD_PNG_MESSAGE = ('Do not include actual screenshots in the '
'changelist. Run '
'tools/translate/upload_screenshots.py to '
'upload them instead:')
GENERATE_SIGNATURES_MESSAGE = ('You are adding or modifying UI strings.\n'
'To ensure the best translations, take '
'screenshots of the relevant UI '
'(https://g.co/chrome/translation) and add '
'these files to your changelist:')
REMOVE_SIGNATURES_MESSAGE = ('You removed strings associated with these '
'files. Remove:')
def makeInputApi(self, files):
input_api = MockInputApi()
input_api.files = files
# Override os_path.exists because the presubmit uses the actual
# os.path.exists.
input_api.CreateMockFileInPath(
[x.LocalPath() for x in input_api.AffectedFiles(include_deletes=True)])
return input_api
""" CL modified and added messages, but didn't add any screenshots."""
def testNoScreenshots(self):
# No new strings (file contents same). Should not warn.
input_api = self.makeInputApi([
MockAffectedFile('test.grd', self.NEW_GRD_CONTENTS1,
self.NEW_GRD_CONTENTS1, action='M'),
MockAffectedFile('part.grdp', self.NEW_GRDP_CONTENTS1,
self.NEW_GRDP_CONTENTS1, action='M')])
warnings = PRESUBMIT._CheckTranslationScreenshots(input_api,
MockOutputApi())
self.assertEqual(0, len(warnings))
# Add two new strings. Should have two warnings.
input_api = self.makeInputApi([
MockAffectedFile('test.grd', self.NEW_GRD_CONTENTS2,
self.NEW_GRD_CONTENTS1, action='M'),
MockAffectedFile('part.grdp', self.NEW_GRDP_CONTENTS2,
self.NEW_GRDP_CONTENTS1, action='M')])
warnings = PRESUBMIT._CheckTranslationScreenshots(input_api,
MockOutputApi())
self.assertEqual(1, len(warnings))
self.assertEqual(self.GENERATE_SIGNATURES_MESSAGE, warnings[0].message)
self.assertEqual([
os.path.join('part_grdp', 'IDS_PART_TEST2.png.sha1'),
os.path.join('test_grd', 'IDS_TEST2.png.sha1')],
warnings[0].items)
# Add four new strings. Should have four warnings.
input_api = self.makeInputApi([
MockAffectedFile('test.grd', self.NEW_GRD_CONTENTS2,
self.OLD_GRD_CONTENTS, action='M'),
MockAffectedFile('part.grdp', self.NEW_GRDP_CONTENTS2,
self.OLD_GRDP_CONTENTS, action='M')])
warnings = PRESUBMIT._CheckTranslationScreenshots(input_api,
MockOutputApi())
self.assertEqual(1, len(warnings))
self.assertEqual(self.GENERATE_SIGNATURES_MESSAGE, warnings[0].message)
self.assertEqual([
os.path.join('part_grdp', 'IDS_PART_TEST1.png.sha1'),
os.path.join('part_grdp', 'IDS_PART_TEST2.png.sha1'),
os.path.join('test_grd', 'IDS_TEST1.png.sha1'),
os.path.join('test_grd', 'IDS_TEST2.png.sha1'),
], warnings[0].items)
def testPngAddedSha1NotAdded(self):
# CL added one new message in a grd file and added the png file associated
# with it, but did not add the corresponding sha1 file. This should warn
# twice:
# - Once for the added png file (because we don't want developers to upload
# actual images)
# - Once for the missing .sha1 file
input_api = self.makeInputApi([
MockAffectedFile(
'test.grd',
self.NEW_GRD_CONTENTS1,
self.OLD_GRD_CONTENTS,
action='M'),
MockAffectedFile(
os.path.join('test_grd', 'IDS_TEST1.png'), 'binary', action='A')
])
warnings = PRESUBMIT._CheckTranslationScreenshots(input_api,
MockOutputApi())
self.assertEqual(2, len(warnings))
self.assertEqual(self.DO_NOT_UPLOAD_PNG_MESSAGE, warnings[0].message)
self.assertEqual([os.path.join('test_grd', 'IDS_TEST1.png')],
warnings[0].items)
self.assertEqual(self.GENERATE_SIGNATURES_MESSAGE, warnings[1].message)
self.assertEqual([os.path.join('test_grd', 'IDS_TEST1.png.sha1')],
warnings[1].items)
# CL added two messages (one in grd, one in grdp) and added the png files
# associated with the messages, but did not add the corresponding sha1
# files. This should warn twice:
# - Once for the added png files (because we don't want developers to upload
# actual images)
# - Once for the missing .sha1 files
input_api = self.makeInputApi([
# Modified files:
MockAffectedFile(
'test.grd',
self.NEW_GRD_CONTENTS1,
self.OLD_GRD_CONTENTS,
action='M'),
MockAffectedFile(
'part.grdp',
self.NEW_GRDP_CONTENTS1,
self.OLD_GRDP_CONTENTS,
action='M'),
# Added files:
MockAffectedFile(
os.path.join('test_grd', 'IDS_TEST1.png'), 'binary', action='A'),
MockAffectedFile(
os.path.join('part_grdp', 'IDS_PART_TEST1.png'), 'binary',
action='A')
])
warnings = PRESUBMIT._CheckTranslationScreenshots(input_api,
MockOutputApi())
self.assertEqual(2, len(warnings))
self.assertEqual(self.DO_NOT_UPLOAD_PNG_MESSAGE, warnings[0].message)
self.assertEqual([os.path.join('part_grdp', 'IDS_PART_TEST1.png'),
os.path.join('test_grd', 'IDS_TEST1.png')],
warnings[0].items)
self.assertEqual(self.GENERATE_SIGNATURES_MESSAGE, warnings[1].message)
self.assertEqual([os.path.join('part_grdp', 'IDS_PART_TEST1.png.sha1'),
os.path.join('test_grd', 'IDS_TEST1.png.sha1')],
warnings[1].items)
def testScreenshotsWithSha1(self):
# CL added four messages (two each in a grd and grdp) and their
# corresponding .sha1 files. No warnings.
input_api = self.makeInputApi([
# Modified files:
MockAffectedFile(
'test.grd',
self.NEW_GRD_CONTENTS2,
self.OLD_GRD_CONTENTS,
action='M'),
MockAffectedFile(
'part.grdp',
self.NEW_GRDP_CONTENTS2,
self.OLD_GRDP_CONTENTS,
action='M'),
# Added files:
MockFile(
os.path.join('test_grd', 'IDS_TEST1.png.sha1'),
'binary',
action='A'),
MockFile(
os.path.join('test_grd', 'IDS_TEST2.png.sha1'),
'binary',
action='A'),
MockFile(
os.path.join('part_grdp', 'IDS_PART_TEST1.png.sha1'),
'binary',
action='A'),
MockFile(
os.path.join('part_grdp', 'IDS_PART_TEST2.png.sha1'),
'binary',
action='A'),
])
warnings = PRESUBMIT._CheckTranslationScreenshots(input_api,
MockOutputApi())
self.assertEqual([], warnings)
def testScreenshotsRemovedWithSha1(self):
# Replace new contents with old contents in grd and grp files, removing
# IDS_TEST1, IDS_TEST2, IDS_PART_TEST1 and IDS_PART_TEST2.
# Should warn to remove the sha1 files associated with these strings.
input_api = self.makeInputApi([
# Modified files:
MockAffectedFile(
'test.grd',
self.OLD_GRD_CONTENTS, # new_contents
self.NEW_GRD_CONTENTS2, # old_contents
action='M'),
MockAffectedFile(
'part.grdp',
self.OLD_GRDP_CONTENTS, # new_contents
self.NEW_GRDP_CONTENTS2, # old_contents
action='M'),
# Unmodified files:
MockFile(os.path.join('test_grd', 'IDS_TEST1.png.sha1'), 'binary', ''),
MockFile(os.path.join('test_grd', 'IDS_TEST2.png.sha1'), 'binary', ''),
MockFile(os.path.join('part_grdp', 'IDS_PART_TEST1.png.sha1'),
'binary', ''),
MockFile(os.path.join('part_grdp', 'IDS_PART_TEST2.png.sha1'),
'binary', '')
])
warnings = PRESUBMIT._CheckTranslationScreenshots(input_api,
MockOutputApi())
self.assertEqual(1, len(warnings))
self.assertEqual(self.REMOVE_SIGNATURES_MESSAGE, warnings[0].message)
self.assertEqual([
os.path.join('part_grdp', 'IDS_PART_TEST1.png.sha1'),
os.path.join('part_grdp', 'IDS_PART_TEST2.png.sha1'),
os.path.join('test_grd', 'IDS_TEST1.png.sha1'),
os.path.join('test_grd', 'IDS_TEST2.png.sha1')
], warnings[0].items)
# Same as above, but this time one of the .sha1 files is also removed.
input_api = self.makeInputApi([
# Modified files:
MockAffectedFile(
'test.grd',
self.OLD_GRD_CONTENTS, # new_contents
self.NEW_GRD_CONTENTS2, # old_contents
action='M'),
MockAffectedFile(
'part.grdp',
self.OLD_GRDP_CONTENTS, # new_contents
self.NEW_GRDP_CONTENTS2, # old_contents
action='M'),
# Unmodified files:
MockFile(os.path.join('test_grd', 'IDS_TEST1.png.sha1'), 'binary', ''),
MockFile(os.path.join('part_grdp', 'IDS_PART_TEST1.png.sha1'),
'binary', ''),
# Deleted files:
MockAffectedFile(
os.path.join('test_grd', 'IDS_TEST2.png.sha1'),
'',
'old_contents',
action='D'),
MockAffectedFile(
os.path.join('part_grdp', 'IDS_PART_TEST2.png.sha1'),
'',
'old_contents',
action='D')
])
warnings = PRESUBMIT._CheckTranslationScreenshots(input_api,
MockOutputApi())
self.assertEqual(1, len(warnings))
self.assertEqual(self.REMOVE_SIGNATURES_MESSAGE, warnings[0].message)
self.assertEqual([os.path.join('part_grdp', 'IDS_PART_TEST1.png.sha1'),
os.path.join('test_grd', 'IDS_TEST1.png.sha1')
], warnings[0].items)
# Remove all sha1 files. There should be no warnings.
input_api = self.makeInputApi([
# Modified files:
MockAffectedFile(
'test.grd',
self.OLD_GRD_CONTENTS,
self.NEW_GRD_CONTENTS2,
action='M'),
MockAffectedFile(
'part.grdp',
self.OLD_GRDP_CONTENTS,
self.NEW_GRDP_CONTENTS2,
action='M'),
# Deleted files:
MockFile(
os.path.join('test_grd', 'IDS_TEST1.png.sha1'),
'binary',
action='D'),
MockFile(
os.path.join('test_grd', 'IDS_TEST2.png.sha1'),
'binary',
action='D'),
MockFile(
os.path.join('part_grdp', 'IDS_PART_TEST1.png.sha1'),
'binary',
action='D'),
MockFile(
os.path.join('part_grdp', 'IDS_PART_TEST2.png.sha1'),
'binary',
action='D')
])
warnings = PRESUBMIT._CheckTranslationScreenshots(input_api,
MockOutputApi())
self.assertEqual([], warnings)
class DISABLETypoInTest(unittest.TestCase):
def testPositive(self):
# Verify the typo "DISABLE_" instead of "DISABLED_" in various contexts
# where the desire is to disable a test.
tests = [
# Disabled on one platform:
'#if defined(OS_WIN)\n'
'#define MAYBE_FoobarTest DISABLE_FoobarTest\n'
'#else\n'
'#define MAYBE_FoobarTest FoobarTest\n'
'#endif\n',
# Disabled on one platform spread cross lines:
'#if defined(OS_WIN)\n'
'#define MAYBE_FoobarTest \\\n'
' DISABLE_FoobarTest\n'
'#else\n'
'#define MAYBE_FoobarTest FoobarTest\n'
'#endif\n',
# Disabled on all platforms:
' TEST_F(FoobarTest, DISABLE_Foo)\n{\n}',
# Disabled on all platforms but multiple lines
' TEST_F(FoobarTest,\n DISABLE_foo){\n}\n',
]
for test in tests:
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('some/path/foo_unittest.cc', test.splitlines()),
]
results = PRESUBMIT._CheckNoDISABLETypoInTests(mock_input_api,
MockOutputApi())
self.assertEqual(
1,
len(results),
msg=('expected len(results) == 1 but got %d in test: %s' %
(len(results), test)))
self.assertTrue(
'foo_unittest.cc' in results[0].message,
msg=('expected foo_unittest.cc in message but got %s in test %s' %
(results[0].message, test)))
def testIngoreNotTestFiles(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('some/path/foo.cc', 'TEST_F(FoobarTest, DISABLE_Foo)'),
]
results = PRESUBMIT._CheckNoDISABLETypoInTests(mock_input_api,
MockOutputApi())
self.assertEqual(0, len(results))
def testIngoreDeletedFiles(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('some/path/foo.cc', 'TEST_F(FoobarTest, Foo)', action='D'),
]
results = PRESUBMIT._CheckNoDISABLETypoInTests(mock_input_api,
MockOutputApi())
self.assertEqual(0, len(results))
class BuildtoolsRevisionsAreInSyncTest(unittest.TestCase):
# TODO(crbug.com/941824): We need to make sure the entries in
# //buildtools/DEPS are kept in sync with the entries in //DEPS
# so that users of //buildtools in other projects get the same tooling
# Chromium gets. If we ever fix the referenced bug and add 'includedeps'
# support to gclient, we can eliminate the duplication and delete
# these tests for the corresponding presubmit check.
def _check(self, files):
mock_input_api = MockInputApi()
mock_input_api.files = []
for fname, contents in files.items():
mock_input_api.files.append(MockFile(fname, contents.splitlines()))
return PRESUBMIT._CheckBuildtoolsRevisionsAreInSync(mock_input_api,
MockOutputApi())
def testOneFileChangedButNotTheOther(self):
results = self._check({
"DEPS": "'libunwind_revision': 'onerev'",
})
self.assertNotEqual(results, [])
def testNeitherFileChanged(self):
results = self._check({
"OWNERS": "foobar@example.com",
})
self.assertEqual(results, [])
def testBothFilesChangedAndMatch(self):
results = self._check({
"DEPS": "'libunwind_revision': 'onerev'",
"buildtools/DEPS": "'libunwind_revision': 'onerev'",
})
self.assertEqual(results, [])
def testBothFilesWereChangedAndDontMatch(self):
results = self._check({
"DEPS": "'libunwind_revision': 'onerev'",
"buildtools/DEPS": "'libunwind_revision': 'anotherrev'",
})
self.assertNotEqual(results, [])
class CheckFuzzTargetsTest(unittest.TestCase):
def _check(self, files):
mock_input_api = MockInputApi()
mock_input_api.files = []
for fname, contents in files.items():
mock_input_api.files.append(MockFile(fname, contents.splitlines()))
return PRESUBMIT._CheckFuzzTargets(mock_input_api, MockOutputApi())
def testLibFuzzerSourcesIgnored(self):
results = self._check({
"third_party/lib/Fuzzer/FuzzerDriver.cpp": "LLVMFuzzerInitialize",
})
self.assertEqual(results, [])
def testNonCodeFilesIgnored(self):
results = self._check({
"README.md": "LLVMFuzzerInitialize",
})
self.assertEqual(results, [])
def testNoErrorHeaderPresent(self):
results = self._check({
"fuzzer.cc": (
"#include \"testing/libfuzzer/libfuzzer_exports.h\"\n" +
"LLVMFuzzerInitialize"
)
})
self.assertEqual(results, [])
def testErrorMissingHeader(self):
results = self._check({
"fuzzer.cc": "LLVMFuzzerInitialize"
})
self.assertEqual(len(results), 1)
self.assertEqual(results[0].items, ['fuzzer.cc'])
class SetNoParentTest(unittest.TestCase):
def testSetNoParentMissing(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('goat/OWNERS',
[
'set noparent',
'jochen@chromium.org',
'per-file *.json=set noparent',
'per-file *.json=jochen@chromium.org',
])
]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckSetNoParent(mock_input_api, mock_output_api)
self.assertEqual(1, len(errors))
self.assertTrue('goat/OWNERS:1' in errors[0].long_text)
self.assertTrue('goat/OWNERS:3' in errors[0].long_text)
def testSetNoParentWithCorrectRule(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('goat/OWNERS',
[
'set noparent',
'file://ipc/SECURITY_OWNERS',
'per-file *.json=set noparent',
'per-file *.json=file://ipc/SECURITY_OWNERS',
])
]
mock_output_api = MockOutputApi()
errors = PRESUBMIT._CheckSetNoParent(mock_input_api, mock_output_api)
self.assertEqual([], errors)
if __name__ == '__main__':
unittest.main()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/itaochi/chromium.git
git@gitee.com:itaochi/chromium.git
itaochi
chromium
chromium
master

搜索帮助