Bug 1507805 [wpt PR 14092] - [css-grid] Some fixes on grid-container-scrollbar-*...
[gecko.git] / config / check_source_count.py
blob1c86b2d16b0786adea1e201c0734bf0a4a5da1ed
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 from __future__ import print_function
13 import sys
14 import re
16 search_string = sys.argv[1]
17 expected_count = int(sys.argv[2])
18 error_location = sys.argv[3]
19 replacement = sys.argv[4]
20 files = sys.argv[5:]
22 details = {}
24 count = 0
25 for f in files:
26 text = file(f).read()
27 match = re.findall(search_string, text)
28 if match:
29 num = len(match)
30 count += num
31 details[f] = num
33 if count == expected_count:
34 print("TEST-PASS | check_source_count.py {0} | {1}"
35 .format(search_string, expected_count))
37 else:
38 print("TEST-UNEXPECTED-FAIL | check_source_count.py {0} | "
39 .format(search_string),
40 end='')
41 if count < expected_count:
42 print("There are fewer occurrences of /{0}/ than expected. "
43 "This may mean that you have removed some, but forgotten to "
44 "account for it {1}.".format(search_string, error_location))
45 else:
46 print("There are more occurrences of /{0}/ than expected. We're trying "
47 "to prevent an increase in the number of {1}'s, using {2} if "
48 "possible. If it is unavoidable, you should update the expected "
49 "count {3}.".format(search_string, search_string, replacement,
50 error_location))
52 print("Expected: {0}; found: {1}".format(expected_count, count))
53 for k in sorted(details):
54 print("Found {0} occurences in {1}".format(details[k], k))
55 sys.exit(-1)