[go: up one dir, main page]

cargo-llvm-cov 0.5.39

Cargo subcommand to easily use LLVM source-based code coverage (-C instrument-coverage).
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
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![cfg(not(miri))] // Miri doesn't support file with non-default mode: https://github.com/rust-lang/miri/pull/2720

mod auxiliary;

use std::path::Path;

use anyhow::Context as _;
use auxiliary::{
    assert_output, cargo_llvm_cov, fixtures_path, normalize_output, perturb_one_header,
    test_project, test_report, CommandExt,
};
use fs_err as fs;
use tempfile::tempdir;

const SUBCOMMANDS: &[&str] = &["", "run", "report", "clean", "show-env", "nextest"];

fn test_set() -> Vec<(&'static str, &'static [&'static str])> {
    vec![
        ("txt", &["--text"]),
        ("hide-instantiations.txt", &["--text", "--hide-instantiations"]),
        ("summary.txt", &[]),
        ("json", &["--json", "--summary-only"]),
        // TODO: full JSON output is unstable between platform.
        // ("full.json", &["--json"]),
        ("lcov.info", &["--lcov", "--summary-only"]),
        // TODO: test Cobertura output
        ("codecov.json", &["--codecov"]),
    ]
}

fn run(model: &str, name: &str, args: &[&str], envs: &[(&str, &str)]) {
    let id = format!("{model}/{name}");
    for (extension, args2) in test_set() {
        test_report(model, name, extension, None, &[args, args2].concat(), envs)
            .context(id.clone())
            .unwrap();
    }
}

// TODO:
// - add tests for non-crates.io dependencies

#[test]
fn real1() {
    run("real1", "workspace_root", &[], &[]);
    run("real1", "all", &["--all"], &[]);
    run("real1", "manifest_path", &["--manifest-path", "member1/member2/Cargo.toml"], &[]);
    run("real1", "package1", &["--package", "member2"], &[]);
    run("real1", "exclude", &["--all", "--exclude", "crate1"], &[]);
}

#[test]
fn virtual1() {
    run("virtual1", "workspace_root", &[], &[]);
    run("virtual1", "package1", &["--package", "member1"], &[]);
    run("virtual1", "package2", &["--package", "member1", "--package", "member2"], &[]);
    run("virtual1", "package3", &["--package", "member2"], &[]);
    run("virtual1", "package4", &["--package", "member3"], &[]);
    run("virtual1", "package5", &["--package", "member4"], &[]);
    run("virtual1", "package6", &["--package", "member3", "--package", "member4"], &[]);
    run("virtual1", "exclude1", &["--workspace", "--exclude", "member1"], &[]);
    run("virtual1", "exclude2", &["--workspace", "--exclude", "member2"], &[]);
    run(
        "virtual1",
        "exclude-from-report1",
        &["--workspace", "--exclude-from-report", "member1"],
        &[],
    );
    run(
        "virtual1",
        "exclude-from-report2",
        &["--workspace", "--exclude-from-report", "member2"],
        &[],
    );
    run("virtual1", "exclude-from-test1", &["--workspace", "--exclude-from-test", "member1"], &[]);
    run("virtual1", "exclude-from-test2", &["--workspace", "--exclude-from-test", "member2"], &[]);
}

#[test]
fn no_test() {
    run("no_test", "no_test", &[], &[]);
    if !(cfg!(windows) && cfg!(target_env = "msvc")) {
        run("no_test", "link_dead_code", &[], &[("RUSTFLAGS", "-C link-dead-code")]);
    }
}

#[test]
fn bin_crate() {
    run("bin_crate", "bin_crate", &[], &[]);

    let model = "bin_crate";
    let name = "run";
    let id = format!("{model}/{name}");
    for (extension, args2) in test_set() {
        test_report(model, name, extension, Some("run"), &[args2, &["--", "1"]].concat(), &[])
            .context(id.clone())
            .unwrap();
    }
}

// nightly-2023-12-10 fixed bug in report generation, so the latest report is not the same as the old report.
#[rustversion::attr(before(1.76), ignore)]
#[test]
fn instantiations() {
    // TODO: fix https://github.com/taiki-e/cargo-llvm-cov/issues/43
    run("instantiations", "instantiations", &[], &[]);
}

// nightly-2023-12-10 fixed bug in report generation, so the latest report is not the same as the old report.
#[rustversion::attr(before(1.76), ignore)]
#[test]
fn cargo_config() {
    run("cargo_config", "cargo_config", &[], &[]);
    run("cargo_config_toml", "cargo_config_toml", &[], &[]);
}

// feature(coverage_attribute) requires nightly
#[rustversion::attr(not(nightly), ignore)]
#[test]
fn no_coverage() {
    let model = "no_coverage";
    let id = format!("{model}/{model}");
    for (extension, args2) in test_set() {
        // TODO: On windows, the order of the instantiations in the generated coverage report will be different.
        if extension == "full.json" && cfg!(windows) {
            continue;
        }
        test_report(model, model, extension, None, args2, &[]).context(id.clone()).unwrap();
    }

    let name = "no_cfg_coverage";
    let id = format!("{model}/{name}");
    for (extension, args2) in test_set() {
        // TODO: On windows, the order of the instantiations in the generated coverage report will be different.
        if extension == "full.json" && cfg!(windows) {
            continue;
        }
        test_report(model, name, extension, None, &[args2, &["--no-cfg-coverage"]].concat(), &[])
            .context(id.clone())
            .unwrap();
    }
}

// feature(coverage_attribute) requires nightly
#[rustversion::attr(not(nightly), ignore)]
#[test]
fn coverage_helper() {
    let model = "coverage_helper";
    let id = format!("{model}/{model}");
    for (extension, args2) in test_set() {
        // TODO: On windows, the order of the instantiations in the generated coverage report will be different.
        if extension == "full.json" && cfg!(windows) {
            continue;
        }
        test_report(model, model, extension, None, args2, &[]).context(id.clone()).unwrap();
    }
}

// The order of the instantiations in the generated coverage report will be different depending on the version.
#[rustversion::attr(not(nightly), ignore)]
#[test]
fn merge() {
    // The order of the instantiations in the generated coverage report will be different depending on the platform.
    if !cfg!(all(target_arch = "x86_64", target_os = "linux")) {
        return;
    }
    let output_dir = fixtures_path().join("coverage-reports").join("merge");
    merge_with_failure_mode(&output_dir, false);
}

#[test]
fn merge_failure_mode_all() {
    let tempdir = tempdir().unwrap();
    merge_with_failure_mode(tempdir.path(), true);
}

fn merge_with_failure_mode(output_dir: &Path, failure_mode_all: bool) {
    let model = "merge";
    fs::create_dir_all(output_dir).unwrap();
    for (extension, args) in test_set() {
        let workspace_root = test_project(model).unwrap();
        let output_path = &output_dir.join(model).with_extension(extension);
        let expected = &fs::read_to_string(output_path).unwrap_or_default();
        cargo_llvm_cov("")
            .args(["--color", "never", "--no-report", "--features", "a"])
            .arg("--remap-path-prefix")
            .current_dir(workspace_root.path())
            .assert_success();
        cargo_llvm_cov("")
            .args(["--color", "never", "--no-report", "--features", "b"])
            .arg("--remap-path-prefix")
            .current_dir(workspace_root.path())
            .assert_success();
        let mut cmd = cargo_llvm_cov("report");
        cmd.args(["--color", "never", "--output-path"])
            .arg(output_path)
            .arg("--remap-path-prefix")
            .args(args)
            .current_dir(workspace_root.path());
        cmd.assert_success();

        if failure_mode_all {
            perturb_one_header(workspace_root.path()).unwrap().unwrap();
            cmd.assert_failure()
                .stderr_contains("unrecognized instrumentation profile encoding format");
            cmd.args(["--failure-mode", "all"]);
            cmd.assert_success();
        } else {
            normalize_output(output_path, args).unwrap();
            assert_output(output_path, expected).unwrap();
        }
    }
}

// nightly-2023-12-10 fixed bug in report generation, so the latest report is not the same as the old report.
#[rustversion::attr(before(1.76), ignore)]
#[test]
fn clean_ws() {
    let model = "merge";
    let name = "clean_ws";
    let output_dir = fixtures_path().join("coverage-reports").join(model);
    fs::create_dir_all(&output_dir).unwrap();
    for (extension, args) in test_set() {
        let workspace_root = test_project(model).unwrap();
        let output_path = &output_dir.join(name).with_extension(extension);
        let expected = &fs::read_to_string(output_path).unwrap_or_default();
        cargo_llvm_cov("")
            .args(["--color", "never", "--no-report", "--features", "a"])
            .arg("--remap-path-prefix")
            .current_dir(workspace_root.path())
            .assert_success();
        cargo_llvm_cov("report")
            .args(["--color", "never", "--output-path"])
            .arg(output_path)
            .arg("--remap-path-prefix")
            .args(args)
            .current_dir(workspace_root.path())
            .assert_success();

        normalize_output(output_path, args).unwrap();
        assert_output(output_path, expected).unwrap();

        cargo_llvm_cov("")
            .args(["clean", "--color", "never", "--workspace"])
            .current_dir(workspace_root.path())
            .assert_success();
        cargo_llvm_cov("")
            .args(["--color", "never", "--no-report", "--features", "a"])
            .arg("--remap-path-prefix")
            .current_dir(workspace_root.path())
            .assert_success();
        cargo_llvm_cov("report")
            .args(["--color", "never", "--output-path"])
            .arg(output_path)
            .arg("--remap-path-prefix")
            .args(args)
            .current_dir(workspace_root.path())
            .assert_success();

        normalize_output(output_path, args).unwrap();
        assert_output(output_path, expected).unwrap();
    }
}

#[cfg_attr(windows, ignore)] // `echo` may not be available
#[test]
fn open_report() {
    let model = "real1";
    let workspace_root = test_project(model).unwrap();
    cargo_llvm_cov("")
        .args(["--color", "never", "--open"])
        .current_dir(workspace_root.path())
        .env("BROWSER", "echo")
        .assert_success()
        .stdout_contains(
            workspace_root.path().join("target/llvm-cov/html/index.html").to_string_lossy(),
        );
}

#[test]
fn show_env() {
    cargo_llvm_cov("show-env").assert_success().stdout_not_contains("export");
    cargo_llvm_cov("show-env").arg("--export-prefix").assert_success().stdout_contains("export");
}

#[allow(clippy::single_element_loop)]
#[test]
fn invalid_arg() {
    for subcommand in ["", "run", "clean", "show-env", "nextest"] {
        if subcommand != "show-env" {
            cargo_llvm_cov(subcommand)
                .arg("--export-prefix")
                .assert_failure()
                .stderr_contains("invalid option '--export-prefix'");
        }
        if !subcommand.is_empty() {
            if subcommand == "nextest" {
                cargo_llvm_cov(subcommand)
                    .arg("--doc")
                    .assert_failure()
                    .stderr_contains("doctest is not supported for nextest");
                cargo_llvm_cov(subcommand)
                    .arg("--doctests")
                    .assert_failure()
                    .stderr_contains("doctest is not supported for nextest");
            } else {
                cargo_llvm_cov(subcommand)
                    .arg("--doc")
                    .assert_failure()
                    .stderr_contains("invalid option '--doc'");
                if subcommand != "show-env" {
                    cargo_llvm_cov(subcommand)
                        .arg("--doctests")
                        .assert_failure()
                        .stderr_contains("invalid option '--doctests'");
                }
            }
        }
        if !matches!(subcommand, "" | "nextest") {
            for arg in [
                "--lib",
                "--bins",
                "--examples",
                "--test=v",
                "--tests",
                "--bench=v",
                "--benches",
                "--all-targets",
                "--no-run",
                "--no-fail-fast",
                "--exclude=v",
                "--exclude-from-test=v",
            ] {
                cargo_llvm_cov(subcommand).arg(arg).assert_failure().stderr_contains(format!(
                    "invalid option '{}' for subcommand '{subcommand}'",
                    arg.strip_suffix("=v").unwrap_or(arg)
                ));
            }
        }
        if !matches!(subcommand, "" | "nextest" | "run") {
            for arg in [
                "--bin=v",
                "--example=v",
                "--exclude-from-report=v",
                "--no-cfg-coverage",
                "--no-cfg-coverage-nightly",
                "--no-report",
                "--no-clean",
                "--ignore-run-fail",
            ] {
                cargo_llvm_cov(subcommand).arg(arg).assert_failure().stderr_contains(format!(
                    "invalid option '{}' for subcommand '{subcommand}'",
                    arg.strip_suffix("=v").unwrap_or(arg)
                ));
            }
        }
        if !matches!(subcommand, "" | "nextest" | "clean") {
            for arg in ["--workspace"] {
                cargo_llvm_cov(subcommand).arg(arg).assert_failure().stderr_contains(format!(
                    "invalid option '{}' for subcommand '{subcommand}'",
                    arg.strip_suffix("=v").unwrap_or(arg)
                ));
            }
        }
    }
}

#[test]
fn invalid_arg_no_passthrough() {
    // These subcommands don't allow passthrough args.
    // In other subcommands, if passthrough args are invalid,
    // it will be detected by cargo or cargo-nextest.
    for subcommand in ["report", "clean", "show-env"] {
        cargo_llvm_cov(subcommand)
            .arg("-a")
            .assert_failure()
            .stderr_contains(format!("invalid option '-a' for subcommand '{subcommand}'"));
        cargo_llvm_cov(subcommand)
            .arg("--b")
            .assert_failure()
            .stderr_contains(format!("invalid option '--b' for subcommand '{subcommand}'"));
        cargo_llvm_cov(subcommand)
            .arg("c")
            .assert_failure()
            .stderr_contains("unexpected argument \"c\"");
    }
}

#[test]
fn help() {
    for &subcommand in SUBCOMMANDS {
        cargo_llvm_cov(subcommand)
            .arg("--help")
            .assert_success()
            .stdout_contains(format!("cargo llvm-cov {subcommand}"));
    }
}

#[test]
fn version() {
    for &subcommand in SUBCOMMANDS {
        if subcommand.is_empty() {
            cargo_llvm_cov(subcommand)
                .arg("--version")
                .assert_success()
                .stdout_contains(env!("CARGO_PKG_VERSION"));
        } else {
            cargo_llvm_cov(subcommand).arg("--version").assert_failure().stderr_contains(format!(
                "invalid option '--version' for subcommand '{subcommand}'"
            ));
        }
    }
}