Bug 1838272 [wpt PR 40523] - Further DOM Parts hierarchy reorg, and connect Document...
[gecko.git] / config / check_source_count.py
bloba0a3f2c6d4f7c217dbd29ac3c418711637992710
1 #!/usr/bin/env python
2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 # Usage: check_source_count.py SEARCH_TERM COUNT ERROR_LOCATION REPLACEMENT [FILES...]
8 # Checks that FILES contains exactly COUNT matches of SEARCH_TERM. If it does
9 # not, an error message is printed, quoting ERROR_LOCATION, which should
10 # probably be the filename and line number of the erroneous call to
11 # check_source_count.py.
12 import re
13 import sys
15 search_string = sys.argv[1]
16 expected_count = int(sys.argv[2])
17 error_location = sys.argv[3]
18 replacement = sys.argv[4]
19 files = sys.argv[5:]
21 details = {}
23 count = 0
24 for f in files:
25 text = file(f).read()
26 match = re.findall(search_string, text)
27 if match:
28 num = len(match)
29 count += num
30 details[f] = num
32 if count == expected_count:
33 print(
34 "TEST-PASS | check_source_count.py {0} | {1}".format(
35 search_string, expected_count
39 else:
40 print(
41 "TEST-UNEXPECTED-FAIL | check_source_count.py {0} | ".format(search_string),
42 end="",
44 if count < expected_count:
45 print(
46 "There are fewer occurrences of /{0}/ than expected. "
47 "This may mean that you have removed some, but forgotten to "
48 "account for it {1}.".format(search_string, error_location)
50 else:
51 print(
52 "There are more occurrences of /{0}/ than expected. We're trying "
53 "to prevent an increase in the number of {1}'s, using {2} if "
54 "possible. If it is unavoidable, you should update the expected "
55 "count {3}.".format(
56 search_string, search_string, replacement, error_location
60 print("Expected: {0}; found: {1}".format(expected_count, count))
61 for k in sorted(details):
62 print("Found {0} occurences in {1}".format(details[k], k))
63 sys.exit(-1)