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 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) 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 ['python', '-m', 'unittest', 'discover', '-s', '.', '-p', '*_test.py'], 33 INFRA_BOTS_DIR) 34 35 36def recipe_test(train): 37 cmd = [ 38 'python', os.path.join(INFRA_BOTS_DIR, 'recipes.py'), 'test'] 39 if train: 40 cmd.append('train') 41 else: 42 cmd.append('run') 43 return test(cmd, SKIA_DIR) 44 45 46def gen_tasks_test(train): 47 cmd = ['go', 'run', 'gen_tasks.go'] 48 if not train: 49 cmd.append('--test') 50 try: 51 output = test(cmd, INFRA_BOTS_DIR) 52 except OSError: 53 return ('Failed to run "%s"; do you have Go installed on your machine?' 54 % ' '.join(cmd)) 55 if output and 'cannot find package "go.skia.org/infra' in output: 56 return ('Failed to run gen_tests.go:\n\n%s\nMaybe you need to run:\n\n' 57 '$ go get -u go.skia.org/infra/...' % output) 58 return output 59 60 61def main(): 62 train = False 63 if '--train' in sys.argv: 64 train = True 65 66 tests = ( 67 python_unit_tests, 68 recipe_test, 69 gen_tasks_test, 70 ) 71 errs = [] 72 for t in tests: 73 err = t(train) 74 if err: 75 errs.append(err) 76 77 if len(errs) > 0: 78 print >> sys.stderr, 'Test failures:\n' 79 for err in errs: 80 print >> sys.stderr, '==============================' 81 print >> sys.stderr, err 82 print >> sys.stderr, '==============================' 83 sys.exit(1) 84 85 if train: 86 print 'Trained tests successfully.' 87 else: 88 print 'All tests passed!' 89 90 91if __name__ == '__main__': 92 main() 93