grcov 0.8.19

Rust tool to collect and aggregate code coverage data for multiple source files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#[cfg(all(unix, feature = "tc"))]
#[global_allocator]
static GLOBAL: tcmalloc::TCMalloc = tcmalloc::TCMalloc;

use clap::{builder::PossibleValue, ArgGroup, Parser, ValueEnum};
use crossbeam_channel::bounded;
use log::error;
use regex::Regex;
use rustc_hash::FxHashMap;
use serde_json::Value;
use simplelog::{ColorChoice, Config, LevelFilter, TermLogger, TerminalMode, WriteLogger};
use std::fs::{self, File};
use std::ops::Deref;
use std::panic;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::{process, thread};

use grcov::*;

#[derive(Clone)]
enum OutputType {
    Ade,
    Lcov,
    Coveralls,
    CoverallsPlus,
    Files,
    Covdir,
    Html,
    Cobertura,
    Markdown,
}

impl FromStr for OutputType {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "ade" => Self::Ade,
            "lcov" => Self::Lcov,
            "coveralls" => Self::Coveralls,
            "coveralls+" => Self::CoverallsPlus,
            "files" => Self::Files,
            "covdir" => Self::Covdir,
            "html" => Self::Html,
            "cobertura" => Self::Cobertura,
            "markdown" => Self::Markdown,
            _ => return Err(format!("{} is not a supported output type", s)),
        })
    }
}

impl OutputType {
    fn to_file_name(&self, output_path: Option<&Path>) -> Option<PathBuf> {
        output_path.map(|path| {
            if path.is_dir() {
                match self {
                    OutputType::Ade => path.join("activedata"),
                    OutputType::Lcov => path.join("lcov"),
                    OutputType::Coveralls => path.join("coveralls"),
                    OutputType::CoverallsPlus => path.join("coveralls+"),
                    OutputType::Files => path.join("files"),
                    OutputType::Covdir => path.join("covdir"),
                    OutputType::Html => path.join("html"),
                    OutputType::Cobertura => path.join("cobertura.xml"),
                    OutputType::Markdown => path.join("markdown.md"),
                }
            } else {
                path.to_path_buf()
            }
        })
    }
}

#[derive(clap::ValueEnum, Clone)]
enum Filter {
    Covered,
    Uncovered,
}

impl FromStr for Filter {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "covered" => Self::Covered,
            "uncovered" => Self::Uncovered,
            _ => return Err(format!("{} is not a supported filter", s)),
        })
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
struct LevelFilterArg(LevelFilter);

impl ValueEnum for LevelFilterArg {
    fn value_variants<'a>() -> &'a [Self] {
        &[
            Self(LevelFilter::Off),
            Self(LevelFilter::Error),
            Self(LevelFilter::Warn),
            Self(LevelFilter::Info),
            Self(LevelFilter::Debug),
            Self(LevelFilter::Trace),
        ]
    }

    fn to_possible_value(&self) -> Option<PossibleValue> {
        match self.0 {
            LevelFilter::Off => Some(PossibleValue::new("OFF")),
            LevelFilter::Error => Some(PossibleValue::new("ERROR")),
            LevelFilter::Warn => Some(PossibleValue::new("WARN")),
            LevelFilter::Info => Some(PossibleValue::new("INFO")),
            LevelFilter::Debug => Some(PossibleValue::new("DEBUG")),
            LevelFilter::Trace => Some(PossibleValue::new("TRACE")),
        }
    }
}

#[derive(Parser)]
#[command(
    author,
    version,
    max_term_width = 100,
    about = "Parse, collect and aggregate code coverage data for multiple source files",
    help_template = "\
{before-help}{name}
{author-with-newline}{about-with-newline}
{usage-heading} {usage}

{all-args}{after-help}
",
    // This group requires that at least one of --token and --service-job-id
    // be present. --service-job-id requires --service-name, so this
    // effectively means we accept the following combinations:
    // - --token
    // - --token --service-job-id --service-name
    // - --service-job-id --service-name
    group = ArgGroup::new("coveralls-auth")
        .args(&["token", "service_job_id"])
        .multiple(true),
)]
struct Opt {
    /// Sets the input paths to use.
    #[arg(required = true)]
    paths: Vec<String>,
    /// Sets the path to the compiled binary to be used.
    #[arg(short, long, value_name = "PATH")]
    binary_path: Option<PathBuf>,
    /// Sets the path to the LLVM bin directory.
    #[arg(long, value_name = "PATH")]
    llvm_path: Option<PathBuf>,
    /// Sets a custom output type.
    #[arg(
        short = 't',
        long,
        long_help = "\
            Comma separated list of custom output types:\n\
            - *html* for a HTML coverage report;\n\
            - *coveralls* for the Coveralls specific format;\n\
            - *lcov* for the lcov INFO format;\n\
            - *covdir* for the covdir recursive JSON format;\n\
            - *coveralls+* for the Coveralls specific format with function information;\n\
            - *ade* for the ActiveData-ETL specific format;\n\
            - *files* to only return a list of files.\n\
            - *markdown* for human easy read.\n\
            - *cobertura* for output in cobertura format.\n\
        ",
        value_name = "OUTPUT TYPE",
        requires_ifs = [
            ("coveralls", "coveralls-auth"),
            ("coveralls+", "coveralls-auth"),
        ],

