1# Copyright (C) 2022 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15from __future__ import print_function 16import time 17import subprocess 18from os.path import relpath 19 20 21USE_PYTHON3 = True 22 23 24def RunAndReportIfLong(func, *args, **kargs): 25 start = time.time() 26 results = func(*args, **kargs) 27 end = time.time() 28 limit = 3.0 # seconds 29 name = func.__name__ 30 runtime = end - start 31 if runtime > limit: 32 print("{} took >{:.2}s ({:.2}s)".format(name, limit, runtime)) 33 return results 34 35 36def CheckChange(input, output): 37 results = [] 38 results += RunAndReportIfLong(CheckSqlTest, input, output) 39 return results 40 41 42def CheckChangeOnUpload(input_api, output_api): 43 return CheckChange(input_api, output_api) 44 45 46def CheckChangeOnCommit(input_api, output_api): 47 return CheckChange(input_api, output_api) 48 49 50def CheckSqlTest(input_api, output_api): 51 52 def file_filter(x): 53 return input_api.FilterSourceFile( 54 x, files_to_check=('.*\.sql',), files_to_skip=('.*\_test\.sql',)) 55 56 non_test_sql = input_api.AffectedSourceFiles(file_filter) 57 if non_test_sql: 58 return [ 59 output_api.PresubmitError("SQL tests should be named *_test.sql:", 60 [f.LocalPath() for f in non_test_sql]) 61 ] 62 return [] 63