Skip to content

Commit 6b48430

Browse files
authored
Add regression test for build.rs generated features (#3207)
1 parent a4f15f5 commit 6b48430

File tree

7 files changed

+140
-28
lines changed

7 files changed

+140
-28
lines changed

cargo/cargo_build_script_runner/lib.rs

+71-28
Original file line numberDiff line numberDiff line change
@@ -231,31 +231,10 @@ mod tests {
231231
use super::*;
232232
use std::io::Cursor;
233233

234-
#[test]
235-
fn test_from_read_buffer_to_env_and_flags() {
236-
let buff = Cursor::new(
237-
"
238-
cargo::rustc-link-lib=sdfsdf
239-
cargo::rustc-env=FOO=BAR
240-
cargo::rustc-link-search=/some/absolute/path/bleh
241-
cargo::rustc-env=BAR=FOO
242-
cargo::rustc-flags=-Lblah
243-
cargo::rerun-if-changed=ignored
244-
cargo::rustc-cfg=feature=awesome
245-
cargo::version=123
246-
cargo::version_number=1010107f
247-
cargo::include_path=/some/absolute/path/include
248-
cargo::rustc-env=SOME_PATH=/some/absolute/path/beep
249-
cargo::rustc-link-arg=-weak_framework
250-
cargo::rustc-link-arg=Metal
251-
cargo::rustc-env=no_trailing_newline=true
252-
non-cargo-prefixes::are-ignored=true
253-
non-assignment-instructions-are-ignored
254-
cargo:rustc-env=old_cargo_colon_prefix_works=true",
255-
);
234+
fn from_read_buffer_to_env_and_flags_test_impl(buff: Cursor<&str>) {
256235
let reader = BufReader::new(buff);
257236
let result = BuildScriptOutput::outputs_from_reader(reader);
258-
assert_eq!(result.len(), 14);
237+
assert_eq!(result.len(), 13);
259238
assert_eq!(result[0], BuildScriptOutput::LinkLib("sdfsdf".to_owned()));
260239
assert_eq!(result[1], BuildScriptOutput::Env("FOO=BAR".to_owned()));
261240
assert_eq!(
@@ -289,17 +268,13 @@ cargo:rustc-env=old_cargo_colon_prefix_works=true",
289268
result[12],
290269
BuildScriptOutput::Env("no_trailing_newline=true".to_owned())
291270
);
292-
assert_eq!(
293-
result[13],
294-
BuildScriptOutput::Env("old_cargo_colon_prefix_works=true".to_owned())
295-
);
296271
assert_eq!(
297272
BuildScriptOutput::outputs_to_dep_env(&result, "ssh2", "/some/absolute/path"),
298273
"DEP_SSH2_VERSION=123\nDEP_SSH2_VERSION_NUMBER=1010107f\nDEP_SSH2_INCLUDE_PATH=${pwd}/include".to_owned()
299274
);
300275
assert_eq!(
301276
BuildScriptOutput::outputs_to_env(&result, "/some/absolute/path"),
302-
"FOO=BAR\nBAR=FOO\nSOME_PATH=${pwd}/beep\nno_trailing_newline=true\nold_cargo_colon_prefix_works=true".to_owned()
277+
"FOO=BAR\nBAR=FOO\nSOME_PATH=${pwd}/beep\nno_trailing_newline=true".to_owned()
303278
);
304279
assert_eq!(
305280
BuildScriptOutput::outputs_to_flags(&result, "/some/absolute/path"),
@@ -315,13 +290,81 @@ cargo:rustc-env=old_cargo_colon_prefix_works=true",
315290
);
316291
}
317292

293+
#[test]
294+
fn test_from_read_buffer_to_env_and_flags() {
295+
let buff = Cursor::new(
296+
"
297+
cargo::rustc-link-lib=sdfsdf
298+
cargo::rustc-env=FOO=BAR
299+
cargo::rustc-link-search=/some/absolute/path/bleh
300+
cargo::rustc-env=BAR=FOO
301+
cargo::rustc-flags=-Lblah
302+
cargo::rerun-if-changed=ignored
303+
cargo::rustc-cfg=feature=awesome
304+
cargo::version=123
305+
cargo::version_number=1010107f
306+
cargo::include_path=/some/absolute/path/include
307+
cargo::rustc-env=SOME_PATH=/some/absolute/path/beep
308+
cargo::rustc-link-arg=-weak_framework
309+
cargo::rustc-link-arg=Metal
310+
cargo::rustc-env=no_trailing_newline=true
311+
non-cargo-prefixes::are-ignored=true
312+
non-assignment-instructions-are-ignored",
313+
);
314+
from_read_buffer_to_env_and_flags_test_impl(buff);
315+
}
316+
317+
/// Demonstrate that the old style single colon flags are all parsable
318+
#[test]
319+
fn test_legacy_from_read_buffer_to_env_and_flags() {
320+
let buff = Cursor::new(
321+
"
322+
cargo:rustc-link-lib=sdfsdf
323+
cargo:rustc-env=FOO=BAR
324+
cargo:rustc-link-search=/some/absolute/path/bleh
325+
cargo:rustc-env=BAR=FOO
326+
cargo:rustc-flags=-Lblah
327+
cargo:rerun-if-changed=ignored
328+
cargo:rustc-cfg=feature=awesome
329+
cargo:version=123
330+
cargo:version_number=1010107f
331+
cargo:include_path=/some/absolute/path/include
332+
cargo:rustc-env=SOME_PATH=/some/absolute/path/beep
333+
cargo:rustc-link-arg=-weak_framework
334+
cargo:rustc-link-arg=Metal
335+
cargo:rustc-env=no_trailing_newline=true
336+
non-cargo-prefixes:are-ignored=true
337+
non-assignment-instructions-are-ignored",
338+
);
339+
from_read_buffer_to_env_and_flags_test_impl(buff);
340+
}
341+
318342
#[test]
319343
fn invalid_utf8() {
320344
let buff = Cursor::new(
321345
b"
322346
cargo::rustc-env=valid1=1
323347
cargo::rustc-env=invalid=\xc3\x28
324348
cargo::rustc-env=valid2=2
349+
",
350+
);
351+
let reader = BufReader::new(buff);
352+
let result = BuildScriptOutput::outputs_from_reader(reader);
353+
assert_eq!(result.len(), 2);
354+
assert_eq!(
355+
&BuildScriptOutput::outputs_to_env(&result, "/some/absolute/path"),
356+
"valid1=1\nvalid2=2"
357+
);
358+
}
359+
360+
/// Demonstrate that the old style single colon flags are all parsable
361+
#[test]
362+
fn invalid_utf8_legacy() {
363+
let buff = Cursor::new(
364+
b"
365+
cargo:rustc-env=valid1=1
366+
cargo:rustc-env=invalid=\xc3\x28
367+
cargo:rustc-env=valid2=2
325368
",
326369
);
327370
let reader = BufReader::new(buff);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
load("//cargo:defs.bzl", "cargo_build_script")
2+
load("//rust:defs.bzl", "rust_library", "rust_test")
3+
4+
cargo_build_script(
5+
name = "build_rs",
6+
srcs = ["build.rs"],
7+
edition = "2021",
8+
)
9+
10+
rust_library(
11+
name = "lib",
12+
srcs = ["lib.rs"],
13+
edition = "2021",
14+
deps = [":build_rs"],
15+
)
16+
17+
rust_test(
18+
name = "test",
19+
srcs = ["test.rs"],
20+
edition = "2018",
21+
deps = [":lib"],
22+
)
23+
24+
rust_library(
25+
name = "transitive",
26+
srcs = ["transitive.rs"],
27+
edition = "2021",
28+
deps = [":lib"],
29+
)
30+
31+
rust_test(
32+
name = "transitive_test",
33+
srcs = ["transitive_test.rs"],
34+
edition = "2018",
35+
deps = [":transitive"],
36+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//! A build.rs script which produces a feature for the consuming crate.
2+
3+
fn main() {
4+
println!("cargo:rustc-cfg=build_rs_generated_feature");
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//! Consumer of build.rs generated feature.
2+
3+
#[cfg(build_rs_generated_feature)]
4+
pub const DATA: &str = "La-Li-Lu-Le-Lo";
5+
6+
#[cfg(not(build_rs_generated_feature))]
7+
pub const DATA: &str = "hello-world";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[test]
2+
pub fn test_data() {
3+
assert_eq!(
4+
"La-Li-Lu-Le-Lo",
5+
lib::DATA,
6+
"The `lib` crate was not compiled with the feature produced from it's `build.rs`"
7+
);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//! A consumer of the `lib` crate which relies on a `build.rs` produced feature.
2+
3+
pub fn data() -> String {
4+
String::from(lib::DATA)
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[test]
2+
pub fn test_data() {
3+
assert_eq!(
4+
"La-Li-Lu-Le-Lo",
5+
transitive::data(),
6+
"A consumer of the `lib` crate consumed this crate without the `build.rs` generated feature."
7+
);
8+
}

0 commit comments

Comments
 (0)