Skip to content

Commit f2e8d6e

Browse files
committed
test: cargo::error build script directive.
1 parent 9e152bb commit f2e8d6e

File tree

2 files changed

+57
-39
lines changed

2 files changed

+57
-39
lines changed

tests/testsuite/build_script.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3846,7 +3846,7 @@ fn panic_abort_with_build_scripts() {
38463846
}
38473847

38483848
#[cargo_test]
3849-
fn warnings_emitted() {
3849+
fn errors_and_warnings_emitted() {
38503850
let p = project()
38513851
.file(
38523852
"Cargo.toml",
@@ -3866,27 +3866,29 @@ fn warnings_emitted() {
38663866
fn main() {
38673867
println!("cargo::warning=foo");
38683868
println!("cargo::warning=bar");
3869+
println!("cargo::error=foo err");
3870+
println!("cargo::error=bar err");
38693871
}
38703872
"#,
38713873
)
38723874
.build();
38733875

38743876
p.cargo("build -v")
3877+
.with_status(101)
38753878
.with_stderr_data(str![[r#"
38763879
[COMPILING] foo v0.5.0 ([ROOT]/foo)
38773880
[RUNNING] `rustc --crate-name build_script_build [..]`
38783881
[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build`
3879-
[WARNING] [email protected]: foo
3880-
[WARNING] [email protected]: bar
3881-
[RUNNING] `rustc --crate-name foo [..]`
3882-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
3882+
[ERROR] invalid output in build script of `foo v0.5.0 ([ROOT]/foo)`: `cargo::error=foo err`
3883+
Unknown key: `error`.
3884+
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script for more information about build script outputs.
38833885
38843886
"#]])
38853887
.run();
38863888
}
38873889

38883890
#[cargo_test]
3889-
fn warnings_emitted_when_build_script_panics() {
3891+
fn errors_and_warnings_emitted_when_build_script_panics() {
38903892
let p = project()
38913893
.file(
38923894
"Cargo.toml",
@@ -3906,6 +3908,8 @@ fn warnings_emitted_when_build_script_panics() {
39063908
fn main() {
39073909
println!("cargo::warning=foo");
39083910
println!("cargo::warning=bar");
3911+
println!("cargo::error=foo err");
3912+
println!("cargo::error=bar err");
39093913
panic!();
39103914
}
39113915
"#,
@@ -3925,14 +3929,16 @@ fn warnings_emitted_when_build_script_panics() {
39253929
}
39263930

39273931
#[cargo_test]
3928-
fn warnings_hidden_for_upstream() {
3932+
fn errors_and_warnings_hidden_for_upstream() {
39293933
Package::new("bar", "0.1.0")
39303934
.file(
39313935
"build.rs",
39323936
r#"
39333937
fn main() {
39343938
println!("cargo::warning=foo");
39353939
println!("cargo::warning=bar");
3940+
println!("cargo::error=foo err");
3941+
println!("cargo::error=bar err");
39363942
}
39373943
"#,
39383944
)
@@ -3968,6 +3974,7 @@ fn warnings_hidden_for_upstream() {
39683974
.build();
39693975

39703976
p.cargo("build -v")
3977+
.with_status(101)
39713978
.with_stderr_data(str![[r#"
39723979
[UPDATING] `dummy-registry` index
39733980
[LOCKING] 2 packages to latest compatible versions
@@ -3976,24 +3983,25 @@ fn warnings_hidden_for_upstream() {
39763983
[COMPILING] bar v0.1.0
39773984
[RUNNING] `rustc --crate-name build_script_build [..]`
39783985
[RUNNING] `[ROOT]/foo/target/debug/build/bar-[HASH]/build-script-build`
3979-
[RUNNING] `rustc --crate-name bar [..]`
3980-
[COMPILING] foo v0.5.0 ([ROOT]/foo)
3981-
[RUNNING] `rustc --crate-name foo [..]`
3982-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
3986+
[ERROR] invalid output in build script of `bar v0.1.0`: `cargo::error=foo err`
3987+
Unknown key: `error`.
3988+
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script for more information about build script outputs.
39833989
39843990
"#]])
39853991
.run();
39863992
}
39873993

39883994
#[cargo_test]
3989-
fn warnings_printed_on_vv() {
3995+
fn errors_and_warnings_printed_on_vv() {
39903996
Package::new("bar", "0.1.0")
39913997
.file(
39923998
"build.rs",
39933999
r#"
39944000
fn main() {
39954001
println!("cargo::warning=foo");
39964002
println!("cargo::warning=bar");
4003+
println!("cargo::error=foo err");
4004+
println!("cargo::error=bar err");
39974005
}
39984006
"#,
39994007
)
@@ -4029,6 +4037,7 @@ fn warnings_printed_on_vv() {
40294037
.build();
40304038

40314039
p.cargo("build -vv")
4040+
.with_status(101)
40324041
.with_stderr_data(str![[r#"
40334042
[UPDATING] `dummy-registry` index
40344043
[LOCKING] 2 packages to latest compatible versions
@@ -4037,12 +4046,9 @@ fn warnings_printed_on_vv() {
40374046
[COMPILING] bar v0.1.0
40384047
[RUNNING] `[..] rustc --crate-name build_script_build [..]`
40394048
[RUNNING] `[..] [ROOT]/foo/target/debug/build/bar-[HASH]/build-script-build`
4040-
[WARNING] [email protected]: foo
4041-
[WARNING] [email protected]: bar
4042-
[RUNNING] `[..] rustc --crate-name bar [..]`
4043-
[COMPILING] foo v0.5.0 ([ROOT]/foo)
4044-
[RUNNING] `[..] rustc --crate-name foo [..]`
4045-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
4049+
[ERROR] invalid output in build script of `bar v0.1.0`: `cargo::error=foo err`
4050+
Unknown key: `error`.
4051+
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script for more information about build script outputs.
40464052
40474053
"#]])
40484054
.run();

tests/testsuite/warn_on_failure.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
//! Tests for whether or not warnings are displayed for build scripts.
1+
//! Tests for whether or not errors and warnings are displayed for build scripts.
22
33
use cargo_test_support::prelude::*;
44
use cargo_test_support::registry::Package;
55
use cargo_test_support::{project, str, Project};
66

7+
static ERROR: &str = "Hello! I'm an error. :)";
78
static WARNING1: &str = "Hello! I'm a warning. :)";
89
static WARNING2: &str = "And one more!";
910

@@ -30,9 +31,10 @@ fn make_lib(lib_src: &str) {
3031
println!("hidden stdout");
3132
write!(&mut ::std::io::stderr(), "hidden stderr");
3233
println!("cargo::warning={{}}", "{}");
34+
println!("cargo::error={{}}", "{}");
3335
}}
3436
"#,
35-
WARNING1, WARNING2
37+
WARNING1, WARNING2, ERROR
3638
),
3739
)
3840
.file("src/lib.rs", &format!("fn f() {{ {} }}", lib_src))
@@ -59,58 +61,68 @@ fn make_upstream(main_src: &str) -> Project {
5961
}
6062

6163
#[cargo_test]
62-
fn no_warning_on_success() {
64+
fn no_error_or_warning_on_success() {
6365
make_lib("");
6466
let upstream = make_upstream("");
6567
upstream
6668
.cargo("build")
69+
.with_status(101)
6770
.with_stderr_data(str![[r#"
6871
[UPDATING] `dummy-registry` index
6972
[LOCKING] 2 packages to latest compatible versions
7073
[DOWNLOADING] crates ...
7174
[DOWNLOADED] bar v0.0.1 (registry `dummy-registry`)
7275
[COMPILING] bar v0.0.1
73-
[COMPILING] foo v0.0.1 ([ROOT]/foo)
74-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
76+
[ERROR] invalid output in build script of `bar v0.0.1`: `cargo::error=Hello! I'm an error. :)`
77+
Unknown key: `error`.
78+
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script for more information about build script outputs.
7579
7680
"#]])
7781
.run();
7882
}
7983

8084
#[allow(deprecated)]
8185
#[cargo_test]
82-
fn no_warning_on_bin_failure() {
86+
fn no_error_or_warning_on_bin_failure() {
8387
make_lib("");
8488
let upstream = make_upstream("hi()");
8589
upstream
8690
.cargo("build")
8791
.with_status(101)
8892
.with_stdout_does_not_contain("hidden stdout")
89-
.with_stderr_does_not_contain("hidden stderr")
90-
.with_stderr_does_not_contain(&format!("[WARNING] {}", WARNING1))
91-
.with_stderr_does_not_contain(&format!("[WARNING] {}", WARNING2))
92-
.with_stderr_contains("[UPDATING] `[..]` index")
93-
.with_stderr_contains("[DOWNLOADED] bar v0.0.1 ([..])")
94-
.with_stderr_contains("[COMPILING] bar v0.0.1")
95-
.with_stderr_contains("[COMPILING] foo v0.0.1 ([..])")
93+
.with_stderr_data(str![[r#"
94+
[UPDATING] `dummy-registry` index
95+
[LOCKING] 2 packages to latest compatible versions
96+
[DOWNLOADING] crates ...
97+
[DOWNLOADED] bar v0.0.1 (registry `dummy-registry`)
98+
[COMPILING] bar v0.0.1
99+
[ERROR] invalid output in build script of `bar v0.0.1`: `cargo::error=Hello! I'm an error. :)`
100+
Unknown key: `error`.
101+
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script for more information about build script outputs.
102+
103+
"#]])
96104
.run();
97105
}
98106

99107
#[allow(deprecated)]
100108
#[cargo_test]
101-
fn warning_on_lib_failure() {
109+
fn errors_and_warnings_on_lib_failure() {
102110
make_lib("err()");
103111
let upstream = make_upstream("");
104112
upstream
105113
.cargo("build")
106114
.with_status(101)
107115
.with_stdout_does_not_contain("hidden stdout")
108-
.with_stderr_does_not_contain("hidden stderr")
109-
.with_stderr_does_not_contain("[COMPILING] foo v0.0.1 ([..])")
110-
.with_stderr_contains("[UPDATING] `[..]` index")
111-
.with_stderr_contains("[DOWNLOADED] bar v0.0.1 ([..])")
112-
.with_stderr_contains("[COMPILING] bar v0.0.1")
113-
.with_stderr_contains(&format!("[WARNING] [email protected]: {}", WARNING1))
114-
.with_stderr_contains(&format!("[WARNING] [email protected]: {}", WARNING2))
116+
.with_stderr_data(str![[r#"
117+
[UPDATING] `dummy-registry` index
118+
[LOCKING] 2 packages to latest compatible versions
119+
[DOWNLOADING] crates ...
120+
[DOWNLOADED] bar v0.0.1 (registry `dummy-registry`)
121+
[COMPILING] bar v0.0.1
122+
[ERROR] invalid output in build script of `bar v0.0.1`: `cargo::error=Hello! I'm an error. :)`
123+
Unknown key: `error`.
124+
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script for more information about build script outputs.
125+
126+
"#]])
115127
.run();
116128
}

0 commit comments

Comments
 (0)