blob: 10546ed7223813aa65262358417e784cb2cd981c [file] [log] [blame]
borenet2dbbfa52016-10-14 06:32:09 -07001#!/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
Eric Boren2ce36ea2017-04-13 10:22:11 -040028def python_unit_tests(train):
29 if train:
30 return None
borenet2dbbfa52016-10-14 06:32:09 -070031 return test(
32 ['python', '-m', 'unittest', 'discover', '-s', '.', '-p', '*_test.py'],
33 INFRA_BOTS_DIR)
34
35
Eric Borena0481b92017-04-18 09:15:53 -040036def recipe_test(train):
Eric Boren2ce36ea2017-04-13 10:22:11 -040037 cmd = [
Eric Boren89cd3572017-06-28 13:50:22 -040038 'python', os.path.join(INFRA_BOTS_DIR, 'recipes.py'), 'test']
Eric Boren2ce36ea2017-04-13 10:22:11 -040039 if train:
Eric Boren89cd3572017-06-28 13:50:22 -040040 cmd.append('train')
41 else:
42 cmd.append('run')
Eric Boren2ce36ea2017-04-13 10:22:11 -040043 return test(cmd, SKIA_DIR)
borenet2dbbfa52016-10-14 06:32:09 -070044
45
Eric Boren2ce36ea2017-04-13 10:22:11 -040046def gen_tasks_test(train):
47 cmd = ['go', 'run', 'gen_tasks.go']
48 if not train:
49 cmd.append('--test')
borenet2dbbfa52016-10-14 06:32:09 -070050 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))
Eric Boren543de8f2017-12-12 13:20:25 -050055 if output:
56 if ('cannot find package "go.skia.org/infra' in output or
57 'gen_tasks.go:' in output):
58 return ('Failed to run gen_tests.go:\n\n%s\nMaybe you need to run:\n\n'
59 '$ go get -u go.skia.org/infra/...' % output)
borenet2dbbfa52016-10-14 06:32:09 -070060 return output
61
62
63def main():
Eric Boren2ce36ea2017-04-13 10:22:11 -040064 train = False
65 if '--train' in sys.argv:
66 train = True
67
borenet2dbbfa52016-10-14 06:32:09 -070068 tests = (
69 python_unit_tests,
Eric Borena0481b92017-04-18 09:15:53 -040070 recipe_test,
borenet2dbbfa52016-10-14 06:32:09 -070071 gen_tasks_test,
72 )
73 errs = []
74 for t in tests:
Eric Boren2ce36ea2017-04-13 10:22:11 -040075 err = t(train)
borenet2dbbfa52016-10-14 06:32:09 -070076 if err:
77 errs.append(err)
78
79 if len(errs) > 0:
80 print >> sys.stderr, 'Test failures:\n'
81 for err in errs:
82 print >> sys.stderr, '=============================='
83 print >> sys.stderr, err
84 print >> sys.stderr, '=============================='
85 sys.exit(1)
86
Eric Boren2ce36ea2017-04-13 10:22:11 -040087 if train:
88 print 'Trained tests successfully.'
89 else:
90 print 'All tests passed!'
borenet2dbbfa52016-10-14 06:32:09 -070091
92
93if __name__ == '__main__':
94 main()