]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - misc/coding_tools/coverage2el.py
a3d8d545617b6f6283a4a82414d4ff7bab513a70
[tahoe-lafs/tahoe-lafs.git] / misc / coding_tools / coverage2el.py
1
2 import os.path
3 from coverage import coverage, summary, misc
4
5 class ElispReporter(summary.SummaryReporter):
6     def report(self):
7         try:
8             # coverage-3.4 has both omit= and include= . include= is applied
9             # first, then omit= removes items from what's left. These are
10             # tested with fnmatch, against fully-qualified filenames.
11             self.find_code_units(None,
12                                  omit=[os.path.abspath("src/allmydata/test/*")],
13                                  include=[os.path.abspath("src/allmydata/*")])
14         except TypeError:
15             # coverage-3.3 only had omit=
16             self.find_code_units(None, ["/System", "/Library", "/usr/lib",
17                                         "support/lib", "src/allmydata/test"])
18
19         out = open(".coverage.el", "w")
20         out.write("""
21 ;; This is an elisp-readable form of the coverage data. It defines a
22 ;; single top-level hash table in which the key is an asolute pathname, and
23 ;; the value is a three-element list. The first element of this list is a
24 ;; list of line numbers that represent actual code statements. The second is
25 ;; a list of line numbers for lines which got used during the unit test. The
26 ;; third is a list of line numbers for code lines that were not covered
27 ;; (since 'code' and 'covered' start as sets, this last list is equal to
28 ;; 'code - covered').
29
30     """)
31         out.write("(let ((results (make-hash-table :test 'equal)))\n")
32         for cu in self.code_units:
33             f = cu.filename
34             try:
35                 (fn, executable, missing, mf) = self.coverage.analysis(cu)
36             except misc.NoSource:
37                 continue
38             code_linenumbers = executable
39             uncovered_code = missing
40             covered_linenumbers = sorted(set(executable) - set(missing))
41             out.write(" (puthash \"%s\" '((%s) (%s) (%s)) results)\n"
42                       % (f,
43                          " ".join([str(ln) for ln in sorted(code_linenumbers)]),
44                          " ".join([str(ln) for ln in sorted(covered_linenumbers)]),
45                          " ".join([str(ln) for ln in sorted(uncovered_code)]),
46                          ))
47         out.write(" results)\n")
48         out.close()
49
50 def main():
51     c = coverage()
52     c.load()
53     ElispReporter(c).report()
54
55 if __name__ == '__main__':
56     main()
57
58