Imported Upstream version 4.5.14
[platform/upstream/findutils.git] / find / testsuite / checklists.py
1 """Check that the list of test files in Makefile.am is complete and not redundant.
2
3 Usage:
4   checklists file-listing-configured-files test-root subdir-1-containing-tests [subdir-2-containing-tests ...]
5 """
6
7 import os
8 import os.path
9 import re
10 import sys
11
12 def report_unlisted(filename):
13     sys.stderr.write(
14         'Error: test file %s is not listed in Makefile.am but exists on disk.\n'
15         % (filename,))
16
17
18 def report_missing(filename):
19     sys.stderr.write(
20         'Error: test file %s is listed in Makefile.am but does not exist on disk.\n'
21         % (filename,))
22
23 def report_dupe(filename):
24     sys.stderr.write(
25         'Error: test file %s is listed more than once in Makefile.am.\n'
26         % (filename,))
27
28
29 def report_problems(problem_filenames, reporting_function):
30     for f in problem_filenames:
31         reporting_function(f)
32     return len(problem_filenames)
33
34
35 def file_names(listfile_name):
36     for line in open(listfile_name, 'r').readlines():
37         yield line.rstrip('\n')
38
39
40 def configured_file_names(listfile_name):
41     dupes = set()
42     result = set()
43     for filename in file_names(listfile_name):
44         if filename in result:
45             dupes.add(filename)
46         else:
47             result.add(filename)
48     return dupes, result
49
50
51 def find_test_files(roots):
52     testfile_rx = re.compile(r'\.(exp|xo)$')
53     for root in roots:
54         for parent, dirs, files in os.walk(root):
55             for file_basename in files:
56                 if testfile_rx.search(file_basename):
57                     yield os.path.join(parent, file_basename)
58
59
60 class TemporaryWorkingDirectory(object):
61
62     def __init__(self, cwd):
63         self.new_cwd = cwd
64         self.old_cwd = os.getcwd()
65
66     def __enter__(self):
67         os.chdir(self.new_cwd)
68
69     def __exit__(self, *unused_args):
70         os.chdir(self.old_cwd)
71
72
73 def main(args):
74     if len(args) < 3:
75         sys.stderr.write(__doc__)
76         return 1
77     dupes, configured = configured_file_names(args[1])
78     with TemporaryWorkingDirectory(args[2]):
79         actual = set(find_test_files(args[3:]))
80     print '%d test files configured for find, %s files on-disk' % (len(configured), len(actual))
81     problem_count = 0
82     problem_count += report_problems(dupes, report_dupe)
83     problem_count += report_problems(configured - actual, report_missing)
84     problem_count += report_problems(actual - configured, report_unlisted)
85     if problem_count:
86         return 1
87     else:
88         return 0
89
90 if __name__ == '__main__':
91     sys.exit(main(sys.argv))