Commit 506e8823 authored by Yuke Liao's avatar Yuke Liao Committed by Commit Bot

[Coverage] Create code coverage script.

This CL adds the first version of the code coverage script, which is
able to generate line by line code coverage report in html for files
involved when running test targets.

Bug: 784464
Change-Id: I0b8f894ca424e02c3e8a3176d2988df0a2e9d239
Reviewed-on: https://chromium-review.googlesource.com/764949Reviewed-by: default avatarDirk Pranke <dpranke@chromium.org>
Reviewed-by: default avatarMax Moroz <mmoroz@chromium.org>
Commit-Queue: Yuke Liao <liaoyuke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521367}
parent 113e9e02
liaoyuke@chromium.org
mmoroz@chromium.org
inferno@chromum.org
This diff is collapsed.
/*
* Copyright (c) 2012 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/*
* croc.css - styles for croc HTML output
*/
body {
font-family:arial;
}
table {
border-collapse:collapse;
border-width:0px;
border-style:solid;
}
thead {
background-color:#C0C0E0;
text-align:center;
}
td {
padding-right:10px;
padding-left:10px;
font-size:small;
border-width:1px;
border-style:solid;
border-color:black;
}
td.secdesc {
text-align:center;
font-size:medium;
font-weight:bold;
border-width:0px;
border-style:none;
padding-top:10px;
padding-bottom:5px;
}
td.section {
background-color:#D0D0F0;
text-align:center;
}
td.stat {
text-align:center;
}
td.number {
text-align:right;
}
td.graph {
/* Hide the dummy character */
color:#FFFFFF;
padding-left:6px;
}
td.high_pct {
text-align:right;
background-color:#B0FFB0;
}
td.mid_pct {
text-align:right;
background-color:#FFFF90;
}
td.low_pct {
text-align:right;
background-color:#FFB0B0;
}
span.missing {
background-color:#FFB0B0;
}
span.instr {
background-color:#FFFF90;
}
span.covered {
background-color:#B0FFB0;
}
span.g_missing {
background-color:#FF4040;
}
span.g_instr {
background-color:#FFFF00;
}
span.g_covered {
background-color:#40FF40;
}
p.time {
padding-top:10px;
font-size:small;
font-style:italic;
}
This diff is collapsed.
This diff is collapsed.
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Crocodile source scanners."""
import re
class Scanner(object):
"""Generic source scanner."""
def __init__(self):
"""Constructor."""
self.re_token = re.compile('#')
self.comment_to_eol = ['#']
self.comment_start = None
self.comment_end = None
def ScanLines(self, lines):
"""Scans the lines for executable statements.
Args:
lines: Iterator returning source lines.
Returns:
An array of line numbers which are executable.
"""
exe_lines = []
lineno = 0
in_string = None
in_comment = None
comment_index = None
for line in lines:
lineno += 1
in_string_at_start = in_string
for t in self.re_token.finditer(line):
tokenstr = t.groups()[0]
if in_comment:
# Inside a multi-line comment, so look for end token
if tokenstr == in_comment:
in_comment = None
# Replace comment with spaces
line = (line[:comment_index]
+ ' ' * (t.end(0) - comment_index)
+ line[t.end(0):])
elif in_string:
# Inside a string, so look for end token
if tokenstr == in_string:
in_string = None
elif tokenstr in self.comment_to_eol:
# Single-line comment, so truncate line at start of token
line = line[:t.start(0)]
break
elif tokenstr == self.comment_start:
# Multi-line comment start - end token is comment_end
in_comment = self.comment_end
comment_index = t.start(0)
else:
# Starting a string - end token is same as start
in_string = tokenstr
# If still in comment at end of line, remove comment
if in_comment:
line = line[:comment_index]
# Next line, delete from the beginnine
comment_index = 0
# If line-sans-comments is not empty, claim it may be executable
if line.strip() or in_string_at_start:
exe_lines.append(lineno)
# Return executable lines
return exe_lines
def Scan(self, filename):
"""Reads the file and scans its lines.
Args:
filename: Path to file to scan.
Returns:
An array of line numbers which are executable.
"""
# TODO: All manner of error checking
f = None
try:
f = open(filename, 'rt')
return self.ScanLines(f)
finally:
if f:
f.close()
class PythonScanner(Scanner):
"""Python source scanner."""
def __init__(self):
"""Constructor."""
Scanner.__init__(self)
# TODO: This breaks for strings ending in more than 2 backslashes. Need
# a pattern which counts only an odd number of backslashes, so the last
# one thus escapes the quote.
self.re_token = re.compile(r'(#|\'\'\'|"""|(?<!(?<!\\)\\)["\'])')
self.comment_to_eol = ['#']
self.comment_start = None
self.comment_end = None
class CppScanner(Scanner):
"""C / C++ / ObjC / ObjC++ source scanner."""
def __init__(self):
"""Constructor."""
Scanner.__init__(self)
# TODO: This breaks for strings ending in more than 2 backslashes. Need
# a pattern which counts only an odd number of backslashes, so the last
# one thus escapes the quote.
self.re_token = re.compile(r'(^\s*#|//|/\*|\*/|(?<!(?<!\\)\\)["\'])')
# TODO: Treat '\' at EOL as a token, and handle it as continuing the
# previous line. That is, if in a comment-to-eol, this line is a comment
# too.
# Note that we treat # at beginning of line as a comment, so that we ignore
# preprocessor definitions
self.comment_to_eol = ['//', '#']
self.comment_start = '/*'
self.comment_end = '*/'
def ScanFile(filename, language):
"""Scans a file for executable lines.
Args:
filename: Path to file to scan.
language: Language for file ('C', 'C++', 'python', 'ObjC', 'ObjC++')
Returns:
A list of executable lines, or an empty list if the file was not a handled
language.
"""
if language == 'python':
return PythonScanner().Scan(filename)
elif language in ['C', 'C++', 'ObjC', 'ObjC++']:
return CppScanner().Scan(filename)
# Something we don't handle
return []
#!/usr/bin/env python
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Unit tests for croc_scan.py."""
import re
import unittest
import croc_scan
class TestScanner(unittest.TestCase):
"""Tests for croc_scan.Scanner."""
def testInit(self):
"""Test __init()__."""
s = croc_scan.Scanner()
self.assertEqual(s.re_token.pattern, '#')
self.assertEqual(s.comment_to_eol, ['#'])
self.assertEqual(s.comment_start, None)
self.assertEqual(s.comment_end, None)
def testScanLines(self):
"""Test ScanLines()."""
s = croc_scan.Scanner()
# Set up imaginary language:
# ':' = comment to EOL
# '"' = string start/end
# '(' = comment start
# ')' = comment end
s.re_token = re.compile(r'([\:\"\(\)])')
s.comment_to_eol = [':']
s.comment_start = '('
s.comment_end = ')'
# No input file = no output lines
self.assertEqual(s.ScanLines([]), [])
# Empty lines and lines with only whitespace are ignored
self.assertEqual(s.ScanLines([
'', # 1
'line', # 2 exe
' \t ', # 3
]), [2])
# Comments to EOL are stripped, but not inside strings
self.assertEqual(s.ScanLines([
'test', # 1 exe
' : A comment', # 2
'"a : in a string"', # 3 exe
'test2 : with comment to EOL', # 4 exe
'foo = "a multiline string with an empty line', # 5 exe
'', # 6 exe
': and a comment-to-EOL character"', # 7 exe
': done', # 8
]), [1, 3, 4, 5, 6, 7])
# Test Comment start/stop detection
self.assertEqual(s.ScanLines([
'( a comment on one line)', # 1
'text (with a comment)', # 2 exe
'( a comment with a : in the middle)', # 3
'( a multi-line', # 4
' comment)', # 5
'a string "with a ( in it"', # 6 exe
'not in a multi-line comment', # 7 exe
'(a comment with a " in it)', # 8
': not in a string, so this gets stripped', # 9
'more text "with an uninteresting string"', # 10 exe
]), [2, 6, 7, 10])
# TODO: Test Scan(). Low priority, since it just wraps ScanLines().
class TestPythonScanner(unittest.TestCase):
"""Tests for croc_scan.PythonScanner."""
def testScanLines(self):
"""Test ScanLines()."""
s = croc_scan.PythonScanner()
# No input file = no output lines
self.assertEqual(s.ScanLines([]), [])
self.assertEqual(s.ScanLines([
'# a comment', # 1
'', # 2
'"""multi-line string', # 3 exe
'# not a comment', # 4 exe
'end of multi-line string"""', # 5 exe
' ', # 6
'"single string with #comment"', # 7 exe
'', # 8
'\'\'\'multi-line string, single-quote', # 9 exe
'# not a comment', # 10 exe
'end of multi-line string\'\'\'', # 11 exe
'', # 12
'"string with embedded \\" is handled"', # 13 exe
'# quoted "', # 14
'"\\""', # 15 exe
'# quoted backslash', # 16
'"\\\\"', # 17 exe
'main()', # 18 exe
'# end', # 19
]), [3, 4, 5, 7, 9, 10, 11, 13, 15, 17, 18])
class TestCppScanner(unittest.TestCase):
"""Tests for croc_scan.CppScanner."""
def testScanLines(self):
"""Test ScanLines()."""
s = croc_scan.CppScanner()
# No input file = no output lines
self.assertEqual(s.ScanLines([]), [])
self.assertEqual(s.ScanLines([
'// a comment', # 1
'# a preprocessor define', # 2
'', # 3
'\'#\', \'"\'', # 4 exe
'', # 5
'/* a multi-line comment', # 6
'with a " in it', # 7
'*/', # 8
'', # 9
'"a string with /* and \' in it"', # 10 exe
'', # 11
'"a multi-line string\\', # 12 exe
'// not a comment\\', # 13 exe
'ending here"', # 14 exe
'', # 15
'"string with embedded \\" is handled"', # 16 exe
'', # 17
'main()', # 18 exe
'// end', # 19
]), [4, 10, 12, 13, 14, 16, 18])
class TestScanFile(unittest.TestCase):
"""Tests for croc_scan.ScanFile()."""
class MockScanner(object):
"""Mock scanner."""
def __init__(self, language):
"""Constructor."""
self.language = language
def Scan(self, filename):
"""Mock Scan() method."""
return 'scan %s %s' % (self.language, filename)
def MockPythonScanner(self):
return self.MockScanner('py')
def MockCppScanner(self):
return self.MockScanner('cpp')
def setUp(self):
"""Per-test setup."""
# Hook scanners
self.old_python_scanner = croc_scan.PythonScanner
self.old_cpp_scanner = croc_scan.CppScanner
croc_scan.PythonScanner = self.MockPythonScanner
croc_scan.CppScanner = self.MockCppScanner
def tearDown(self):
"""Per-test cleanup."""
croc_scan.PythonScanner = self.old_python_scanner
croc_scan.CppScanner = self.old_cpp_scanner
def testScanFile(self):
"""Test ScanFile()."""
self.assertEqual(croc_scan.ScanFile('foo', 'python'), 'scan py foo')
self.assertEqual(croc_scan.ScanFile('bar1', 'C'), 'scan cpp bar1')
self.assertEqual(croc_scan.ScanFile('bar2', 'C++'), 'scan cpp bar2')
self.assertEqual(croc_scan.ScanFile('bar3', 'ObjC'), 'scan cpp bar3')
self.assertEqual(croc_scan.ScanFile('bar4', 'ObjC++'), 'scan cpp bar4')
self.assertEqual(croc_scan.ScanFile('bar', 'fortran'), [])
if __name__ == '__main__':
unittest.main()
This diff is collapsed.
# -*- python -*-
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Example configuration file for Croc
# Basic formatting rules:
# * It looks like JSON.
# * It's really python.
# * Dictionaries are wrapped in {}. Order does not matter. Entries are of
# the form:
# 'key':value,
# Note the trailing comma, which will help save you from python's built-in
# string concatenation.
# * Lists are wrapped in []. Order does matter. Entries should be followed
# with a trailing comma, which will help save you from python's built-in
# string concatenation.
# * Comments start with # and extend to end of line.
# * Strings are wrapped in ''. Backslashes must be escaped ('foo\\bar', not
# 'foo\bar') - this is particularly important in rule regular expressions.
# What follows is the main configuration dictionary.
{
# List of root directories, applied in order.
#
# Typically, coverage data files contain absolute paths to the sources.
# What you care about is usually a relative path from the top of your source
# tree (referred to here as a 'source root') to the sources.
#
# Roots may also be specified on the command line via the --root option.
# Roots specified by --root are applied before those specified in config
# files.
'roots' : [
# Each entry is a dict.
# * It must contain a 'root' entry, which is the start of a path.
# * Root entries may be absolute paths
# * Root entries starting with './' or '../' are relative paths, and
# are taken relative to the current directory where you run croc.
# * Root entries may start with previously defined altnames.
# * Use '/' as a path separator, even on Windows.
# * It may contain a 'altname' entry. If the root matches the start of
# a filename, that start is replaced with the 'altname', or with '_'
# if no default is specified.
# * Multiple root entries may share the same altname. This is commonly
# used when combining LCOV files from different platforms into one
# coverage report, when each platform checks out source code into a
# different source tree.
{'root' : 'c:/P4/EarthHammer'},
{'root' : 'd:/pulse/recipes/330137642/base'},
{'root' : '/Volumes/BuildData/PulseData/data/recipes/330137640/base'},
{'root' : '/usr/local/google/builder/.pulse-agent/data/recipes/330137641/base'},
# Sub-paths we specifically care about and want to call out. Note that
# these are relative to the default '_' altname.
{
'root' : '_/googleclient/third_party/software_construction_toolkit/files',
'altname' : 'SCT',
},
{
'root' : '_/googleclient/tools/hammer',
'altname' : 'HAMMER',
},
],
# List of rules, applied in order.
'rules' : [
# Each rule is a dict.
# * It must contaihn a 'regexp' entry. Filenames which match this
# regular expression (after applying mappings from 'roots') are
# affected by the rule.
#
# * Other entries in the dict are attributes to apply to matching files.
#
# Allowed attributes:
#
# 'include' : If 1, the file will be included in coverage reports. If 0,
# it won't be included in coverage reports.
#
# 'group' : Name of the group the file belongs to. The most common
# group names are 'source' and 'test'. Files must belong to
# a group to be included in coverage reports.
#
# 'language' : Programming language for the file. The most common
# languages are 'C', 'C++', 'python', 'ObjC', 'ObjC++'.
# Files must have a language to be included in coverage
# reports.
#
# 'add_if_missing' : If 1, and the file was not referenced by any LCOV
# files, it will be be scanned for executable lines
# and added to the coverage report. If 0, if the
# file is not referenced by any LCOV files, it will
# simply be ignored and not present in coverage
# reports.
# Files/paths to include
{
'regexp' : '^(SCT|HAMMER)/',
'include' : 1,
'add_if_missing': 1,
},
{
'regexp' : '.*/(\\.svn|\\.hg)/',
'include' : 0,
},
# Groups
{
'regexp' : '',
'group' : 'source',
},
{
'regexp' : '.*_(test|test_mac|unittest)\\.',
'group' : 'test',
},
# Languages
{
'regexp' : '.*\\.py$',
'language' : 'python',
},
],
# List of paths to add source from.
#
# Each entry is a path. It may be a local path, or one relative to a root
# altname (see 'roots' above).
#
# If more than one root's altname matches the start of this path, all matches
# will be attempted; matches where the candidate directory doesn't exist will
# be ignored. For example, if you're combining data from multiple platforms'
# LCOV files, you probably defined at least one root per LCOV, but only have
# one copy of the source on your local platform. That's fine; Croc will use
# the source it can find and not worry about the source it can't.
#
# Source files must be added via 'add_files' to generate line-by-line HTML
# output (via the --html option) and/or to scan for missing executable lines
# (if 'add_if_missing' is 1).
'add_files' : [
'SCT',
'HAMMER',
],
# Statistics to print.
#
'print_stats' : [
# Each entry is a dict.
#
# It must have a 'stat' entry, which is the statistic to print. This may
# be one of the following stats:
#
# * files_executable
# * files_instrumented
# * files_covered
# * lines_executable
# * lines_instrumented
# * lines_covered
#
# or an expression using those stats.
#
# It may have a 'format' entry, which is a python formatting string (very
# printf-like) for the statistic.
#
# It may have a 'group' entry. If this is specified, only files from the
# matching group will be included in the statistic. If not specified, the
# group defaults to 'all', which means all groups.
{
'stat' : 'files_executable',
'format' : '*RESULT FilesKnown: files_executable= %d files',
},
{
'stat' : 'files_instrumented',
'format' : '*RESULT FilesInstrumented: files_instrumented= %d files',
},
{
'stat' : '100.0 * files_instrumented / files_executable',
'format' : '*RESULT FilesInstrumentedPercent: files_instrumented_percent= %g',
},
{
'stat' : 'lines_instrumented',
'format' : '*RESULT LinesInstrumented: lines_instrumented= %d lines',
},
{
'stat' : 'lines_covered',
'format' : '*RESULT LinesCoveredSource: lines_covered_source= %d lines',
'group' : 'source',
},
{
'stat' : 'lines_covered',
'format' : '*RESULT LinesCoveredTest: lines_covered_test= %d lines',
'group' : 'test',
},
],
}
Name: SortTable
Short Name: sorttable.js
URL: http://www.kryogenix.org/code/browser/sorttable/
Version: 2
Date: 7th April 2007
License: Licenced as X11: http://www.kryogenix.org/code/browser/licence.html
Description:
Add <script src="sorttable.js"></script> to your HTML
Add class="sortable" to any table you'd like to make sortable
Click on the headers to sort
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment