1#!/usr/bin/env python3 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 12import os 13import subprocess 14import sys 15 16 17INFRA_BOTS_DIR = os.path.dirname(os.path.realpath(__file__)) 18SKIA_DIR = os.path.abspath(os.path.join(INFRA_BOTS_DIR, os.pardir, os.pardir)) 19 20 21def test(cmd, cwd): 22 try: 23 subprocess.check_output(cmd, cwd=cwd, stderr=subprocess.STDOUT, encoding='utf-8') 24 except subprocess.CalledProcessError as e: 25 return e.output 26 27 28def python_unit_tests(train): 29 if train: 30 return None 31 return test( 32 [sys.executable, '-u', '-m', 'unittest', 'discover', '-s', '.', '-p', 33 '*_test.py'], 34 INFRA_BOTS_DIR) 35 36 37def recipe_test(train): 38 cmd = [ 39 sys.executable, '-u', os.path.join(INFRA_BOTS_DIR, 'recipes.py'), 'test'] 40 if train: 41 cmd.append('train') 42 else: 43 cmd.append('run') 44 return test(cmd, SKIA_DIR) 45 46 47def gen_tasks_test(train): 48 cmd = ['go', 'run', 'gen_tasks.go'] 49 if not train: 50 cmd.append('--test') 51 try: 52 output = test(cmd, INFRA_BOTS_DIR) 53 except OSError: 54 return ('Failed to run "%s"; do you have Go installed on your machine?' 55 % ' '.join(cmd)) 56 return output 57 58 59def main(): 60 train = False 61 if '--train' in sys.argv: 62 train = True 63 64 tests = ( 65 python_unit_tests, 66 recipe_test, 67 gen_tasks_test, 68 ) 69 errs = [] 70 for t in tests: 71 err = t(train) 72 if err: 73 errs.append(err) 74 75 if len(errs) > 0: 76 print('Test failures:\n', file=sys.stderr) 77 for err in errs: 78 print('==============================', file=sys.stderr) 79 print(err, file=sys.stderr) 80 print('==============================', file=sys.stderr) 81 sys.exit(1) 82 83 if train: 84 print('Trained tests successfully.') 85 else: 86 print('All tests passed!') 87 88 89if __name__ == '__main__': 90 main() 91