File: rebase_dump_accessibility_tree_test.py

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (162 lines) | stat: -rwxr-xr-x 5,002 bytes parent folder | download
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
#!/usr/bin/env python3
# Copyright 2014 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Rebase DumpAccessibilityTree Tests.

This script is intended to be run when you make a change that could affect the
expected results of tests in:

    content/test/data/accessibility

It assumes that you've already uploaded a change and the try jobs have finished.
It collects all of the results from try jobs on all platforms and updates the
expectation files locally. From there you can run 'git diff' to make sure all
of the changes look reasonable, then upload the change for code review.

Optional argument: patchset number, otherwise will default to latest patchset
"""

from __future__ import print_function

import json
import os
import re
import sys
import tempfile
import time
import urllib
import urllib.parse

# The location of the DumpAccessibilityTree html test files and expectations.
TEST_DATA_PATH = os.path.join(os.getcwd(), 'content/test/data/accessibility')

# Colors for easier debugging.
# TODO check environment  to determine whether terminal is rich or interactive.
BRIGHT_COLOR = '\033[93m'
NORMAL_COLOR = '\033[0m'

# A global that keeps track of files we've already updated, so we don't
# bother to update the same file twice.
completed_files = set()

def Fix(line):
  if line[:3] == '@@@':
    result = re.search('[^@]@([^@]*)@@@', line)
    if result:
      line = result.group(1)
  # For Android tests:
  if line[:2] == 'I ':
    result = re.search('I.*run_tests_on_device\([^\)]+\)\s+(.*)',
                       line)
    if result:
      line = result.group(1)
  # For Android content_shell_test_apk tests:
  elif line[:2] == 'C ':
    result = re.search('C\s+\d+\.\d+s Main\s+([T|E|A|a|W|\+](.*))', line)

    if result:
      line = result.group(1)
  return line

def ParseLog(logdata):
  '''Parse the log file for failing tests and overwrite the expected
     result file locally with the actual results from the log.'''
  lines = logdata.splitlines()
  test_file = None
  expected_file = None
  start = None
  for i in range(len(lines)):
    line = Fix(lines[i])
    if line.find('Testing:') >= 0:
      result = re.search('content.test.*accessibility.([^@]*)', line)
      if result:
        test_file = result.group(1)
      expected_file = None
      start = None
    if line.find('Expected output:') >= 0:
      result = re.search('content.test.*accessibility.([^@]*)', line)
      if result:
        expected_file = result.group(1)
    if line == 'Actual':
      start = i + 2
    if start and test_file and expected_file and line.find('End-of-file') >= 0:
      dst_fullpath = os.path.join(TEST_DATA_PATH, expected_file)
      if dst_fullpath in completed_files:
        continue

      if line[:3] != '---':
        start = start + 1  # Skip separator line of hyphens
      actual = [Fix(line) for line in lines[start : i] if line]
      fp = open(dst_fullpath, 'w')
      fp.write('\n'.join(actual))
      fp.close()
      print("* %s" % os.path.relpath(dst_fullpath))
      completed_files.add(dst_fullpath)
      start = None
      test_file = None
      expected_file = None

def Run():
  '''Main. Get the issue number and parse the code review page.'''
  if len(sys.argv) == 2:
    patchSetArg = '--patchset=%s' % sys.argv[1]
  else:
    patchSetArg = '';

  try:
    (fd, tmppath) = tempfile.mkstemp()
    print('Temp file: %s' % tmppath)
    os.system('git cl try-results --json %s %s' % (tmppath, patchSetArg))

    with open(tmppath) as file:
      try_result = file.read()
      if len(try_result) < 1000:
        print('Did not seem to get try bot data.')
        print(try_result)
        return
  finally:
    os.close(fd)
    os.unlink(tmppath)

  data = json.loads(try_result)

  for builder in data:
    print(builder['builder']['builder'], builder['status'])
    if builder['status'] == 'FAILURE':
      bb_command = [
          'bb',
          'get',
          builder['id'],
          '-steps',
          '-json',
      ]
      bb_command_expanded = ' '.join(bb_command)
      # print((BRIGHT_COLOR + '=> %s' + NORMAL_COLOR) % bb_command_expanded)
      output = os.popen(bb_command_expanded).read()
      steps_json = json.loads(output)

      s_name = None
      for step in steps_json['steps']:
        name = step['name']
        if (name.startswith('content_shell_test_apk') or
            name.startswith('content_browsertests')) and '(with patch)' in name:
          s_name = name

      bb_command = [
          'bb',
          'log',
          builder['id'],
          '\"%s\"' % s_name,
      ]
      bb_command_expanded = ' '.join(bb_command)
      # print((BRIGHT_COLOR + '=> %s' + NORMAL_COLOR) % bb_command_expanded)
      output = os.popen(bb_command_expanded).readlines()
      ParseLog('\n'.join(output))
      if not output:
        print('No content_browsertests (with patch) step found')
        continue

if __name__ == '__main__':
  sys.exit(Run())