git_cl_test.py 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482
  1. #!/usr/bin/env python
  2. # Copyright (c) 2012 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Unit tests for git_cl.py."""
  6. import datetime
  7. import json
  8. import logging
  9. import os
  10. import shutil
  11. import StringIO
  12. import sys
  13. import tempfile
  14. import unittest
  15. import urlparse
  16. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  17. from testing_support.auto_stub import TestCase
  18. from third_party import mock
  19. import metrics
  20. # We have to disable monitoring before importing git_cl.
  21. metrics.DISABLE_METRICS_COLLECTION = True
  22. import gerrit_util
  23. import git_cl
  24. import git_common
  25. import git_footers
  26. import subprocess2
  27. def callError(code=1, cmd='', cwd='', stdout='', stderr=''):
  28. return subprocess2.CalledProcessError(code, cmd, cwd, stdout, stderr)
  29. def _constantFn(return_value):
  30. def f(*args, **kwargs):
  31. return return_value
  32. return f
  33. CERR1 = callError(1)
  34. def MakeNamedTemporaryFileMock(expected_content):
  35. class NamedTemporaryFileMock(object):
  36. def __init__(self, *args, **kwargs):
  37. self.name = '/tmp/named'
  38. self.expected_content = expected_content
  39. def __enter__(self):
  40. return self
  41. def __exit__(self, _type, _value, _tb):
  42. pass
  43. def write(self, content):
  44. if self.expected_content:
  45. assert content == self.expected_content
  46. def close(self):
  47. pass
  48. return NamedTemporaryFileMock
  49. class ChangelistMock(object):
  50. # A class variable so we can access it when we don't have access to the
  51. # instance that's being set.
  52. desc = ''
  53. def __init__(self, **kwargs):
  54. pass
  55. def GetIssue(self):
  56. return 1
  57. def GetDescription(self, force=False):
  58. return ChangelistMock.desc
  59. def UpdateDescription(self, desc, force=False):
  60. ChangelistMock.desc = desc
  61. class PresubmitMock(object):
  62. def __init__(self, *args, **kwargs):
  63. self.reviewers = []
  64. self.more_cc = ['chromium-reviews+test-more-cc@chromium.org']
  65. @staticmethod
  66. def should_continue():
  67. return True
  68. class WatchlistsMock(object):
  69. def __init__(self, _):
  70. pass
  71. @staticmethod
  72. def GetWatchersForPaths(_):
  73. return ['joe@example.com']
  74. class CodereviewSettingsFileMock(object):
  75. def __init__(self):
  76. pass
  77. # pylint: disable=no-self-use
  78. def read(self):
  79. return ('CODE_REVIEW_SERVER: gerrit.chromium.org\n' +
  80. 'GERRIT_HOST: True\n')
  81. class AuthenticatorMock(object):
  82. def __init__(self, *_args):
  83. pass
  84. def has_cached_credentials(self):
  85. return True
  86. def authorize(self, http):
  87. return http
  88. def CookiesAuthenticatorMockFactory(hosts_with_creds=None, same_auth=False):
  89. """Use to mock Gerrit/Git credentials from ~/.netrc or ~/.gitcookies.
  90. Usage:
  91. >>> self.mock(git_cl.gerrit_util, "CookiesAuthenticator",
  92. CookiesAuthenticatorMockFactory({'host': ('user', _, 'pass')})
  93. OR
  94. >>> self.mock(git_cl.gerrit_util, "CookiesAuthenticator",
  95. CookiesAuthenticatorMockFactory(
  96. same_auth=('user', '', 'pass'))
  97. """
  98. class CookiesAuthenticatorMock(git_cl.gerrit_util.CookiesAuthenticator):
  99. def __init__(self): # pylint: disable=super-init-not-called
  100. # Intentionally not calling super() because it reads actual cookie files.
  101. pass
  102. @classmethod
  103. def get_gitcookies_path(cls):
  104. return '~/.gitcookies'
  105. @classmethod
  106. def get_netrc_path(cls):
  107. return '~/.netrc'
  108. def _get_auth_for_host(self, host):
  109. if same_auth:
  110. return same_auth
  111. return (hosts_with_creds or {}).get(host)
  112. return CookiesAuthenticatorMock
  113. class MockChangelistWithBranchAndIssue():
  114. def __init__(self, branch, issue):
  115. self.branch = branch
  116. self.issue = issue
  117. def GetBranch(self):
  118. return self.branch
  119. def GetIssue(self):
  120. return self.issue
  121. class SystemExitMock(Exception):
  122. pass
  123. class TestGitClBasic(unittest.TestCase):
  124. def test_get_description(self):
  125. cl = git_cl.Changelist(issue=1, codereview_host='host')
  126. cl.description = 'x'
  127. cl.has_description = True
  128. cl.FetchDescription = lambda *a, **kw: 'y'
  129. self.assertEqual(cl.GetDescription(), 'x')
  130. self.assertEqual(cl.GetDescription(force=True), 'y')
  131. self.assertEqual(cl.GetDescription(), 'y')
  132. def test_description_footers(self):
  133. cl = git_cl.Changelist(issue=1, codereview_host='host')
  134. cl.description = '\n'.join([
  135. 'This is some message',
  136. '',
  137. 'It has some lines',
  138. 'and, also',
  139. '',
  140. 'Some: Really',
  141. 'Awesome: Footers',
  142. ])
  143. cl.has_description = True
  144. cl.UpdateDescriptionRemote = lambda *a, **kw: 'y'
  145. msg, footers = cl.GetDescriptionFooters()
  146. self.assertEqual(
  147. msg, ['This is some message', '', 'It has some lines', 'and, also'])
  148. self.assertEqual(footers, [('Some', 'Really'), ('Awesome', 'Footers')])
  149. msg.append('wut')
  150. footers.append(('gnarly-dude', 'beans'))
  151. cl.UpdateDescriptionFooters(msg, footers)
  152. self.assertEqual(cl.GetDescription().splitlines(), [
  153. 'This is some message',
  154. '',
  155. 'It has some lines',
  156. 'and, also',
  157. 'wut'
  158. '',
  159. 'Some: Really',
  160. 'Awesome: Footers',
  161. 'Gnarly-Dude: beans',
  162. ])
  163. def test_set_preserve_tryjobs(self):
  164. d = git_cl.ChangeDescription('Simple.')
  165. d.set_preserve_tryjobs()
  166. self.assertEqual(d.description.splitlines(), [
  167. 'Simple.',
  168. '',
  169. 'Cq-Do-Not-Cancel-Tryjobs: true',
  170. ])
  171. before = d.description
  172. d.set_preserve_tryjobs()
  173. self.assertEqual(before, d.description)
  174. d = git_cl.ChangeDescription('\n'.join([
  175. 'One is enough',
  176. '',
  177. 'Cq-Do-Not-Cancel-Tryjobs: dups not encouraged, but don\'t hurt',
  178. 'Change-Id: Ideadbeef',
  179. ]))
  180. d.set_preserve_tryjobs()
  181. self.assertEqual(d.description.splitlines(), [
  182. 'One is enough',
  183. '',
  184. 'Cq-Do-Not-Cancel-Tryjobs: dups not encouraged, but don\'t hurt',
  185. 'Change-Id: Ideadbeef',
  186. 'Cq-Do-Not-Cancel-Tryjobs: true',
  187. ])
  188. def test_get_bug_line_values(self):
  189. f = lambda p, bugs: list(git_cl._get_bug_line_values(p, bugs))
  190. self.assertEqual(f('', ''), [])
  191. self.assertEqual(f('', '123,v8:456'), ['123', 'v8:456'])
  192. self.assertEqual(f('v8', '456'), ['v8:456'])
  193. self.assertEqual(f('v8', 'chromium:123,456'), ['v8:456', 'chromium:123'])
  194. # Not nice, but not worth carying.
  195. self.assertEqual(f('v8', 'chromium:123,456,v8:123'),
  196. ['v8:456', 'chromium:123', 'v8:123'])
  197. def _test_git_number(self, parent_msg, dest_ref, child_msg,
  198. parent_hash='parenthash'):
  199. desc = git_cl.ChangeDescription(child_msg)
  200. desc.update_with_git_number_footers(parent_hash, parent_msg, dest_ref)
  201. return desc.description
  202. def assertEqualByLine(self, actual, expected):
  203. self.assertEqual(actual.splitlines(), expected.splitlines())
  204. def test_git_number_bad_parent(self):
  205. with self.assertRaises(ValueError):
  206. self._test_git_number('Parent', 'refs/heads/master', 'Child')
  207. def test_git_number_bad_parent_footer(self):
  208. with self.assertRaises(AssertionError):
  209. self._test_git_number(
  210. 'Parent\n'
  211. '\n'
  212. 'Cr-Commit-Position: wrong',
  213. 'refs/heads/master', 'Child')
  214. def test_git_number_bad_lineage_ignored(self):
  215. actual = self._test_git_number(
  216. 'Parent\n'
  217. '\n'
  218. 'Cr-Commit-Position: refs/heads/master@{#1}\n'
  219. 'Cr-Branched-From: mustBeReal40CharHash-branch@{#pos}',
  220. 'refs/heads/master', 'Child')
  221. self.assertEqualByLine(
  222. actual,
  223. 'Child\n'
  224. '\n'
  225. 'Cr-Commit-Position: refs/heads/master@{#2}\n'
  226. 'Cr-Branched-From: mustBeReal40CharHash-branch@{#pos}')
  227. def test_git_number_same_branch(self):
  228. actual = self._test_git_number(
  229. 'Parent\n'
  230. '\n'
  231. 'Cr-Commit-Position: refs/heads/master@{#12}',
  232. dest_ref='refs/heads/master',
  233. child_msg='Child')
  234. self.assertEqualByLine(
  235. actual,
  236. 'Child\n'
  237. '\n'
  238. 'Cr-Commit-Position: refs/heads/master@{#13}')
  239. def test_git_number_same_branch_mixed_footers(self):
  240. actual = self._test_git_number(
  241. 'Parent\n'
  242. '\n'
  243. 'Cr-Commit-Position: refs/heads/master@{#12}',
  244. dest_ref='refs/heads/master',
  245. child_msg='Child\n'
  246. '\n'
  247. 'Broken-by: design\n'
  248. 'BUG=123')
  249. self.assertEqualByLine(
  250. actual,
  251. 'Child\n'
  252. '\n'
  253. 'Broken-by: design\n'
  254. 'BUG=123\n'
  255. 'Cr-Commit-Position: refs/heads/master@{#13}')
  256. def test_git_number_same_branch_with_originals(self):
  257. actual = self._test_git_number(
  258. 'Parent\n'
  259. '\n'
  260. 'Cr-Commit-Position: refs/heads/master@{#12}',
  261. dest_ref='refs/heads/master',
  262. child_msg='Child\n'
  263. '\n'
  264. 'Some users are smart and insert their own footers\n'
  265. '\n'
  266. 'Cr-Whatever: value\n'
  267. 'Cr-Commit-Position: refs/copy/paste@{#22}')
  268. self.assertEqualByLine(
  269. actual,
  270. 'Child\n'
  271. '\n'
  272. 'Some users are smart and insert their own footers\n'
  273. '\n'
  274. 'Cr-Original-Whatever: value\n'
  275. 'Cr-Original-Commit-Position: refs/copy/paste@{#22}\n'
  276. 'Cr-Commit-Position: refs/heads/master@{#13}')
  277. def test_git_number_new_branch(self):
  278. actual = self._test_git_number(
  279. 'Parent\n'
  280. '\n'
  281. 'Cr-Commit-Position: refs/heads/master@{#12}',
  282. dest_ref='refs/heads/branch',
  283. child_msg='Child')
  284. self.assertEqualByLine(
  285. actual,
  286. 'Child\n'
  287. '\n'
  288. 'Cr-Commit-Position: refs/heads/branch@{#1}\n'
  289. 'Cr-Branched-From: parenthash-refs/heads/master@{#12}')
  290. def test_git_number_lineage(self):
  291. actual = self._test_git_number(
  292. 'Parent\n'
  293. '\n'
  294. 'Cr-Commit-Position: refs/heads/branch@{#1}\n'
  295. 'Cr-Branched-From: somehash-refs/heads/master@{#12}',
  296. dest_ref='refs/heads/branch',
  297. child_msg='Child')
  298. self.assertEqualByLine(
  299. actual,
  300. 'Child\n'
  301. '\n'
  302. 'Cr-Commit-Position: refs/heads/branch@{#2}\n'
  303. 'Cr-Branched-From: somehash-refs/heads/master@{#12}')
  304. def test_git_number_moooooooore_lineage(self):
  305. actual = self._test_git_number(
  306. 'Parent\n'
  307. '\n'
  308. 'Cr-Commit-Position: refs/heads/branch@{#5}\n'
  309. 'Cr-Branched-From: somehash-refs/heads/master@{#12}',
  310. dest_ref='refs/heads/mooore',
  311. child_msg='Child')
  312. self.assertEqualByLine(
  313. actual,
  314. 'Child\n'
  315. '\n'
  316. 'Cr-Commit-Position: refs/heads/mooore@{#1}\n'
  317. 'Cr-Branched-From: parenthash-refs/heads/branch@{#5}\n'
  318. 'Cr-Branched-From: somehash-refs/heads/master@{#12}')
  319. def test_git_number_ever_moooooooore_lineage(self):
  320. self.maxDiff = 10000 # pylint: disable=attribute-defined-outside-init
  321. actual = self._test_git_number(
  322. 'CQ commit on fresh new branch + numbering.\n'
  323. '\n'
  324. 'NOTRY=True\n'
  325. 'NOPRESUBMIT=True\n'
  326. 'BUG=\n'
  327. '\n'
  328. 'Review-Url: https://codereview.chromium.org/2577703003\n'
  329. 'Cr-Commit-Position: refs/heads/gnumb-test/br@{#1}\n'
  330. 'Cr-Branched-From: 0749ff9edc-refs/heads/gnumb-test/cq@{#4}\n'
  331. 'Cr-Branched-From: 5c49df2da6-refs/heads/master@{#41618}',
  332. dest_ref='refs/heads/gnumb-test/cl',
  333. child_msg='git cl on fresh new branch + numbering.\n'
  334. '\n'
  335. 'Review-Url: https://codereview.chromium.org/2575043003 .\n')
  336. self.assertEqualByLine(
  337. actual,
  338. 'git cl on fresh new branch + numbering.\n'
  339. '\n'
  340. 'Review-Url: https://codereview.chromium.org/2575043003 .\n'
  341. 'Cr-Commit-Position: refs/heads/gnumb-test/cl@{#1}\n'
  342. 'Cr-Branched-From: parenthash-refs/heads/gnumb-test/br@{#1}\n'
  343. 'Cr-Branched-From: 0749ff9edc-refs/heads/gnumb-test/cq@{#4}\n'
  344. 'Cr-Branched-From: 5c49df2da6-refs/heads/master@{#41618}')
  345. def test_git_number_cherry_pick(self):
  346. actual = self._test_git_number(
  347. 'Parent\n'
  348. '\n'
  349. 'Cr-Commit-Position: refs/heads/branch@{#1}\n'
  350. 'Cr-Branched-From: somehash-refs/heads/master@{#12}',
  351. dest_ref='refs/heads/branch',
  352. child_msg='Child, which is cherry-pick from master\n'
  353. '\n'
  354. 'Cr-Commit-Position: refs/heads/master@{#100}\n'
  355. '(cherry picked from commit deadbeef12345678deadbeef12345678deadbeef)')
  356. self.assertEqualByLine(
  357. actual,
  358. 'Child, which is cherry-pick from master\n'
  359. '\n'
  360. '(cherry picked from commit deadbeef12345678deadbeef12345678deadbeef)\n'
  361. '\n'
  362. 'Cr-Original-Commit-Position: refs/heads/master@{#100}\n'
  363. 'Cr-Commit-Position: refs/heads/branch@{#2}\n'
  364. 'Cr-Branched-From: somehash-refs/heads/master@{#12}')
  365. def test_gerrit_mirror_hack(self):
  366. cr = 'chromium-review.googlesource.com'
  367. url0 = 'https://%s/a/changes/x?a=b' % cr
  368. origMirrors = git_cl.gerrit_util._GERRIT_MIRROR_PREFIXES
  369. try:
  370. git_cl.gerrit_util._GERRIT_MIRROR_PREFIXES = ['us1', 'us2']
  371. url1 = git_cl.gerrit_util._UseGerritMirror(url0, cr)
  372. url2 = git_cl.gerrit_util._UseGerritMirror(url1, cr)
  373. url3 = git_cl.gerrit_util._UseGerritMirror(url2, cr)
  374. self.assertNotEqual(url1, url2)
  375. self.assertEqual(sorted((url1, url2)), [
  376. 'https://us1-mirror-chromium-review.googlesource.com/a/changes/x?a=b',
  377. 'https://us2-mirror-chromium-review.googlesource.com/a/changes/x?a=b'])
  378. self.assertEqual(url1, url3)
  379. finally:
  380. git_cl.gerrit_util._GERRIT_MIRROR_PREFIXES = origMirrors
  381. def test_valid_accounts(self):
  382. mock_per_account = {
  383. 'u1': None, # 404, doesn't exist.
  384. 'u2': {
  385. '_account_id': 123124,
  386. 'avatars': [],
  387. 'email': 'u2@example.com',
  388. 'name': 'User Number 2',
  389. 'status': 'OOO',
  390. },
  391. 'u3': git_cl.gerrit_util.GerritError(500, 'retries didn\'t help :('),
  392. }
  393. def GetAccountDetailsMock(_, account):
  394. # Poor-man's mock library's side_effect.
  395. v = mock_per_account.pop(account)
  396. if isinstance(v, Exception):
  397. raise v
  398. return v
  399. original = git_cl.gerrit_util.GetAccountDetails
  400. try:
  401. git_cl.gerrit_util.GetAccountDetails = GetAccountDetailsMock
  402. actual = git_cl.gerrit_util.ValidAccounts(
  403. 'host', ['u1', 'u2', 'u3'], max_threads=1)
  404. finally:
  405. git_cl.gerrit_util.GetAccountDetails = original
  406. self.assertEqual(actual, {
  407. 'u2': {
  408. '_account_id': 123124,
  409. 'avatars': [],
  410. 'email': 'u2@example.com',
  411. 'name': 'User Number 2',
  412. 'status': 'OOO',
  413. },
  414. })
  415. class TestParseIssueURL(unittest.TestCase):
  416. def _validate(self, parsed, issue=None, patchset=None, hostname=None,
  417. fail=False):
  418. self.assertIsNotNone(parsed)
  419. if fail:
  420. self.assertFalse(parsed.valid)
  421. return
  422. self.assertTrue(parsed.valid)
  423. self.assertEqual(parsed.issue, issue)
  424. self.assertEqual(parsed.patchset, patchset)
  425. self.assertEqual(parsed.hostname, hostname)
  426. def test_ParseIssueNumberArgument(self):
  427. def test(arg, *args, **kwargs):
  428. self._validate(git_cl.ParseIssueNumberArgument(arg), *args, **kwargs)
  429. test('123', 123)
  430. test('', fail=True)
  431. test('abc', fail=True)
  432. test('123/1', fail=True)
  433. test('123a', fail=True)
  434. test('ssh://chrome-review.source.com/#/c/123/4/', fail=True)
  435. test('https://codereview.source.com/123',
  436. 123, None, 'codereview.source.com')
  437. test('http://chrome-review.source.com/c/123',
  438. 123, None, 'chrome-review.source.com')
  439. test('https://chrome-review.source.com/c/123/',
  440. 123, None, 'chrome-review.source.com')
  441. test('https://chrome-review.source.com/c/123/4',
  442. 123, 4, 'chrome-review.source.com')
  443. test('https://chrome-review.source.com/#/c/123/4',
  444. 123, 4, 'chrome-review.source.com')
  445. test('https://chrome-review.source.com/c/123/4',
  446. 123, 4, 'chrome-review.source.com')
  447. test('https://chrome-review.source.com/123',
  448. 123, None, 'chrome-review.source.com')
  449. test('https://chrome-review.source.com/123/4',
  450. 123, 4, 'chrome-review.source.com')
  451. test('https://chrome-review.source.com/bad/123/4', fail=True)
  452. test('https://chrome-review.source.com/c/123/1/whatisthis', fail=True)
  453. test('https://chrome-review.source.com/c/abc/', fail=True)
  454. test('ssh://chrome-review.source.com/c/123/1/', fail=True)
  455. class GitCookiesCheckerTest(TestCase):
  456. def setUp(self):
  457. super(GitCookiesCheckerTest, self).setUp()
  458. self.c = git_cl._GitCookiesChecker()
  459. self.c._all_hosts = []
  460. def mock_hosts_creds(self, subhost_identity_pairs):
  461. def ensure_googlesource(h):
  462. if not h.endswith(self.c._GOOGLESOURCE):
  463. assert not h.endswith('.')
  464. return h + '.' + self.c._GOOGLESOURCE
  465. return h
  466. self.c._all_hosts = [(ensure_googlesource(h), i, '.gitcookies')
  467. for h, i in subhost_identity_pairs]
  468. def test_identity_parsing(self):
  469. self.assertEqual(self.c._parse_identity('ldap.google.com'),
  470. ('ldap', 'google.com'))
  471. self.assertEqual(self.c._parse_identity('git-ldap.example.com'),
  472. ('ldap', 'example.com'))
  473. # Specical case because we know there are no subdomains in chromium.org.
  474. self.assertEqual(self.c._parse_identity('git-note.period.chromium.org'),
  475. ('note.period', 'chromium.org'))
  476. # Pathological: ".period." can be either username OR domain, more likely
  477. # domain.
  478. self.assertEqual(self.c._parse_identity('git-note.period.example.com'),
  479. ('note', 'period.example.com'))
  480. def test_analysis_nothing(self):
  481. self.c._all_hosts = []
  482. self.assertFalse(self.c.has_generic_host())
  483. self.assertEqual(set(), self.c.get_conflicting_hosts())
  484. self.assertEqual(set(), self.c.get_duplicated_hosts())
  485. self.assertEqual(set(), self.c.get_partially_configured_hosts())
  486. self.assertEqual(set(), self.c.get_hosts_with_wrong_identities())
  487. def test_analysis(self):
  488. self.mock_hosts_creds([
  489. ('.googlesource.com', 'git-example.chromium.org'),
  490. ('chromium', 'git-example.google.com'),
  491. ('chromium-review', 'git-example.google.com'),
  492. ('chrome-internal', 'git-example.chromium.org'),
  493. ('chrome-internal-review', 'git-example.chromium.org'),
  494. ('conflict', 'git-example.google.com'),
  495. ('conflict-review', 'git-example.chromium.org'),
  496. ('dup', 'git-example.google.com'),
  497. ('dup', 'git-example.google.com'),
  498. ('dup-review', 'git-example.google.com'),
  499. ('partial', 'git-example.google.com'),
  500. ('gpartial-review', 'git-example.google.com'),
  501. ])
  502. self.assertTrue(self.c.has_generic_host())
  503. self.assertEqual(set(['conflict.googlesource.com']),
  504. self.c.get_conflicting_hosts())
  505. self.assertEqual(set(['dup.googlesource.com']),
  506. self.c.get_duplicated_hosts())
  507. self.assertEqual(set(['partial.googlesource.com',
  508. 'gpartial-review.googlesource.com']),
  509. self.c.get_partially_configured_hosts())
  510. self.assertEqual(set(['chromium.googlesource.com',
  511. 'chrome-internal.googlesource.com']),
  512. self.c.get_hosts_with_wrong_identities())
  513. def test_report_no_problems(self):
  514. self.test_analysis_nothing()
  515. self.mock(sys, 'stdout', StringIO.StringIO())
  516. self.assertFalse(self.c.find_and_report_problems())
  517. self.assertEqual(sys.stdout.getvalue(), '')
  518. def test_report(self):
  519. self.test_analysis()
  520. self.mock(sys, 'stdout', StringIO.StringIO())
  521. self.mock(git_cl.gerrit_util.CookiesAuthenticator, 'get_gitcookies_path',
  522. classmethod(lambda _: '~/.gitcookies'))
  523. self.assertTrue(self.c.find_and_report_problems())
  524. with open(os.path.join(os.path.dirname(__file__),
  525. 'git_cl_creds_check_report.txt')) as f:
  526. expected = f.read()
  527. def by_line(text):
  528. return [l.rstrip() for l in text.rstrip().splitlines()]
  529. self.maxDiff = 10000 # pylint: disable=attribute-defined-outside-init
  530. self.assertEqual(by_line(sys.stdout.getvalue().strip()), by_line(expected))
  531. class TestGitCl(TestCase):
  532. def setUp(self):
  533. super(TestGitCl, self).setUp()
  534. self.calls = []
  535. self._calls_done = []
  536. self.mock(git_cl, 'time_time',
  537. lambda: self._mocked_call('time.time'))
  538. self.mock(git_cl.metrics.collector, 'add_repeated',
  539. lambda *a: self._mocked_call('add_repeated', *a))
  540. self.mock(subprocess2, 'call', self._mocked_call)
  541. self.mock(subprocess2, 'check_call', self._mocked_call)
  542. self.mock(subprocess2, 'check_output', self._mocked_call)
  543. self.mock(subprocess2, 'communicate',
  544. lambda *a, **kw: ([self._mocked_call(*a, **kw), ''], 0))
  545. self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call)
  546. self.mock(git_common, 'is_dirty_git_tree', lambda x: False)
  547. self.mock(git_common, 'get_or_create_merge_base',
  548. lambda *a: (
  549. self._mocked_call(['get_or_create_merge_base'] + list(a))))
  550. self.mock(git_cl, 'BranchExists', lambda _: True)
  551. self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
  552. self.mock(git_cl, 'SaveDescriptionBackup', lambda _:
  553. self._mocked_call('SaveDescriptionBackup'))
  554. self.mock(git_cl, 'ask_for_data', lambda *a, **k: self._mocked_call(
  555. *(['ask_for_data'] + list(a)), **k))
  556. self.mock(git_cl, 'write_json', lambda path, contents:
  557. self._mocked_call('write_json', path, contents))
  558. self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
  559. self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
  560. self.mock(git_cl.auth, 'get_authenticator', AuthenticatorMock)
  561. self.mock(git_cl.gerrit_util, 'GetChangeDetail',
  562. lambda *args, **kwargs: self._mocked_call(
  563. 'GetChangeDetail', *args, **kwargs))
  564. self.mock(git_cl.gerrit_util, 'GetChangeComments',
  565. lambda *args, **kwargs: self._mocked_call(
  566. 'GetChangeComments', *args, **kwargs))
  567. self.mock(git_cl.gerrit_util, 'GetChangeRobotComments',
  568. lambda *args, **kwargs: self._mocked_call(
  569. 'GetChangeRobotComments', *args, **kwargs))
  570. self.mock(git_cl.gerrit_util, 'AddReviewers',
  571. lambda h, i, reviewers, ccs, notify: self._mocked_call(
  572. 'AddReviewers', h, i, reviewers, ccs, notify))
  573. self.mock(git_cl.gerrit_util, 'SetReview',
  574. lambda h, i, msg=None, labels=None, notify=None:
  575. self._mocked_call('SetReview', h, i, msg, labels, notify))
  576. self.mock(git_cl.gerrit_util.LuciContextAuthenticator, 'is_luci',
  577. staticmethod(lambda: False))
  578. self.mock(git_cl.gerrit_util.GceAuthenticator, 'is_gce',
  579. classmethod(lambda _: False))
  580. self.mock(git_cl.gerrit_util, 'ValidAccounts',
  581. lambda host, accounts:
  582. self._mocked_call('ValidAccounts', host, accounts))
  583. self.mock(git_cl, 'DieWithError',
  584. lambda msg, change=None: self._mocked_call(['DieWithError', msg]))
  585. # It's important to reset settings to not have inter-tests interference.
  586. git_cl.settings = None
  587. def tearDown(self):
  588. try:
  589. self.assertEqual([], self.calls)
  590. except AssertionError:
  591. if not self.has_failed():
  592. raise
  593. # Sadly, has_failed() returns True if this OR any other tests before this
  594. # one have failed.
  595. git_cl.logging.error(
  596. '!!!!!! IF YOU SEE THIS, READ BELOW, IT WILL SAVE YOUR TIME !!!!!\n'
  597. 'There are un-consumed self.calls after this test has finished.\n'
  598. 'If you don\'t know which test this is, run:\n'
  599. ' tests/git_cl_tests.py -v\n'
  600. 'If you are already running only this test, then **first** fix the '
  601. 'problem whose exception is emitted below by unittest runner.\n'
  602. 'Else, to be sure what\'s going on, run this test **alone** with \n'
  603. ' tests/git_cl_tests.py TestGitCl.<name>\n'
  604. 'and follow instructions above.\n' +
  605. '=' * 80)
  606. finally:
  607. super(TestGitCl, self).tearDown()
  608. def _mocked_call(self, *args, **_kwargs):
  609. self.assertTrue(
  610. self.calls,
  611. '@%d Expected: <Missing> Actual: %r' % (len(self._calls_done), args))
  612. top = self.calls.pop(0)
  613. expected_args, result = top
  614. # Also logs otherwise it could get caught in a try/finally and be hard to
  615. # diagnose.
  616. if expected_args != args:
  617. N = 5
  618. prior_calls = '\n '.join(
  619. '@%d: %r' % (len(self._calls_done) - N + i, c[0])
  620. for i, c in enumerate(self._calls_done[-N:]))
  621. following_calls = '\n '.join(
  622. '@%d: %r' % (len(self._calls_done) + i + 1, c[0])
  623. for i, c in enumerate(self.calls[:N]))
  624. extended_msg = (
  625. 'A few prior calls:\n %s\n\n'
  626. 'This (expected):\n @%d: %r\n'
  627. 'This (actual):\n @%d: %r\n\n'
  628. 'A few following expected calls:\n %s' %
  629. (prior_calls, len(self._calls_done), expected_args,
  630. len(self._calls_done), args, following_calls))
  631. git_cl.logging.error(extended_msg)
  632. self.fail('@%d\n'
  633. ' Expected: %r\n'
  634. ' Actual: %r' % (
  635. len(self._calls_done), expected_args, args))
  636. self._calls_done.append(top)
  637. if isinstance(result, Exception):
  638. raise result
  639. return result
  640. def test_ask_for_explicit_yes_true(self):
  641. self.calls = [
  642. (('ask_for_data', 'prompt [Yes/No]: '), 'blah'),
  643. (('ask_for_data', 'Please, type yes or no: '), 'ye'),
  644. ]
  645. self.assertTrue(git_cl.ask_for_explicit_yes('prompt'))
  646. def test_LoadCodereviewSettingsFromFile_gerrit(self):
  647. codereview_file = StringIO.StringIO('GERRIT_HOST: true')
  648. self.calls = [
  649. ((['git', 'config', '--unset-all', 'rietveld.cc'],), CERR1),
  650. ((['git', 'config', '--unset-all', 'rietveld.tree-status-url'],), CERR1),
  651. ((['git', 'config', '--unset-all', 'rietveld.viewvc-url'],), CERR1),
  652. ((['git', 'config', '--unset-all', 'rietveld.bug-prefix'],), CERR1),
  653. ((['git', 'config', '--unset-all', 'rietveld.cpplint-regex'],), CERR1),
  654. ((['git', 'config', '--unset-all', 'rietveld.cpplint-ignore-regex'],),
  655. CERR1),
  656. ((['git', 'config', '--unset-all', 'rietveld.run-post-upload-hook'],),
  657. CERR1),
  658. ((['git', 'config', 'gerrit.host', 'true'],), ''),
  659. ]
  660. self.assertIsNone(git_cl.LoadCodereviewSettingsFromFile(codereview_file))
  661. @classmethod
  662. def _is_gerrit_calls(cls, gerrit=False):
  663. return [((['git', 'config', 'rietveld.autoupdate'],), ''),
  664. ((['git', 'config', 'gerrit.host'],), 'True' if gerrit else '')]
  665. @classmethod
  666. def _git_post_upload_calls(cls):
  667. return [
  668. ((['git', 'rev-parse', 'HEAD'],), 'hash'),
  669. ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
  670. ((['git',
  671. 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
  672. ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
  673. ]
  674. @staticmethod
  675. def _git_sanity_checks(diff_base, working_branch, get_remote_branch=True):
  676. fake_ancestor = 'fake_ancestor'
  677. fake_cl = 'fake_cl_for_patch'
  678. return [
  679. ((['git',
  680. 'rev-parse', '--verify', diff_base],), fake_ancestor),
  681. ((['git',
  682. 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
  683. ((['git',
  684. 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
  685. # Mock a config miss (error code 1)
  686. ((['git',
  687. 'config', 'gitcl.remotebranch'],), CERR1),
  688. ] + ([
  689. # Call to GetRemoteBranch()
  690. ((['git',
  691. 'config', 'branch.%s.merge' % working_branch],),
  692. 'refs/heads/master'),
  693. ((['git',
  694. 'config', 'branch.%s.remote' % working_branch],), 'origin'),
  695. ] if get_remote_branch else []) + [
  696. ((['git', 'rev-list', '^' + fake_ancestor,
  697. 'refs/remotes/origin/master'],), ''),
  698. ]
  699. @classmethod
  700. def _gerrit_ensure_auth_calls(
  701. cls, issue=None, skip_auth_check=False, short_hostname='chromium',
  702. custom_cl_base=None):
  703. cmd = ['git', 'config', '--bool', 'gerrit.skip-ensure-authenticated']
  704. if skip_auth_check:
  705. return [((cmd, ), 'true')]
  706. calls = [((cmd, ), CERR1)]
  707. if custom_cl_base:
  708. calls += [
  709. ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
  710. ]
  711. calls.extend([
  712. ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'),
  713. ((['git', 'config', 'branch.master.remote'],), 'origin'),
  714. ((['git', 'config', 'remote.origin.url'],),
  715. 'https://%s.googlesource.com/my/repo' % short_hostname),
  716. ])
  717. calls += [
  718. ((['git', 'config', 'branch.master.gerritissue'],),
  719. CERR1 if issue is None else str(issue)),
  720. ]
  721. if issue:
  722. calls.extend([
  723. ((['git', 'config', 'branch.master.gerritserver'],), CERR1),
  724. ])
  725. return calls
  726. @classmethod
  727. def _gerrit_base_calls(cls, issue=None, fetched_description=None,
  728. fetched_status=None, other_cl_owner=None,
  729. custom_cl_base=None, short_hostname='chromium',
  730. change_id=None):
  731. calls = cls._is_gerrit_calls(True)
  732. if not custom_cl_base:
  733. calls += [
  734. ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
  735. ]
  736. if custom_cl_base:
  737. ancestor_revision = custom_cl_base
  738. else:
  739. # Determine ancestor_revision to be merge base.
  740. ancestor_revision = 'fake_ancestor_sha'
  741. calls += [
  742. ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'),
  743. ((['git', 'config', 'branch.master.remote'],), 'origin'),
  744. ((['get_or_create_merge_base', 'master',
  745. 'refs/remotes/origin/master'],), ancestor_revision),
  746. ]
  747. # Calls to verify branch point is ancestor
  748. calls += cls._gerrit_ensure_auth_calls(
  749. issue=issue, short_hostname=short_hostname,
  750. custom_cl_base=custom_cl_base)
  751. if issue:
  752. calls += [
  753. (('GetChangeDetail', '%s-review.googlesource.com' % short_hostname,
  754. 'my%2Frepo~123456',
  755. ['DETAILED_ACCOUNTS', 'CURRENT_REVISION', 'CURRENT_COMMIT', 'LABELS']
  756. ),
  757. {
  758. 'owner': {'email': (other_cl_owner or 'owner@example.com')},
  759. 'change_id': (change_id or '123456789'),
  760. 'current_revision': 'sha1_of_current_revision',
  761. 'revisions': {'sha1_of_current_revision': {
  762. 'commit': {'message': fetched_description},
  763. }},
  764. 'status': fetched_status or 'NEW',
  765. }),
  766. ]
  767. if fetched_status == 'ABANDONED':
  768. calls += [
  769. (('DieWithError', 'Change https://%s-review.googlesource.com/'
  770. '123456 has been abandoned, new uploads are not '
  771. 'allowed' % short_hostname), SystemExitMock()),
  772. ]
  773. return calls
  774. if other_cl_owner:
  775. calls += [
  776. (('ask_for_data', 'Press Enter to upload, or Ctrl+C to abort'), ''),
  777. ]
  778. calls += cls._git_sanity_checks(ancestor_revision, 'master',
  779. get_remote_branch=False)
  780. calls += [
  781. ((['git', 'rev-parse', '--show-cdup'],), ''),
  782. ((['git', 'rev-parse', 'HEAD'],), '12345'),
  783. ((['git', '-c', 'core.quotePath=false', 'diff', '--name-status',
  784. '--no-renames', '-r', ancestor_revision + '...', '.'],),
  785. 'M\t.gitignore\n'),
  786. ((['git', 'config', 'branch.master.gerritpatchset'],), CERR1),
  787. ]
  788. if not issue:
  789. calls += [
  790. ((['git', 'log', '--pretty=format:%s%n%n%b',
  791. ancestor_revision + '...'],),
  792. 'foo'),
  793. ]
  794. calls += [
  795. ((['git', 'config', 'user.email'],), 'me@example.com'),
  796. (('time.time',), 1000,),
  797. (('time.time',), 3000,),
  798. (('add_repeated', 'sub_commands', {
  799. 'execution_time': 2000,
  800. 'command': 'presubmit',
  801. 'exit_code': 0
  802. }), None,),
  803. ((['git', 'diff', '--no-ext-diff', '--stat', '-l100000', '-C50'] +
  804. ([custom_cl_base] if custom_cl_base else
  805. [ancestor_revision, 'HEAD']),),
  806. '+dat'),
  807. ]
  808. return calls
  809. @classmethod
  810. def _gerrit_upload_calls(cls, description, reviewers, squash,
  811. squash_mode='default',
  812. expected_upstream_ref='origin/refs/heads/master',
  813. title=None, notify=False,
  814. post_amend_description=None, issue=None, cc=None,
  815. custom_cl_base=None, tbr=None,
  816. short_hostname='chromium',
  817. labels=None, change_id=None, original_title=None,
  818. final_description=None, gitcookies_exists=True,
  819. force=False):
  820. if post_amend_description is None:
  821. post_amend_description = description
  822. cc = cc or []
  823. # Determined in `_gerrit_base_calls`.
  824. determined_ancestor_revision = custom_cl_base or 'fake_ancestor_sha'
  825. calls = []
  826. if squash_mode == 'default':
  827. calls.extend([
  828. ((['git', 'config', '--bool', 'gerrit.override-squash-uploads'],), ''),
  829. ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), ''),
  830. ])
  831. elif squash_mode in ('override_squash', 'override_nosquash'):
  832. calls.extend([
  833. ((['git', 'config', '--bool', 'gerrit.override-squash-uploads'],),
  834. 'true' if squash_mode == 'override_squash' else 'false'),
  835. ])
  836. else:
  837. assert squash_mode in ('squash', 'nosquash')
  838. # If issue is given, then description is fetched from Gerrit instead.
  839. if issue is None:
  840. calls += [
  841. ((['git', 'log', '--pretty=format:%s\n\n%b',
  842. ((custom_cl_base + '..') if custom_cl_base else
  843. 'fake_ancestor_sha..HEAD')],),
  844. description),
  845. ]
  846. if squash:
  847. title = 'Initial_upload'
  848. else:
  849. if not title:
  850. calls += [
  851. ((['git', 'show', '-s', '--format=%s', 'HEAD'],), ''),
  852. (('ask_for_data', 'Title for patchset []: '), 'User input'),
  853. ]
  854. title = 'User_input'
  855. if not git_footers.get_footer_change_id(description) and not squash:
  856. calls += [
  857. (('DownloadGerritHook', False), ''),
  858. # Amending of commit message to get the Change-Id.
  859. ((['git', 'log', '--pretty=format:%s\n\n%b',
  860. determined_ancestor_revision + '..HEAD'],),
  861. description),
  862. ((['git', 'commit', '--amend', '-m', description],), ''),
  863. ((['git', 'log', '--pretty=format:%s\n\n%b',
  864. determined_ancestor_revision + '..HEAD'],),
  865. post_amend_description)
  866. ]
  867. if squash:
  868. if force or not issue:
  869. if issue:
  870. calls += [
  871. ((['git', 'config', 'rietveld.bug-prefix'],), ''),
  872. ]
  873. # Prompting to edit description on first upload.
  874. calls += [
  875. ((['git', 'config', 'rietveld.bug-prefix'],), ''),
  876. ]
  877. if not force:
  878. calls += [
  879. ((['git', 'config', 'core.editor'],), ''),
  880. ((['RunEditor'],), description),
  881. ]
  882. ref_to_push = 'abcdef0123456789'
  883. calls += [
  884. ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'),
  885. ((['git', 'config', 'branch.master.remote'],), 'origin'),
  886. ]
  887. if custom_cl_base is None:
  888. calls += [
  889. ((['get_or_create_merge_base', 'master',
  890. 'refs/remotes/origin/master'],),
  891. 'origin/master'),
  892. ]
  893. parent = 'origin/master'
  894. else:
  895. calls += [
  896. ((['git', 'merge-base', '--is-ancestor', custom_cl_base,
  897. 'refs/remotes/origin/master'],),
  898. callError(1)), # Means not ancenstor.
  899. (('ask_for_data',
  900. 'Do you take responsibility for cleaning up potential mess '
  901. 'resulting from proceeding with upload? Press Enter to upload, '
  902. 'or Ctrl+C to abort'), ''),
  903. ]
  904. parent = custom_cl_base
  905. calls += [
  906. ((['git', 'rev-parse', 'HEAD:'],), # `HEAD:` means HEAD's tree hash.
  907. '0123456789abcdef'),
  908. ((['git', 'commit-tree', '0123456789abcdef', '-p', parent,
  909. '-F', '/tmp/named'],),
  910. ref_to_push),
  911. ]
  912. else:
  913. ref_to_push = 'HEAD'
  914. calls += [
  915. (('SaveDescriptionBackup',), None),
  916. ((['git', 'rev-list',
  917. (custom_cl_base if custom_cl_base else expected_upstream_ref) + '..' +
  918. ref_to_push],),
  919. '1hashPerLine\n'),
  920. ]
  921. metrics_arguments = []
  922. if notify:
  923. ref_suffix = '%ready,notify=ALL'
  924. metrics_arguments += ['ready', 'notify=ALL']
  925. else:
  926. if not issue and squash:
  927. ref_suffix = '%wip'
  928. metrics_arguments.append('wip')
  929. else:
  930. ref_suffix = '%notify=NONE'
  931. metrics_arguments.append('notify=NONE')
  932. if title:
  933. ref_suffix += ',m=' + title
  934. metrics_arguments.append('m')
  935. if issue is None:
  936. calls += [
  937. ((['git', 'config', 'rietveld.cc'],), ''),
  938. ]
  939. if short_hostname == 'chromium':
  940. # All reviwers and ccs get into ref_suffix.
  941. for r in sorted(reviewers):
  942. ref_suffix += ',r=%s' % r
  943. metrics_arguments.append('r')
  944. if issue is None:
  945. cc += ['chromium-reviews+test-more-cc@chromium.org', 'joe@example.com']
  946. for c in sorted(cc):
  947. ref_suffix += ',cc=%s' % c
  948. metrics_arguments.append('cc')
  949. reviewers, cc = [], []
  950. else:
  951. # TODO(crbug/877717): remove this case.
  952. calls += [
  953. (('ValidAccounts', '%s-review.googlesource.com' % short_hostname,
  954. sorted(reviewers) + ['joe@example.com',
  955. 'chromium-reviews+test-more-cc@chromium.org'] + cc),
  956. {
  957. e: {'email': e}
  958. for e in (reviewers + ['joe@example.com'] + cc)
  959. })
  960. ]
  961. for r in sorted(reviewers):
  962. if r != 'bad-account-or-email':
  963. ref_suffix += ',r=%s' % r
  964. metrics_arguments.append('r')
  965. reviewers.remove(r)
  966. if issue is None:
  967. cc += ['joe@example.com']
  968. for c in sorted(cc):
  969. ref_suffix += ',cc=%s' % c
  970. metrics_arguments.append('cc')
  971. if c in cc:
  972. cc.remove(c)
  973. for k, v in sorted((labels or {}).items()):
  974. ref_suffix += ',l=%s+%d' % (k, v)
  975. metrics_arguments.append('l=%s+%d' % (k, v))
  976. if tbr:
  977. calls += [
  978. (('GetCodeReviewTbrScore',
  979. '%s-review.googlesource.com' % short_hostname,
  980. 'my/repo'),
  981. 2,),
  982. ]
  983. calls += [
  984. (('time.time',), 1000,),
  985. ((['git', 'push',
  986. 'https://%s.googlesource.com/my/repo' % short_hostname,
  987. ref_to_push + ':refs/for/refs/heads/master' + ref_suffix],),
  988. (('remote:\n'
  989. 'remote: Processing changes: (\)\n'
  990. 'remote: Processing changes: (|)\n'
  991. 'remote: Processing changes: (/)\n'
  992. 'remote: Processing changes: (-)\n'
  993. 'remote: Processing changes: new: 1 (/)\n'
  994. 'remote: Processing changes: new: 1, done\n'
  995. 'remote:\n'
  996. 'remote: New Changes:\n'
  997. 'remote: https://%s-review.googlesource.com/#/c/my/repo/+/123456'
  998. ' XXX\n'
  999. 'remote:\n'
  1000. 'To https://%s.googlesource.com/my/repo\n'
  1001. ' * [new branch] hhhh -> refs/for/refs/heads/master\n'
  1002. ) % (short_hostname, short_hostname)),),
  1003. (('time.time',), 2000,),
  1004. (('add_repeated',
  1005. 'sub_commands',
  1006. {
  1007. 'execution_time': 1000,
  1008. 'command': 'git push',
  1009. 'exit_code': 0,
  1010. 'arguments': sorted(metrics_arguments),
  1011. }),
  1012. None,),
  1013. ]
  1014. final_description = final_description or post_amend_description.strip()
  1015. original_title = original_title or title or '<untitled>'
  1016. # Trace-related calls
  1017. calls += [
  1018. # Write a description with context for the current trace.
  1019. ((['FileWrite', 'TRACES_DIR/20170316T200041.000000-README',
  1020. 'Thu Mar 16 20:00:41 2017\n'
  1021. '%(short_hostname)s-review.googlesource.com\n'
  1022. '%(change_id)s\n'
  1023. '%(title)s\n'
  1024. '%(description)s\n'
  1025. '1000\n'
  1026. '0\n'
  1027. '%(trace_name)s' % {
  1028. 'short_hostname': short_hostname,
  1029. 'change_id': change_id,
  1030. 'description': final_description,
  1031. 'title': original_title,
  1032. 'trace_name': 'TRACES_DIR/20170316T200041.000000',
  1033. }],),
  1034. None,
  1035. ),
  1036. # Read traces and shorten git hashes.
  1037. ((['os.path.isfile', 'TEMP_DIR/trace-packet'],),
  1038. True,
  1039. ),
  1040. ((['FileRead', 'TEMP_DIR/trace-packet'],),
  1041. ('git-hash: 0123456789012345678901234567890123456789\n'
  1042. 'git-hash: abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde\n'),
  1043. ),
  1044. ((['FileWrite', 'TEMP_DIR/trace-packet',
  1045. 'git-hash: 012345\n'
  1046. 'git-hash: abcdea\n'],),
  1047. None,
  1048. ),
  1049. # Make zip file for the git traces.
  1050. ((['make_archive', 'TRACES_DIR/20170316T200041.000000-traces', 'zip',
  1051. 'TEMP_DIR'],),
  1052. None,
  1053. ),
  1054. # Collect git config and gitcookies.
  1055. ((['git', 'config', '-l'],),
  1056. 'git-config-output',
  1057. ),
  1058. ((['FileWrite', 'TEMP_DIR/git-config', 'git-config-output'],),
  1059. None,
  1060. ),
  1061. ((['os.path.isfile', '~/.gitcookies'],),
  1062. gitcookies_exists,
  1063. ),
  1064. ]
  1065. if gitcookies_exists:
  1066. calls += [
  1067. ((['FileRead', '~/.gitcookies'],),
  1068. 'gitcookies 1/SECRET',
  1069. ),
  1070. ((['FileWrite', 'TEMP_DIR/gitcookies', 'gitcookies REDACTED'],),
  1071. None,
  1072. ),
  1073. ]
  1074. calls += [
  1075. # Make zip file for the git config and gitcookies.
  1076. ((['make_archive', 'TRACES_DIR/20170316T200041.000000-git-info', 'zip',
  1077. 'TEMP_DIR'],),
  1078. None,
  1079. ),
  1080. ]
  1081. if squash:
  1082. calls += [
  1083. ((['git', 'config', 'branch.master.gerritissue', '123456'],),
  1084. ''),
  1085. ((['git', 'config', 'branch.master.gerritserver',
  1086. 'https://chromium-review.googlesource.com'],), ''),
  1087. ((['git', 'config', 'branch.master.gerritsquashhash',
  1088. 'abcdef0123456789'],), ''),
  1089. ]
  1090. # TODO(crbug/877717): this should never be used.
  1091. if squash and short_hostname != 'chromium':
  1092. calls += [
  1093. (('AddReviewers',
  1094. 'chromium-review.googlesource.com', 'my%2Frepo~123456',
  1095. sorted(reviewers),
  1096. cc + ['chromium-reviews+test-more-cc@chromium.org'],
  1097. notify),
  1098. ''),
  1099. ]
  1100. calls += cls._git_post_upload_calls()
  1101. return calls
  1102. def _run_gerrit_upload_test(
  1103. self,
  1104. upload_args,
  1105. description,
  1106. reviewers=None,
  1107. squash=True,
  1108. squash_mode=None,
  1109. expected_upstream_ref='origin/refs/heads/master',
  1110. title=None,
  1111. notify=False,
  1112. post_amend_description=None,
  1113. issue=None,
  1114. cc=None,
  1115. fetched_status=None,
  1116. other_cl_owner=None,
  1117. custom_cl_base=None,
  1118. tbr=None,
  1119. short_hostname='chromium',
  1120. labels=None,
  1121. change_id=None,
  1122. original_title=None,
  1123. final_description=None,
  1124. gitcookies_exists=True,
  1125. force=False,
  1126. fetched_description=None):
  1127. """Generic gerrit upload test framework."""
  1128. if squash_mode is None:
  1129. if '--no-squash' in upload_args:
  1130. squash_mode = 'nosquash'
  1131. elif '--squash' in upload_args:
  1132. squash_mode = 'squash'
  1133. else:
  1134. squash_mode = 'default'
  1135. reviewers = reviewers or []
  1136. cc = cc or []
  1137. self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
  1138. self.mock(git_cl.gerrit_util, 'CookiesAuthenticator',
  1139. CookiesAuthenticatorMockFactory(
  1140. same_auth=('git-owner.example.com', '', 'pass')))
  1141. self.mock(git_cl.Changelist, '_GerritCommitMsgHookCheck',
  1142. lambda _, offer_removal: None)
  1143. self.mock(git_cl.gclient_utils, 'RunEditor',
  1144. lambda *_, **__: self._mocked_call(['RunEditor']))
  1145. self.mock(git_cl, 'DownloadGerritHook', lambda force: self._mocked_call(
  1146. 'DownloadGerritHook', force))
  1147. self.mock(git_cl.gclient_utils, 'FileRead',
  1148. lambda path: self._mocked_call(['FileRead', path]))
  1149. self.mock(git_cl.gclient_utils, 'FileWrite',
  1150. lambda path, contents: self._mocked_call(
  1151. ['FileWrite', path, contents]))
  1152. self.mock(git_cl, 'datetime_now',
  1153. lambda: datetime.datetime(2017, 3, 16, 20, 0, 41, 0))
  1154. self.mock(git_cl.tempfile, 'mkdtemp', lambda: 'TEMP_DIR')
  1155. self.mock(git_cl, 'TRACES_DIR', 'TRACES_DIR')
  1156. self.mock(git_cl, 'TRACES_README_FORMAT',
  1157. '%(now)s\n'
  1158. '%(gerrit_host)s\n'
  1159. '%(change_id)s\n'
  1160. '%(title)s\n'
  1161. '%(description)s\n'
  1162. '%(execution_time)s\n'
  1163. '%(exit_code)s\n'
  1164. '%(trace_name)s')
  1165. self.mock(git_cl.shutil, 'make_archive',
  1166. lambda *args: self._mocked_call(['make_archive'] + list(args)))
  1167. self.mock(os.path, 'isfile',
  1168. lambda path: self._mocked_call(['os.path.isfile', path]))
  1169. self.calls = self._gerrit_base_calls(
  1170. issue=issue,
  1171. fetched_description=fetched_description or description,
  1172. fetched_status=fetched_status,
  1173. other_cl_owner=other_cl_owner,
  1174. custom_cl_base=custom_cl_base,
  1175. short_hostname=short_hostname,
  1176. change_id=change_id)
  1177. if fetched_status != 'ABANDONED':
  1178. self.mock(tempfile, 'NamedTemporaryFile', MakeNamedTemporaryFileMock(
  1179. expected_content=description))
  1180. self.mock(os, 'remove', lambda _: True)
  1181. self.calls += self._gerrit_upload_calls(
  1182. description, reviewers, squash,
  1183. squash_mode=squash_mode,
  1184. expected_upstream_ref=expected_upstream_ref,
  1185. title=title, notify=notify,
  1186. post_amend_description=post_amend_description,
  1187. issue=issue, cc=cc,
  1188. custom_cl_base=custom_cl_base, tbr=tbr,
  1189. short_hostname=short_hostname,
  1190. labels=labels,
  1191. change_id=change_id,
  1192. original_title=original_title,
  1193. final_description=final_description,
  1194. gitcookies_exists=gitcookies_exists,
  1195. force=force)
  1196. # Uncomment when debugging.
  1197. # print('\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls))))
  1198. git_cl.main(['upload'] + upload_args)
  1199. def test_gerrit_upload_traces_no_gitcookies(self):
  1200. self._run_gerrit_upload_test(
  1201. ['--no-squash'],
  1202. 'desc\n\nBUG=\n',
  1203. [],
  1204. squash=False,
  1205. post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx',
  1206. change_id='Ixxx',
  1207. gitcookies_exists=False)
  1208. def test_gerrit_upload_without_change_id(self):
  1209. self._run_gerrit_upload_test(
  1210. ['--no-squash'],
  1211. 'desc\n\nBUG=\n',
  1212. [],
  1213. squash=False,
  1214. post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx',
  1215. change_id='Ixxx')
  1216. def test_gerrit_upload_without_change_id_override_nosquash(self):
  1217. self._run_gerrit_upload_test(
  1218. [],
  1219. 'desc\n\nBUG=\n',
  1220. [],
  1221. squash=False,
  1222. squash_mode='override_nosquash',
  1223. post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx',
  1224. change_id='Ixxx')
  1225. def test_gerrit_no_reviewer(self):
  1226. self._run_gerrit_upload_test(
  1227. [],
  1228. 'desc\n\nBUG=\n\nChange-Id: I123456789\n',
  1229. [],
  1230. squash=False,
  1231. squash_mode='override_nosquash',
  1232. change_id='I123456789')
  1233. def test_gerrit_no_reviewer_non_chromium_host(self):
  1234. # TODO(crbug/877717): remove this test case.
  1235. self._run_gerrit_upload_test(
  1236. [],
  1237. 'desc\n\nBUG=\n\nChange-Id: I123456789\n',
  1238. [],
  1239. squash=False,
  1240. squash_mode='override_nosquash',
  1241. short_hostname='other',
  1242. change_id='I123456789')
  1243. def test_gerrit_patchset_title_special_chars(self):
  1244. self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
  1245. self._run_gerrit_upload_test(
  1246. ['-f', '-t', 'We\'ll escape ^_ ^ special chars...@{u}'],
  1247. 'desc\n\nBUG=\n\nChange-Id: I123456789',
  1248. squash=False,
  1249. squash_mode='override_nosquash',
  1250. title='We%27ll_escape_%5E%5F_%5E_special_chars%2E%2E%2E%40%7Bu%7D',
  1251. change_id='I123456789',
  1252. original_title='We\'ll escape ^_ ^ special chars...@{u}')
  1253. def test_gerrit_reviewers_cmd_line(self):
  1254. self._run_gerrit_upload_test(
  1255. ['-r', 'foo@example.com', '--send-mail'],
  1256. 'desc\n\nBUG=\n\nChange-Id: I123456789',
  1257. ['foo@example.com'],
  1258. squash=False,
  1259. squash_mode='override_nosquash',
  1260. notify=True,
  1261. change_id='I123456789',
  1262. final_description=(
  1263. 'desc\n\nBUG=\nR=foo@example.com\n\nChange-Id: I123456789'))
  1264. def test_gerrit_upload_force_sets_bug(self):
  1265. self._run_gerrit_upload_test(
  1266. ['-b', '10000', '-f'],
  1267. u'desc=\n\nBug: 10000\nChange-Id: Ixxx',
  1268. [],
  1269. force=True,
  1270. expected_upstream_ref='origin/master',
  1271. fetched_description='desc=\n\nChange-Id: Ixxx',
  1272. original_title='Initial upload',
  1273. change_id='Ixxx')
  1274. def test_gerrit_upload_force_sets_bug_if_wrong_changeid(self):
  1275. self._run_gerrit_upload_test(
  1276. ['-b', '10000', '-f', '-m', 'Title'],
  1277. u'desc=\n\nChange-Id: Ixxxx\n\nChange-Id: Izzzz\nBug: 10000',
  1278. [],
  1279. force=True,
  1280. issue='123456',
  1281. expected_upstream_ref='origin/master',
  1282. fetched_description='desc=\n\nChange-Id: Ixxxx',
  1283. original_title='Title',
  1284. title='Title',
  1285. change_id='Izzzz')
  1286. def test_gerrit_upload_force_sets_fixed(self):
  1287. self._run_gerrit_upload_test(
  1288. ['-x', '10000', '-f'],
  1289. u'desc=\n\nFixed: 10000\nChange-Id: Ixxx',
  1290. [],
  1291. force=True,
  1292. expected_upstream_ref='origin/master',
  1293. fetched_description='desc=\n\nChange-Id: Ixxx',
  1294. original_title='Initial upload',
  1295. change_id='Ixxx')
  1296. def test_gerrit_reviewer_multiple(self):
  1297. self.mock(git_cl.gerrit_util, 'GetCodeReviewTbrScore',
  1298. lambda *a: self._mocked_call('GetCodeReviewTbrScore', *a))
  1299. self._run_gerrit_upload_test(
  1300. [],
  1301. 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n'
  1302. 'CC=more@example.com,people@example.com\n\n'
  1303. 'Change-Id: 123456789',
  1304. ['reviewer@example.com', 'another@example.com'],
  1305. expected_upstream_ref='origin/master',
  1306. cc=['more@example.com', 'people@example.com'],
  1307. tbr='reviewer@example.com',
  1308. labels={'Code-Review': 2},
  1309. change_id='123456789',
  1310. original_title='Initial upload')
  1311. def test_gerrit_upload_squash_first_is_default(self):
  1312. self._run_gerrit_upload_test(
  1313. [],
  1314. 'desc\nBUG=\n\nChange-Id: 123456789',
  1315. [],
  1316. expected_upstream_ref='origin/master',
  1317. change_id='123456789',
  1318. original_title='Initial upload')
  1319. def test_gerrit_upload_squash_first(self):
  1320. self._run_gerrit_upload_test(
  1321. ['--squash'],
  1322. 'desc\nBUG=\n\nChange-Id: 123456789',
  1323. [],
  1324. squash=True,
  1325. expected_upstream_ref='origin/master',
  1326. change_id='123456789',
  1327. original_title='Initial upload')
  1328. def test_gerrit_upload_squash_first_with_labels(self):
  1329. self._run_gerrit_upload_test(
  1330. ['--squash', '--cq-dry-run', '--enable-auto-submit'],
  1331. 'desc\nBUG=\n\nChange-Id: 123456789',
  1332. [],
  1333. squash=True,
  1334. expected_upstream_ref='origin/master',
  1335. labels={'Commit-Queue': 1, 'Auto-Submit': 1},
  1336. change_id='123456789',
  1337. original_title='Initial upload')
  1338. def test_gerrit_upload_squash_first_against_rev(self):
  1339. custom_cl_base = 'custom_cl_base_rev_or_branch'
  1340. self._run_gerrit_upload_test(
  1341. ['--squash', custom_cl_base],
  1342. 'desc\nBUG=\n\nChange-Id: 123456789',
  1343. [],
  1344. squash=True,
  1345. expected_upstream_ref='origin/master',
  1346. custom_cl_base=custom_cl_base,
  1347. change_id='123456789',
  1348. original_title='Initial upload')
  1349. self.assertIn(
  1350. 'If you proceed with upload, more than 1 CL may be created by Gerrit',
  1351. sys.stdout.getvalue())
  1352. def test_gerrit_upload_squash_reupload(self):
  1353. description = 'desc\nBUG=\n\nChange-Id: 123456789'
  1354. self._run_gerrit_upload_test(
  1355. ['--squash'],
  1356. description,
  1357. [],
  1358. squash=True,
  1359. expected_upstream_ref='origin/master',
  1360. issue=123456,
  1361. change_id='123456789',
  1362. original_title='User input')
  1363. def test_gerrit_upload_squash_reupload_to_abandoned(self):
  1364. self.mock(git_cl, 'DieWithError',
  1365. lambda msg, change=None: self._mocked_call('DieWithError', msg))
  1366. description = 'desc\nBUG=\n\nChange-Id: 123456789'
  1367. with self.assertRaises(SystemExitMock):
  1368. self._run_gerrit_upload_test(
  1369. ['--squash'],
  1370. description,
  1371. [],
  1372. squash=True,
  1373. expected_upstream_ref='origin/master',
  1374. issue=123456,
  1375. fetched_status='ABANDONED',
  1376. change_id='123456789')
  1377. def test_gerrit_upload_squash_reupload_to_not_owned(self):
  1378. self.mock(git_cl.gerrit_util, 'GetAccountDetails',
  1379. lambda *_, **__: {'email': 'yet-another@example.com'})
  1380. description = 'desc\nBUG=\n\nChange-Id: 123456789'
  1381. self._run_gerrit_upload_test(
  1382. ['--squash'],
  1383. description,
  1384. [],
  1385. squash=True,
  1386. expected_upstream_ref='origin/master',
  1387. issue=123456,
  1388. other_cl_owner='other@example.com',
  1389. change_id='123456789',
  1390. original_title='User input')
  1391. self.assertIn(
  1392. 'WARNING: Change 123456 is owned by other@example.com, but you '
  1393. 'authenticate to Gerrit as yet-another@example.com.\n'
  1394. 'Uploading may fail due to lack of permissions',
  1395. git_cl.sys.stdout.getvalue())
  1396. def test_upload_branch_deps(self):
  1397. self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
  1398. def mock_run_git(*args, **_kwargs):
  1399. if args[0] == ['for-each-ref',
  1400. '--format=%(refname:short) %(upstream:short)',
  1401. 'refs/heads']:
  1402. # Create a local branch dependency tree that looks like this:
  1403. # test1 -> test2 -> test3 -> test4 -> test5
  1404. # -> test3.1
  1405. # test6 -> test0
  1406. branch_deps = [
  1407. 'test2 test1', # test1 -> test2
  1408. 'test3 test2', # test2 -> test3
  1409. 'test3.1 test2', # test2 -> test3.1
  1410. 'test4 test3', # test3 -> test4
  1411. 'test5 test4', # test4 -> test5
  1412. 'test6 test0', # test0 -> test6
  1413. 'test7', # test7
  1414. ]
  1415. return '\n'.join(branch_deps)
  1416. self.mock(git_cl, 'RunGit', mock_run_git)
  1417. class RecordCalls:
  1418. times_called = 0
  1419. record_calls = RecordCalls()
  1420. def mock_CMDupload(*args, **_kwargs):
  1421. record_calls.times_called += 1
  1422. return 0
  1423. self.mock(git_cl, 'CMDupload', mock_CMDupload)
  1424. self.calls = [
  1425. (('ask_for_data', 'This command will checkout all dependent branches '
  1426. 'and run "git cl upload". Press Enter to continue, '
  1427. 'or Ctrl+C to abort'), ''),
  1428. ]
  1429. class MockChangelist():
  1430. def __init__(self):
  1431. pass
  1432. def GetBranch(self):
  1433. return 'test1'
  1434. def GetIssue(self):
  1435. return '123'
  1436. def GetPatchset(self):
  1437. return '1001'
  1438. def IsGerrit(self):
  1439. return False
  1440. ret = git_cl.upload_branch_deps(MockChangelist(), [])
  1441. # CMDupload should have been called 5 times because of 5 dependent branches.
  1442. self.assertEqual(5, record_calls.times_called)
  1443. self.assertEqual(0, ret)
  1444. def test_gerrit_change_id(self):
  1445. self.calls = [
  1446. ((['git', 'write-tree'], ),
  1447. 'hashtree'),
  1448. ((['git', 'rev-parse', 'HEAD~0'], ),
  1449. 'branch-parent'),
  1450. ((['git', 'var', 'GIT_AUTHOR_IDENT'], ),
  1451. 'A B <a@b.org> 1456848326 +0100'),
  1452. ((['git', 'var', 'GIT_COMMITTER_IDENT'], ),
  1453. 'C D <c@d.org> 1456858326 +0100'),
  1454. ((['git', 'hash-object', '-t', 'commit', '--stdin'], ),
  1455. 'hashchange'),
  1456. ]
  1457. change_id = git_cl.GenerateGerritChangeId('line1\nline2\n')
  1458. self.assertEqual(change_id, 'Ihashchange')
  1459. def test_desecription_append_footer(self):
  1460. for init_desc, footer_line, expected_desc in [
  1461. # Use unique desc first lines for easy test failure identification.
  1462. ('foo', 'R=one', 'foo\n\nR=one'),
  1463. ('foo\n\nR=one', 'BUG=', 'foo\n\nR=one\nBUG='),
  1464. ('foo\n\nR=one', 'Change-Id: Ixx', 'foo\n\nR=one\n\nChange-Id: Ixx'),
  1465. ('foo\n\nChange-Id: Ixx', 'R=one', 'foo\n\nR=one\n\nChange-Id: Ixx'),
  1466. ('foo\n\nR=one\n\nChange-Id: Ixx', 'TBR=two',
  1467. 'foo\n\nR=one\nTBR=two\n\nChange-Id: Ixx'),
  1468. ('foo\n\nR=one\n\nChange-Id: Ixx', 'Foo-Bar: baz',
  1469. 'foo\n\nR=one\n\nChange-Id: Ixx\nFoo-Bar: baz'),
  1470. ('foo\n\nChange-Id: Ixx', 'Foo-Bak: baz',
  1471. 'foo\n\nChange-Id: Ixx\nFoo-Bak: baz'),
  1472. ('foo', 'Change-Id: Ixx', 'foo\n\nChange-Id: Ixx'),
  1473. ]:
  1474. desc = git_cl.ChangeDescription(init_desc)
  1475. desc.append_footer(footer_line)
  1476. self.assertEqual(desc.description, expected_desc)
  1477. def test_update_reviewers(self):
  1478. data = [
  1479. ('foo', [], [],
  1480. 'foo'),
  1481. ('foo\nR=xx', [], [],
  1482. 'foo\nR=xx'),
  1483. ('foo\nTBR=xx', [], [],
  1484. 'foo\nTBR=xx'),
  1485. ('foo', ['a@c'], [],
  1486. 'foo\n\nR=a@c'),
  1487. ('foo\nR=xx', ['a@c'], [],
  1488. 'foo\n\nR=a@c, xx'),
  1489. ('foo\nTBR=xx', ['a@c'], [],
  1490. 'foo\n\nR=a@c\nTBR=xx'),
  1491. ('foo\nTBR=xx\nR=yy', ['a@c'], [],
  1492. 'foo\n\nR=a@c, yy\nTBR=xx'),
  1493. ('foo\nBUG=', ['a@c'], [],
  1494. 'foo\nBUG=\nR=a@c'),
  1495. ('foo\nR=xx\nTBR=yy\nR=bar', ['a@c'], [],
  1496. 'foo\n\nR=a@c, bar, xx\nTBR=yy'),
  1497. ('foo', ['a@c', 'b@c'], [],
  1498. 'foo\n\nR=a@c, b@c'),
  1499. ('foo\nBar\n\nR=\nBUG=', ['c@c'], [],
  1500. 'foo\nBar\n\nR=c@c\nBUG='),
  1501. ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], [],
  1502. 'foo\nBar\n\nR=c@c\nBUG='),
  1503. # Same as the line before, but full of whitespaces.
  1504. (
  1505. 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'], [],
  1506. 'foo\nBar\n\nR=c@c\n BUG =',
  1507. ),
  1508. # Whitespaces aren't interpreted as new lines.
  1509. ('foo BUG=allo R=joe ', ['c@c'], [],
  1510. 'foo BUG=allo R=joe\n\nR=c@c'),
  1511. # Redundant TBRs get promoted to Rs
  1512. ('foo\n\nR=a@c\nTBR=t@c', ['b@c', 'a@c'], ['a@c', 't@c'],
  1513. 'foo\n\nR=a@c, b@c\nTBR=t@c'),
  1514. ]
  1515. expected = [i[-1] for i in data]
  1516. actual = []
  1517. for orig, reviewers, tbrs, _expected in data:
  1518. obj = git_cl.ChangeDescription(orig)
  1519. obj.update_reviewers(reviewers, tbrs)
  1520. actual.append(obj.description)
  1521. self.assertEqual(expected, actual)
  1522. def test_get_hash_tags(self):
  1523. cases = [
  1524. ('', []),
  1525. ('a', []),
  1526. ('[a]', ['a']),
  1527. ('[aa]', ['aa']),
  1528. ('[a ]', ['a']),
  1529. ('[a- ]', ['a']),
  1530. ('[a- b]', ['a-b']),
  1531. ('[a--b]', ['a-b']),
  1532. ('[a', []),
  1533. ('[a]x', ['a']),
  1534. ('[aa]x', ['aa']),
  1535. ('[a b]', ['a-b']),
  1536. ('[a b]', ['a-b']),
  1537. ('[a__b]', ['a-b']),
  1538. ('[a] x', ['a']),
  1539. ('[a][b]', ['a', 'b']),
  1540. ('[a] [b]', ['a', 'b']),
  1541. ('[a][b]x', ['a', 'b']),
  1542. ('[a][b] x', ['a', 'b']),
  1543. ('[a]\n[b]', ['a']),
  1544. ('[a\nb]', []),
  1545. ('[a][', ['a']),
  1546. ('Revert "[a] feature"', ['a']),
  1547. ('Reland "[a] feature"', ['a']),
  1548. ('Revert: [a] feature', ['a']),
  1549. ('Reland: [a] feature', ['a']),
  1550. ('Revert "Reland: [a] feature"', ['a']),
  1551. ('Foo: feature', ['foo']),
  1552. ('Foo Bar: feature', ['foo-bar']),
  1553. ('Revert "Foo bar: feature"', ['foo-bar']),
  1554. ('Reland "Foo bar: feature"', ['foo-bar']),
  1555. ]
  1556. for desc, expected in cases:
  1557. change_desc = git_cl.ChangeDescription(desc)
  1558. actual = change_desc.get_hash_tags()
  1559. self.assertEqual(
  1560. actual,
  1561. expected,
  1562. 'GetHashTags(%r) == %r, expected %r' % (desc, actual, expected))
  1563. self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master'))
  1564. self.assertEqual(None, git_cl.GetTargetRef(None,
  1565. 'refs/remotes/origin/master',
  1566. 'master'))
  1567. # Check default target refs for branches.
  1568. self.assertEqual('refs/heads/master',
  1569. git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
  1570. None))
  1571. self.assertEqual('refs/heads/master',
  1572. git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr',
  1573. None))
  1574. self.assertEqual('refs/heads/master',
  1575. git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr',
  1576. None))
  1577. self.assertEqual('refs/branch-heads/123',
  1578. git_cl.GetTargetRef('origin',
  1579. 'refs/remotes/branch-heads/123',
  1580. None))
  1581. self.assertEqual('refs/diff/test',
  1582. git_cl.GetTargetRef('origin',
  1583. 'refs/remotes/origin/refs/diff/test',
  1584. None))
  1585. self.assertEqual('refs/heads/chrome/m42',
  1586. git_cl.GetTargetRef('origin',
  1587. 'refs/remotes/origin/chrome/m42',
  1588. None))
  1589. # Check target refs for user-specified target branch.
  1590. for branch in ('branch-heads/123', 'remotes/branch-heads/123',
  1591. 'refs/remotes/branch-heads/123'):
  1592. self.assertEqual('refs/branch-heads/123',
  1593. git_cl.GetTargetRef('origin',
  1594. 'refs/remotes/origin/master',
  1595. branch))
  1596. for branch in ('origin/master', 'remotes/origin/master',
  1597. 'refs/remotes/origin/master'):
  1598. self.assertEqual('refs/heads/master',
  1599. git_cl.GetTargetRef('origin',
  1600. 'refs/remotes/branch-heads/123',
  1601. branch))
  1602. for branch in ('master', 'heads/master', 'refs/heads/master'):
  1603. self.assertEqual('refs/heads/master',
  1604. git_cl.GetTargetRef('origin',
  1605. 'refs/remotes/branch-heads/123',
  1606. branch))
  1607. def test_patch_when_dirty(self):
  1608. # Patch when local tree is dirty.
  1609. self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
  1610. self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
  1611. @staticmethod
  1612. def _get_gerrit_codereview_server_calls(branch, value=None,
  1613. git_short_host='host',
  1614. detect_branch=True,
  1615. detect_server=True):
  1616. """Returns calls executed by Changelist.GetCodereviewServer.
  1617. If value is given, branch.<BRANCH>.gerritcodereview is already set.
  1618. """
  1619. calls = []
  1620. if detect_branch:
  1621. calls.append(((['git', 'symbolic-ref', 'HEAD'],), branch))
  1622. if detect_server:
  1623. calls.append(((['git', 'config', 'branch.' + branch + '.gerritserver'],),
  1624. CERR1 if value is None else value))
  1625. if value is None:
  1626. calls += [
  1627. ((['git', 'config', 'branch.' + branch + '.merge'],),
  1628. 'refs/heads' + branch),
  1629. ((['git', 'config', 'branch.' + branch + '.remote'],),
  1630. 'origin'),
  1631. ((['git', 'config', 'remote.origin.url'],),
  1632. 'https://%s.googlesource.com/my/repo' % git_short_host),
  1633. ]
  1634. return calls
  1635. def _patch_common(self, force_codereview=False,
  1636. new_branch=False, git_short_host='host',
  1637. detect_gerrit_server=False,
  1638. actual_codereview=None,
  1639. codereview_in_url=False):
  1640. self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
  1641. self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True)
  1642. if new_branch:
  1643. self.calls = [((['git', 'new-branch', 'master'],), '')]
  1644. if codereview_in_url and actual_codereview == 'rietveld':
  1645. self.calls += [
  1646. ((['git', 'rev-parse', '--show-cdup'],), ''),
  1647. ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
  1648. ]
  1649. if not force_codereview and not codereview_in_url:
  1650. # These calls detect codereview to use.
  1651. self.calls += [
  1652. ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
  1653. ]
  1654. if detect_gerrit_server:
  1655. self.calls += self._get_gerrit_codereview_server_calls(
  1656. 'master', git_short_host=git_short_host,
  1657. detect_branch=not new_branch and force_codereview)
  1658. actual_codereview = 'gerrit'
  1659. if actual_codereview == 'gerrit':
  1660. self.calls += [
  1661. (('GetChangeDetail', git_short_host + '-review.googlesource.com',
  1662. 'my%2Frepo~123456', ['ALL_REVISIONS', 'CURRENT_COMMIT']),
  1663. {
  1664. 'current_revision': '7777777777',
  1665. 'revisions': {
  1666. '1111111111': {
  1667. '_number': 1,
  1668. 'fetch': {'http': {
  1669. 'url': 'https://%s.googlesource.com/my/repo' % git_short_host,
  1670. 'ref': 'refs/changes/56/123456/1',
  1671. }},
  1672. },
  1673. '7777777777': {
  1674. '_number': 7,
  1675. 'fetch': {'http': {
  1676. 'url': 'https://%s.googlesource.com/my/repo' % git_short_host,
  1677. 'ref': 'refs/changes/56/123456/7',
  1678. }},
  1679. },
  1680. },
  1681. }),
  1682. ]
  1683. def test_patch_gerrit_default(self):
  1684. self._patch_common(git_short_host='chromium', detect_gerrit_server=True)
  1685. self.calls += [
  1686. ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
  1687. 'refs/changes/56/123456/7'],), ''),
  1688. ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
  1689. ((['git', 'config', 'branch.master.gerritissue', '123456'],),
  1690. ''),
  1691. ((['git', 'config', 'branch.master.gerritserver',
  1692. 'https://chromium-review.googlesource.com'],), ''),
  1693. ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''),
  1694. ((['git', 'rev-parse', 'FETCH_HEAD'],), 'deadbeef'),
  1695. ((['git', 'config', 'branch.master.last-upload-hash', 'deadbeef'],), ''),
  1696. ((['git', 'config', 'branch.master.gerritsquashhash', 'deadbeef'],), ''),
  1697. ]
  1698. self.assertEqual(git_cl.main(['patch', '123456']), 0)
  1699. def test_patch_gerrit_new_branch(self):
  1700. self._patch_common(
  1701. git_short_host='chromium', detect_gerrit_server=True, new_branch=True)
  1702. self.calls += [
  1703. ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
  1704. 'refs/changes/56/123456/7'],), ''),
  1705. ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
  1706. ((['git', 'config', 'branch.master.gerritissue', '123456'],),
  1707. ''),
  1708. ((['git', 'config', 'branch.master.gerritserver',
  1709. 'https://chromium-review.googlesource.com'],), ''),
  1710. ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''),
  1711. ((['git', 'rev-parse', 'FETCH_HEAD'],), 'deadbeef'),
  1712. ((['git', 'config', 'branch.master.last-upload-hash', 'deadbeef'],), ''),
  1713. ((['git', 'config', 'branch.master.gerritsquashhash', 'deadbeef'],), ''),
  1714. ]
  1715. self.assertEqual(git_cl.main(['patch', '-b', 'master', '123456']), 0)
  1716. def test_patch_gerrit_force(self):
  1717. self._patch_common(
  1718. force_codereview=True, git_short_host='host', detect_gerrit_server=True)
  1719. self.calls += [
  1720. ((['git', 'fetch', 'https://host.googlesource.com/my/repo',
  1721. 'refs/changes/56/123456/7'],), ''),
  1722. ((['git', 'reset', '--hard', 'FETCH_HEAD'],), ''),
  1723. ((['git', 'config', 'branch.master.gerritissue', '123456'],),
  1724. ''),
  1725. ((['git', 'config', 'branch.master.gerritserver',
  1726. 'https://host-review.googlesource.com'],), ''),
  1727. ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''),
  1728. ((['git', 'rev-parse', 'FETCH_HEAD'],), 'deadbeef'),
  1729. ((['git', 'config', 'branch.master.last-upload-hash', 'deadbeef'],), ''),
  1730. ((['git', 'config', 'branch.master.gerritsquashhash', 'deadbeef'],), ''),
  1731. ]
  1732. self.assertEqual(git_cl.main(['patch', '--gerrit', '123456', '--force']), 0)
  1733. def test_patch_gerrit_guess_by_url(self):
  1734. self.calls += self._get_gerrit_codereview_server_calls(
  1735. 'master', git_short_host='else', detect_server=False)
  1736. self._patch_common(
  1737. actual_codereview='gerrit', git_short_host='else',
  1738. codereview_in_url=True, detect_gerrit_server=False)
  1739. self.calls += [
  1740. ((['git', 'fetch', 'https://else.googlesource.com/my/repo',
  1741. 'refs/changes/56/123456/1'],), ''),
  1742. ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
  1743. ((['git', 'config', 'branch.master.gerritissue', '123456'],),
  1744. ''),
  1745. ((['git', 'config', 'branch.master.gerritserver',
  1746. 'https://else-review.googlesource.com'],), ''),
  1747. ((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''),
  1748. ((['git', 'rev-parse', 'FETCH_HEAD'],), 'deadbeef'),
  1749. ((['git', 'config', 'branch.master.last-upload-hash', 'deadbeef'],), ''),
  1750. ((['git', 'config', 'branch.master.gerritsquashhash', 'deadbeef'],), ''),
  1751. ]
  1752. self.assertEqual(git_cl.main(
  1753. ['patch', 'https://else-review.googlesource.com/#/c/123456/1']), 0)
  1754. def test_patch_gerrit_guess_by_url_with_repo(self):
  1755. self.calls += self._get_gerrit_codereview_server_calls(
  1756. 'master', git_short_host='else', detect_server=False)
  1757. self._patch_common(
  1758. actual_codereview='gerrit', git_short_host='else',
  1759. codereview_in_url=True, detect_gerrit_server=False)
  1760. self.calls += [
  1761. ((['git', 'fetch', 'https://else.googlesource.com/my/repo',
  1762. 'refs/changes/56/123456/1'],), ''),
  1763. ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
  1764. ((['git', 'config', 'branch.master.gerritissue', '123456'],),
  1765. ''),
  1766. ((['git', 'config', 'branch.master.gerritserver',
  1767. 'https://else-review.googlesource.com'],), ''),
  1768. ((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''),
  1769. ((['git', 'rev-parse', 'FETCH_HEAD'],), 'deadbeef'),
  1770. ((['git', 'config', 'branch.master.last-upload-hash', 'deadbeef'],), ''),
  1771. ((['git', 'config', 'branch.master.gerritsquashhash', 'deadbeef'],), ''),
  1772. ]
  1773. self.assertEqual(git_cl.main(
  1774. ['patch', 'https://else-review.googlesource.com/c/my/repo/+/123456/1']),
  1775. 0)
  1776. def test_patch_gerrit_conflict(self):
  1777. self._patch_common(detect_gerrit_server=True, git_short_host='chromium')
  1778. self.calls += [
  1779. ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
  1780. 'refs/changes/56/123456/7'],), ''),
  1781. ((['git', 'cherry-pick', 'FETCH_HEAD'],), CERR1),
  1782. ((['DieWithError', 'Command "git cherry-pick FETCH_HEAD" failed.\n'],),
  1783. SystemExitMock()),
  1784. ]
  1785. with self.assertRaises(SystemExitMock):
  1786. git_cl.main(['patch', '123456'])
  1787. def test_patch_gerrit_not_exists(self):
  1788. def notExists(_issue, *_, **kwargs):
  1789. raise git_cl.gerrit_util.GerritError(404, '')
  1790. self.mock(git_cl.gerrit_util, 'GetChangeDetail', notExists)
  1791. self.calls = [
  1792. ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
  1793. ((['git', 'config', 'branch.master.gerritserver'],), CERR1),
  1794. ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'),
  1795. ((['git', 'config', 'branch.master.remote'],), 'origin'),
  1796. ((['git', 'config', 'remote.origin.url'],),
  1797. 'https://chromium.googlesource.com/my/repo'),
  1798. ((['DieWithError',
  1799. 'change 123456 at https://chromium-review.googlesource.com does not '
  1800. 'exist or you have no access to it'],), SystemExitMock()),
  1801. ]
  1802. with self.assertRaises(SystemExitMock):
  1803. self.assertEqual(1, git_cl.main(['patch', '123456']))
  1804. def _checkout_calls(self):
  1805. return [
  1806. ((['git', 'config', '--local', '--get-regexp',
  1807. 'branch\\..*\\.gerritissue'], ),
  1808. ('branch.ger-branch.gerritissue 123456\n'
  1809. 'branch.gbranch654.gerritissue 654321\n')),
  1810. ]
  1811. def test_checkout_gerrit(self):
  1812. """Tests git cl checkout <issue>."""
  1813. self.calls = self._checkout_calls()
  1814. self.calls += [((['git', 'checkout', 'ger-branch'], ), '')]
  1815. self.assertEqual(0, git_cl.main(['checkout', '123456']))
  1816. def test_checkout_not_found(self):
  1817. """Tests git cl checkout <issue>."""
  1818. self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
  1819. self.calls = self._checkout_calls()
  1820. self.assertEqual(1, git_cl.main(['checkout', '99999']))
  1821. def test_checkout_no_branch_issues(self):
  1822. """Tests git cl checkout <issue>."""
  1823. self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
  1824. self.calls = [
  1825. ((['git', 'config', '--local', '--get-regexp',
  1826. 'branch\\..*\\.gerritissue'], ), CERR1),
  1827. ]
  1828. self.assertEqual(1, git_cl.main(['checkout', '99999']))
  1829. def _test_gerrit_ensure_authenticated_common(self, auth,
  1830. skip_auth_check=False):
  1831. self.mock(git_cl.gerrit_util, 'CookiesAuthenticator',
  1832. CookiesAuthenticatorMockFactory(hosts_with_creds=auth))
  1833. self.mock(git_cl, 'DieWithError',
  1834. lambda msg, change=None: self._mocked_call(['DieWithError', msg]))
  1835. self.calls = self._gerrit_ensure_auth_calls(skip_auth_check=skip_auth_check)
  1836. cl = git_cl.Changelist()
  1837. cl.branch = 'master'
  1838. cl.branchref = 'refs/heads/master'
  1839. return cl
  1840. def test_gerrit_ensure_authenticated_missing(self):
  1841. cl = self._test_gerrit_ensure_authenticated_common(auth={
  1842. 'chromium.googlesource.com': ('git-is.ok', '', 'but gerrit is missing'),
  1843. })
  1844. self.calls.append(
  1845. ((['DieWithError',
  1846. 'Credentials for the following hosts are required:\n'
  1847. ' chromium-review.googlesource.com\n'
  1848. 'These are read from ~/.gitcookies (or legacy ~/.netrc)\n'
  1849. 'You can (re)generate your credentials by visiting '
  1850. 'https://chromium-review.googlesource.com/new-password'],), ''),)
  1851. self.assertIsNone(cl.EnsureAuthenticated(force=False))
  1852. def test_gerrit_ensure_authenticated_conflict(self):
  1853. self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
  1854. cl = self._test_gerrit_ensure_authenticated_common(auth={
  1855. 'chromium.googlesource.com':
  1856. ('git-one.example.com', None, 'secret1'),
  1857. 'chromium-review.googlesource.com':
  1858. ('git-other.example.com', None, 'secret2'),
  1859. })
  1860. self.calls.append(
  1861. (('ask_for_data', 'If you know what you are doing '
  1862. 'press Enter to continue, or Ctrl+C to abort'), ''))
  1863. self.assertIsNone(cl.EnsureAuthenticated(force=False))
  1864. def test_gerrit_ensure_authenticated_ok(self):
  1865. cl = self._test_gerrit_ensure_authenticated_common(auth={
  1866. 'chromium.googlesource.com':
  1867. ('git-same.example.com', None, 'secret'),
  1868. 'chromium-review.googlesource.com':
  1869. ('git-same.example.com', None, 'secret'),
  1870. })
  1871. self.assertIsNone(cl.EnsureAuthenticated(force=False))
  1872. def test_gerrit_ensure_authenticated_skipped(self):
  1873. cl = self._test_gerrit_ensure_authenticated_common(
  1874. auth={}, skip_auth_check=True)
  1875. self.assertIsNone(cl.EnsureAuthenticated(force=False))
  1876. def test_gerrit_ensure_authenticated_bearer_token(self):
  1877. cl = self._test_gerrit_ensure_authenticated_common(auth={
  1878. 'chromium.googlesource.com':
  1879. ('', None, 'secret'),
  1880. 'chromium-review.googlesource.com':
  1881. ('', None, 'secret'),
  1882. })
  1883. self.assertIsNone(cl.EnsureAuthenticated(force=False))
  1884. header = gerrit_util.CookiesAuthenticator().get_auth_header(
  1885. 'chromium.googlesource.com')
  1886. self.assertTrue('Bearer' in header)
  1887. def test_gerrit_ensure_authenticated_non_https(self):
  1888. self.calls = [
  1889. ((['git', 'config', '--bool',
  1890. 'gerrit.skip-ensure-authenticated'],), CERR1),
  1891. ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'),
  1892. ((['git', 'config', 'branch.master.remote'],), 'origin'),
  1893. ((['git', 'config', 'remote.origin.url'],), 'custom-scheme://repo'),
  1894. ]
  1895. self.mock(git_cl.gerrit_util, 'CookiesAuthenticator',
  1896. CookiesAuthenticatorMockFactory(hosts_with_creds={}))
  1897. cl = git_cl.Changelist()
  1898. cl.branch = 'master'
  1899. cl.branchref = 'refs/heads/master'
  1900. cl.lookedup_issue = True
  1901. self.assertIsNone(cl.EnsureAuthenticated(force=False))
  1902. def _cmd_set_commit_gerrit_common(self, vote, notify=None):
  1903. self.mock(git_cl.gerrit_util, 'SetReview',
  1904. lambda h, i, labels, notify=None:
  1905. self._mocked_call(['SetReview', h, i, labels, notify]))
  1906. self.calls = [
  1907. ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
  1908. ((['git', 'config', 'branch.feature.gerritissue'],), '123'),
  1909. ((['git', 'config', 'branch.feature.gerritserver'],),
  1910. 'https://chromium-review.googlesource.com'),
  1911. ((['git', 'config', 'branch.feature.merge'],), 'refs/heads/master'),
  1912. ((['git', 'config', 'branch.feature.remote'],), 'origin'),
  1913. ((['git', 'config', 'remote.origin.url'],),
  1914. 'https://chromium.googlesource.com/infra/infra.git'),
  1915. ((['SetReview', 'chromium-review.googlesource.com',
  1916. 'infra%2Finfra~123',
  1917. {'Commit-Queue': vote}, notify],), ''),
  1918. ]
  1919. def test_cmd_set_commit_gerrit_clear(self):
  1920. self._cmd_set_commit_gerrit_common(0)
  1921. self.assertEqual(0, git_cl.main(['set-commit', '-c']))
  1922. def test_cmd_set_commit_gerrit_dry(self):
  1923. self._cmd_set_commit_gerrit_common(1, notify=False)
  1924. self.assertEqual(0, git_cl.main(['set-commit', '-d']))
  1925. def test_cmd_set_commit_gerrit(self):
  1926. self._cmd_set_commit_gerrit_common(2)
  1927. self.assertEqual(0, git_cl.main(['set-commit']))
  1928. def test_description_display(self):
  1929. out = StringIO.StringIO()
  1930. self.mock(git_cl.sys, 'stdout', out)
  1931. self.mock(git_cl, 'Changelist', ChangelistMock)
  1932. ChangelistMock.desc = 'foo\n'
  1933. self.assertEqual(0, git_cl.main(['description', '-d']))
  1934. self.assertEqual('foo\n', out.getvalue())
  1935. def test_StatusFieldOverrideIssueMissingArgs(self):
  1936. out = StringIO.StringIO()
  1937. self.mock(git_cl.sys, 'stderr', out)
  1938. try:
  1939. self.assertEqual(git_cl.main(['status', '--issue', '1']), 0)
  1940. except SystemExit as ex:
  1941. self.assertEqual(ex.code, 2)
  1942. self.assertRegexpMatches(out.getvalue(), r'--field must be specified')
  1943. out = StringIO.StringIO()
  1944. self.mock(git_cl.sys, 'stderr', out)
  1945. try:
  1946. self.assertEqual(git_cl.main(['status', '--issue', '1', '--gerrit']), 0)
  1947. except SystemExit as ex:
  1948. self.assertEqual(ex.code, 2)
  1949. self.assertRegexpMatches(out.getvalue(), r'--field must be specified')
  1950. def test_StatusFieldOverrideIssue(self):
  1951. out = StringIO.StringIO()
  1952. self.mock(git_cl.sys, 'stdout', out)
  1953. def assertIssue(cl_self, *_args):
  1954. self.assertEqual(cl_self.issue, 1)
  1955. return 'foobar'
  1956. self.mock(git_cl.Changelist, 'GetDescription', assertIssue)
  1957. self.assertEqual(
  1958. git_cl.main(['status', '--issue', '1', '--gerrit', '--field', 'desc']),
  1959. 0)
  1960. self.assertEqual(out.getvalue(), 'foobar\n')
  1961. def test_SetCloseOverrideIssue(self):
  1962. def assertIssue(cl_self, *_args):
  1963. self.assertEqual(cl_self.issue, 1)
  1964. return 'foobar'
  1965. self.mock(git_cl.Changelist, 'GetDescription', assertIssue)
  1966. self.mock(git_cl.Changelist, 'CloseIssue', lambda *_: None)
  1967. self.assertEqual(
  1968. git_cl.main(['set-close', '--issue', '1', '--gerrit']), 0)
  1969. def test_description(self):
  1970. out = StringIO.StringIO()
  1971. self.mock(git_cl.sys, 'stdout', out)
  1972. self.calls = [
  1973. ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
  1974. ((['git', 'config', 'branch.feature.merge'],), 'feature'),
  1975. ((['git', 'config', 'branch.feature.remote'],), 'origin'),
  1976. ((['git', 'config', 'remote.origin.url'],),
  1977. 'https://chromium.googlesource.com/my/repo'),
  1978. (('GetChangeDetail', 'chromium-review.googlesource.com',
  1979. 'my%2Frepo~123123', ['CURRENT_REVISION', 'CURRENT_COMMIT']),
  1980. {
  1981. 'current_revision': 'sha1',
  1982. 'revisions': {'sha1': {
  1983. 'commit': {'message': 'foobar'},
  1984. }},
  1985. }),
  1986. ]
  1987. self.assertEqual(0, git_cl.main([
  1988. 'description',
  1989. 'https://chromium-review.googlesource.com/c/my/repo/+/123123',
  1990. '-d']))
  1991. self.assertEqual('foobar\n', out.getvalue())
  1992. def test_description_set_raw(self):
  1993. out = StringIO.StringIO()
  1994. self.mock(git_cl.sys, 'stdout', out)
  1995. self.mock(git_cl, 'Changelist', ChangelistMock)
  1996. self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hihi'))
  1997. self.assertEqual(0, git_cl.main(['description', '-n', 'hihi']))
  1998. self.assertEqual('hihi', ChangelistMock.desc)
  1999. def test_description_appends_bug_line(self):
  2000. current_desc = 'Some.\n\nChange-Id: xxx'
  2001. def RunEditor(desc, _, **kwargs):
  2002. self.assertEqual(
  2003. '# Enter a description of the change.\n'
  2004. '# This will be displayed on the codereview site.\n'
  2005. '# The first line will also be used as the subject of the review.\n'
  2006. '#--------------------This line is 72 characters long'
  2007. '--------------------\n'
  2008. 'Some.\n\nChange-Id: xxx\nBug: ',
  2009. desc)
  2010. # Simulate user changing something.
  2011. return 'Some.\n\nChange-Id: xxx\nBug: 123'
  2012. def UpdateDescriptionRemote(_, desc, force=False):
  2013. self.assertEqual(desc, 'Some.\n\nChange-Id: xxx\nBug: 123')
  2014. self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
  2015. self.mock(git_cl.Changelist, 'GetDescription',
  2016. lambda *args: current_desc)
  2017. self.mock(git_cl.Changelist, 'UpdateDescriptionRemote',
  2018. UpdateDescriptionRemote)
  2019. self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
  2020. self.calls = [
  2021. ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
  2022. ((['git', 'config', 'branch.feature.gerritissue'],), '123'),
  2023. ((['git', 'config', 'rietveld.autoupdate'],), CERR1),
  2024. ((['git', 'config', 'rietveld.bug-prefix'],), CERR1),
  2025. ((['git', 'config', 'core.editor'],), 'vi'),
  2026. ]
  2027. self.assertEqual(0, git_cl.main(['description', '--gerrit']))
  2028. def test_description_does_not_append_bug_line_if_fixed_is_present(self):
  2029. current_desc = 'Some.\n\nFixed: 123\nChange-Id: xxx'
  2030. def RunEditor(desc, _, **kwargs):
  2031. self.assertEqual(
  2032. '# Enter a description of the change.\n'
  2033. '# This will be displayed on the codereview site.\n'
  2034. '# The first line will also be used as the subject of the review.\n'
  2035. '#--------------------This line is 72 characters long'
  2036. '--------------------\n'
  2037. 'Some.\n\nFixed: 123\nChange-Id: xxx',
  2038. desc)
  2039. return desc
  2040. self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
  2041. self.mock(git_cl.Changelist, 'GetDescription',
  2042. lambda *args: current_desc)
  2043. self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
  2044. self.calls = [
  2045. ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
  2046. ((['git', 'config', 'branch.feature.gerritissue'],), '123'),
  2047. ((['git', 'config', 'rietveld.autoupdate'],), CERR1),
  2048. ((['git', 'config', 'rietveld.bug-prefix'],), CERR1),
  2049. ((['git', 'config', 'core.editor'],), 'vi'),
  2050. ]
  2051. self.assertEqual(0, git_cl.main(['description', '--gerrit']))
  2052. def test_description_set_stdin(self):
  2053. out = StringIO.StringIO()
  2054. self.mock(git_cl.sys, 'stdout', out)
  2055. self.mock(git_cl, 'Changelist', ChangelistMock)
  2056. self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hi \r\n\t there\n\nman'))
  2057. self.assertEqual(0, git_cl.main(['description', '-n', '-']))
  2058. self.assertEqual('hi\n\t there\n\nman', ChangelistMock.desc)
  2059. def test_archive(self):
  2060. self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
  2061. self.calls = [
  2062. ((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],),
  2063. 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'),
  2064. ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
  2065. ((['git', 'tag', 'git-cl-archived-456-foo', 'foo'],), ''),
  2066. ((['git', 'branch', '-D', 'foo'],), '')
  2067. ]
  2068. self.mock(git_cl, 'get_cl_statuses',
  2069. lambda branches, fine_grained, max_processes:
  2070. [(MockChangelistWithBranchAndIssue('master', 1), 'open'),
  2071. (MockChangelistWithBranchAndIssue('foo', 456), 'closed'),
  2072. (MockChangelistWithBranchAndIssue('bar', 789), 'open')])
  2073. self.assertEqual(0, git_cl.main(['archive', '-f']))
  2074. def test_archive_current_branch_fails(self):
  2075. self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
  2076. self.calls = [
  2077. ((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],),
  2078. 'refs/heads/master'),
  2079. ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
  2080. ]
  2081. self.mock(git_cl, 'get_cl_statuses',
  2082. lambda branches, fine_grained, max_processes:
  2083. [(MockChangelistWithBranchAndIssue('master', 1), 'closed')])
  2084. self.assertEqual(1, git_cl.main(['archive', '-f']))
  2085. def test_archive_dry_run(self):
  2086. self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
  2087. self.calls = [
  2088. ((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],),
  2089. 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'),
  2090. ((['git', 'symbolic-ref', 'HEAD'],), 'master')
  2091. ]
  2092. self.mock(git_cl, 'get_cl_statuses',
  2093. lambda branches, fine_grained, max_processes:
  2094. [(MockChangelistWithBranchAndIssue('master', 1), 'open'),
  2095. (MockChangelistWithBranchAndIssue('foo', 456), 'closed'),
  2096. (MockChangelistWithBranchAndIssue('bar', 789), 'open')])
  2097. self.assertEqual(0, git_cl.main(['archive', '-f', '--dry-run']))
  2098. def test_archive_no_tags(self):
  2099. self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
  2100. self.calls = [
  2101. ((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],),
  2102. 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'),
  2103. ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
  2104. ((['git', 'branch', '-D', 'foo'],), '')
  2105. ]
  2106. self.mock(git_cl, 'get_cl_statuses',
  2107. lambda branches, fine_grained, max_processes:
  2108. [(MockChangelistWithBranchAndIssue('master', 1), 'open'),
  2109. (MockChangelistWithBranchAndIssue('foo', 456), 'closed'),
  2110. (MockChangelistWithBranchAndIssue('bar', 789), 'open')])
  2111. self.assertEqual(0, git_cl.main(['archive', '-f', '--notags']))
  2112. def test_cmd_issue_erase_existing(self):
  2113. out = StringIO.StringIO()
  2114. self.mock(git_cl.sys, 'stdout', out)
  2115. self.calls = [
  2116. ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
  2117. # Let this command raise exception (retcode=1) - it should be ignored.
  2118. ((['git', 'config', '--unset', 'branch.feature.last-upload-hash'],),
  2119. CERR1),
  2120. ((['git', 'config', '--unset', 'branch.feature.gerritissue'],), ''),
  2121. ((['git', 'config', '--unset', 'branch.feature.gerritpatchset'],), ''),
  2122. ((['git', 'config', '--unset', 'branch.feature.gerritserver'],), ''),
  2123. ((['git', 'config', '--unset', 'branch.feature.gerritsquashhash'],),
  2124. ''),
  2125. ((['git', 'log', '-1', '--format=%B'],), 'This is a description'),
  2126. ]
  2127. self.assertEqual(0, git_cl.main(['issue', '0']))
  2128. def test_cmd_issue_erase_existing_with_change_id(self):
  2129. out = StringIO.StringIO()
  2130. self.mock(git_cl.sys, 'stdout', out)
  2131. self.mock(git_cl.Changelist, 'GetDescription',
  2132. lambda _: 'This is a description\n\nChange-Id: Ideadbeef')
  2133. self.calls = [
  2134. ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
  2135. # Let this command raise exception (retcode=1) - it should be ignored.
  2136. ((['git', 'config', '--unset', 'branch.feature.last-upload-hash'],),
  2137. CERR1),
  2138. ((['git', 'config', '--unset', 'branch.feature.gerritissue'],), ''),
  2139. ((['git', 'config', '--unset', 'branch.feature.gerritpatchset'],), ''),
  2140. ((['git', 'config', '--unset', 'branch.feature.gerritserver'],), ''),
  2141. ((['git', 'config', '--unset', 'branch.feature.gerritsquashhash'],),
  2142. ''),
  2143. ((['git', 'log', '-1', '--format=%B'],),
  2144. 'This is a description\n\nChange-Id: Ideadbeef'),
  2145. ((['git', 'commit', '--amend', '-m', 'This is a description\n'],), ''),
  2146. ]
  2147. self.assertEqual(0, git_cl.main(['issue', '0']))
  2148. def test_cmd_issue_json(self):
  2149. out = StringIO.StringIO()
  2150. self.mock(git_cl.sys, 'stdout', out)
  2151. self.calls = [
  2152. ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
  2153. ((['git', 'config', 'branch.feature.gerritissue'],), '123'),
  2154. ((['git', 'config', 'branch.feature.gerritserver'],),
  2155. 'https://chromium-review.googlesource.com'),
  2156. (('write_json', 'output.json',
  2157. {'issue': 123,
  2158. 'issue_url': 'https://chromium-review.googlesource.com/123'}),
  2159. ''),
  2160. ]
  2161. self.assertEqual(0, git_cl.main(['issue', '--json', 'output.json']))
  2162. def _common_GerritCommitMsgHookCheck(self):
  2163. self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
  2164. self.mock(git_cl.os.path, 'abspath',
  2165. lambda path: self._mocked_call(['abspath', path]))
  2166. self.mock(git_cl.os.path, 'exists',
  2167. lambda path: self._mocked_call(['exists', path]))
  2168. self.mock(git_cl.gclient_utils, 'FileRead',
  2169. lambda path: self._mocked_call(['FileRead', path]))
  2170. self.mock(git_cl.gclient_utils, 'rm_file_or_tree',
  2171. lambda path: self._mocked_call(['rm_file_or_tree', path]))
  2172. self.calls = [
  2173. ((['git', 'rev-parse', '--show-cdup'],), '../'),
  2174. ((['abspath', '../'],), '/abs/git_repo_root'),
  2175. ]
  2176. return git_cl.Changelist(issue=123)
  2177. def test_GerritCommitMsgHookCheck_custom_hook(self):
  2178. cl = self._common_GerritCommitMsgHookCheck()
  2179. self.calls += [
  2180. ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), True),
  2181. ((['FileRead', '/abs/git_repo_root/.git/hooks/commit-msg'],),
  2182. '#!/bin/sh\necho "custom hook"')
  2183. ]
  2184. cl._GerritCommitMsgHookCheck(offer_removal=True)
  2185. def test_GerritCommitMsgHookCheck_not_exists(self):
  2186. cl = self._common_GerritCommitMsgHookCheck()
  2187. self.calls += [
  2188. ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), False),
  2189. ]
  2190. cl._GerritCommitMsgHookCheck(offer_removal=True)
  2191. def test_GerritCommitMsgHookCheck(self):
  2192. cl = self._common_GerritCommitMsgHookCheck()
  2193. self.calls += [
  2194. ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), True),
  2195. ((['FileRead', '/abs/git_repo_root/.git/hooks/commit-msg'],),
  2196. '...\n# From Gerrit Code Review\n...\nadd_ChangeId()\n'),
  2197. (('ask_for_data', 'Do you want to remove it now? [Yes/No]: '), 'Yes'),
  2198. ((['rm_file_or_tree', '/abs/git_repo_root/.git/hooks/commit-msg'],),
  2199. ''),
  2200. ]
  2201. cl._GerritCommitMsgHookCheck(offer_removal=True)
  2202. def test_GerritCmdLand(self):
  2203. self.calls += [
  2204. ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
  2205. ((['git', 'config', 'branch.feature.gerritsquashhash'],),
  2206. 'deadbeaf'),
  2207. ((['git', 'diff', 'deadbeaf'],), ''), # No diff.
  2208. ((['git', 'config', 'branch.feature.gerritserver'],),
  2209. 'chromium-review.googlesource.com'),
  2210. ]
  2211. cl = git_cl.Changelist(issue=123)
  2212. cl._GetChangeDetail = lambda *args, **kwargs: {
  2213. 'labels': {},
  2214. 'current_revision': 'deadbeaf',
  2215. }
  2216. cl._GetChangeCommit = lambda: {
  2217. 'commit': 'deadbeef',
  2218. 'web_links': [{'name': 'gitiles',
  2219. 'url': 'https://git.googlesource.com/test/+/deadbeef'}],
  2220. }
  2221. cl.SubmitIssue = lambda wait_for_merge: None
  2222. out = StringIO.StringIO()
  2223. self.mock(sys, 'stdout', out)
  2224. self.assertEqual(0, cl.CMDLand(force=True,
  2225. bypass_hooks=True,
  2226. verbose=True,
  2227. parallel=False))
  2228. self.assertRegexpMatches(out.getvalue(), 'Issue.*123 has been submitted')
  2229. self.assertRegexpMatches(out.getvalue(), 'Landed as: .*deadbeef')
  2230. def _mock_gerrit_changes_for_detail_cache(self):
  2231. self.mock(git_cl.Changelist, '_GetGerritHost', lambda _: 'host')
  2232. def test_gerrit_change_detail_cache_simple(self):
  2233. self._mock_gerrit_changes_for_detail_cache()
  2234. self.calls = [
  2235. (('GetChangeDetail', 'host', 'my%2Frepo~1', []), 'a'),
  2236. (('GetChangeDetail', 'host', 'ab%2Frepo~2', []), 'b'),
  2237. (('GetChangeDetail', 'host', 'ab%2Frepo~2', []), 'b2'),
  2238. ]
  2239. cl1 = git_cl.Changelist(issue=1)
  2240. cl1._cached_remote_url = (
  2241. True, 'https://chromium.googlesource.com/a/my/repo.git/')
  2242. cl2 = git_cl.Changelist(issue=2)
  2243. cl2._cached_remote_url = (
  2244. True, 'https://chromium.googlesource.com/ab/repo')
  2245. self.assertEqual(cl1._GetChangeDetail(), 'a') # Miss.
  2246. self.assertEqual(cl1._GetChangeDetail(), 'a')
  2247. self.assertEqual(cl2._GetChangeDetail(), 'b') # Miss.
  2248. self.assertEqual(cl2._GetChangeDetail(no_cache=True), 'b2') # Miss.
  2249. self.assertEqual(cl1._GetChangeDetail(), 'a')
  2250. self.assertEqual(cl2._GetChangeDetail(), 'b2')
  2251. def test_gerrit_change_detail_cache_options(self):
  2252. self._mock_gerrit_changes_for_detail_cache()
  2253. self.calls = [
  2254. (('GetChangeDetail', 'host', 'repo~1', ['C', 'A', 'B']), 'cab'),
  2255. (('GetChangeDetail', 'host', 'repo~1', ['A', 'D']), 'ad'),
  2256. (('GetChangeDetail', 'host', 'repo~1', ['A']), 'a'), # no_cache=True
  2257. # no longer in cache.
  2258. (('GetChangeDetail', 'host', 'repo~1', ['B']), 'b'),
  2259. ]
  2260. cl = git_cl.Changelist(issue=1)
  2261. cl._cached_remote_url = (True, 'https://chromium.googlesource.com/repo/')
  2262. self.assertEqual(cl._GetChangeDetail(options=['C', 'A', 'B']), 'cab')
  2263. self.assertEqual(cl._GetChangeDetail(options=['A', 'B', 'C']), 'cab')
  2264. self.assertEqual(cl._GetChangeDetail(options=['B', 'A']), 'cab')
  2265. self.assertEqual(cl._GetChangeDetail(options=['C']), 'cab')
  2266. self.assertEqual(cl._GetChangeDetail(options=['A']), 'cab')
  2267. self.assertEqual(cl._GetChangeDetail(), 'cab')
  2268. self.assertEqual(cl._GetChangeDetail(options=['A', 'D']), 'ad')
  2269. self.assertEqual(cl._GetChangeDetail(options=['A']), 'cab')
  2270. self.assertEqual(cl._GetChangeDetail(options=['D']), 'ad')
  2271. self.assertEqual(cl._GetChangeDetail(), 'cab')
  2272. # Finally, no_cache should invalidate all caches for given change.
  2273. self.assertEqual(cl._GetChangeDetail(options=['A'], no_cache=True), 'a')
  2274. self.assertEqual(cl._GetChangeDetail(options=['B']), 'b')
  2275. def test_gerrit_description_caching(self):
  2276. def gen_detail(rev, desc):
  2277. return {
  2278. 'current_revision': rev,
  2279. 'revisions': {rev: {'commit': {'message': desc}}}
  2280. }
  2281. self.calls = [
  2282. (('GetChangeDetail', 'host', 'my%2Frepo~1',
  2283. ['CURRENT_REVISION', 'CURRENT_COMMIT']),
  2284. gen_detail('rev1', 'desc1')),
  2285. (('GetChangeDetail', 'host', 'my%2Frepo~1',
  2286. ['CURRENT_REVISION', 'CURRENT_COMMIT']),
  2287. gen_detail('rev2', 'desc2')),
  2288. ]
  2289. self._mock_gerrit_changes_for_detail_cache()
  2290. cl = git_cl.Changelist(issue=1)
  2291. cl._cached_remote_url = (
  2292. True, 'https://chromium.googlesource.com/a/my/repo.git/')
  2293. self.assertEqual(cl.GetDescription(), 'desc1')
  2294. self.assertEqual(cl.GetDescription(), 'desc1') # cache hit.
  2295. self.assertEqual(cl.GetDescription(force=True), 'desc2')
  2296. def test_print_current_creds(self):
  2297. class CookiesAuthenticatorMock(object):
  2298. def __init__(self):
  2299. self.gitcookies = {
  2300. 'host.googlesource.com': ('user', 'pass'),
  2301. 'host-review.googlesource.com': ('user', 'pass'),
  2302. }
  2303. self.netrc = self
  2304. self.netrc.hosts = {
  2305. 'github.com': ('user2', None, 'pass2'),
  2306. 'host2.googlesource.com': ('user3', None, 'pass'),
  2307. }
  2308. self.mock(git_cl.gerrit_util, 'CookiesAuthenticator',
  2309. CookiesAuthenticatorMock)
  2310. self.mock(sys, 'stdout', StringIO.StringIO())
  2311. git_cl._GitCookiesChecker().print_current_creds(include_netrc=True)
  2312. self.assertEqual(list(sys.stdout.getvalue().splitlines()), [
  2313. ' Host\t User\t Which file',
  2314. '============================\t=====\t===========',
  2315. 'host-review.googlesource.com\t user\t.gitcookies',
  2316. ' host.googlesource.com\t user\t.gitcookies',
  2317. ' host2.googlesource.com\tuser3\t .netrc',
  2318. ])
  2319. sys.stdout.buf = ''
  2320. git_cl._GitCookiesChecker().print_current_creds(include_netrc=False)
  2321. self.assertEqual(list(sys.stdout.getvalue().splitlines()), [
  2322. ' Host\tUser\t Which file',
  2323. '============================\t====\t===========',
  2324. 'host-review.googlesource.com\tuser\t.gitcookies',
  2325. ' host.googlesource.com\tuser\t.gitcookies',
  2326. ])
  2327. def _common_creds_check_mocks(self):
  2328. def exists_mock(path):
  2329. dirname = os.path.dirname(path)
  2330. if dirname == os.path.expanduser('~'):
  2331. dirname = '~'
  2332. base = os.path.basename(path)
  2333. if base in ('.netrc', '.gitcookies'):
  2334. return self._mocked_call('os.path.exists', '%s/%s' % (dirname, base))
  2335. # git cl also checks for existence other files not relevant to this test.
  2336. return None
  2337. self.mock(os.path, 'exists', exists_mock)
  2338. self.mock(sys, 'stdout', StringIO.StringIO())
  2339. def test_creds_check_gitcookies_not_configured(self):
  2340. self._common_creds_check_mocks()
  2341. self.mock(git_cl._GitCookiesChecker, 'get_hosts_with_creds',
  2342. lambda _, include_netrc=False: [])
  2343. self.calls = [
  2344. ((['git', 'config', '--path', 'http.cookiefile'],), CERR1),
  2345. ((['git', 'config', '--global', 'http.cookiefile'],), CERR1),
  2346. (('os.path.exists', '~/.netrc'), True),
  2347. (('ask_for_data', 'Press Enter to setup .gitcookies, '
  2348. 'or Ctrl+C to abort'), ''),
  2349. ((['git', 'config', '--global', 'http.cookiefile',
  2350. os.path.expanduser('~/.gitcookies')], ), ''),
  2351. ]
  2352. self.assertEqual(0, git_cl.main(['creds-check']))
  2353. self.assertRegexpMatches(
  2354. sys.stdout.getvalue(),
  2355. '^You seem to be using outdated .netrc for git credentials:')
  2356. self.assertRegexpMatches(
  2357. sys.stdout.getvalue(),
  2358. '\nConfigured git to use .gitcookies from')
  2359. def test_creds_check_gitcookies_configured_custom_broken(self):
  2360. self._common_creds_check_mocks()
  2361. self.mock(git_cl._GitCookiesChecker, 'get_hosts_with_creds',
  2362. lambda _, include_netrc=False: [])
  2363. self.calls = [
  2364. ((['git', 'config', '--path', 'http.cookiefile'],), CERR1),
  2365. ((['git', 'config', '--global', 'http.cookiefile'],),
  2366. '/custom/.gitcookies'),
  2367. (('os.path.exists', '/custom/.gitcookies'), False),
  2368. (('ask_for_data', 'Reconfigure git to use default .gitcookies? '
  2369. 'Press Enter to reconfigure, or Ctrl+C to abort'), ''),
  2370. ((['git', 'config', '--global', 'http.cookiefile',
  2371. os.path.expanduser('~/.gitcookies')], ), ''),
  2372. ]
  2373. self.assertEqual(0, git_cl.main(['creds-check']))
  2374. self.assertRegexpMatches(
  2375. sys.stdout.getvalue(),
  2376. 'WARNING: You have configured custom path to .gitcookies: ')
  2377. self.assertRegexpMatches(
  2378. sys.stdout.getvalue(),
  2379. 'However, your configured .gitcookies file is missing.')
  2380. def test_git_cl_comment_add_gerrit(self):
  2381. self.mock(git_cl.gerrit_util, 'SetReview',
  2382. lambda host, change, msg, ready:
  2383. self._mocked_call('SetReview', host, change, msg, ready))
  2384. self.calls = [
  2385. ((['git', 'symbolic-ref', 'HEAD'],), CERR1),
  2386. ((['git', 'symbolic-ref', 'HEAD'],), CERR1),
  2387. ((['git', 'config', 'rietveld.upstream-branch'],), CERR1),
  2388. ((['git', 'branch', '-r'],), 'origin/HEAD -> origin/master\n'
  2389. 'origin/master'),
  2390. ((['git', 'config', 'remote.origin.url'],),
  2391. 'https://chromium.googlesource.com/infra/infra'),
  2392. (('SetReview', 'chromium-review.googlesource.com', 'infra%2Finfra~10',
  2393. 'msg', None),
  2394. None),
  2395. ]
  2396. self.assertEqual(0, git_cl.main(['comment', '--gerrit', '-i', '10',
  2397. '-a', 'msg']))
  2398. def test_git_cl_comments_fetch_gerrit(self):
  2399. self.mock(sys, 'stdout', StringIO.StringIO())
  2400. self.calls = [
  2401. ((['git', 'config', 'branch.foo.gerritserver'],), ''),
  2402. ((['git', 'config', 'branch.foo.merge'],), ''),
  2403. ((['git', 'config', 'rietveld.upstream-branch'],), CERR1),
  2404. ((['git', 'branch', '-r'],), 'origin/HEAD -> origin/master\n'
  2405. 'origin/master'),
  2406. ((['git', 'config', 'remote.origin.url'],),
  2407. 'https://chromium.googlesource.com/infra/infra'),
  2408. (('GetChangeDetail', 'chromium-review.googlesource.com',
  2409. 'infra%2Finfra~1',
  2410. ['MESSAGES', 'DETAILED_ACCOUNTS', 'CURRENT_REVISION',
  2411. 'CURRENT_COMMIT']), {
  2412. 'owner': {'email': 'owner@example.com'},
  2413. 'current_revision': 'ba5eba11',
  2414. 'revisions': {
  2415. 'deadbeaf': {
  2416. '_number': 1,
  2417. },
  2418. 'ba5eba11': {
  2419. '_number': 2,
  2420. },
  2421. },
  2422. 'messages': [
  2423. {
  2424. u'_revision_number': 1,
  2425. u'author': {
  2426. u'_account_id': 1111084,
  2427. u'email': u'commit-bot@chromium.org',
  2428. u'name': u'Commit Bot'
  2429. },
  2430. u'date': u'2017-03-15 20:08:45.000000000',
  2431. u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046dc50b',
  2432. u'message': u'Patch Set 1:\n\nDry run: CQ is trying the patch...',
  2433. u'tag': u'autogenerated:cq:dry-run'
  2434. },
  2435. {
  2436. u'_revision_number': 2,
  2437. u'author': {
  2438. u'_account_id': 11151243,
  2439. u'email': u'owner@example.com',
  2440. u'name': u'owner'
  2441. },
  2442. u'date': u'2017-03-16 20:00:41.000000000',
  2443. u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046d1234',
  2444. u'message': u'PTAL',
  2445. },
  2446. {
  2447. u'_revision_number': 2,
  2448. u'author': {
  2449. u'_account_id': 148512,
  2450. u'email': u'reviewer@example.com',
  2451. u'name': u'reviewer'
  2452. },
  2453. u'date': u'2017-03-17 05:19:37.500000000',
  2454. u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046d4568',
  2455. u'message': u'Patch Set 2: Code-Review+1',
  2456. },
  2457. ]
  2458. }),
  2459. (('GetChangeComments', 'chromium-review.googlesource.com',
  2460. 'infra%2Finfra~1'), {
  2461. '/COMMIT_MSG': [
  2462. {
  2463. 'author': {'email': u'reviewer@example.com'},
  2464. 'updated': u'2017-03-17 05:19:37.500000000',
  2465. 'patch_set': 2,
  2466. 'side': 'REVISION',
  2467. 'message': 'Please include a bug link',
  2468. },
  2469. ],
  2470. 'codereview.settings': [
  2471. {
  2472. 'author': {'email': u'owner@example.com'},
  2473. 'updated': u'2017-03-16 20:00:41.000000000',
  2474. 'patch_set': 2,
  2475. 'side': 'PARENT',
  2476. 'line': 42,
  2477. 'message': 'I removed this because it is bad',
  2478. },
  2479. ]
  2480. }),
  2481. (('GetChangeRobotComments', 'chromium-review.googlesource.com',
  2482. 'infra%2Finfra~1'), {}),
  2483. ((['git', 'config', 'branch.foo.gerritpatchset', '2'],), ''),
  2484. ] * 2 + [
  2485. (('write_json', 'output.json', [
  2486. {
  2487. u'date': u'2017-03-16 20:00:41.000000',
  2488. u'message': (
  2489. u'PTAL\n' +
  2490. u'\n' +
  2491. u'codereview.settings\n' +
  2492. u' Base, Line 42: https://chromium-review.googlesource.com/' +
  2493. u'c/1/2/codereview.settings#b42\n' +
  2494. u' I removed this because it is bad\n'),
  2495. u'autogenerated': False,
  2496. u'approval': False,
  2497. u'disapproval': False,
  2498. u'sender': u'owner@example.com'
  2499. }, {
  2500. u'date': u'2017-03-17 05:19:37.500000',
  2501. u'message': (
  2502. u'Patch Set 2: Code-Review+1\n' +
  2503. u'\n' +
  2504. u'/COMMIT_MSG\n' +
  2505. u' PS2, File comment: https://chromium-review.googlesource' +
  2506. u'.com/c/1/2//COMMIT_MSG#\n' +
  2507. u' Please include a bug link\n'),
  2508. u'autogenerated': False,
  2509. u'approval': False,
  2510. u'disapproval': False,
  2511. u'sender': u'reviewer@example.com'
  2512. }
  2513. ]), '')
  2514. ]
  2515. expected_comments_summary = [
  2516. git_cl._CommentSummary(
  2517. message=(
  2518. u'PTAL\n' +
  2519. u'\n' +
  2520. u'codereview.settings\n' +
  2521. u' Base, Line 42: https://chromium-review.googlesource.com/' +
  2522. u'c/1/2/codereview.settings#b42\n' +
  2523. u' I removed this because it is bad\n'),
  2524. date=datetime.datetime(2017, 3, 16, 20, 0, 41, 0),
  2525. autogenerated=False,
  2526. disapproval=False, approval=False, sender=u'owner@example.com'),
  2527. git_cl._CommentSummary(
  2528. message=(
  2529. u'Patch Set 2: Code-Review+1\n' +
  2530. u'\n' +
  2531. u'/COMMIT_MSG\n' +
  2532. u' PS2, File comment: https://chromium-review.googlesource.com/' +
  2533. u'c/1/2//COMMIT_MSG#\n' +
  2534. u' Please include a bug link\n'),
  2535. date=datetime.datetime(2017, 3, 17, 5, 19, 37, 500000),
  2536. autogenerated=False,
  2537. disapproval=False, approval=False, sender=u'reviewer@example.com'),
  2538. ]
  2539. cl = git_cl.Changelist(
  2540. issue=1, branchref='refs/heads/foo')
  2541. self.assertEqual(cl.GetCommentsSummary(), expected_comments_summary)
  2542. self.mock(git_cl.Changelist, 'GetBranch', lambda _: 'foo')
  2543. self.assertEqual(
  2544. 0, git_cl.main(['comments', '-i', '1', '-j', 'output.json']))
  2545. def test_git_cl_comments_robot_comments(self):
  2546. # git cl comments also fetches robot comments (which are considered a type
  2547. # of autogenerated comment), and unlike other types of comments, only robot
  2548. # comments from the latest patchset are shown.
  2549. self.mock(sys, 'stdout', StringIO.StringIO())
  2550. self.calls = [
  2551. ((['git', 'config', 'branch.foo.gerritserver'],), ''),
  2552. ((['git', 'config', 'branch.foo.merge'],), ''),
  2553. ((['git', 'config', 'rietveld.upstream-branch'],), CERR1),
  2554. ((['git', 'branch', '-r'],), 'origin/HEAD -> origin/master\n'
  2555. 'origin/master'),
  2556. ((['git', 'config', 'remote.origin.url'],),
  2557. 'https://chromium.googlesource.com/infra/infra'),
  2558. (('GetChangeDetail', 'chromium-review.googlesource.com',
  2559. 'infra%2Finfra~1',
  2560. ['MESSAGES', 'DETAILED_ACCOUNTS', 'CURRENT_REVISION',
  2561. 'CURRENT_COMMIT']), {
  2562. 'owner': {'email': 'owner@example.com'},
  2563. 'current_revision': 'ba5eba11',
  2564. 'revisions': {
  2565. 'deadbeaf': {
  2566. '_number': 1,
  2567. },
  2568. 'ba5eba11': {
  2569. '_number': 2,
  2570. },
  2571. },
  2572. 'messages': [
  2573. {
  2574. u'_revision_number': 1,
  2575. u'author': {
  2576. u'_account_id': 1111084,
  2577. u'email': u'commit-bot@chromium.org',
  2578. u'name': u'Commit Bot'
  2579. },
  2580. u'date': u'2017-03-15 20:08:45.000000000',
  2581. u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046dc50b',
  2582. u'message': u'Patch Set 1:\n\nDry run: CQ is trying the patch...',
  2583. u'tag': u'autogenerated:cq:dry-run'
  2584. },
  2585. {
  2586. u'_revision_number': 1,
  2587. u'author': {
  2588. u'_account_id': 123,
  2589. u'email': u'tricium@serviceaccount.com',
  2590. u'name': u'Tricium'
  2591. },
  2592. u'date': u'2017-03-16 20:00:41.000000000',
  2593. u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046d1234',
  2594. u'message': u'(1 comment)',
  2595. u'tag': u'autogenerated:tricium',
  2596. },
  2597. {
  2598. u'_revision_number': 1,
  2599. u'author': {
  2600. u'_account_id': 123,
  2601. u'email': u'tricium@serviceaccount.com',
  2602. u'name': u'Tricium'
  2603. },
  2604. u'date': u'2017-03-16 20:00:41.000000000',
  2605. u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046d1234',
  2606. u'message': u'(1 comment)',
  2607. u'tag': u'autogenerated:tricium',
  2608. },
  2609. {
  2610. u'_revision_number': 2,
  2611. u'author': {
  2612. u'_account_id': 123,
  2613. u'email': u'tricium@serviceaccount.com',
  2614. u'name': u'reviewer'
  2615. },
  2616. u'date': u'2017-03-17 05:30:37.000000000',
  2617. u'tag': u'autogenerated:tricium',
  2618. u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046d4568',
  2619. u'message': u'(1 comment)',
  2620. },
  2621. ]
  2622. }),
  2623. (('GetChangeComments', 'chromium-review.googlesource.com',
  2624. 'infra%2Finfra~1'), {}),
  2625. (('GetChangeRobotComments', 'chromium-review.googlesource.com',
  2626. 'infra%2Finfra~1'), {
  2627. 'codereview.settings': [
  2628. {
  2629. u'author': {u'email': u'tricium@serviceaccount.com'},
  2630. u'updated': u'2017-03-17 05:30:37.000000000',
  2631. u'robot_run_id': u'5565031076855808',
  2632. u'robot_id': u'Linter/Category',
  2633. u'tag': u'autogenerated:tricium',
  2634. u'patch_set': 2,
  2635. u'side': u'REVISION',
  2636. u'message': u'Linter warning message text',
  2637. u'line': 32,
  2638. },
  2639. ],
  2640. }),
  2641. ((['git', 'config', 'branch.foo.gerritpatchset', '2'],), ''),
  2642. ]
  2643. expected_comments_summary = [
  2644. git_cl._CommentSummary(date=datetime.datetime(2017, 3, 17, 5, 30, 37),
  2645. message=(
  2646. u'(1 comment)\n\ncodereview.settings\n'
  2647. u' PS2, Line 32: https://chromium-review.googlesource.com/'
  2648. u'c/1/2/codereview.settings#32\n'
  2649. u' Linter warning message text\n'),
  2650. sender=u'tricium@serviceaccount.com',
  2651. autogenerated=True, approval=False, disapproval=False)
  2652. ]
  2653. cl = git_cl.Changelist(
  2654. issue=1, branchref='refs/heads/foo')
  2655. self.assertEqual(cl.GetCommentsSummary(), expected_comments_summary)
  2656. def test_get_remote_url_with_mirror(self):
  2657. original_os_path_isdir = os.path.isdir
  2658. def selective_os_path_isdir_mock(path):
  2659. if path == '/cache/this-dir-exists':
  2660. return self._mocked_call('os.path.isdir', path)
  2661. return original_os_path_isdir(path)
  2662. self.mock(os.path, 'isdir', selective_os_path_isdir_mock)
  2663. url = 'https://chromium.googlesource.com/my/repo'
  2664. self.calls = [
  2665. ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
  2666. ((['git', 'config', 'branch.master.merge'],), 'master'),
  2667. ((['git', 'config', 'branch.master.remote'],), 'origin'),
  2668. ((['git', 'config', 'remote.origin.url'],),
  2669. '/cache/this-dir-exists'),
  2670. (('os.path.isdir', '/cache/this-dir-exists'),
  2671. True),
  2672. # Runs in /cache/this-dir-exists.
  2673. ((['git', 'config', 'remote.origin.url'],),
  2674. url),
  2675. ]
  2676. cl = git_cl.Changelist(issue=1)
  2677. self.assertEqual(cl.GetRemoteUrl(), url)
  2678. self.assertEqual(cl.GetRemoteUrl(), url) # Must be cached.
  2679. def test_get_remote_url_non_existing_mirror(self):
  2680. original_os_path_isdir = os.path.isdir
  2681. def selective_os_path_isdir_mock(path):
  2682. if path == '/cache/this-dir-doesnt-exist':
  2683. return self._mocked_call('os.path.isdir', path)
  2684. return original_os_path_isdir(path)
  2685. self.mock(os.path, 'isdir', selective_os_path_isdir_mock)
  2686. self.mock(logging, 'error',
  2687. lambda fmt, *a: self._mocked_call('logging.error', fmt % a))
  2688. self.calls = [
  2689. ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
  2690. ((['git', 'config', 'branch.master.merge'],), 'master'),
  2691. ((['git', 'config', 'branch.master.remote'],), 'origin'),
  2692. ((['git', 'config', 'remote.origin.url'],),
  2693. '/cache/this-dir-doesnt-exist'),
  2694. (('os.path.isdir', '/cache/this-dir-doesnt-exist'),
  2695. False),
  2696. (('logging.error',
  2697. 'Remote "origin" for branch "master" points to'
  2698. ' "/cache/this-dir-doesnt-exist", but it doesn\'t exist.'), None),
  2699. ]
  2700. cl = git_cl.Changelist(issue=1)
  2701. self.assertIsNone(cl.GetRemoteUrl())
  2702. def test_get_remote_url_misconfigured_mirror(self):
  2703. original_os_path_isdir = os.path.isdir
  2704. def selective_os_path_isdir_mock(path):
  2705. if path == '/cache/this-dir-exists':
  2706. return self._mocked_call('os.path.isdir', path)
  2707. return original_os_path_isdir(path)
  2708. self.mock(os.path, 'isdir', selective_os_path_isdir_mock)
  2709. self.mock(logging, 'error',
  2710. lambda *a: self._mocked_call('logging.error', *a))
  2711. self.calls = [
  2712. ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
  2713. ((['git', 'config', 'branch.master.merge'],), 'master'),
  2714. ((['git', 'config', 'branch.master.remote'],), 'origin'),
  2715. ((['git', 'config', 'remote.origin.url'],),
  2716. '/cache/this-dir-exists'),
  2717. (('os.path.isdir', '/cache/this-dir-exists'), True),
  2718. # Runs in /cache/this-dir-exists.
  2719. ((['git', 'config', 'remote.origin.url'],), ''),
  2720. (('logging.error',
  2721. 'Remote "%(remote)s" for branch "%(branch)s" points to '
  2722. '"%(cache_path)s", but it is misconfigured.\n'
  2723. '"%(cache_path)s" must be a git repo and must have a remote named '
  2724. '"%(remote)s" pointing to the git host.', {
  2725. 'remote': 'origin',
  2726. 'cache_path': '/cache/this-dir-exists',
  2727. 'branch': 'master'}
  2728. ), None),
  2729. ]
  2730. cl = git_cl.Changelist(issue=1)
  2731. self.assertIsNone(cl.GetRemoteUrl())
  2732. def test_gerrit_change_identifier_with_project(self):
  2733. self.calls = [
  2734. ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
  2735. ((['git', 'config', 'branch.master.merge'],), 'master'),
  2736. ((['git', 'config', 'branch.master.remote'],), 'origin'),
  2737. ((['git', 'config', 'remote.origin.url'],),
  2738. 'https://chromium.googlesource.com/a/my/repo.git/'),
  2739. ]
  2740. cl = git_cl.Changelist(issue=123456)
  2741. self.assertEqual(cl._GerritChangeIdentifier(), 'my%2Frepo~123456')
  2742. def test_gerrit_change_identifier_without_project(self):
  2743. self.calls = [
  2744. ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
  2745. ((['git', 'config', 'branch.master.merge'],), 'master'),
  2746. ((['git', 'config', 'branch.master.remote'],), 'origin'),
  2747. ((['git', 'config', 'remote.origin.url'],), CERR1),
  2748. ]
  2749. cl = git_cl.Changelist(issue=123456)
  2750. self.assertEqual(cl._GerritChangeIdentifier(), '123456')
  2751. class CMDTestCaseBase(unittest.TestCase):
  2752. _STATUSES = [
  2753. 'STATUS_UNSPECIFIED', 'SCHEDULED', 'STARTED', 'SUCCESS', 'FAILURE',
  2754. 'INFRA_FAILURE', 'CANCELED',
  2755. ]
  2756. _CHANGE_DETAIL = {
  2757. 'project': 'depot_tools',
  2758. 'status': 'OPEN',
  2759. 'owner': {'email': 'owner@e.mail'},
  2760. 'current_revision': 'beeeeeef',
  2761. 'revisions': {
  2762. 'deadbeaf': {'_number': 6},
  2763. 'beeeeeef': {
  2764. '_number': 7,
  2765. 'fetch': {'http': {
  2766. 'url': 'https://chromium.googlesource.com/depot_tools',
  2767. 'ref': 'refs/changes/56/123456/7'
  2768. }},
  2769. },
  2770. },
  2771. }
  2772. _DEFAULT_RESPONSE = {
  2773. 'builds': [{
  2774. 'id': str(100 + idx),
  2775. 'builder': {
  2776. 'project': 'chromium',
  2777. 'bucket': 'try',
  2778. 'builder': 'bot_' + status.lower(),
  2779. },
  2780. 'createTime': '2019-10-09T08:00:0%d.854286Z' % (idx % 10),
  2781. 'tags': [],
  2782. 'status': status,
  2783. } for idx, status in enumerate(_STATUSES)]
  2784. }
  2785. def setUp(self):
  2786. super(CMDTestCaseBase, self).setUp()
  2787. mock.patch('git_cl.sys.stdout', StringIO.StringIO()).start()
  2788. mock.patch('git_cl.uuid.uuid4', return_value='uuid4').start()
  2789. mock.patch('git_cl.Changelist.GetIssue', return_value=123456).start()
  2790. mock.patch('git_cl.Changelist.GetCodereviewServer',
  2791. return_value='https://chromium-review.googlesource.com').start()
  2792. mock.patch('git_cl.Changelist.GetMostRecentPatchset',
  2793. return_value=7).start()
  2794. mock.patch('git_cl.auth.get_authenticator',
  2795. return_value=AuthenticatorMock()).start()
  2796. mock.patch('git_cl.Changelist._GetChangeDetail',
  2797. return_value=self._CHANGE_DETAIL).start()
  2798. mock.patch('git_cl._call_buildbucket',
  2799. return_value = self._DEFAULT_RESPONSE).start()
  2800. mock.patch('git_common.is_dirty_git_tree', return_value=False).start()
  2801. self.addCleanup(mock.patch.stopall)
  2802. class CMDTryResultsTestCase(CMDTestCaseBase):
  2803. _DEFAULT_REQUEST = {
  2804. 'predicate': {
  2805. "gerritChanges": [{
  2806. "project": "depot_tools",
  2807. "host": "chromium-review.googlesource.com",
  2808. "patchset": 7,
  2809. "change": 123456,
  2810. }],
  2811. },
  2812. 'fields': ('builds.*.id,builds.*.builder,builds.*.status' +
  2813. ',builds.*.createTime,builds.*.tags'),
  2814. }
  2815. def testNoJobs(self):
  2816. git_cl._call_buildbucket.return_value = {}
  2817. self.assertEqual(0, git_cl.main(['try-results']))
  2818. self.assertEqual('No tryjobs scheduled.\n', sys.stdout.getvalue())
  2819. git_cl._call_buildbucket.assert_called_once_with(
  2820. mock.ANY, 'cr-buildbucket.appspot.com', 'SearchBuilds',
  2821. self._DEFAULT_REQUEST)
  2822. def testPrintToStdout(self):
  2823. self.assertEqual(0, git_cl.main(['try-results']))
  2824. self.assertEqual([
  2825. 'Successes:',
  2826. ' bot_success https://ci.chromium.org/b/103',
  2827. 'Infra Failures:',
  2828. ' bot_infra_failure https://ci.chromium.org/b/105',
  2829. 'Failures:',
  2830. ' bot_failure https://ci.chromium.org/b/104',
  2831. 'Canceled:',
  2832. ' bot_canceled ',
  2833. 'Started:',
  2834. ' bot_started https://ci.chromium.org/b/102',
  2835. 'Scheduled:',
  2836. ' bot_scheduled id=101',
  2837. 'Other:',
  2838. ' bot_status_unspecified id=100',
  2839. 'Total: 7 tryjobs',
  2840. ], sys.stdout.getvalue().splitlines())
  2841. git_cl._call_buildbucket.assert_called_once_with(
  2842. mock.ANY, 'cr-buildbucket.appspot.com', 'SearchBuilds',
  2843. self._DEFAULT_REQUEST)
  2844. def testPrintToStdoutWithMasters(self):
  2845. self.assertEqual(0, git_cl.main(['try-results', '--print-master']))
  2846. self.assertEqual([
  2847. 'Successes:',
  2848. ' try bot_success https://ci.chromium.org/b/103',
  2849. 'Infra Failures:',
  2850. ' try bot_infra_failure https://ci.chromium.org/b/105',
  2851. 'Failures:',
  2852. ' try bot_failure https://ci.chromium.org/b/104',
  2853. 'Canceled:',
  2854. ' try bot_canceled ',
  2855. 'Started:',
  2856. ' try bot_started https://ci.chromium.org/b/102',
  2857. 'Scheduled:',
  2858. ' try bot_scheduled id=101',
  2859. 'Other:',
  2860. ' try bot_status_unspecified id=100',
  2861. 'Total: 7 tryjobs',
  2862. ], sys.stdout.getvalue().splitlines())
  2863. git_cl._call_buildbucket.assert_called_once_with(
  2864. mock.ANY, 'cr-buildbucket.appspot.com', 'SearchBuilds',
  2865. self._DEFAULT_REQUEST)
  2866. @mock.patch('git_cl.write_json')
  2867. def testWriteToJson(self, mockJsonDump):
  2868. self.assertEqual(0, git_cl.main(['try-results', '--json', 'file.json']))
  2869. git_cl._call_buildbucket.assert_called_once_with(
  2870. mock.ANY, 'cr-buildbucket.appspot.com', 'SearchBuilds',
  2871. self._DEFAULT_REQUEST)
  2872. mockJsonDump.assert_called_once_with(
  2873. 'file.json', self._DEFAULT_RESPONSE['builds'])
  2874. def test_filter_failed_for_one_simple(self):
  2875. self.assertEqual({}, git_cl._filter_failed_for_retry([]))
  2876. self.assertEqual({
  2877. 'chromium/try': {
  2878. 'bot_failure': [],
  2879. 'bot_infra_failure': []
  2880. },
  2881. }, git_cl._filter_failed_for_retry(self._DEFAULT_RESPONSE['builds']))
  2882. def test_filter_failed_for_retry_many_builds(self):
  2883. def _build(name, created_sec, status, experimental=False):
  2884. assert 0 <= created_sec < 100, created_sec
  2885. b = {
  2886. 'id': 112112,
  2887. 'builder': {
  2888. 'project': 'chromium',
  2889. 'bucket': 'try',
  2890. 'builder': name,
  2891. },
  2892. 'createTime': '2019-10-09T08:00:%02d.854286Z' % created_sec,
  2893. 'status': status,
  2894. 'tags': [],
  2895. }
  2896. if experimental:
  2897. b['tags'].append({'key': 'cq_experimental', 'value': 'true'})
  2898. return b
  2899. builds = [
  2900. _build('flaky-last-green', 1, 'FAILURE'),
  2901. _build('flaky-last-green', 2, 'SUCCESS'),
  2902. _build('flaky', 1, 'SUCCESS'),
  2903. _build('flaky', 2, 'FAILURE'),
  2904. _build('running', 1, 'FAILED'),
  2905. _build('running', 2, 'SCHEDULED'),
  2906. _build('yep-still-running', 1, 'STARTED'),
  2907. _build('yep-still-running', 2, 'FAILURE'),
  2908. _build('cq-experimental', 1, 'SUCCESS', experimental=True),
  2909. _build('cq-experimental', 2, 'FAILURE', experimental=True),
  2910. # Simulate experimental in CQ builder, which developer decided
  2911. # to retry manually which resulted in 2nd build non-experimental.
  2912. _build('sometimes-experimental', 1, 'FAILURE', experimental=True),
  2913. _build('sometimes-experimental', 2, 'FAILURE', experimental=False),
  2914. ]
  2915. builds.sort(key=lambda b: b['status']) # ~deterministic shuffle.
  2916. self.assertEqual({
  2917. 'chromium/try': {
  2918. 'flaky': [],
  2919. 'sometimes-experimental': []
  2920. },
  2921. }, git_cl._filter_failed_for_retry(builds))
  2922. class CMDTryTestCase(CMDTestCaseBase):
  2923. @mock.patch('git_cl.Changelist.SetCQState')
  2924. @mock.patch('git_cl._get_bucket_map', return_value={})
  2925. def testSetCQDryRunByDefault(self, _mockGetBucketMap, mockSetCQState):
  2926. mockSetCQState.return_value = 0
  2927. self.assertEqual(0, git_cl.main(['try']))
  2928. git_cl.Changelist.SetCQState.assert_called_with(git_cl._CQState.DRY_RUN)
  2929. self.assertEqual(
  2930. sys.stdout.getvalue(),
  2931. 'Scheduling CQ dry run on: '
  2932. 'https://chromium-review.googlesource.com/123456\n')
  2933. @mock.patch('git_cl._call_buildbucket')
  2934. def testScheduleOnBuildbucket(self, mockCallBuildbucket):
  2935. mockCallBuildbucket.return_value = {}
  2936. self.assertEqual(0, git_cl.main([
  2937. 'try', '-B', 'luci.chromium.try', '-b', 'win',
  2938. '-p', 'key=val', '-p', 'json=[{"a":1}, null]']))
  2939. self.assertIn(
  2940. 'Scheduling jobs on:\nBucket: luci.chromium.try',
  2941. git_cl.sys.stdout.getvalue())
  2942. expected_request = {
  2943. "requests": [{
  2944. "scheduleBuild": {
  2945. "requestId": "uuid4",
  2946. "builder": {
  2947. "project": "chromium",
  2948. "builder": "win",
  2949. "bucket": "try",
  2950. },
  2951. "gerritChanges": [{
  2952. "project": "depot_tools",
  2953. "host": "chromium-review.googlesource.com",
  2954. "patchset": 7,
  2955. "change": 123456,
  2956. }],
  2957. "properties": {
  2958. "category": "git_cl_try",
  2959. "json": [{"a": 1}, None],
  2960. "key": "val",
  2961. },
  2962. "tags": [
  2963. {"value": "win", "key": "builder"},
  2964. {"value": "git_cl_try", "key": "user_agent"},
  2965. ],
  2966. },
  2967. }],
  2968. }
  2969. mockCallBuildbucket.assert_called_with(
  2970. mock.ANY, 'cr-buildbucket.appspot.com', 'Batch', expected_request)
  2971. def testScheduleOnBuildbucket_WrongBucket(self):
  2972. self.assertEqual(0, git_cl.main([
  2973. 'try', '-B', 'not-a-bucket', '-b', 'win',
  2974. '-p', 'key=val', '-p', 'json=[{"a":1}, null]']))
  2975. self.assertIn(
  2976. 'WARNING Could not parse bucket "not-a-bucket". Skipping.',
  2977. git_cl.sys.stdout.getvalue())
  2978. @mock.patch('git_cl._call_buildbucket')
  2979. @mock.patch('git_cl.fetch_try_jobs')
  2980. def testScheduleOnBuildbucketRetryFailed(
  2981. self, mockFetchTryJobs, mockCallBuildbucket):
  2982. git_cl.fetch_try_jobs.side_effect = lambda *_, **kw: {
  2983. 7: [],
  2984. 6: [{
  2985. 'id': 112112,
  2986. 'builder': {
  2987. 'project': 'chromium',
  2988. 'bucket': 'try',
  2989. 'builder': 'linux',},
  2990. 'createTime': '2019-10-09T08:00:01.854286Z',
  2991. 'tags': [],
  2992. 'status': 'FAILURE',}],}[kw['patchset']]
  2993. mockCallBuildbucket.return_value = {}
  2994. self.assertEqual(0, git_cl.main(['try', '--retry-failed']))
  2995. self.assertIn(
  2996. 'Scheduling jobs on:\nBucket: chromium/try',
  2997. git_cl.sys.stdout.getvalue())
  2998. expected_request = {
  2999. "requests": [{
  3000. "scheduleBuild": {
  3001. "requestId": "uuid4",
  3002. "builder": {
  3003. "project": "chromium",
  3004. "bucket": "try",
  3005. "builder": "linux",
  3006. },
  3007. "gerritChanges": [{
  3008. "project": "depot_tools",
  3009. "host": "chromium-review.googlesource.com",
  3010. "patchset": 7,
  3011. "change": 123456,
  3012. }],
  3013. "properties": {
  3014. "category": "git_cl_try",
  3015. },
  3016. "tags": [
  3017. {"value": "linux", "key": "builder"},
  3018. {"value": "git_cl_try", "key": "user_agent"},
  3019. {"value": "1", "key": "retry_failed"},
  3020. ],
  3021. },
  3022. }],
  3023. }
  3024. mockCallBuildbucket.assert_called_with(
  3025. mock.ANY, 'cr-buildbucket.appspot.com', 'Batch', expected_request)
  3026. def test_parse_bucket(self):
  3027. test_cases = [
  3028. {
  3029. 'bucket': 'chromium/try',
  3030. 'result': ('chromium', 'try'),
  3031. },
  3032. {
  3033. 'bucket': 'luci.chromium.try',
  3034. 'result': ('chromium', 'try'),
  3035. 'has_warning': True,
  3036. },
  3037. {
  3038. 'bucket': 'skia.primary',
  3039. 'result': ('skia', 'skia.primary'),
  3040. 'has_warning': True,
  3041. },
  3042. {
  3043. 'bucket': 'not-a-bucket',
  3044. 'result': (None, None),
  3045. },
  3046. ]
  3047. for test_case in test_cases:
  3048. git_cl.sys.stdout.truncate(0)
  3049. self.assertEqual(
  3050. test_case['result'], git_cl._parse_bucket(test_case['bucket']))
  3051. if test_case.get('has_warning'):
  3052. expected_warning = 'WARNING Please use %s/%s to specify the bucket' % (
  3053. test_case['result'])
  3054. self.assertIn(expected_warning, git_cl.sys.stdout.getvalue())
  3055. class CMDUploadTestCase(CMDTestCaseBase):
  3056. def setUp(self):
  3057. super(CMDUploadTestCase, self).setUp()
  3058. mock.patch('git_cl.fetch_try_jobs').start()
  3059. mock.patch('git_cl._trigger_try_jobs', return_value={}).start()
  3060. mock.patch('git_cl.Changelist.CMDUpload', return_value=0).start()
  3061. self.addCleanup(mock.patch.stopall)
  3062. def testUploadRetryFailed(self):
  3063. # This test mocks out the actual upload part, and just asserts that after
  3064. # upload, if --retry-failed is added, then the tool will fetch try jobs
  3065. # from the previous patchset and trigger the right builders on the latest
  3066. # patchset.
  3067. git_cl.fetch_try_jobs.side_effect = [
  3068. # Latest patchset: No builds.
  3069. [],
  3070. # Patchset before latest: Some builds.
  3071. [{
  3072. 'id': str(100 + idx),
  3073. 'builder': {
  3074. 'project': 'chromium',
  3075. 'bucket': 'try',
  3076. 'builder': 'bot_' + status.lower(),
  3077. },
  3078. 'createTime': '2019-10-09T08:00:0%d.854286Z' % (idx % 10),
  3079. 'tags': [],
  3080. 'status': status,
  3081. } for idx, status in enumerate(self._STATUSES)],
  3082. ]
  3083. self.assertEqual(0, git_cl.main(['upload', '--retry-failed']))
  3084. self.assertEqual([
  3085. mock.call(mock.ANY, mock.ANY, 'cr-buildbucket.appspot.com', patchset=7),
  3086. mock.call(mock.ANY, mock.ANY, 'cr-buildbucket.appspot.com', patchset=6),
  3087. ], git_cl.fetch_try_jobs.mock_calls)
  3088. expected_buckets = {
  3089. 'chromium/try': {'bot_failure': [], 'bot_infra_failure': []},
  3090. }
  3091. git_cl._trigger_try_jobs.assert_called_once_with(
  3092. mock.ANY, mock.ANY, expected_buckets, mock.ANY, 8)
  3093. class CMDFormatTestCase(TestCase):
  3094. def setUp(self):
  3095. super(CMDFormatTestCase, self).setUp()
  3096. self._top_dir = tempfile.mkdtemp()
  3097. def tearDown(self):
  3098. shutil.rmtree(self._top_dir)
  3099. super(CMDFormatTestCase, self).tearDown()
  3100. def _make_yapfignore(self, contents):
  3101. with open(os.path.join(self._top_dir, '.yapfignore'), 'w') as yapfignore:
  3102. yapfignore.write('\n'.join(contents))
  3103. def _make_files(self, file_dict):
  3104. for directory, files in file_dict.iteritems():
  3105. subdir = os.path.join(self._top_dir, directory)
  3106. if not os.path.exists(subdir):
  3107. os.makedirs(subdir)
  3108. for f in files:
  3109. with open(os.path.join(subdir, f), 'w'):
  3110. pass
  3111. def testYapfignoreBasic(self):
  3112. self._make_yapfignore(['test.py', '*/bar.py'])
  3113. self._make_files({
  3114. '.': ['test.py', 'bar.py'],
  3115. 'foo': ['bar.py'],
  3116. })
  3117. self.assertEqual(
  3118. set(['test.py', 'foo/bar.py']),
  3119. git_cl._GetYapfIgnoreFilepaths(self._top_dir))
  3120. def testYapfignoreMissingYapfignore(self):
  3121. self.assertEqual(set(), git_cl._GetYapfIgnoreFilepaths(self._top_dir))
  3122. def testYapfignoreMissingFile(self):
  3123. self._make_yapfignore(['test.py', 'test2.py', 'test3.py'])
  3124. self._make_files({
  3125. '.': ['test.py', 'test3.py'],
  3126. })
  3127. self.assertEqual(
  3128. set(['test.py', 'test3.py']),
  3129. git_cl._GetYapfIgnoreFilepaths(self._top_dir))
  3130. def testYapfignoreComments(self):
  3131. self._make_yapfignore(['test.py', '#test2.py'])
  3132. self._make_files({
  3133. '.': ['test.py', 'test2.py'],
  3134. })
  3135. self.assertEqual(
  3136. set(['test.py']), git_cl._GetYapfIgnoreFilepaths(self._top_dir))
  3137. def testYapfignoreBlankLines(self):
  3138. self._make_yapfignore(['test.py', '', '', 'test2.py'])
  3139. self._make_files({'.': ['test.py', 'test2.py']})
  3140. self.assertEqual(
  3141. set(['test.py', 'test2.py']),
  3142. git_cl._GetYapfIgnoreFilepaths(self._top_dir))
  3143. def testYapfignoreWhitespace(self):
  3144. self._make_yapfignore([' test.py '])
  3145. self._make_files({'.': ['test.py']})
  3146. self.assertEqual(
  3147. set(['test.py']), git_cl._GetYapfIgnoreFilepaths(self._top_dir))
  3148. def testYapfignoreMultiWildcard(self):
  3149. self._make_yapfignore(['*es*.py'])
  3150. self._make_files({
  3151. '.': ['test.py', 'test2.py'],
  3152. })
  3153. self.assertEqual(
  3154. set(['test.py', 'test2.py']),
  3155. git_cl._GetYapfIgnoreFilepaths(self._top_dir))
  3156. def testYapfignoreRestoresDirectory(self):
  3157. self._make_yapfignore(['test.py'])
  3158. self._make_files({
  3159. '.': ['test.py'],
  3160. })
  3161. old_cwd = os.getcwd()
  3162. self.assertEqual(
  3163. set(['test.py']), git_cl._GetYapfIgnoreFilepaths(self._top_dir))
  3164. self.assertEqual(old_cwd, os.getcwd())
  3165. if __name__ == '__main__':
  3166. logging.basicConfig(
  3167. level=logging.DEBUG if '-v' in sys.argv else logging.ERROR)
  3168. unittest.main()