1#!/usr/bin/env python 2# 3# Copyright 2016 Google Inc. 4# 5# Use of this source code is governed by a BSD-style license that can be 6# found in the LICENSE file. 7 8 9"""Run all infrastructure-related tests.""" 10 11 12from __future__ import print_function 13import os 14import subprocess 15import sys 16 17 18INFRA_BOTS_DIR = os.path.dirname(os.path.realpath(__file__)) 19SKIA_DIR = os.path.abspath(os.path.join(INFRA_BOTS_DIR, os.pardir, os.pardir)) 20 21 22def test(cmd, cwd): 23 try: 24 subprocess.check_output(cmd, cwd=cwd, stderr=subprocess.STDOUT) 25 except subprocess.CalledProcessError as e: 26 return e.output 27 28 29def python_unit_tests(train): 30 if train: 31 return None 32 return test( 33 ['python', '-u', '-m', 'unittest', 'discover', '-s', '.', '-p', 34 '*_test.py'], 35 INFRA_BOTS_DIR) 36 37 38def recipe_test(train): 39 cmd = [ 40 'python', '-u', os.path.join(INFRA_BOTS_DIR, 'recipes.py'), 'test'] 41 if train: 42 cmd.append('train') 43 else: 44 cmd.append('run') 45 return test(cmd, SKIA_DIR) 46 47 48def gen_tasks_test(train): 49 cmd = ['go', 'run', 'gen_tasks.go'] 50 if not train: 51 cmd.append('--test') 52 try: 53 output = test(cmd, INFRA_BOTS_DIR) 54 except OSError: 55 return ('Failed to run "%s"; do you have Go installed on your machine?' 56 % ' '.join(cmd)) 57 return output 58 59 60def main(): 61 train = False 62 if '--train' in sys.argv: 63 train = True 64 65 tests = ( 66 python_unit_tests, 67 recipe_test, 68 gen_tasks_test, 69 ) 70 errs = [] 71 for t in tests: 72 err = t(train) 73 if err: 74 errs.append(err) 75 76 if len(errs) > 0: 77 print('Test failures:\n', file=sys.stderr) 78 for err in errs: 79 print('==============================', file=sys.stderr) 80 print(err, file=sys.stderr) 81 print('==============================', file=sys.stderr) 82 sys.exit(1) 83 84 if train: 85 print('Trained tests successfully.') 86 else: 87 print('All tests passed!') 88 89 90if __name__ == '__main__': 91 main() 92