blob: 7150e0408e1e77820e3bbf8b9be584dd422e9262 [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))
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():
Eric Boren2ce36ea2017-04-13 10:22:11 -040062 train = False
63 if '--train' in sys.argv:
64 train = True
65
borenet2dbbfa52016-10-14 06:32:09 -070066 tests = (
67 python_unit_tests,
Eric Borena0481b92017-04-18 09:15:53 -040068 recipe_test,
borenet2dbbfa52016-10-14 06:32:09 -070069 gen_tasks_test,
70 )
71 errs = []
72 for t in tests:
Eric Boren2ce36ea2017-04-13 10:22:11 -040073 err = t(train)
borenet2dbbfa52016-10-14 06:32:09 -070074 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
Eric Boren2ce36ea2017-04-13 10:22:11 -040085 if train:
86 print 'Trained tests successfully.'
87 else:
88 print 'All tests passed!'
borenet2dbbfa52016-10-14 06:32:09 -070089
90
91if __name__ == '__main__':
92 main()