| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2016 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Helper script to update the test error expectations based on actual results. | 6 """Helper script to update the test error expectations based on actual results. |
| 7 | 7 |
| 8 This is useful for regenerating test expectations after making changes to the | 8 This is useful for regenerating test expectations after making changes to the |
| 9 error format. | 9 error format. |
| 10 | 10 |
| 11 To use this run the affected tests, and then pass the input to this script | 11 To use this run the affected tests, and then pass the input to this script |
| 12 (either via stdin, or as the first argument). For instance: | 12 (either via stdin, or as the first argument). For instance: |
| 13 | 13 |
| 14 $ ./out/Release/net_unittests --gtest_filter="*VerifyCertificateChain*" | \ | 14 $ ./out/Release/net_unittests --gtest_filter="*VerifyCertificateChain*" | \ |
| 15 net/data/verify_certificate_chain_unittest/rebase-errors.py | 15 net/data/verify_certificate_chain_unittest/rebase-errors.py |
| 16 | 16 |
| 17 The script works by scanning the stdout looking for gtest failures when | 17 The script works by scanning the stdout looking for gtest failures having a |
| 18 comparing "errors.ToDebugString(chain)". The C++ test side should have been | 18 particular format. The C++ test side should have been instrumented to dump out |
| 19 instrumented to dump out the test file's path on mismatch. | 19 the test file's path on mismatch. |
| 20 | 20 |
| 21 This script will then update the corresponding .test file that contains the | 21 This script will then update the corresponding test/error file that contains the |
| 22 error expectation. | 22 error expectation. |
| 23 """ | 23 """ |
| 24 | 24 |
| 25 import os | 25 import os |
| 26 import sys | 26 import sys |
| 27 import re | 27 import re |
| 28 | 28 |
| 29 # Regular expression to find the failed errors in test stdout. | 29 # Regular expression to find the failed errors in test stdout. |
| 30 # * Group 1 of the match is the actual error text (backslash-escaped) | 30 # * Group 1 of the match is file path (relative to //src) where the |
| 31 # * Group 2 of the match is file path (relative to //src) where the expected | 31 # expected errors were read from. |
| 32 # errors were read from. | 32 # * Group 2 of the match is the actual error text |
| 33 failed_test_regex = re.compile(r""" | 33 failed_test_regex = re.compile(r""" |
| 34 Value of: errors.ToDebugString\((?:test.chain)?\) | 34 Cert path errors don't match expectations \((.+?)\) |
| 35 Actual: "(.*)" | 35 |
| 36 (?:.|\n)+? | 36 EXPECTED: |
| 37 Test file: (.*[.]test) | 37 |
| 38 (?:.|\n)*? |
| 39 ACTUAL: |
| 40 |
| 41 ((?:.|\n)*?) |
| 42 ===> Use net/data/verify_certificate_chain_unittest/rebase-errors.py to rebaseli
ne. |
| 38 """, re.MULTILINE) | 43 """, re.MULTILINE) |
| 39 | 44 |
| 40 | 45 |
| 41 def read_file_to_string(path): | 46 def read_file_to_string(path): |
| 42 """Reads a file entirely to a string""" | 47 """Reads a file entirely to a string""" |
| 43 with open(path, 'r') as f: | 48 with open(path, 'r') as f: |
| 44 return f.read() | 49 return f.read() |
| 45 | 50 |
| 46 | 51 |
| 47 def write_string_to_file(data, path): | 52 def write_string_to_file(data, path): |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 102 |
| 98 # Read the input either from a file, or from stdin. | 103 # Read the input either from a file, or from stdin. |
| 99 test_stdout = None | 104 test_stdout = None |
| 100 if len(sys.argv) == 2: | 105 if len(sys.argv) == 2: |
| 101 test_stdout = read_file_to_string(sys.argv[1]) | 106 test_stdout = read_file_to_string(sys.argv[1]) |
| 102 else: | 107 else: |
| 103 print 'Reading input from stdin...' | 108 print 'Reading input from stdin...' |
| 104 test_stdout = sys.stdin.read() | 109 test_stdout = sys.stdin.read() |
| 105 | 110 |
| 106 for m in failed_test_regex.finditer(test_stdout): | 111 for m in failed_test_regex.finditer(test_stdout): |
| 107 actual_errors = m.group(1) | 112 src_relative_errors_path = m.group(1) |
| 108 actual_errors = actual_errors.decode('string-escape') | 113 errors_path = get_abs_path(src_relative_errors_path) |
| 109 relative_test_path = m.group(2) | 114 actual_errors = m.group(2) |
| 110 fixup_errors_for_file(actual_errors, get_abs_path(relative_test_path)) | 115 |
| 116 if errors_path.endswith(".test"): |
| 117 fixup_errors_for_file(actual_errors, errors_path) |
| 118 elif errors_path.endswith(".txt"): |
| 119 write_string_to_file(actual_errors, errors_path) |
| 120 else: |
| 121 print 'Unknown file extension' |
| 122 sys.exit(1) |
| 123 |
| 111 | 124 |
| 112 | 125 |
| 113 if __name__ == "__main__": | 126 if __name__ == "__main__": |
| 114 main() | 127 main() |
| OLD | NEW |