1 # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 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 # ============================================================================== 15 """Tests to check that py_test are properly loaded in BUILD files.""" 16 17 from __future__ import absolute_import 18 from __future__ import division 19 from __future__ import print_function 20 21 import os 22 import subprocess 23 24 25 os.chdir(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../..'))) 26 27 28 def check_output_despite_error(args): 29 """Get output of args from command line, even if there are errors. 30 31 Args: 32 args: a list of command line args. 33 34 Returns: 35 output as string. 36 """ 37 try: 38 output = subprocess.check_output(args, stderr=subprocess.STDOUT) 39 except subprocess.CalledProcessError as e: 40 output = e.output 41 return output.strip() 42 43 44 def main(): 45 # Get all py_test target, note bazel query result will also include 46 # cuda_py_test etc. 47 try: 48 targets = subprocess.check_output([ 49 'bazel', 'query', 50 'kind(py_test, //tensorflow/contrib/... + ' 51 '//tensorflow/python/... - ' 52 '//tensorflow/contrib/tensorboard/...)']).strip() 53 except subprocess.CalledProcessError as e: 54 targets = e.output 55 56 # Only keep py_test targets, and filter out targets with 'no_pip' tag. 57 valid_targets = [] 58 for target in targets.split('\n'): 59 kind = check_output_despite_error(['buildozer', 'print kind', target]) 60 if kind == 'py_test': 61 tags = check_output_despite_error(['buildozer', 'print tags', target]) 62 if 'no_pip' not in tags: 63 valid_targets.append(target) 64 65 # Get all BUILD files for all valid targets. 66 build_files = set() 67 for target in valid_targets: 68 build_files.add(os.path.join(target[2:].split(':')[0], 'BUILD')) 69 70 # Check if BUILD files load py_test. 71 files_missing_load = [] 72 for build_file in build_files: 73 updated_build_file = subprocess.check_output( 74 ['buildozer', '-stdout', 'new_load //tensorflow:tensorflow.bzl py_test', 75 build_file]) 76 with open(build_file, 'r') as f: 77 if f.read() != updated_build_file: 78 files_missing_load.append(build_file) 79 80 if files_missing_load: 81 raise RuntimeError('The following files are missing %s:\n %s' % ( 82 'load("//tensorflow:tensorflow.bzl", "py_test").\nThis load statement' 83 ' is needed because otherwise pip tests will try to use their ' 84 'dependencies, which are not visible to them.', 85 '\n'.join(files_missing_load))) 86 else: 87 print('TEST PASSED.') 88 89 90 if __name__ == '__main__': 91 main() 92