-
Notifications
You must be signed in to change notification settings - Fork 802
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[alias] | ||
xtask = "run --package xtask --" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "xtask" | ||
version = "0.1.0" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
anyhow = "1.0.51" | ||
|
||
# Clap 3 requires MSRV 1.54 | ||
rustversion = "1.0" | ||
structopt = { version = "0.3", default-features = false } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
use anyhow::{Context, Result}; | ||
use std::{collections::HashMap, process::Command}; | ||
use structopt::StructOpt; | ||
|
||
#[derive(StructOpt)] | ||
enum Subcommand { | ||
/// Runs `cargo llvm-cov` for the PyO3 codebase. | ||
Coverage, | ||
/// Runs tests in examples/ and pytests/ | ||
TestPy, | ||
} | ||
|
||
impl Subcommand { | ||
fn execute(self) -> Result<()> { | ||
match self { | ||
Subcommand::Coverage => subcommand_coverage(), | ||
Subcommand::TestPy => run_python_tests(None), | ||
} | ||
} | ||
} | ||
|
||
fn main() -> Result<()> { | ||
Subcommand::from_args().execute() | ||
} | ||
|
||
/// Runs `cargo llvm-cov` for the PyO3 codebase. | ||
fn subcommand_coverage() -> Result<()> { | ||
run(&mut llvm_cov_command(&["clean", "--workspace"]))?; | ||
run(&mut llvm_cov_command(&["--no-report"]))?; | ||
|
||
// FIXME: add various feature combinations using 'full' feature. | ||
// run(&mut llvm_cov_command(&["--no-report"]))?; | ||
|
||
// XXX: the following block doesn't work until https://github.com/taiki-e/cargo-llvm-cov/pull/115 is merged | ||
let env = get_coverage_env()?; | ||
run_python_tests(&env)?; | ||
// (after here works with stable llvm-cov) | ||
|
||
// TODO: add an argument to make it possible to generate lcov report & use this in CI. | ||
run(&mut llvm_cov_command(&["--no-run", "--summary-only"]))?; | ||
Ok(()) | ||
} | ||
|
||
fn run(command: &mut Command) -> Result<()> { | ||
println!("running: {}", format_command(command)); | ||
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 run_python_tests<'a>( | ||
env: impl IntoIterator<Item = (&'a String, &'a String)> + Copy, | ||
) -> Result<()> { | ||
for entry in std::fs::read_dir("pytests")? { | ||
let path = entry?.path(); | ||
if path.is_dir() && path.join("tox.ini").exists() { | ||
run(Command::new("tox").arg("-c").arg(path).envs(env))?; | ||
} | ||
} | ||
for entry in std::fs::read_dir("examples")? { | ||
let path = entry?.path(); | ||
if path.is_dir() && path.join("tox.ini").exists() { | ||
run(Command::new("tox").arg("-c").arg(path).envs(env))?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
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') { | ||
let (key, value) = split_once(line, '=').context("expected '=' in each output line")?; | ||
env.insert(key.to_owned(), value.trim_matches('"').to_owned()); | ||
} | ||
|
||
env.insert("TOX_TESTENV_PASSENV".to_owned(), "*".to_owned()); | ||
env.insert("RUSTUP_TOOLCHAIN".to_owned(), "nightly".to_owned()); | ||
|
||
Ok(env) | ||
} | ||
|
||
// Replacement for str.split_once() on Rust older than 1.52 | ||
#[rustversion::before(1.52)] | ||
fn split_once(s: &str, pat: char) -> Option<(&str, &str)> { | ||
let mut iter = s.splitn(2, pat); | ||
Some((iter.next()?, iter.next()?)) | ||
} | ||
|
||
#[rustversion::since(1.52)] | ||
fn split_once(s: &str, pat: char) -> Option<(&str, &str)> { | ||
s.split_once(pat) | ||
} | ||
|
||
#[rustversion::since(1.57)] | ||
fn format_command(command: &Command) -> String { | ||
let mut buf = String::new(); | ||
buf.push('`'); | ||
buf.push_str(&command.get_program().to_string_lossy()); | ||
for arg in command.get_args() { | ||
buf.push(' '); | ||
buf.push_str(&arg.to_string_lossy()); | ||
} | ||
buf.push('`'); | ||
buf | ||
} | ||
|
||
#[rustversion::before(1.57)] | ||
fn format_command(command: &Command) -> String { | ||
// Debug impl isn't as nice as the above, but will do on < 1.57 | ||
format!("{:?}", command) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ;)