@ -27,6 +27,7 @@ import owners
import owners_finder
import subprocess2 as subprocess
import presubmit_support as presubmit
import rietveld
import auth
import git_cl
import git_common as git
@ -176,11 +177,11 @@ class PresubmitUnittest(PresubmitTestsBase):
' GitChange ' , ' InputApi ' , ' ListRelevantPresubmitFiles ' , ' main ' ,
' NonexistantCannedCheckFilter ' , ' OutputApi ' , ' ParseFiles ' ,
' PresubmitFailure ' , ' PresubmitExecuter ' , ' PresubmitOutput ' , ' ScanSubDirs ' ,
' ast ' , ' cPickle' , ' cpplint ' , ' cStringIO ' , ' contextlib ' ,
' ast ' , ' auth' , ' cPickle' , ' cpplint ' , ' cStringIO ' , ' contextlib ' ,
' canned_check_filter ' , ' fix_encoding ' , ' fnmatch ' , ' gclient_utils ' ,
' git_footers ' , ' glob ' , ' inspect ' , ' json ' , ' load_files ' , ' logging ' ,
' marshal ' , ' normpath ' , ' optparse ' , ' os ' , ' owners ' , ' owners_finder ' ,
' pickle ' , ' presubmit_canned_checks ' , ' random ' , ' re ' , ' scm' ,
' pickle ' , ' presubmit_canned_checks ' , ' random ' , ' re ' , ' rietveld' , ' scm' ,
' subprocess ' , ' sys ' , ' tempfile ' , ' time ' , ' traceback ' , ' types ' , ' unittest ' ,
' urllib2 ' , ' warn ' , ' multiprocessing ' , ' DoGetTryMasters ' ,
' GetTryMastersExecuter ' , ' itertools ' , ' urlparse ' , ' gerrit_util ' ,
@ -620,7 +621,7 @@ class PresubmitUnittest(PresubmitTestsBase):
output = presubmit . DoPresubmitChecks (
change = change , committing = False , verbose = True ,
output_stream = None , input_stream = None ,
default_presubmit = None , may_prompt = False , ger rit_obj= None )
default_presubmit = None , may_prompt = False , rie tveld _obj= None )
self . failUnless ( output . should_continue ( ) )
self . assertEqual ( output . getvalue ( ) . count ( ' !! ' ) , 0 )
self . assertEqual ( output . getvalue ( ) . count ( ' ?? ' ) , 0 )
@ -655,7 +656,7 @@ class PresubmitUnittest(PresubmitTestsBase):
output = presubmit . DoPresubmitChecks (
change = change , committing = False , verbose = True ,
output_stream = None , input_stream = input_buf ,
default_presubmit = None , may_prompt = True , ger rit_obj= None )
default_presubmit = None , may_prompt = True , rie tveld _obj= None )
self . failIf ( output . should_continue ( ) )
self . assertEqual ( output . getvalue ( ) . count ( ' ?? ' ) , 2 )
@ -663,7 +664,7 @@ class PresubmitUnittest(PresubmitTestsBase):
output = presubmit . DoPresubmitChecks (
change = change , committing = False , verbose = True ,
output_stream = None , input_stream = input_buf ,
default_presubmit = None , may_prompt = True , ger rit_obj= None )
default_presubmit = None , may_prompt = True , rie tveld _obj= None )
self . failUnless ( output . should_continue ( ) )
self . assertEquals ( output . getvalue ( ) . count ( ' ?? ' ) , 2 )
self . assertEqual ( output . getvalue ( ) . count (
@ -694,7 +695,7 @@ class PresubmitUnittest(PresubmitTestsBase):
output = presubmit . DoPresubmitChecks (
change = change , committing = False , verbose = True ,
output_stream = None , input_stream = None ,
default_presubmit = None , may_prompt = False , ger rit_obj= None )
default_presubmit = None , may_prompt = False , rie tveld _obj= None )
# A warning is printed, and should_continue is True.
self . failUnless ( output . should_continue ( ) )
self . assertEquals ( output . getvalue ( ) . count ( ' ?? ' ) , 2 )
@ -725,7 +726,7 @@ class PresubmitUnittest(PresubmitTestsBase):
output = presubmit . DoPresubmitChecks (
change = change , committing = False , verbose = True ,
output_stream = None , input_stream = None ,
default_presubmit = None , may_prompt = True , ger rit_obj= None )
default_presubmit = None , may_prompt = True , rie tveld _obj= None )
self . failIf ( output . should_continue ( ) )
self . assertEqual ( output . getvalue ( ) . count ( ' ?? ' ) , 0 )
self . assertEqual ( output . getvalue ( ) . count ( ' !! ' ) , 2 )
@ -759,7 +760,7 @@ def CheckChangeOnCommit(input_api, output_api):
change = change , committing = False , verbose = True ,
output_stream = None , input_stream = input_buf ,
default_presubmit = always_fail_presubmit_script ,
may_prompt = False , ger rit_obj= None )
may_prompt = False , rie tveld _obj= None )
self . failIf ( output . should_continue ( ) )
text = (
' Running presubmit upload checks ... \n '
@ -819,7 +820,7 @@ def CheckChangeOnCommit(input_api, output_api):
change = change , committing = False , verbose = True ,
output_stream = output_buf , input_stream = input_buf ,
default_presubmit = tag_checker_presubmit_script ,
may_prompt = False , ger rit_obj= None )
may_prompt = False , rie tveld _obj= None )
self . failUnless ( presubmit_output )
self . assertEquals ( output_buf . getvalue ( ) ,
@ -961,7 +962,7 @@ def CheckChangeOnCommit(input_api, output_api):
presubmit . DoPresubmitChecks ( mox . IgnoreArg ( ) , False , False ,
mox . IgnoreArg ( ) ,
mox . IgnoreArg ( ) ,
None , False , None , None ). AndReturn ( output )
None , False , None , None , None ). AndReturn ( output )
self . mox . ReplayAll ( )
self . assertEquals (
@ -1022,6 +1023,7 @@ class InputApiUnittest(PresubmitTestsBase):
' environ ' ,
' fnmatch ' ,
' glob ' ,
' host_url ' ,
' is_committing ' ,
' is_windows ' ,
' json ' ,
@ -1037,6 +1039,7 @@ class InputApiUnittest(PresubmitTestsBase):
' platform ' ,
' python_executable ' ,
' re ' ,
' rietveld ' ,
' subprocess ' ,
' tbr ' ,
' tempfile ' ,
@ -1059,9 +1062,10 @@ class InputApiUnittest(PresubmitTestsBase):
api = presubmit . InputApi (
self . fake_change ,
presubmit_path = ' foo/path/PRESUBMIT.py ' ,
is_committing = False , ger rit_obj= None , verbose = False )
is_committing = False , rie tveld _obj= None , verbose = False )
self . assertEquals ( api . PresubmitLocalPath ( ) , ' foo/path ' )
self . assertEquals ( api . change , self . fake_change )
self . assertEquals ( api . host_url , ' http://codereview.chromium.org ' )
def testInputApiPresubmitScriptFiltering ( self ) :
description_lines = ( ' Hello there ' ,
@ -1293,7 +1297,7 @@ class InputApiUnittest(PresubmitTestsBase):
self . fake_root_dir , ' isdir ' , ' PRESUBMIT.py ' )
api = presubmit . InputApi (
change = change , presubmit_path = presubmit_path ,
is_committing = True , ger rit_obj= None , verbose = False )
is_committing = True , rie tveld _obj= None , verbose = False )
paths_from_api = api . AbsoluteLocalPaths ( )
self . assertEqual ( len ( paths_from_api ) , 2 )
for absolute_paths in [ paths_from_change , paths_from_api ] :
@ -1370,7 +1374,7 @@ class InputApiUnittest(PresubmitTestsBase):
input_api = presubmit . InputApi (
self . fake_change ,
presubmit_path = ' foo/path/PRESUBMIT.py ' ,
is_committing = False , ger rit_obj= None , verbose = False )
is_committing = False , rie tveld _obj= None , verbose = False )
input_api . tempfile . NamedTemporaryFile = self . mox . CreateMock (
input_api . tempfile . NamedTemporaryFile )
input_api . tempfile . NamedTemporaryFile (
@ -1736,7 +1740,8 @@ class CannedChecksUnittest(PresubmitTestsBase):
input_api . os_walk = self . mox . CreateMockAnything ( )
input_api . os_path = presubmit . os . path
input_api . re = presubmit . re
input_api . gerrit = self . mox . CreateMock ( presubmit . GerritAccessor )
input_api . rietveld = self . mox . CreateMock ( rietveld . Rietveld )
input_api . gerrit = None
input_api . traceback = presubmit . traceback
input_api . urllib2 = self . mox . CreateMock ( presubmit . urllib2 )
input_api . unittest = unittest
@ -1750,6 +1755,7 @@ class CannedChecksUnittest(PresubmitTestsBase):
input_api . is_windows = False
input_api . change = change
input_api . host_url = ' http://localhost '
input_api . is_committing = committing
input_api . tbr = False
input_api . dry_run = None
@ -1785,6 +1791,7 @@ class CannedChecksUnittest(PresubmitTestsBase):
' CheckOwners ' ,
' CheckPatchFormatted ' ,
' CheckGNFormatted ' ,
' CheckRietveldTryJobExecution ' ,
' CheckSingletonInHeaders ' ,
' CheckVPythonSpec ' ,
' RunPythonUnitTests ' , ' RunPylint ' ,
@ -2398,8 +2405,10 @@ class CannedChecksUnittest(PresubmitTestsBase):
presubmit . OutputApi . PresubmitNotifyResult )
def AssertOwnersWorks ( self , tbr = False , issue = ' 1 ' , approvers = None ,
reviewers = None , is_committing = True , response = None , uncovered_files = None ,
expected_output = ' ' , manually_specified_reviewers = None , dry_run = None ,
reviewers = None , is_committing = True ,
rietveld_response = None , gerrit_response = None ,
uncovered_files = None , expected_output = ' ' ,
manually_specified_reviewers = None , dry_run = None ,
modified_file = ' foo/xyz.cc ' ) :
if approvers is None :
# The set of people who lgtm'ed a change.
@ -2422,6 +2431,9 @@ class CannedChecksUnittest(PresubmitTestsBase):
change . RepositoryRoot = lambda : None
affected_file = self . mox . CreateMock ( presubmit . GitAffectedFile )
input_api = self . MockInputApi ( change , False )
if gerrit_response :
assert not rietveld_response
input_api . rietveld = None
input_api . gerrit = presubmit . GerritAccessor ( ' host ' )
fake_db = self . mox . CreateMock ( owners . Database )
@ -2442,22 +2454,14 @@ class CannedChecksUnittest(PresubmitTestsBase):
change . AffectedFiles ( file_filter = None ) . AndReturn ( [ affected_file ] )
if not is_committing or ( not tbr and issue ) or ( ' OWNERS ' in modified_file ) :
change . OriginalOwnersFiles ( ) . AndReturn ( { } )
if issue and not response :
response = {
" owner " : { " email " : change . author_email } ,
" labels " : { " Code-Review " : {
u ' all ' : [
{
u ' email ' : a ,
u ' value ' : + 1
} for a in approvers
if issue and not rietveld_response and not gerrit_response :
rietveld_response = {
" owner_email " : change . author_email ,
" messages " : [
{ " sender " : a , " text " : " I approve " , " approval " : True }
for a in approvers
] ,
u ' default_value ' : 0 ,
u ' values ' : { u ' 0 ' : u ' No score ' ,
u ' +1 ' : u ' Looks good to me ' ,
u ' -1 ' : u " I would prefer that you didn ' t submit this " }
} } ,
" reviewers " : { " REVIEWER " : [ { u ' email ' : a } ] for a in approvers } ,
" reviewers " : reviewers
}
if is_committing :
@ -2466,7 +2470,12 @@ class CannedChecksUnittest(PresubmitTestsBase):
people = reviewers
if issue :
input_api . gerrit . _FetchChangeDetail = lambda _ : response
if rietveld_response :
input_api . rietveld . get_issue_properties (
issue = int ( input_api . change . issue ) , messages = True ) . AndReturn (
rietveld_response )
elif gerrit_response :
input_api . gerrit . _FetchChangeDetail = lambda _ : gerrit_response
people . add ( change . author_email )
change . OriginalOwnersFiles ( ) . AndReturn ( { } )
@ -2484,25 +2493,12 @@ class CannedChecksUnittest(PresubmitTestsBase):
def testCannedCheckOwners_DryRun ( self ) :
response = {
" owner " : { " email " : " john@example.com " } ,
" labels " : { " Code-Review " : {
u ' all ' : [
{
u ' email ' : u ' ben@example.com ' ,
u ' value ' : 0
} ,
] ,
u ' approved ' : { u ' email ' : u ' ben@example.org ' } ,
u ' default_value ' : 0 ,
u ' values ' : { u ' 0 ' : u ' No score ' ,
u ' +1 ' : u ' Looks good to me ' ,
u ' -1 ' : u " I would prefer that you didn ' t submit this " }
} } ,
" reviewers " : { " REVIEWER " : [ { u ' email ' : u ' ben@example.com ' } ] } ,
" owner_email " : " john@example.com " ,
" reviewers " : [ " ben@example.com " ] ,
}
self . AssertOwnersWorks ( approvers = set ( ) ,
dry_run = True ,
r esponse= response ,
rietveld_response = response ,
reviewers = set ( [ " ben@example.com " ] ) ,
expected_output = ' This is a dry run, but these failures would be ' +
' reported on commit: \n Missing LGTM from someone ' +
@ -2510,10 +2506,10 @@ class CannedChecksUnittest(PresubmitTestsBase):
self . AssertOwnersWorks ( approvers = set ( [ ' ben@example.com ' ] ) ,
is_committing = False ,
r esponse= response ,
r ietveld_r esponse= response ,
expected_output = ' ' )
def testCannedCheckOwners_Approved ( self ) :
def testCannedCheckOwners_Approved _Gerrit ( self ) :
response = {
" owner " : { " email " : " john@example.com " } ,
" labels " : { " Code-Review " : {
@ -2538,13 +2534,13 @@ class CannedChecksUnittest(PresubmitTestsBase):
" reviewers " : { " REVIEWER " : [ { u ' email ' : u ' ben@example.com ' } ] } ,
}
self . AssertOwnersWorks ( approvers = set ( [ ' ben@example.com ' ] ) ,
response= response ,
gerrit_ response= response ,
is_committing = True ,
expected_output = ' ' )
self . AssertOwnersWorks ( approvers = set ( [ ' ben@example.com ' ] ) ,
is_committing = False ,
response= response ,
gerrit_ response= response ,
expected_output = ' ' )
# Testing configuration with on -1..+1.
@ -2566,11 +2562,31 @@ class CannedChecksUnittest(PresubmitTestsBase):
" reviewers " : { " REVIEWER " : [ { u ' email ' : u ' ben@example.com ' } ] } ,
}
self . AssertOwnersWorks ( approvers = set ( [ ' ben@example.com ' ] ) ,
response= response ,
gerrit_ response= response ,
is_committing = True ,
expected_output = ' ' )
def testCannedCheckOwners_NotApproved ( self ) :
def testCannedCheckOwners_Approved ( self ) :
response = {
" owner_email " : " john@example.com " ,
" messages " : [
{
" sender " : " ben@example.com " , " text " : " foo " , " approval " : True ,
} ,
] ,
" reviewers " : [ " ben@example.com " ] ,
}
self . AssertOwnersWorks ( approvers = set ( [ ' ben@example.com ' ] ) ,
rietveld_response = response ,
expected_output = ' ' )
self . AssertOwnersWorks ( approvers = set ( [ ' ben@example.com ' ] ) ,
is_committing = False ,
rietveld_response = response ,
expected_output = ' ' )
def testCannedCheckOwners_NotApproved_Gerrit ( self ) :
response = {
" owner " : { " email " : " john@example.com " } ,
" labels " : { " Code-Review " : {
@ -2597,7 +2613,7 @@ class CannedChecksUnittest(PresubmitTestsBase):
self . AssertOwnersWorks (
approvers = set ( ) ,
reviewers = set ( [ " ben@example.com " ] ) ,
response= response ,
gerrit_ response= response ,
is_committing = True ,
expected_output =
' Missing LGTM from someone other than john@example.com \n ' )
@ -2606,7 +2622,7 @@ class CannedChecksUnittest(PresubmitTestsBase):
approvers = set ( ) ,
reviewers = set ( [ " ben@example.com " ] ) ,
is_committing = False ,
response= response ,
gerrit_ response= response ,
expected_output = ' ' )
# Testing configuration with on -1..+1.
@ -2630,26 +2646,49 @@ class CannedChecksUnittest(PresubmitTestsBase):
self . AssertOwnersWorks (
approvers = set ( ) ,
reviewers = set ( [ " ben@example.com " ] ) ,
response= response ,
gerrit_ response= response ,
is_committing = True ,
expected_output =
' Missing LGTM from someone other than john@example.com \n ' )
def testCannedCheckOwners_NotApproved ( self ) :
response = {
" owner_email " : " john@example.com " ,
" messages " : [
{
" sender " : " ben@example.com " , " text " : " foo " , " approval " : False ,
} ,
] ,
" reviewers " : [ " ben@example.com " ] ,
}
self . AssertOwnersWorks (
approvers = set ( ) ,
reviewers = set ( [ " ben@example.com " ] ) ,
rietveld_response = response ,
expected_output =
' Missing LGTM from someone other than john@example.com \n ' )
self . AssertOwnersWorks (
approvers = set ( ) ,
reviewers = set ( [ " ben@example.com " ] ) ,
is_committing = False ,
rietveld_response = response ,
expected_output = ' ' )
def testCannedCheckOwners_NoReviewers ( self ) :
response = {
" owner " : { " email " : " john@example.com " } ,
" labels " : { " Code-Review " : {
u ' default_value ' : 0 ,
u ' values ' : { u ' 0 ' : u ' No score ' ,
u ' +1 ' : u ' Looks good to me ' ,
u ' -1 ' : u " I would prefer that you didn ' t submit this " }
} } ,
" reviewers " : { } ,
" owner_email " : " john@example.com " ,
" messages " : [
{
" sender " : " ben@example.com " , " text " : " foo " , " approval " : False ,
} ,
] ,
" reviewers " : [ ] ,
}
self . AssertOwnersWorks (
approvers = set ( ) ,
reviewers = set ( ) ,
response = response ,
r ietveld_r esponse= response ,
expected_output =
' Missing LGTM from someone other than john@example.com \n ' )
@ -2657,7 +2696,7 @@ class CannedChecksUnittest(PresubmitTestsBase):
approvers = set ( ) ,
reviewers = set ( ) ,
is_committing = False ,
r esponse= response ,
r ietveld_r esponse= response ,
expected_output = ' ' )
def testCannedCheckOwners_NoIssueNoFiles ( self ) :