Skip to content

Commit

Permalink
Improve warning for expandtest
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jan 11, 2021
1 parent 3a3a3d8 commit 5134e49
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
1 change: 1 addition & 0 deletions tests/expand/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ publish = false
[dev-dependencies]
pin-project-lite = { path = "../.." }
macrotest = "1.0.6"
rustversion = "1"
9 changes: 2 additions & 7 deletions tests/expand/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@ fn main() {
return;
}

let is_ci = env::var_os("CI").is_some();
if is_ci {
println!("cargo:rustc-cfg=ci");
}

let cargo = &*env::var("CARGO").unwrap_or_else(|_| "cargo".into());
if is_ci || has_command(&[cargo, "expand"]) && has_command(&[cargo, "fmt"]) {
println!("cargo:rustc-cfg=expandtest");
if !has_command(&[cargo, "expand"]) || !has_command(&[cargo, "fmt"]) {
println!("cargo:warning=rustfmt or cargo-expand not found, skipping expandtest");
}
}

Expand Down
38 changes: 30 additions & 8 deletions tests/expand/tests/expandtest.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
#![warn(rust_2018_idioms, single_use_lifetimes)]

#[cfg(not(ci))]
use macrotest::expand;
#[cfg(ci)]
use macrotest::expand_without_refresh as expand;
use std::env;
use std::{
env,
process::{Command, ExitStatus, Stdio},
};

#[cfg_attr(not(expandtest), ignore)]
#[rustversion::attr(not(nightly), ignore)]
#[test]
fn expandtest() {
if !cfg!(ci) {
let is_ci = env::var_os("CI").is_some();
let cargo = &*env::var("CARGO").unwrap_or_else(|_| "cargo".into());
if !has_command(&[cargo, "expand"]) || !has_command(&[cargo, "fmt"]) {
if is_ci {
panic!("expandtest requires rustfmt and cargo-expand")
}
return;
}

if is_ci {
macrotest::expand_without_refresh("tests/expand/*.rs");
} else {
env::set_var("MACROTEST", "overwrite");
macrotest::expand("tests/expand/*.rs");
}
}

expand("tests/expand/*.rs");
fn has_command(command: &[&str]) -> bool {
Command::new(command[0])
.args(&command[1..])
.arg("--version")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.as_ref()
.map(ExitStatus::success)
.unwrap_or(false)
}

0 comments on commit 5134e49

Please sign in to comment.