-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
cargo_warnings
config to control the use of the cargo warning i…
…nstruction (#917) * fix some instances of broken cargo:warning instruction * allow disabling cargo warnings for compilation * comply with MSRV * allow dead_code to make ci pass * make print thread optional * simplify warnings using macros * add a test case * this line in the docstring is no longer true * apply review suggestions * write warnings to buffered stdout * add proper output tests * reduce import diff and always print stdout in tests * correct println * add an output abstraction * hopefully make the tests work on win32 * msvc-compatible warnings * turns out we can't capture warnings for msvc... * fix unconditional metadata output * skip warnings_on test for msvc * Update src/lib.rs Co-authored-by: Jiahao XU <Jiahao_XU@outlook.com> * Update src/lib.rs Co-authored-by: Jiahao XU <Jiahao_XU@outlook.com> * Update src/lib.rs Co-authored-by: Jiahao XU <Jiahao_XU@outlook.com> * Update src/lib.rs Co-authored-by: Jiahao XU <Jiahao_XU@outlook.com> --------- Co-authored-by: Jiahao XU <Jiahao_XU@outlook.com>
- Loading branch information
1 parent
385b43f
commit 8cf5455
Showing
5 changed files
with
306 additions
and
82 deletions.
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
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 @@ | ||
#error "if you see this, cargo_warnings(false) didn't do its job" |
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,3 @@ | ||
/* just an empty file */ | ||
|
||
void dummy(void) {} |
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,68 @@ | ||
use std::fs; | ||
use std::path::PathBuf; | ||
|
||
#[test] | ||
fn cargo_warnings_on() { | ||
if env!("TEST_WARNINGS_ON") == "0" { | ||
// in some cases we don't catch compiler warnings and turn them into cargo | ||
// instructions. | ||
return; | ||
} | ||
let (stdout, stderr) = load_output("warnings-on"); | ||
assert!(stderr.is_empty()); | ||
assert!(stdout.contains("cargo:warning=")); | ||
} | ||
|
||
#[test] | ||
fn cargo_warnings_off() { | ||
let (stdout, stderr) = load_output("warnings-off"); | ||
assert!(stderr.is_empty()); | ||
assert!(!stdout.contains("cargo:warning=")); | ||
} | ||
|
||
#[test] | ||
fn cargo_metadata_on() { | ||
let (stdout, stderr) = load_output("metadata-on"); | ||
assert!(stderr.is_empty()); | ||
assert!(stdout.contains("cargo:rustc-link-lib=")); | ||
assert!(stdout.contains("cargo:rustc-link-search=")); | ||
} | ||
|
||
#[test] | ||
fn cargo_metadata_off() { | ||
let (stdout, stderr) = load_output("metadata-off"); | ||
assert!(stderr.is_empty()); | ||
|
||
// most of the instructions aren't currently used | ||
const INSTRUCTIONS: &[&str] = &[ | ||
"cargo:rerun-if-changed=", | ||
"cargo:rerun-if-env-changed=", | ||
"cargo:rustc-cdylib-link-arg=", | ||
"cargo:rustc-cfg=", | ||
"cargo:rustc-env=", | ||
"cargo:rustc-flags=", | ||
"cargo:rustc-link-arg-benches=", | ||
"cargo:rustc-link-arg-bin=", | ||
"cargo:rustc-link-arg-bins=", | ||
"cargo:rustc-link-arg-examples=", | ||
"cargo:rustc-link-arg-tests=", | ||
"cargo:rustc-link-arg=", | ||
"cargo:rustc-link-lib=", | ||
"cargo:rustc-link-search=", | ||
]; | ||
for instr in INSTRUCTIONS { | ||
assert!(!stdout.contains(instr), "instruction present: {}", instr); | ||
} | ||
} | ||
|
||
#[track_caller] | ||
fn load_output(action: &str) -> (String, String) { | ||
// these files are written by the `run_forked_capture_output` function in the | ||
// build script. | ||
let action_dir = PathBuf::from(env!("OUT_DIR")).join(action); | ||
let stdout = fs::read_to_string(action_dir.join("stdout")).unwrap(); | ||
let stderr = fs::read_to_string(action_dir.join("stderr")).unwrap(); | ||
println!("compile stdout: {:?}", stdout); | ||
println!("compile stderr: {:?}", stderr); | ||
(stdout, stderr) | ||
} |
Oops, something went wrong.