Skip to content

Commit 805f5c2

Browse files
committed
Add tests for cargo:rustc-check-cfg
1 parent d16631f commit 805f5c2

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

tests/testsuite/check_cfg.rs

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,174 @@ fn features_doc() {
458458
.with_stderr_contains(x!("rustdoc" => "values" of "feature" with "default" "f_a" "f_b"))
459459
.run();
460460
}
461+
462+
#[cargo_test]
463+
fn build_script_feedback() {
464+
if !is_nightly() {
465+
// rustc-check-cfg: is only availaible on nightly
466+
return;
467+
}
468+
469+
let p = project()
470+
.file(
471+
"Cargo.toml",
472+
r#"
473+
[package]
474+
name = "foo"
475+
version = "0.0.1"
476+
authors = []
477+
build = "build.rs"
478+
"#,
479+
)
480+
.file("src/main.rs", "fn main() {}")
481+
.file(
482+
"build.rs",
483+
r#"fn main() { println!("cargo:rustc-check-cfg=names(foo)"); }"#,
484+
)
485+
.build();
486+
487+
p.cargo("build -v -Zcheck-cfg=output")
488+
.masquerade_as_nightly_cargo()
489+
.with_stderr_contains(x!("rustc" => "names" of "foo"))
490+
.run();
491+
}
492+
493+
#[cargo_test]
494+
fn build_script_doc() {
495+
if !is_nightly() {
496+
// rustc-check-cfg: is only availaible on nightly
497+
return;
498+
}
499+
500+
let p = project()
501+
.file(
502+
"Cargo.toml",
503+
r#"
504+
[package]
505+
name = "foo"
506+
version = "0.0.1"
507+
authors = []
508+
build = "build.rs"
509+
"#,
510+
)
511+
.file("src/main.rs", "fn main() {}")
512+
.file(
513+
"build.rs",
514+
r#"fn main() { println!("cargo:rustc-check-cfg=names(foo)"); }"#,
515+
)
516+
.build();
517+
p.cargo("doc -v -Zcheck-cfg=output")
518+
.with_stderr_does_not_contain("rustc [..] --check-cfg [..]")
519+
.with_stderr_contains(x!("rustdoc" => "names" of "foo"))
520+
.with_stderr(
521+
"\
522+
[COMPILING] foo v0.0.1 ([CWD])
523+
[RUNNING] `rustc [..] build.rs [..]`
524+
[RUNNING] `[..]/build-script-build`
525+
[DOCUMENTING] foo [..]
526+
[RUNNING] `rustdoc [..] src/main.rs [..]
527+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
528+
)
529+
.masquerade_as_nightly_cargo()
530+
.run();
531+
}
532+
533+
#[cargo_test]
534+
fn build_script_override() {
535+
if !is_nightly() {
536+
// rustc-check-cfg: is only availaible on nightly
537+
return;
538+
}
539+
let target = cargo_test_support::rustc_host();
540+
541+
let p = project()
542+
.file(
543+
"Cargo.toml",
544+
r#"
545+
[project]
546+
name = "foo"
547+
version = "0.5.0"
548+
authors = []
549+
links = "a"
550+
build = "build.rs"
551+
"#,
552+
)
553+
.file("src/main.rs", "fn main() {}")
554+
.file("build.rs", "")
555+
.file(
556+
".cargo/config",
557+
&format!(
558+
r#"
559+
[target.{}.a]
560+
rustc-check-cfg = ["names(foo)"]
561+
"#,
562+
target
563+
),
564+
)
565+
.build();
566+
567+
p.cargo("build -v -Zcheck-cfg=output")
568+
.with_stderr_contains(x!("rustc" => "names" of "foo"))
569+
.masquerade_as_nightly_cargo()
570+
.run();
571+
}
572+
573+
#[cargo_test]
574+
fn build_script_test() {
575+
if !is_nightly() {
576+
// rustc-check-cfg: is only availaible on nightly
577+
return;
578+
}
579+
580+
let p = project()
581+
.file(
582+
"Cargo.toml",
583+
r#"
584+
[package]
585+
name = "foo"
586+
version = "0.0.1"
587+
authors = []
588+
build = "build.rs"
589+
"#,
590+
)
591+
.file(
592+
"build.rs",
593+
r#"fn main() {
594+
println!("cargo:rustc-check-cfg=names(foo)");
595+
println!("cargo:rustc-cfg=foo");
596+
}"#,
597+
)
598+
.file(
599+
"src/lib.rs",
600+
r#"
601+
///
602+
/// ```
603+
/// extern crate foo;
604+
///
605+
/// fn main() {
606+
/// foo::foo()
607+
/// }
608+
/// ```
609+
///
610+
#[cfg(foo)]
611+
pub fn foo() {}
612+
613+
#[cfg(foo)]
614+
#[test]
615+
fn test_foo() {
616+
foo()
617+
}
618+
"#,
619+
)
620+
.file("tests/test.rs", "#[cfg(foo)] #[test] fn test_bar() {}")
621+
.build();
622+
623+
p.cargo("test -v -Zcheck-cfg=output")
624+
.with_stderr_contains(x!("rustc" => "names" of "foo"))
625+
.with_stderr_contains(x!("rustdoc" => "names" of "foo"))
626+
.with_stdout_contains("test test_foo ... ok")
627+
.with_stdout_contains("test test_bar ... ok")
628+
.with_stdout_contains_n("test [..] ... ok", 3)
629+
.masquerade_as_nightly_cargo()
630+
.run();
631+
}

0 commit comments

Comments
 (0)