File: test_finder.py

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (280 lines) | stat: -rwxr-xr-x 9,162 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python
# Copyright 2022 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# -----------------------------FILE OVERVIEW----------------------------- #
# This script generates a list of tests that exist for given parameters.
#
# This script was created so that we could determine which tests are used
# by specific components, so that coverage can be generated for specific
# components.
#
# Use the built in help for instructions.
# ----------------------------------------------------------------------- #

import os
import sys
import argparse
import fnmatch
import re
import time
import json
from threading import Thread, Lock

sys.path.append(os.path.abspath("./third_party/blink/tools"))
sys.path.append(os.path.abspath("./"))
# Chromium check
NOT_CHROME_ERROR = (
    'You must be in the chromium src directory to run this command.')
try:
  file = open('./DIR_METADATA', 'r')
  if not 'project: "chromium"' in file.read():
    print(NOT_CHROME_ERROR)
    exit()
except IOError:
  print(NOT_CHROME_ERROR)
  exit()

from third_party.blink.tools.blinkpy.w3c.directory_owners_extractor import DirectoryOwnersExtractor
from third_party.blink.tools.blinkpy.common.host import Host

# CONSTANTS
DIR_METADATA = 'DIR_METADATA'
COMMON_METADATA = 'COMMON_METADATA'
OWNERS = 'OWNERS'
TEST_NAME_REGEX = re.compile(
    r"TEST(_[FP])?\(\s*'?([a-zA-Z][a-zA-Z0-9]*)'?,"
    "\s*'?([a-zA-Z][a-zA-Z0-9_]*)'?", re.MULTILINE)
TEST_SUITE_REGEX = re.compile(
    r'TEST_SUITE(_[FP])?\(\s*([a-zA-Z][a-zA-Z0-9]*),\s*([a-zA-Z][a-zA-Z0-9]*),',
    re.MULTILINE)

parser = argparse.ArgumentParser(
    "Determines test suites for given directories and components")
parser.add_argument(
    '-c',
    '--components',
    nargs='+',
    # TODO: Adjust to buganizer once the
    # coverage team updates the tool
    help='A monorail component to collect suites for',
    default=[])
parser.add_argument(
    '-d',
    '--dirs',
    nargs='*',
    help='Directory to search through, at least one must be present',
    default=['./'])
parser.add_argument('-i',
                    '--ignore_dir',
                    nargs='*',
                    help='Directories to ignore',
                    default=[])
parser.add_argument('-o',
                    '--out_file',
                    help='The file to write the suites to.',
                    required=True)
parser.add_argument('-j',
                    '--jobs',
                    required=False,
                    help='The number of threads to allow',
                    type=int,
                    default=0)
args = parser.parse_args()


class ThreadManager(int):

  def __init__(self, max_threads):
    self.num_threads = 0
    self.max_threads = max_threads
    self.lock = Lock()

  def _thread_wrapper(self, func, args):
    func(*args)
    with self.lock:
      self.num_threads -= 1

  def run(self, func, args):
    if self.num_threads < self.max_threads:
      thread = Thread(target=self._thread_wrapper, args=(func, args))
      with self.lock:
        self.num_threads += 1
      thread.start()
      return True
    else:
      return False

  def wait_until_threads_done(self):
    while self.num_threads > 0:
      time.sleep(0.1)


