Skip to content

Commit 4dfede9

Browse files
committed
Add tests for check-cfg config of unexpected_cfgs lint
1 parent a8041d1 commit 4dfede9

File tree

1 file changed

+326
-0
lines changed

1 file changed

+326
-0
lines changed

tests/testsuite/check_cfg.rs

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

0 commit comments

Comments
 (0)