Skip to content

Commit

Permalink
Various refinements and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mejrs committed Mar 16, 2022
1 parent 3c29bab commit 394d4d2
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 33 deletions.
9 changes: 3 additions & 6 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ Please consider adding the following to your pull request:
- docs to all new functions and / or detail in the guide
- tests for all new or changed functions

Be aware the CI pipeline will check your pull request for the following. This is done using `nox` (you can install with `pip install nox`):
- Rust tests (`cargo test` or `nox -s test-rust`)
- Examples (`nox -s test-py`)
- Rust lints (`nox -s clippy`)
- Rust formatting (`nox -s fmt-rust`)
- Python formatting (`nox -s fmt-py`)
PyO3's CI pipeline will check your pull request. To run its tests
locally, you can run ```cargo xtask ci```. See its documentation
[here](https://github.com/PyO3/pyo3/tree/main/xtask#readme).
23 changes: 23 additions & 0 deletions xtask/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Commands to test PyO3.

To run these commands, you should be in PyO3's root directory, and run (for example) `cargo xtask all`.

```
USAGE:
xtask.exe <SUBCOMMAND>
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
SUBCOMMANDS:
ci Runs everything
clippy Runs `clippy`, denying all warnings
coverage Runs `cargo llvm-cov` for the PyO3 codebase
default Only runs the fast things (this is used if no command is specified)
doc Attempts to render the documentation
fmt Checks Rust and Python code formatting with `rustfmt` and `black`
help Prints this message or the help of the given subcommand(s)
test Runs various variations on `cargo test`
test-py Runs the tests in examples/ and pytests/
```
56 changes: 43 additions & 13 deletions xtask/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
use crate::utils::*;
use anyhow::{ensure, Result};
use std::io;
use std::process::Command;
use std::process::{Command, Stdio};
use std::time::Instant;
use structopt::StructOpt;

pub const MSRV: &'static str = "1.48";

#[derive(StructOpt)]
pub enum Subcommand {
/// Only runs the fast things (this is used if no command is specified)
Default,
/// Runs everything
All,
Ci,
/// Checks Rust and Python code formatting with `rustfmt` and `black`
Fmt,
/// Runs clippy, denying all warnings.
/// Runs `clippy`, denying all warnings.
Clippy,
/// Runs `cargo llvm-cov` for the PyO3 codebase.
Coverage(CoverageOpts),
/// Render documentation
/// Attempts to render the documentation.
Doc(DocOpts),
/// Runs various incantations of `cargo test`
/// Runs various variations on `cargo test`
Test,
/// Runs tests in examples/ and pytests/
/// Runs the tests in examples/ and pytests/
TestPy,
}

impl Default for Subcommand {
fn default() -> Self {
Self::All
Self::Default
}
}

Expand Down Expand Up @@ -60,10 +65,19 @@ impl Default for DocOpts {

impl Subcommand {
pub fn execute(self) -> Result<()> {
let installed = Installed::new()?;
print_metadata()?;

let start = Instant::now();

match self {
Subcommand::All => {
Subcommand::Default => {
crate::fmt::rust::run()?;
crate::clippy::run()?;
crate::test::run()?;
crate::doc::run(DocOpts::default())?;
}
Subcommand::Ci => {
let installed = Installed::new()?;
crate::fmt::rust::run()?;
if installed.black {
crate::fmt::python::run()?;
Expand All @@ -78,6 +92,7 @@ impl Subcommand {
} else {
Installed::warn_nox()
};
crate::llvm_cov::run(CoverageOpts::default())?;
installed.assert()?
}

Expand All @@ -92,21 +107,36 @@ impl Subcommand {
Subcommand::Test => crate::test::run()?,
};

let dt = start.elapsed();
println!("\nFinished program in {} s.", dt.as_secs());

Ok(())
}
}

pub fn run(command: &mut Command) -> Result<()> {
println!("Running: {}", format_command(command));
let status = command.spawn()?.wait()?;

let output = command
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?
.wait_with_output()?;

// io::stdout().write_all(&output.stdout).unwrap();
// io::stdout().write_all(&output.stderr).unwrap();

ensure! {
status.success(),
"process did not run successfully ({exit}): {command}",
exit = match status.code() {
output.status.success(),
"process did not run successfully ({exit}): {command}/n {out} {err}",
exit = match output.status.code() {
Some(code) => format!("exit code {}", code),
None => "terminated by signal".into(),
},
command = format_command(command),
out = String::from_utf8_lossy(&output.stdout),
err = String::from_utf8_lossy(&output.stderr)

};
Ok(())
}
Expand Down
4 changes: 1 addition & 3 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ pub mod test;
pub mod utils;

fn main() -> anyhow::Result<()> {
utils::print_metadata()?;

// Avoid spewing backtraces all over the command line
// For some reason this is automatically enabled on nightly compilers...
std::env::set_var("RUST_LIB_BACKTRACE", "0");

match cli::Subcommand::from_args_safe() {
Ok(c) => c.execute()?,
Err(e) if e.kind == MissingArgumentOrSubcommand => cli::Subcommand::All.execute()?,
Err(e) if e.kind == MissingArgumentOrSubcommand => cli::Subcommand::default().execute()?,
Err(e) => return Err(e)?,
}
Ok(())
Expand Down
43 changes: 32 additions & 11 deletions xtask/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,72 @@
use crate::cli;
use crate::cli::{self, MSRV};
use std::process::Command;

pub fn run() -> anyhow::Result<()> {
cli::run(
Command::new("cargo")
.arg("test")
.arg("--no-default-features")
.arg("--lib")
.arg("--tests"),
.arg("--no-default-features")
.arg("--tests")
.arg("--quiet"),
)?;

cli::run(
Command::new("cargo")
.arg("test")
.arg("--no-default-features")
.arg("--features=full")
.arg("--quiet"),
)?;

cli::run(
Command::new("cargo")
.arg("test")
.arg("--no-default-features")
.arg("--features=full"),
.arg("--features=abi3,full")
.arg("--quiet"),
)?;

// Install 1.48 for testing msrv
// If the MSRV toolchain is not installed, this will install it
cli::run(
Command::new("rustup")
.arg("toolchain")
.arg("install")
.arg("1.48"),
.arg(MSRV),
)?;

// Test msrv
// Test MSRV
cli::run(
Command::new("cargo")
.arg("+1.48")
.arg(format!("+{}", MSRV))
.arg("test")
.arg("--no-default-features")
.arg("--features=full,auto-initialize"),
.arg("--features=full,auto-initialize")
.arg("--quiet"),
)?;

cli::run(
Command::new("cargo")
.arg("+nightly")
.arg("test")
.arg("--no-default-features")
.arg("--features=full,nightly"),
.arg("--features=full,nightly")
.arg("--quiet"),
)?;

cli::run(
Command::new("cargo")
.arg("test")
.arg("--manifest-path=pyo3-ffi/Cargo.toml"),
.arg("--manifest-path=pyo3-ffi/Cargo.toml")
.arg("--quiet"),
)?;

cli::run(
Command::new("cargo")
.arg("test")
.arg("--no-default-features")
.arg("--manifest-path=pyo3-build-config/Cargo.toml")
.arg("--quiet"),
)?;

Ok(())
Expand Down

0 comments on commit 394d4d2

Please sign in to comment.