Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xtask: add coverage command #2067

Merged
merged 3 commits into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
xtask = "run --package xtask --"
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ harness = false
members = [
"pyo3-macros",
"pyo3-macros-backend",
"examples"
"examples",
"xtask"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't necessarily need to name this "xtask". I don't have a motivation for changing it. But we could.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I don't personally love the name xtask but I do like that it's a documented "approach" (https://github.com/matklad/cargo-xtask), so without good motivation I don't feel a need to change it.

For example I'd personally enjoy the alias being called cargo run-script. There's nothing stopping us adding a couple extra aliases that all do the same later, if we wanted ;)

]

[package.metadata.docs.rs]
Expand Down
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.PHONY: test test_py publish clippy lint fmt fmt_py fmt_rust

ALL_ADDITIVE_FEATURES = macros multiple-pymethods num-bigint num-complex hashbrown serde indexmap eyre anyhow
COVERAGE_PACKAGES = --package pyo3 --package pyo3-build-config --package pyo3-macros-backend --package pyo3-macros

list_all_additive_features:
@echo $(ALL_ADDITIVE_FEATURES)
Expand All @@ -25,6 +26,21 @@ fmt_rust:
fmt: fmt_rust fmt_py
@true

coverage:
# cargo llvm-cov clean --workspace
# cargo llvm-cov $(COVERAGE_PACKAGES) --no-report
# cargo llvm-cov $(COVERAGE_PACKAGES) --no-report --features abi3
# cargo llvm-cov $(COVERAGE_PACKAGES) --no-report --features $(ALL_ADDITIVE_FEATURES)
# cargo llvm-cov $(COVERAGE_PACKAGES) --no-report --features abi3 $(ALL_ADDITIVE_FEATURES)
bash -c "\
set -a\
source <(cargo llvm-cov show-env)\
export TOX_TESTENV_PASSENV=*\
make test_py\
"
cargo llvm-cov $(COVERAGE_PACKAGES) --no-run --summary-only


clippy:
cargo clippy --features="$(ALL_ADDITIVE_FEATURES)" --all-targets --workspace -- -Dwarnings
cargo clippy --features="abi3 $(ALL_ADDITIVE_FEATURES)" --all-targets --workspace -- -Dwarnings
Expand Down
8 changes: 8 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "xtask"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0.51"
clap = { version = "3.0.0-rc.7", features = ["derive"] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this add clap to pyo3's dependency tree? If so can we avoid that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe it does, no. This is an excerpt of Cargo.lock from a test project built against this branch:

[[package]]
name = "pyo3"
version = "0.15.1"
dependencies = [
 "cfg-if",
 "indoc",
 "libc",
 "num-bigint",
 "parking_lot",
 "paste",
 "pyo3-build-config",
 "pyo3-macros",
 "unindent",
]

So clap hasn't come through as a dependency here.

57 changes: 57 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use anyhow::Result;
use clap::Parser;
use std::{collections::HashMap, process::Command};

#[derive(Parser)]
enum Subcommand {
Coverage,
}

fn main() -> Result<()> {
match Subcommand::parse() {
Subcommand::Coverage => {
run(&mut llvm_cov_command(&["clean", "--workspace"]))?;
// FIXME: run (including with various feature combinations)
// run(&mut llvm_cov_command(&["--no-report"]))?;
let env = get_coverage_env()?;
for entry in std::fs::read_dir("pytests")? {
let path = entry?.path();
if path.is_dir() {
run(Command::new("tox").arg("-c").arg(path).envs(&env))?;
}
}
// FIXME: also run for examples
// FIXME: make it possible to generate lcov report too
run(&mut llvm_cov_command(&["--no-run", "--summary-only"]))?;
}
}
Ok(())
}

fn run(command: &mut Command) -> Result<()> {
command.spawn()?.wait()?;
Ok(())
}

fn llvm_cov_command(args: &[&str]) -> Command {
let mut command = Command::new("cargo");
command.args(["llvm-cov", "--package=pyo3"]).args(args);
command
}

fn get_coverage_env() -> Result<HashMap<String, String>> {
let mut env = HashMap::new();

let output = String::from_utf8(llvm_cov_command(&["show-env"]).output()?.stdout)?;

for line in output.trim().split('\n') {
// TODO use split_once on MSRV 1.52
let mut iter = line.splitn(2, '=');
env.insert(iter.next().unwrap().into(), iter.next().unwrap().trim_matches('"').into());
}

env.insert("TOX_TESTENV_PASSENV".to_owned(), "*".to_owned());
env.insert("RUSTUP_TOOLCHAIN".to_owned(), "nightly".to_owned());

Ok(env)
}