Skip to content

Commit d3cfef0

Browse files
committed
Add tests for check-cfg config of unexpected_cfgs lint
1 parent dc96b95 commit d3cfef0

File tree

1 file changed

+325
-0
lines changed

1 file changed

+325
-0
lines changed

tests/testsuite/check_cfg.rs

Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,328 @@ fn build_script_test() {
496496
.with_stdout_contains_n("test [..] ... ok", 3)
497497
.run();
498498
}
499+
500+
#[cargo_test(>=1.79, reason = "--check-cfg was stabilized in Rust 1.79")]
501+
fn config_simple() {
502+
let p = project()
503+
.file(
504+
"Cargo.toml",
505+
r#"
506+
[package]
507+
name = "foo"
508+
version = "0.1.0"
509+
edition = "2015"
510+
511+
[lints.rust]
512+
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(has_foo)", "cfg(has_bar, values(\"yes\", \"no\"))"] }
513+
"#,
514+
)
515+
.file("src/main.rs", "fn main() {}")
516+
.build();
517+
518+
p.cargo("check -v")
519+
.with_stderr_contains(x!("rustc" => "cfg" of "has_foo"))
520+
.with_stderr_contains(x!("rustc" => "cfg" of "has_bar" with "yes" "no"))
521+
.run();
522+
}
523+
524+
#[cargo_test(>=1.79, reason = "--check-cfg was stabilized in Rust 1.79")]
525+
fn config_workspace() {
526+
let p = project()
527+
.file(
528+
"Cargo.toml",
529+
r#"
530+
[workspace]
531+
members = ["foo/"]
532+
533+
[workspace.lints.rust]
534+
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(has_foo)"] }
535+
"#,
536+
)
537+
.file(
538+
"foo/Cargo.toml",
539+
r#"
540+
[package]
541+
name = "foo"
542+
version = "0.1.0"
543+
edition = "2015"
544+
545+
[lints]
546+
workspace = true
547+
"#,
548+
)
549+
.file("foo/src/main.rs", "fn main() {}")
550+
.build();
551+
552+
p.cargo("check -v")
553+
.with_stderr_contains(x!("rustc" => "cfg" of "has_foo"))
554+
.with_stderr_does_not_contain("unexpected_cfgs")
555+
.run();
556+
}
557+
558+
#[cargo_test(>=1.79, reason = "--check-cfg was stabilized in Rust 1.79")]
559+
fn config_workspace_not_inherited() {
560+
let p = project()
561+
.file(
562+
"Cargo.toml",
563+
r#"
564+
[workspace]
565+
members = ["foo/"]
566+
567+
[workspace.lints.rust]
568+
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(has_foo)"] }
569+
"#,
570+
)
571+
.file(
572+
"foo/Cargo.toml",
573+
r#"
574+
[package]
575+
name = "foo"
576+
version = "0.1.0"
577+
edition = "2015"
578+
"#,
579+
)
580+
.file("foo/src/main.rs", "fn main() {}")
581+
.build();
582+
583+
p.cargo("check -v")
584+
.with_stderr_does_not_contain(x!("rustc" => "cfg" of "has_foo"))
585+
.with_stderr_does_not_contain("unexpected_cfgs")
586+
.run();
587+
}
588+
589+
#[cargo_test(>=1.79, reason = "--check-cfg was stabilized in Rust 1.79")]
590+
fn config_invalid_position() {
591+
let p = project()
592+
.file(
593+
"Cargo.toml",
594+
r#"
595+
[package]
596+
name = "foo"
597+
version = "0.1.0"
598+
edition = "2015"
599+
600+
[lints.rust]
601+
use_bracket = { level = "warn", check-cfg = ["cfg(has_foo)"] }
602+
"#,
603+
)
604+
.file("src/main.rs", "fn main() {}")
605+
.build();
606+
607+
p.cargo("check -v")
608+
.with_stderr_contains("[..]unused manifest key: `lints.rust.use_bracket.check-cfg`[..]")
609+
.with_stderr_does_not_contain(x!("rustc" => "cfg" of "has_foo"))
610+
.run();
611+
}
612+
613+
#[cargo_test(>=1.79, reason = "--check-cfg was stabilized in Rust 1.79")]
614+
fn config_invalid_empty() {
615+
let p = project()
616+
.file(
617+
"Cargo.toml",
618+
r#"
619+
[package]
620+
name = "foo"
621+
version = "0.1.0"
622+
edition = "2015"
623+
624+
[lints.rust]
625+
unexpected_cfgs = { }
626+
"#,
627+
)
628+
.file("src/main.rs", "fn main() {}")
629+
.build();
630+
631+
p.cargo("check")
632+
.with_stderr_contains("[..]missing field `level`[..]")
633+
.run_expect_error();
634+
}
635+
636+
#[cargo_test(>=1.79, reason = "--check-cfg was stabilized in Rust 1.79")]
637+
fn config_invalid_not_list() {
638+
let p = project()
639+
.file(
640+
"Cargo.toml",
641+
r#"
642+
[package]
643+
name = "foo"
644+
version = "0.1.0"
645+
edition = "2015"
646+
647+
[lints.rust]
648+
unexpected_cfgs = { level = "warn", check-cfg = "cfg()" }
649+
"#,
650+
)
651+
.file("src/main.rs", "fn main() {}")
652+
.build();
653+
654+
p.cargo("check")
655+
.with_stderr_contains(
656+
"[..]`lints.rust.unexpected_cfgs.check-cfg` must be a list of string[..]",
657+
)
658+
.run();
659+
}
660+
661+
#[cargo_test(>=1.79, reason = "--check-cfg was stabilized in Rust 1.79")]
662+
fn config_invalid_not_list_string() {
663+
let p = project()
664+
.file(
665+
"Cargo.toml",
666+
r#"
667+
[package]
668+
name = "foo"
669+
version = "0.1.0"
670+
edition = "2015"
671+
672+
[lints.rust]
673+
unexpected_cfgs = { level = "warn", check-cfg = [12] }
674+
"#,
675+
)
676+
.file("src/main.rs", "fn main() {}")
677+
.build();
678+
679+
p.cargo("check")
680+
.with_stderr_contains(
681+
"[..]`lints.rust.unexpected_cfgs.check-cfg` must be a list of string[..]",
682+
)
683+
.run();
684+
}
685+
686+
#[cargo_test(>=1.79, reason = "--check-cfg was stabilized in Rust 1.79")]
687+
fn config_and_features() {
688+
let p = project()
689+
.file(
690+
"Cargo.toml",
691+
r#"
692+
[package]
693+
name = "foo"
694+
version = "0.1.0"
695+
edition = "2015"
696+
697+
[features]
698+
my_feature = []
699+
alloc = []
700+
701+
[lints.rust]
702+
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(has_foo)", "cfg(has_bar, values(\"yes\", \"no\"))"] }
703+
"#,
704+
)
705+
.file("src/main.rs", "fn main() {}")
706+
.build();
707+
708+
p.cargo("check -v")
709+
.with_stderr_contains(x!("rustc" => "cfg" of "has_foo"))
710+
.with_stderr_contains(x!("rustc" => "cfg" of "has_bar" with "yes" "no"))
711+
.with_stderr_contains(x!("rustc" => "cfg" of "feature" with "alloc" "my_feature"))
712+
.run();
713+
}
714+
715+
#[cargo_test(>=1.79, reason = "--check-cfg was stabilized in Rust 1.79")]
716+
fn config_with_cargo_doc() {
717+
let p = project()
718+
.file(
719+
"Cargo.toml",
720+
r#"
721+
[package]
722+
name = "foo"
723+
version = "0.1.0"
724+
edition = "2015"
725+
726+
[lints.rust]
727+
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(has_foo)"] }
728+
"#,
729+
)
730+
.file("src/main.rs", "fn main() {}")
731+
.build();
732+
733+
p.cargo("doc -v")
734+
.with_stderr_contains(x!("rustdoc" => "cfg" of "has_foo"))
735+
.run();
736+
}
737+
738+
#[cargo_test(>=1.79, reason = "--check-cfg was stabilized in Rust 1.79")]
739+
fn config_with_cargo_test() {
740+
let p = project()
741+
.file(
742+
"Cargo.toml",
743+
r#"
744+
[package]
745+
name = "foo"
746+
version = "0.1.0"
747+
edition = "2015"
748+
749+
[lints.rust]
750+
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(has_foo)"] }
751+
"#,
752+
)
753+
.file("src/main.rs", "fn main() {}")
754+
.build();
755+
756+
p.cargo("test -v")
757+
.with_stderr_contains(x!("rustc" => "cfg" of "has_foo"))
758+
.run();
759+
}
760+
761+
#[cargo_test(>=1.79, reason = "--check-cfg was stabilized in Rust 1.79")]
762+
fn config_and_build_script() {
763+
let p = project()
764+
.file(
765+
"Cargo.toml",
766+
r#"
767+
[package]
768+
name = "foo"
769+
version = "0.0.1"
770+
edition = "2021"
771+
build = "build.rs"
772+
773+
[lints.rust]
774+
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(bar)"] }
775+
"#,
776+
)
777+
.file("src/main.rs", "fn main() {}")
778+
.file(
779+
"build.rs",
780+
r#"fn main() { println!("cargo::rustc-check-cfg=cfg(foo)"); }"#,
781+
)
782+
.build();
783+
784+
p.cargo("check -v")
785+
.with_stderr_contains(x!("rustc" => "cfg" of "foo")) // from build.rs
786+
.with_stderr_contains(x!("rustc" => "cfg" of "bar")) // from config
787+
.run();
788+
}
789+
790+
#[cargo_test(>=1.79, reason = "--check-cfg was stabilized in Rust 1.79")]
791+
fn config_features_and_build_script() {
792+
let p = project()
793+
.file(
794+
"Cargo.toml",
795+
r#"
796+
[package]
797+
name = "foo"
798+
version = "0.0.1"
799+
edition = "2021"
800+
build = "build.rs"
801+
802+
[features]
803+
serde = []
804+
json = []
805+
806+
[lints.rust]
807+
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(bar)"] }
808+
"#,
809+
)
810+
.file("src/main.rs", "fn main() {}")
811+
.file(
812+
"build.rs",
813+
r#"fn main() { println!("cargo::rustc-check-cfg=cfg(foo)"); }"#,
814+
)
815+
.build();
816+
817+
p.cargo("check -v")
818+
.with_stderr_contains(x!("rustc" => "cfg" of "foo")) // from build.rs
819+
.with_stderr_contains(x!("rustc" => "cfg" of "bar")) // from config
820+
.with_stderr_contains(x!("rustc" => "cfg" of "feature" with "json" "serde")) // features
821+
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs")) // Cargo well known
822+
.run();
823+
}

0 commit comments

Comments
 (0)