        value_delimiter = ',',
        alias = "output-type",
        default_value = "lcov",
    )]
    output_types: Vec<OutputType>,
    /// Specifies the output path. This is a file for a single output type and must be a folder
    /// for multiple output types.
    #[arg(short, long, value_name = "PATH", alias = "output-file")]
    output_path: Option<PathBuf>,
    /// Specifies the output config file.
    #[arg(long, value_name = "PATH", alias = "output-config-file")]
    output_config_file: Option<PathBuf>,
    /// Specifies the root directory of the source files.
    #[arg(short, long, value_name = "DIRECTORY")]
    source_dir: Option<PathBuf>,
    /// Specifies a prefix to remove from the paths (e.g. if grcov is run on a different machine
    /// than the one that generated the code coverage information).
    #[arg(short, long, value_name = "PATH")]
    prefix_dir: Option<PathBuf>,
    /// Ignore source files that can't be found on the disk.
    #[arg(long)]
    ignore_not_existing: bool,
    /// Ignore files/directories specified as globs.
    #[arg(long = "ignore", value_name = "PATH", num_args = 1)]
    ignore_dir: Vec<String>,
    /// Keep only files/directories specified as globs.
    #[arg(long = "keep-only", value_name = "PATH", num_args = 1)]
    keep_dir: Vec<String>,
    #[arg(long, value_name = "PATH")]
    path_mapping: Option<PathBuf>,
    /// Enables parsing branch coverage information.
    #[arg(long)]
    branch: bool,
    /// Filters out covered/uncovered files. Use 'covered' to only return covered files, 'uncovered'
    /// to only return uncovered files.
    #[arg(long, value_enum)]
    filter: Option<Filter>,
    /// Speeds-up parsing, when the code coverage information is exclusively coming from a llvm
    /// build.
    #[arg(long)]
    llvm: bool,
    /// Sets the repository token from Coveralls, required for the 'coveralls' and 'coveralls+'
    /// formats.
    #[arg(long, value_name = "TOKEN")]
    token: Option<String>,
    /// Sets the hash of the commit used to generate the code coverage data.
    #[arg(long, value_name = "COMMIT HASH")]
    commit_sha: Option<String>,
    /// Sets the service name.
    #[arg(long, value_name = "SERVICE NAME")]
    service_name: Option<String>,
    /// Sets the service number.
    #[arg(long, value_name = "SERVICE NUMBER")]
    service_number: Option<String>,
    /// Sets the service job id.
    #[arg(
        long,
        value_name = "SERVICE JOB ID",
        visible_alias = "service-job-number",
        requires = "service_name"
    )]
    service_job_id: Option<String>,
    /// Sets the service pull request number.
    #[arg(long, value_name = "SERVICE PULL REQUEST")]
    service_pull_request: Option<String>,
    /// Sets the service flag name for coveralls parallel/carryover mode
    #[arg(long, value_name = "SERVICE FLAG NAME")]
    service_flag_name: Option<String>,
    /// Sets the build type to be parallel for 'coveralls' and 'coveralls+' formats.
    #[arg(long)]
    parallel: bool,
    #[arg(long, value_name = "NUMBER")]
    threads: Option<usize>,
    /// Sets coverage decimal point precision on output reports.
    #[arg(long, value_name = "NUMBER", default_value = "2")]
    precision: usize,
    #[arg(long = "guess-directory-when-missing")]
    guess_directory: bool,
    /// Set the branch for coveralls report. Defaults to 'master'.
    #[arg(long, value_name = "VCS BRANCH", default_value = "master")]
    vcs_branch: String,
    /// Set the file where to log (or stderr or stdout). Defaults to 'stderr'.
    #[arg(long, value_name = "LOG", default_value = "stderr")]
    log: PathBuf,
    /// Set the log level.
    #[arg(long, value_name = "LEVEL", default_value = "ERROR", value_enum)]
    log_level: LevelFilterArg,
    /// Lines in covered files containing this marker will be excluded.
    #[arg(long, value_name = "regex")]
    excl_line: Option<Regex>,
    /// Marks the beginning of an excluded section. The current line is part of this section.
    #[arg(long, value_name = "regex")]
    excl_start: Option<Regex>,
    /// Marks the end of an excluded section. The current line is part of this section.
    #[arg(long, value_name = "regex")]
    excl_stop: Option<Regex>,
    /// Lines in covered files containing this marker will be excluded from branch coverage.
    #[arg(long, value_name = "regex")]
    excl_br_line: Option<Regex>,
    /// Marks the beginning of a section excluded from branch coverage. The current line is part of
    /// this section.
    #[arg(long, value_name = "regex")]
    excl_br_start: Option<Regex>,
    /// Marks the end of a section excluded from branch coverage. The current line is part of this
    /// section.
    #[arg(long, value_name = "regex")]
    excl_br_stop: Option<Regex>,
    /// No symbol demangling.
    #[arg(long)]
    no_demangle: bool,
}

