Skip to content

Commit

Permalink
feat: add version check for cargo-llvm-cov
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaka91 committed Jun 22, 2023
1 parent 2518ff7 commit 624ab1c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -37,6 +37,7 @@ ignore = "0.4.20"
minify-html = "0.11.1"
rayon = "1.7.0"
rustc-demangle = "0.1.23"
semver = "1.0.17"
serde = { version = "1.0.164", features = ["derive"] }
serde_json = "1.0.97"
time = { version = "0.3.22", features = ["formatting", "local-offset", "macros"] }
Expand Down
47 changes: 46 additions & 1 deletion src/cargo.rs
@@ -1,6 +1,6 @@
use std::process::Command;

use anyhow::{bail, Result};
use anyhow::{bail, ensure, Context, Result};
use camino::{Utf8Path, Utf8PathBuf};
use serde::Deserialize;

Expand Down Expand Up @@ -67,3 +67,48 @@ fn find_target_dir(root: &Utf8Path) -> Result<Utf8PathBuf> {
.map(|data| data.target_directory)
.map_err(Into::into)
}

/// Ensure the globally installed `cargo-llvm-cov` is a recent _known-to-be-working_ version, to
/// avoid possible errors due to different output in older versions.
pub fn check_version() -> Result<()> {
use semver::{Comparator, Op, Prerelease, Version};

static MIN_VERSION: Comparator = Comparator {
op: Op::GreaterEq,
major: 0,
minor: Some(5),
patch: None,
pre: Prerelease::EMPTY,
};

let output = Command::new("cargo-llvm-cov")
.args(["llvm-cov", "--version"])
.output()?;

if !output.status.success() {
bail!(
"failed running cargo-llvm-cov (llvm-cov --version):\n{}",
String::from_utf8_lossy(&output.stderr)
);
}

let output = String::from_utf8_lossy(&output.stdout);
let (name, version) = output
.trim()
.split_once(' ')
.context("no separator between name and version")?;

ensure!(
name == "cargo-llvm-cov",
"program doesn't appear to be cargo-llvm-cov"
);

let version = version.parse::<Version>()?;

ensure!(
MIN_VERSION.matches(&version),
"cargo-llvm-cov version {version} is too old, need at least {MIN_VERSION}"
);

Ok(())
}
4 changes: 3 additions & 1 deletion src/main.rs
Expand Up @@ -10,7 +10,7 @@ use std::{
ops::RangeInclusive,
};

use anyhow::Result;
use anyhow::{Context, Result};
use askama::Template;
use camino::{Utf8Path, Utf8PathBuf};
use rayon::iter::{IntoParallelIterator, IntoParallelRefMutIterator, ParallelIterator};
Expand Down Expand Up @@ -47,6 +47,8 @@ fn main() -> Result<()> {
return Ok(());
}

cargo::check_version().context("failed checking cargo-llvm-cov version")?;

let JsonExport { data: [export], .. } = if let Some(input) = cli.input {
let file = BufReader::new(File::open(input)?);
serde_json::from_reader::<_, JsonExport>(file)?
Expand Down

0 comments on commit 624ab1c

Please sign in to comment.