xref: /aosp_15_r20/external/pdfium/docs/code-coverage.md (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1*3ac0a46fSAndroid Build Coastguard Worker# Code Coverage Support for PDFium
2*3ac0a46fSAndroid Build Coastguard Worker
3*3ac0a46fSAndroid Build Coastguard Worker[TOC]
4*3ac0a46fSAndroid Build Coastguard Worker
5*3ac0a46fSAndroid Build Coastguard WorkerThis guide explains how to generate code coverage information for the PDFium
6*3ac0a46fSAndroid Build Coastguard Workerlibrary on a local computer.
7*3ac0a46fSAndroid Build Coastguard Worker
8*3ac0a46fSAndroid Build Coastguard Worker## Prerequisites
9*3ac0a46fSAndroid Build Coastguard Worker
10*3ac0a46fSAndroid Build Coastguard WorkerYou will need the PDFium source code on your computer. You can see
11*3ac0a46fSAndroid Build Coastguard Workerthe [README](/README.md) for instructions on checking out PDFium's source.
12*3ac0a46fSAndroid Build Coastguard Worker
13*3ac0a46fSAndroid Build Coastguard WorkerThe tools used for code coverage are known to work on Ubuntu and Debian. They
14*3ac0a46fSAndroid Build Coastguard Workershould work correctly on newer versions of Ubuntu, Debian and related Linux
15*3ac0a46fSAndroid Build Coastguard Workerdistros. They have not been tested on Windows and Mac.
16*3ac0a46fSAndroid Build Coastguard Worker
17*3ac0a46fSAndroid Build Coastguard WorkerPreviously, the code coverage scripts required specific versions of `lcov` and
18*3ac0a46fSAndroid Build Coastguard Worker`llvm-cov` to be present. This is no longer true, so if you have no other need
19*3ac0a46fSAndroid Build Coastguard Workerfor them they can be removed. All of the required tools will be pulled into the
20*3ac0a46fSAndroid Build Coastguard WorkerClang build tools directory by the script.
21*3ac0a46fSAndroid Build Coastguard Worker
22*3ac0a46fSAndroid Build Coastguard Worker## Generating Code Coverage
23*3ac0a46fSAndroid Build Coastguard Worker
24*3ac0a46fSAndroid Build Coastguard Worker### Setup
25*3ac0a46fSAndroid Build Coastguard Worker
26*3ac0a46fSAndroid Build Coastguard WorkerThis step assumes that you have already checked out the PDFium source code. If
27*3ac0a46fSAndroid Build Coastguard Workeryou have not, please consult the Prerequisites section above.
28*3ac0a46fSAndroid Build Coastguard Worker
29*3ac0a46fSAndroid Build Coastguard WorkerBefore generating code coverage information, you will need to have a build
30*3ac0a46fSAndroid Build Coastguard Workerdirectory with coverage enabled. This can be done by running the `gn args`
31*3ac0a46fSAndroid Build Coastguard Workercommand and adding `use_clang_coverage = true` in the editor that is opened.
32*3ac0a46fSAndroid Build Coastguard Worker
33*3ac0a46fSAndroid Build Coastguard WorkerIf not using the default directory, `out/Coverage`, then replace it with the
34*3ac0a46fSAndroid Build Coastguard Workercorrect location in the following command.
35*3ac0a46fSAndroid Build Coastguard Worker
36*3ac0a46fSAndroid Build Coastguard Worker```shell
37*3ac0a46fSAndroid Build Coastguard Workergn args out/Coverage
38*3ac0a46fSAndroid Build Coastguard Worker```
39*3ac0a46fSAndroid Build Coastguard Worker
40*3ac0a46fSAndroid Build Coastguard WorkerIf you already have a build directory, you can append the coverage flag to the
41*3ac0a46fSAndroid Build Coastguard Workerexisting `args.gn` as follows. If not using the default directory,
42*3ac0a46fSAndroid Build Coastguard Worker`out/Coverage`, then replace it with the correct location in the following
43*3ac0a46fSAndroid Build Coastguard Workercommand.
44*3ac0a46fSAndroid Build Coastguard Worker
45*3ac0a46fSAndroid Build Coastguard Worker```shell
46*3ac0a46fSAndroid Build Coastguard Workerecho "use_clang_coverage = true" >> out/Coverage/args.gn
47*3ac0a46fSAndroid Build Coastguard Worker```
48*3ac0a46fSAndroid Build Coastguard Worker
49*3ac0a46fSAndroid Build Coastguard WorkerPrevious versions of code coverage used **use_coverage = true** in args.gn; this
50*3ac0a46fSAndroid Build Coastguard Workerneeds to be changed to **use_clang_coverage = true**
51*3ac0a46fSAndroid Build Coastguard Worker
52*3ac0a46fSAndroid Build Coastguard Worker### Usage
53*3ac0a46fSAndroid Build Coastguard Worker
54*3ac0a46fSAndroid Build Coastguard WorkerGenerating code coverage information is done via the
55*3ac0a46fSAndroid Build Coastguard Worker`testing/tools/coverage/coverage_report.py` script. This script will download
56*3ac0a46fSAndroid Build Coastguard Workerthe Clang coverage tools if needed, build any binaries that it needs, perform
57*3ac0a46fSAndroid Build Coastguard Workertest runs, collect coverage data, and generate a HTML coverage report. It is
58*3ac0a46fSAndroid Build Coastguard Workerbased on the Chromium coverage scripts, so will generate the same style of
59*3ac0a46fSAndroid Build Coastguard Workerreport.
60*3ac0a46fSAndroid Build Coastguard Worker
61*3ac0a46fSAndroid Build Coastguard WorkerRunning the script with no arguments, as below, it will assume that you are
62*3ac0a46fSAndroid Build Coastguard Workercurrently at the root of your PDFium checkout, the build directory to use is
63*3ac0a46fSAndroid Build Coastguard Worker`./out/Coverage/` and that HTML should be outputted to `./coverage_report/`.
64*3ac0a46fSAndroid Build Coastguard Worker
65*3ac0a46fSAndroid Build Coastguard Worker```shell
66*3ac0a46fSAndroid Build Coastguard Workertesting/tools/coverage/coverage_report.py
67*3ac0a46fSAndroid Build Coastguard Worker```
68*3ac0a46fSAndroid Build Coastguard Worker
69*3ac0a46fSAndroid Build Coastguard WorkerIf the current working directory is not the root of your PDFium checkout, then
70*3ac0a46fSAndroid Build Coastguard Workeryou will need to pass in `--source-directory` with the appropriate directory. If
71*3ac0a46fSAndroid Build Coastguard Workeryou are using a different build directory, then `--build-directory` will need to
72*3ac0a46fSAndroid Build Coastguard Workerbe passed in. Finally, if you want the HTML report in a different location then
73*3ac0a46fSAndroid Build Coastguard Workeryou will need to pass in `--output-directory`.
74*3ac0a46fSAndroid Build Coastguard Worker
75*3ac0a46fSAndroid Build Coastguard WorkerAn example of all these flags being used:
76*3ac0a46fSAndroid Build Coastguard Worker
77*3ac0a46fSAndroid Build Coastguard Worker```shell
78*3ac0a46fSAndroid Build Coastguard Workertesting/tools/coverage/coverage_report.py \
79*3ac0a46fSAndroid Build Coastguard Worker    --source-directory ~/pdfium/pdfium \
80*3ac0a46fSAndroid Build Coastguard Worker    --build-directory ~/pdfium/pdfium/out/Debug_with_Coverage \
81*3ac0a46fSAndroid Build Coastguard Worker    --output-directory ~/Documents/PDFium_coverage
82*3ac0a46fSAndroid Build Coastguard Worker```
83*3ac0a46fSAndroid Build Coastguard Worker
84*3ac0a46fSAndroid Build Coastguard WorkerTo run different tests than the default set, including running just a single
85*3ac0a46fSAndroid Build Coastguard Workertest, you can specify the test names on the command line. The list of supported
86*3ac0a46fSAndroid Build Coastguard Workertests can be found by running the script with `--help`.
87*3ac0a46fSAndroid Build Coastguard Worker
88*3ac0a46fSAndroid Build Coastguard WorkerFor example, running all of the tests that don't use pdfium_test:
89*3ac0a46fSAndroid Build Coastguard Worker
90*3ac0a46fSAndroid Build Coastguard Worker```shell
91*3ac0a46fSAndroid Build Coastguard Workertesting/tools/coverage/coverage_report.py pdfium_unittests pdfium_embeddertests
92*3ac0a46fSAndroid Build Coastguard Worker```
93*3ac0a46fSAndroid Build Coastguard Worker
94*3ac0a46fSAndroid Build Coastguard WorkerNOTE: At the present time, there is no mechanism for combining data from
95*3ac0a46fSAndroid Build Coastguard Workerdifferent invocations of `coverage_report.py`. Instead, you must specify all of
96*3ac0a46fSAndroid Build Coastguard Workerthe tests to be included in the report in a single invocation. Alternatively,
97*3ac0a46fSAndroid Build Coastguard Workeryou can collect the profiling data that is generated from each run and manually
98*3ac0a46fSAndroid Build Coastguard Workerinvoke `tools/code_coverage/coverage.py` to generate a combined report.
99*3ac0a46fSAndroid Build Coastguard Worker
100*3ac0a46fSAndroid Build Coastguard WorkerThere are additional developer debugging flags available, `--dry-run` and
101*3ac0a46fSAndroid Build Coastguard Worker`--verbose`. `--dry-run` will output a trace of commands that would have been
102*3ac0a46fSAndroid Build Coastguard Workerrun, but doesn't actually execute them. `--verbose` turns on outputting
103*3ac0a46fSAndroid Build Coastguard Workeradditional logging information.
104*3ac0a46fSAndroid Build Coastguard Worker
105*3ac0a46fSAndroid Build Coastguard Worker### Viewing
106*3ac0a46fSAndroid Build Coastguard Worker
107*3ac0a46fSAndroid Build Coastguard WorkerOnce the script has run, the output directory should contain a set of HTML files
108*3ac0a46fSAndroid Build Coastguard Workercontaining the coverage report.
109*3ac0a46fSAndroid Build Coastguard Worker
110*3ac0a46fSAndroid Build Coastguard WorkerThese files are static HTML, so you can point your browser at them directly on
111*3ac0a46fSAndroid Build Coastguard Workeryour local file system and they should render fine. You can also serve them via a
112*3ac0a46fSAndroid Build Coastguard Workerweb server if you want, but how to achieve that is beyond the scope of this
113*3ac0a46fSAndroid Build Coastguard Workerdocumentation.
114*3ac0a46fSAndroid Build Coastguard Worker
115*3ac0a46fSAndroid Build Coastguard Worker## Issues
116*3ac0a46fSAndroid Build Coastguard Worker
117*3ac0a46fSAndroid Build Coastguard WorkerFor help with using the code coverage tools please contact the PDFium
118*3ac0a46fSAndroid Build Coastguard Workermaintainers via the PDFium
119*3ac0a46fSAndroid Build Coastguard Workermailing [list](https://groups.google.com/forum/#!forum/pdfium).
120*3ac0a46fSAndroid Build Coastguard Worker
121*3ac0a46fSAndroid Build Coastguard WorkerPlease file bugs against the code coverage
122*3ac0a46fSAndroid Build Coastguard Workersupport [here](https://bugs.chromium.org/p/pdfium/issues/list).
123