fn main() {
    let opt = Opt::parse();

    if let Some(path) = opt.llvm_path {
        LLVM_PATH.set(path).unwrap();
    }

    let filter_option = opt.filter.map(|filter| match filter {
        Filter::Covered => true,
        Filter::Uncovered => false,
    });
    let stdout = Path::new("stdout");
    let stderr = Path::new("stderr");

    if opt.log == stdout {
        let _ = TermLogger::init(
            opt.log_level.0,
            Config::default(),
            TerminalMode::Stdout,
            ColorChoice::Auto,
        );
    } else if opt.log == stderr {
        let _ = TermLogger::init(
            opt.log_level.0,
            Config::default(),
            TerminalMode::Stderr,
            ColorChoice::Auto,
        );
    } else if let Ok(file) = File::create(&opt.log) {
        let _ = WriteLogger::init(opt.log_level.0, Config::default(), file);
    } else {
        let _ = TermLogger::init(
            opt.log_level.0,
            Config::default(),
            TerminalMode::Stderr,
            ColorChoice::Auto,
        );
        error!(
            "Unable to create log file: {}. Switch to stderr",
            opt.log.display()
        );
    }

    let file_filter = FileFilter::new(
        opt.excl_line,
        opt.excl_start,
        opt.excl_stop,
        opt.excl_br_line,
        opt.excl_br_start,
        opt.excl_br_stop,
    );
    let demangle = !opt.no_demangle;

    panic::set_hook(Box::new(|panic_info| {
        let (filename, line) = panic_info
            .location()
            .map(|loc| (loc.file(), loc.line()))
            .unwrap_or(("<unknown>", 0));
        let cause = panic_info
            .payload()
            .downcast_ref::<String>()
            .map(String::deref);
        let cause = cause.unwrap_or_else(|| {
            panic_info
                .payload()
                .downcast_ref::<&str>()
                .copied()
                .unwrap_or("<cause unknown>")
        });
        error!("A panic occurred at {}:{}: {}", filename, line, cause);
    }));

    let num_threads: usize = opt.threads.unwrap_or_else(|| 1.max(num_cpus::get() - 1));
    let source_root = opt
        .source_dir
        .filter(|source_dir| source_dir != Path::new(""))
        .map(|source_dir| canonicalize_path(source_dir).expect("Source directory does not exist."));

    let prefix_dir = opt.prefix_dir.or_else(|| source_root.clone());

    let tmp_dir = tempfile::tempdir().expect("Failed to create temporary directory");
    let tmp_path = tmp_dir.path().to_owned();
    assert!(tmp_path.exists());

    let result_map: Arc<SyncCovResultMap> = Arc::new(Mutex::new(
        FxHashMap::with_capacity_and_hasher(20_000, Default::default()),
    ));
    let (sender, receiver) = bounded(2 * num_threads);
    let path_mapping: Arc<Mutex<Option<Value>>> = Arc::new(Mutex::new(None));

    let producer = {
        let sender: JobSender = sender.clone();
        let tmp_path = tmp_path.clone();
        let path_mapping_file = opt.path_mapping;
        let path_mapping = Arc::clone(&path_mapping);
        let paths = opt.paths;
        let is_llvm = opt.llvm;

        thread::Builder::new()
            .name(String::from("Producer"))
            .spawn(move || {
                let producer_path_mapping_buf = producer(
                    &tmp_path,
                    &paths,
                    &sender,
                    filter_option.is_some() && filter_option.unwrap(),
                    is_llvm,
                );

                let mut path_mapping = path_mapping.lock().unwrap();
                *path_mapping = if let Some(path) = path_mapping_file {
                    let file = File::open(path).unwrap();
                    Some(serde_json::from_reader(file).unwrap())
                } else {
                    producer_path_mapping_buf.map(|producer_path_mapping_buf| {
                        serde_json::from_slice(&producer_path_mapping_buf).unwrap()
                    })
                };
            })
            .unwrap()
    };

    let mut parsers = Vec::new();

    for i in 0..num_threads {
        let receiver = receiver.clone();
        let result_map = Arc::clone(&result_map);
        let working_dir = tmp_path.join(format!("{}", i));
        let source_root = source_root.clone();
        let binary_path = opt.binary_path.clone();
        let branch_enabled = opt.branch;
        let guess_directory = opt.guess_directory;

        let t = thread::Builder::new()
            .name(format!("Consumer {}", i))
            .spawn(move || {
                fs::create_dir(&working_dir).expect("Failed to create working directory");
                consumer(
                    &working_dir,
                    source_root.as_deref(),
                    &result_map,
                    receiver,
                    branch_enabled,
                    guess_directory,
                    binary_path.as_deref(),
                );
            })
            .unwrap();

        parsers.push(t);
    }

    if producer.join().is_err() {
        process::exit(1);
    }

    // Poison the receiver, now that the producer is finished.
    for _ in 0..num_threads {
        sender.send(None).unwrap();
    }

    for parser in parsers {
        if parser.join().is_err() {
            process::exit(1);
        }
    }

    let result_map_mutex = Arc::try_unwrap(result_map).unwrap();
    let result_map = result_map_mutex.into_inner().unwrap();

    let path_mapping_mutex = Arc::try_unwrap(path_mapping).unwrap();
    let path_mapping = path_mapping_mutex.into_inner().unwrap();

    let iterator = rewrite_paths(
        result_map,
        path_mapping,
        source_root.as_deref(),
        prefix_dir.as_deref(),
        opt.ignore_not_existing,
        &opt.ignore_dir,
        &opt.keep_dir,
        filter_option,
        file_filter,
    );

    let service_number = opt.service_number.unwrap_or_default();
    let service_pull_request = opt.service_pull_request.unwrap_or_default();
    let commit_sha = opt.commit_sha.unwrap_or_default();

    let output_types = opt.output_types;

    let output_path = match output_types.len() {
        0 => unreachable!("Output types has a default value"),
        1 => opt.output_path.as_deref(),
        _ => match opt.output_path.as_deref() {
            Some(output_path) => {
                if output_path.is_dir() {
                    Some(output_path)
                } else {
                    panic!("output_path must be a directory when using multiple outputs");
                }
            }
            _ => None,
        },
    };

    for output_type in &output_types {
        let output_path = output_type.to_file_name(output_path);

        match output_type {
            OutputType::Ade => output_activedata_etl(&iterator, output_path.as_deref(), demangle),
            OutputType::Lcov => output_lcov(&iterator, output_path.as_deref(), demangle),
            OutputType::Coveralls => output_coveralls(
                &iterator,
                opt.token.as_deref(),
                opt.service_name.as_deref(),
                &service_number,
                opt.service_job_id.as_deref(),
                &service_pull_request,
                opt.service_flag_name.as_deref(),
                &commit_sha,
                false,
                output_path.as_deref(),
                &opt.vcs_branch,
                opt.parallel,
                demangle,
            ),
            OutputType::CoverallsPlus => output_coveralls(
                &iterator,
                opt.token.as_deref(),
                opt.service_name.as_deref(),
                &service_number,
                opt.service_job_id.as_deref(),
                &service_pull_request,
                opt.service_flag_name.as_deref(),
                &commit_sha,
                true,
                output_path.as_deref(),
                &opt.vcs_branch,
                opt.parallel,
                demangle,
            ),
            OutputType::Files => output_files(&iterator, output_path.as_deref()),
            OutputType::Covdir => output_covdir(&iterator, output_path.as_deref(), opt.precision),
            OutputType::Html => output_html(
                &iterator,
                output_path.as_deref(),
                num_threads,
                opt.branch,
                opt.output_config_file.as_deref(),
                opt.precision,
            ),
            OutputType::Cobertura => output_cobertura(
                source_root.as_deref(),
                &iterator,
                output_path.as_deref(),
                demangle,
            ),
            OutputType::Markdown => {
                output_markdown(&iterator, output_path.as_deref(), opt.precision)
            }
        };
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use clap::CommandFactory;

    #[test]
    fn clap_debug_assert() {
        Opt::command().debug_assert();
    }
}