1#!/usr/bin/env python
2#
3# Copyright 2017 - The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Script to run */tests/*_unittest.py files."""
18
19from multiprocessing import Process
20import os
21import runpy
22
23
24def get_unittest_files():
25  matches = []
26  for dirpath, _, filenames in os.walk('.'):
27    if os.path.basename(dirpath) == 'tests':
28      matches.extend(os.path.join(dirpath, f)
29                     for f in filenames if f.endswith('_unittest.py'))
30  return matches
31
32
33def run_test(unittest_file):
34  runpy.run_path(unittest_file, run_name='__main__')
35
36
37if __name__ == '__main__':
38  for path in get_unittest_files():
39    # Forks a process to run the unittest.
40    # Otherwise, it only runs one unittest.
41    p = Process(target=run_test, args=(path,))
42    p.start()
43    p.join()
44    if p.exitcode != 0:
45      break  # stops on any failure unittest
46