Skip to content

Commit

Permalink
feat: allow manually defining the project's manifest path
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaka91 committed Jun 22, 2023
1 parent 1443ab1 commit 5d972b8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
20 changes: 14 additions & 6 deletions src/cargo.rs
Expand Up @@ -8,8 +8,12 @@ use color_eyre::{
use serde::Deserialize;

/// Locate the root directory of the project under the current working directory.
pub fn project_dir() -> Result<Utf8PathBuf> {
let manifest_path = cargo_locate_project().wrap_err("failed to locate project")?;
pub fn project_dir(manifest_path: Option<&Utf8Path>) -> Result<Utf8PathBuf> {
let manifest_path = match manifest_path {
Some(path) => path.to_owned(),
None => cargo_locate_project().wrap_err("failed to locate project")?,
};

cargo_metadata(&manifest_path)
.map(|meta| meta.workspace_root)
.wrap_err("failed to load project metadata")
Expand All @@ -21,8 +25,12 @@ pub fn project_dir() -> Result<Utf8PathBuf> {
/// our own `target/llvm-cov-pretty` folder that holds the report files.
///
/// This will only work if the current working directory contains a Rust project.
pub fn output_dir() -> Result<Utf8PathBuf> {
let manifest_path = cargo_locate_project().wrap_err("failed to locate project")?;
pub fn output_dir(manifest_path: Option<&Utf8Path>) -> Result<Utf8PathBuf> {
let manifest_path = match manifest_path {
Some(path) => path.to_owned(),
None => cargo_locate_project().wrap_err("failed to locate project")?,
};

cargo_metadata(&manifest_path)
.map(|meta| meta.target_directory.join(env!("CARGO_PKG_NAME")))
.wrap_err("failed to load project metadata")
Expand Down Expand Up @@ -131,12 +139,12 @@ pub fn check_version() -> Result<()> {
mod tests {
#[test]
fn project_dir() {
super::project_dir().unwrap();
super::project_dir(None).unwrap();
}

#[test]
fn output_dir() {
super::output_dir().unwrap();
super::output_dir(None).unwrap();
}

#[test]
Expand Down
3 changes: 3 additions & 0 deletions src/cli.rs
Expand Up @@ -24,6 +24,9 @@ pub struct Cli {
/// The highlighting theme to use, if not disabled.
#[arg(long, default_value_t = Theme::OneHalf)]
pub theme: Theme,
/// Location of the project's Cargo.toml, in case the default detection isn't sufficient.
#[arg(long)]
pub manifest_path: Option<Utf8PathBuf>,
/// Input coverage file encoded as JSON, or STDIN if omitted.
pub input: Option<Utf8PathBuf>,
#[command(subcommand)]
Expand Down
11 changes: 5 additions & 6 deletions src/main.rs
Expand Up @@ -60,8 +60,10 @@ fn main() -> Result<()> {
.wrap_err("failed parsing report data from STDIN")?
};

let project_dir = cargo::project_dir().wrap_err("failed to locate project directory")?;
let output_dir = cargo::output_dir().wrap_err("failed to locate output directory")?;
let project_dir = cargo::project_dir(cli.manifest_path.as_deref())
.wrap_err("failed to locate project directory")?;
let output_dir = cargo::output_dir(cli.manifest_path.as_deref())
.wrap_err("failed to locate output directory")?;

let files = collect_project_files(&project_dir)?;
let mut files = merge_file_info(files, &export.files);
Expand Down Expand Up @@ -189,10 +191,7 @@ fn segments_to_ranges(

fn merge_function_info(files: &mut Vec<FileInfo>, functions: &[schema::Function]) {
files.par_iter_mut().for_each(|file| {
for function in functions
.iter()
.filter(|f| f.filenames[0] == file.path)
{
for function in functions.iter().filter(|f| f.filenames[0] == file.path) {
for region in &function.regions {
if region.execution_count > 0 {
file.called
Expand Down

0 comments on commit 5d972b8

Please sign in to comment.