class TestFinder(argparse.Namespace):

  def __init__(self, args):
    self.host = Host()
    self.component_map = dict()
    self.dirs_with_tests = set()
    self.disabled_tests = dict()
    self.maybe_tests = dict()
    self.total_disabled = 0
    self.total_maybe = 0
    self.total_working = 0
    self.dirs = args.dirs
    self.comps = args.components
    self.dir_thread_manager = ThreadManager(args.jobs * .75)
    self.read_thread_manager = ThreadManager(args.jobs / 4)
    self.out_file = args.out_file

  def is_hidden(self, path):
    name = os.path.basename(os.path.abspath(path))
    return name.startswith('.')

  def is_output_dir(self, path):
    name = os.path.basename(os.path.abspath(path))
    return name.startswith('out')

  def is_test_file(self, filepath):
    if filepath.endswith('.py'):
      return False
    if filepath.endswith('.png'):
      return False
    noext = os.path.splitext(os.path.abspath(filepath))[0]
    name = os.path.basename(noext)
    return name.endswith('test')

  def get_items_in_dir(self, path):
    items_list = os.listdir(path)
    files = list()
    dirs = list()
    for item in items_list:
      item_path = os.path.abspath(os.path.join(path, item))
      if os.path.isfile(item_path):
        files.append(item_path)
      else:
        dirs.append(item_path)
    return [files, dirs]

  def get_component_for_dir(self, path, type):
    # Python does something weird with hidden dirs.
    # We don't care about those anyway.
    if self.is_hidden(path):
      return None
    metadata_path = os.path.join(path, type)
    extractor = DirectoryOwnersExtractor(Host())
    try:
      return extractor.extract_component(metadata_path)
    except KeyError:
      return None

  def get_test_suites_for_file(self, filepath, suites):
    try:
      new_suites = set()
      text = open(filepath, 'r').read()
      relPath = os.path.relpath(filepath)
      # Find regular tests
      matches = re.findall(TEST_NAME_REGEX, text)
      for m in matches:
        new_suites.add(m[1])
        # Check for disabled and maybe tests
        if m[2] != None and m[2].startswith('DISABLED'):
          disabled = self.disabled_tests.get(relPath)
          self.total_disabled += 1
          if disabled == None:
            disabled = []
            self.disabled_tests[relPath] = disabled
          disabled.append(m[1] + "." + m[2])
        elif m[2] != None and m[2].startswith('MAYBE'):
          self.total_maybe += 1
          maybe = self.maybe_tests.get(relPath)
          if maybe == None:
            maybe = []
            self.maybe_tests[relPath] = maybe
          maybe.append(m[1] + "." + m[2])
        else:
          self.total_working += 1
      # Find suites
      matches = re.findall(TEST_SUITE_REGEX, text)
      for m in matches:
        new_suites.add(m[2])
      suites.extend(new_suites)
    except IOError:
      print('Failed to open ' + filepath)
      return list()

  def get_test_suites_for_dir(self, dir, suites):
    files, _ = self.get_items_in_dir(dir)
    files = list(filter(lambda file: self.is_test_file(file), files))
    if files.__len__() > 0:
      self.dirs_with_tests.add(os.path.relpath(dir))
    for file in files:
      if not self.read_thread_manager.run(self.get_test_suites_for_file,
                                          (file, suites)):
        self.get_test_suites_for_file(file, suites)

  def start(self):
    for dir in self.dirs:
      self.build_component_map(dir, None)
    self.dir_thread_manager.wait_until_threads_done()
    self.save()

  def save(self):
    with open(self.out_file, 'w') as outfile:
      data = {
          "components": self.component_map,
          "directories": [*self.dirs_with_tests],
          "disabled_tests": self.disabled_tests,
          "maybe_tests": self.maybe_tests,
          "total_disabled": self.total_disabled,
          "total_maybe": self.total_maybe,
          "total_working": self.total_working,
          "total_tests":
          self.total_working + self.total_maybe + self.total_disabled
      }
      json.dump(data, outfile, indent=2)

  def build_component_map(self, cur_dir, common_component):
    # Normalize cur_dir
    cur_dir = os.path.abspath(cur_dir)
    # Get the component
    component = self.get_component_for_dir(cur_dir, DIR_METADATA)
    using_common_metadata = False
    # If the component is in common metadata, it should pass through to sub dir.
    if component == None and common_component == None:
      component = self.get_component_for_dir(cur_dir, COMMON_METADATA)
      using_common_metadata = True
    if component != None:
      allowed = self.comps.__len__() == 0
      for comp_glob in self.comps:
        result = fnmatch.filter([component], comp_glob)
        allowed = result.__len__() > 0
        if allowed:
          break
      if allowed:
        tests_for_comp = self.component_map.get(component)
        # If the component isn't in the map yet, add it
        if tests_for_comp == None:
          tests_for_comp = list()
          self.component_map[component] = tests_for_comp
        self.get_test_suites_for_dir(cur_dir, tests_for_comp)
    # Even if the dir didn't have a component,
    #   we still need to check it's subdirs.
    _, sub_dirs = self.get_items_in_dir(cur_dir)
    for sub_dir in sub_dirs:
      # skip hidden directories
      if not self.is_hidden(sub_dir) and not self.is_output_dir(sub_dir):
        common_component_to_pass = (component if using_common_metadata else
                                    common_component)
        if not self.dir_thread_manager.run(self.build_component_map,
                                           (sub_dir, common_component_to_pass)):
          self.build_component_map(sub_dir, common_component_to_pass)


try:
  finder = TestFinder(args)
  finder.start()
except KeyboardInterrupt:
  print('\nUser Interrupted')
  exit()