Skip to main content

flowey_hvlite/pipelines/
unit_test_coverage.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Opt-in local unit coverage, independent of PR CI publication.
5
6use flowey::node::prelude::ReadVar;
7use flowey::pipeline::prelude::*;
8use flowey_lib_hvlite::build_nextest_unit_tests::BuildNextestUnitTestMode;
9use flowey_lib_hvlite::collect_unit_test_coverage::Options;
10use flowey_lib_hvlite::common::CommonProfile;
11use std::path::PathBuf;
12
13#[derive(clap::Args)]
14pub struct UnitTestCoverageCli {
15    /// Exact workspace package names to test (repeat for multiple packages).
16    #[clap(short = 'p', long = "package", required = true)]
17    packages: Vec<String>,
18
19    /// Nextest filter expression, within the selected packages.
20    #[clap(short = 'E', long)]
21    filter: Option<String>,
22
23    /// Also report changed-line coverage against the merge base of this revision and HEAD.
24    #[clap(long)]
25    base: Option<String>,
26
27    /// Fresh result directory. Defaults to a unique directory under target/unit-coverage.
28    #[clap(long)]
29    output: Option<PathBuf>,
30
31    /// Install missing build dependencies and Rust components using Flowey's normal setup.
32    #[clap(long)]
33    install_missing_deps: bool,
34}
35
36impl IntoPipeline for UnitTestCoverageCli {
37    fn into_pipeline(self, backend: PipelineBackendHint) -> anyhow::Result<Pipeline> {
38        anyhow::ensure!(
39            matches!(backend, PipelineBackendHint::Local)
40                && matches!(FlowPlatform::host(backend), FlowPlatform::Linux(_)),
41            "unit-test-coverage currently requires local x64 or ARM64 Linux"
42        );
43        let target = match FlowArch::host(backend) {
44            FlowArch::X86_64 => target_lexicon::triple!("x86_64-unknown-linux-gnu"),
45            FlowArch::Aarch64 => target_lexicon::triple!("aarch64-unknown-linux-gnu"),
46            arch => anyhow::bail!("unsupported Linux unit coverage architecture: {arch:?}"),
47        };
48        for package in &self.packages {
49            anyhow::ensure!(
50                !package.is_empty()
51                    && package
52                        .bytes()
53                        .all(|c| c.is_ascii_alphanumeric() || c == b'_' || c == b'-'),
54                "use an exact workspace package name, not a package pattern: {package}"
55            );
56        }
57        let output = std::path::absolute(self.output.unwrap_or_else(|| {
58            crate::repo_root()
59                .join("target/unit-coverage")
60                .join(format!(
61                    "{}-{}",
62                    std::process::id(),
63                    std::time::SystemTime::now()
64                        .duration_since(std::time::UNIX_EPOCH)
65                        .unwrap_or_default()
66                        .as_nanos()
67                ))
68        }))?;
69        anyhow::ensure!(
70            !output.exists(),
71            "coverage output already exists; choose a fresh directory: {}",
72            output.display()
73        );
74        let mut pipeline = Pipeline::new();
75        let begin = pipeline
76            .new_job(
77                FlowPlatform::host(backend),
78                FlowArch::host(backend),
79                "begin unit test coverage",
80            )
81            .dep_on(
82                |ctx| flowey_lib_hvlite::collect_unit_test_coverage::Request::Begin {
83                    repo_root: crate::repo_root(),
84                    done: ctx.new_done_handle(),
85                },
86            )
87            .finish();
88        let collect = pipeline
89            .new_job(
90                FlowPlatform::host(backend),
91                FlowArch::host(backend),
92                "local unit test coverage",
93            )
94            .dep_on(|_| flowey_lib_hvlite::_jobs::cfg_versions::Request::Init)
95            .dep_on(
96                |_| flowey_lib_hvlite::_jobs::cfg_hvlite_reposource::Params {
97                    hvlite_repo_source: flowey_lib_common::git_checkout::RepoSource::ExistingClone(
98                        ReadVar::from_static(crate::repo_root()),
99                    ),
100                },
101            )
102            .dep_on(|_| flowey_lib_hvlite::_jobs::cfg_common::Params {
103                local_only: Some(flowey_lib_hvlite::_jobs::cfg_common::LocalOnlyParams {
104                    interactive: true,
105                    auto_install: self.install_missing_deps,
106                    ignore_rust_version: true,
107                }),
108                verbose: ReadVar::from_static(false),
109                locked: true,
110                deny_warnings: false,
111                no_incremental: false,
112            })
113            .dep_on(|ctx| flowey_lib_hvlite::build_nextest_unit_tests::Request {
114                target,
115                profile: CommonProfile::Debug,
116                build_mode: BuildNextestUnitTestMode::Coverage {
117                    packages: self.packages,
118                    options: Options {
119                        output,
120                        filter: self.filter,
121                        base: self.base,
122                    },
123                    done: ctx.new_done_handle(),
124                },
125            })
126            .finish();
127        pipeline.non_artifact_dep(&collect, &begin);
128        Ok(pipeline)
129    }
130}