Skip to content

Commit 9eeffbf

Browse files
committed
Add some syntax tests
Signed-off-by: hi-rustin <[email protected]>
1 parent bd230e8 commit 9eeffbf

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ impl BuildOutput {
952952
}
953953
_ => bail!(
954954
"invalid output in {whence}: `{line}`\n\
955-
Unknown key: `{key}`\n\
955+
Unknown key: `{key}`.\n\
956956
{DOCS_LINK_SUGGESTION}",
957957
),
958958
}

tests/testsuite/build_script.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5202,3 +5202,119 @@ fn custom_build_closes_stdin() {
52025202
.build();
52035203
p.cargo("build").run();
52045204
}
5205+
5206+
#[cargo_test]
5207+
fn test_old_syntax() {
5208+
let p = project()
5209+
.file(
5210+
"Cargo.toml",
5211+
r#"
5212+
[package]
5213+
name = "foo"
5214+
version = "0.0.1"
5215+
authors = []
5216+
build = "build.rs"
5217+
"#,
5218+
)
5219+
.file(
5220+
"src/main.rs",
5221+
r#"
5222+
const FOO: &'static str = env!("FOO");
5223+
fn main() {
5224+
println!("{}", FOO);
5225+
}
5226+
"#,
5227+
)
5228+
.file(
5229+
"build.rs",
5230+
r#"fn main() {
5231+
println!("cargo:rustc-env=FOO=foo");
5232+
println!("cargo:foo=foo");
5233+
}"#,
5234+
)
5235+
.build();
5236+
p.cargo("build -v").run();
5237+
p.cargo("run -v").with_stdout("foo\n").run();
5238+
}
5239+
5240+
#[cargo_test]
5241+
fn test_invalid_old_syntax() {
5242+
// Unexpected metadata value.
5243+
let p = project()
5244+
.file("src/lib.rs", "")
5245+
.file(
5246+
"build.rs",
5247+
r#"
5248+
fn main() {
5249+
println!("cargo:foo");
5250+
}
5251+
"#,
5252+
)
5253+
.build();
5254+
p.cargo("build")
5255+
.with_status(101)
5256+
.with_stderr(
5257+
"\
5258+
[COMPILING] foo [..]
5259+
error: invalid output in build script of `foo v0.0.1 ([ROOT]/foo)`: `cargo:foo`
5260+
Expected a line with `cargo:KEY=VALUE` with an `=` character, but none was found.
5261+
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
5262+
for more information about build script outputs.
5263+
",
5264+
)
5265+
.run();
5266+
}
5267+
5268+
#[cargo_test]
5269+
fn test_invalid_new_syntax() {
5270+
// Unexpected metadata value.
5271+
let p = project()
5272+
.file("src/lib.rs", "")
5273+
.file(
5274+
"build.rs",
5275+
r#"
5276+
fn main() {
5277+
println!("cargo::metadata=foo");
5278+
}
5279+
"#,
5280+
)
5281+
.build();
5282+
5283+
p.cargo("build")
5284+
.with_status(101)
5285+
.with_stderr(
5286+
"\
5287+
[COMPILING] foo [..]
5288+
error: invalid output in build script of `foo v0.0.1 ([ROOT]/foo)`: `cargo::metadata=foo`
5289+
Expected a line with `cargo::metadata=KEY=VALUE` with an `=` character, but none was found.
5290+
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
5291+
for more information about build script outputs.
5292+
",
5293+
)
5294+
.run();
5295+
// `cargo::` can not be used with the unknown key.
5296+
let p = project()
5297+
.file("src/lib.rs", "")
5298+
.file(
5299+
"build.rs",
5300+
r#"
5301+
fn main() {
5302+
println!("cargo::foo=bar");
5303+
}
5304+
"#,
5305+
)
5306+
.build();
5307+
5308+
p.cargo("build")
5309+
.with_status(101)
5310+
.with_stderr(
5311+
"\
5312+
[COMPILING] foo [..]
5313+
error: invalid output in build script of `foo v0.0.1 ([ROOT]/foo)`: `cargo::foo=bar`
5314+
Unknown key: `foo`.
5315+
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
5316+
for more information about build script outputs.
5317+
",
5318+
)
5319+
.run();
5320+
}

0 commit comments

Comments
 (0)