Skip to content

Commit bd6b647

Browse files
mo8itepage
andcommitted
test: Show current behavior for cfg_version
Co-authored-by: Ed Page <[email protected]>
1 parent b1d153e commit bd6b647

File tree

2 files changed

+243
-9
lines changed

2 files changed

+243
-9
lines changed

crates/cargo-platform/tests/test_cfg.rs

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ macro_rules! e {
4949
($($t:tt)*) => (CfgExpr::Value(c!($($t)*)));
5050
}
5151

52+
#[track_caller]
5253
fn good<T>(s: &str, expected: T)
5354
where
5455
T: FromStr + PartialEq + fmt::Debug,
@@ -87,6 +88,43 @@ fn cfg_syntax() {
8788
good(" foo=\"3\" ", c!(foo = "3"));
8889
good("foo = \"3 e\"", c!(foo = "3 e"));
8990
good(" r#foo = \"3 e\"", c!(r # foo = "3 e"));
91+
bad::<Cfg>(
92+
"version(\"1.23.4\")",
93+
str![[
94+
r#"failed to parse `version("1.23.4")` as a cfg expression: unexpected content `("1.23.4")` found after cfg expression"#
95+
]],
96+
);
97+
bad::<Cfg>(
98+
"version(\"1.23\")",
99+
str![[
100+
r#"failed to parse `version("1.23")` as a cfg expression: unexpected content `("1.23")` found after cfg expression"#
101+
]],
102+
);
103+
bad::<Cfg>(
104+
"version(\"1.234.56\")",
105+
str![[
106+
r#"failed to parse `version("1.234.56")` as a cfg expression: unexpected content `("1.234.56")` found after cfg expression"#
107+
]],
108+
);
109+
bad::<Cfg>(
110+
" version(\"1.23.4\")",
111+
str![[
112+
r#"failed to parse ` version("1.23.4")` as a cfg expression: unexpected content `("1.23.4")` found after cfg expression"#
113+
]],
114+
);
115+
bad::<Cfg>(
116+
"version(\"1.23.4\") ",
117+
str![[
118+
r#"failed to parse `version("1.23.4") ` as a cfg expression: unexpected content `("1.23.4") ` found after cfg expression"#
119+
]],
120+
);
121+
bad::<Cfg>(
122+
" version(\"1.23.4\") ",
123+
str![[
124+
r#"failed to parse ` version("1.23.4") ` as a cfg expression: unexpected content `("1.23.4") ` found after cfg expression"#
125+
]],
126+
);
127+
good("version = \"1.23.4\"", c!(version = "1.23.4"));
90128
}
91129

92130
#[test]
@@ -112,17 +150,48 @@ fn cfg_syntax_bad() {
112150
"(",
113151
str!["failed to parse `(` as a cfg expression: expected identifier, found `(`"],
114152
);
115-
bad::<Cfg>("foo (", str!["failed to parse `foo (` as a cfg expression: unexpected content `(` found after cfg expression"]);
116-
bad::<Cfg>("bar =", str!["failed to parse `bar =` as a cfg expression: expected a string, but cfg expression ended"]);
117153
bad::<Cfg>(
118-
"bar = \"",
119-
str![[r#"failed to parse `bar = "` as a cfg expression: unterminated string in cfg"#]],
154+
"version(\"1\")",
155+
str![[
156+
r#"failed to parse `version("1")` as a cfg expression: unexpected content `("1")` found after cfg expression"#
157+
]],
158+
);
159+
bad::<Cfg>(
160+
"version(\"1.\")",
161+
str![[
162+
r#"failed to parse `version("1.")` as a cfg expression: unexpected content `("1.")` found after cfg expression"#
163+
]],
164+
);
165+
bad::<Cfg>(
166+
"version(\"1.2.\")",
167+
str![[
168+
r#"failed to parse `version("1.2.")` as a cfg expression: unexpected content `("1.2.")` found after cfg expression"#
169+
]],
170+
);
171+
bad::<Cfg>(
172+
"version(\"1.2.3.\")",
173+
str![[
174+
r#"failed to parse `version("1.2.3.")` as a cfg expression: unexpected content `("1.2.3.")` found after cfg expression"#
175+
]],
176+
);
177+
bad::<Cfg>(
178+
"version(\"1.2.3-stable\")",
179+
str![[
180+
r#"failed to parse `version("1.2.3-stable")` as a cfg expression: unexpected content `("1.2.3-stable")` found after cfg expression"#
181+
]],
182+
);
183+
bad::<Cfg>(
184+
"version(\"2.3\")",
185+
str![[
186+
r#"failed to parse `version("2.3")` as a cfg expression: unexpected content `("2.3")` found after cfg expression"#
187+
]],
188+
);
189+
bad::<Cfg>(
190+
"version(\"0.99.9\")",
191+
str![[
192+
r#"failed to parse `version("0.99.9")` as a cfg expression: unexpected content `("0.99.9")` found after cfg expression"#
193+
]],
120194
);
121-
bad::<Cfg>("foo, bar", str!["failed to parse `foo, bar` as a cfg expression: unexpected content `, bar` found after cfg expression"]);
122-
bad::<Cfg>("r# foo", str!["failed to parse `r# foo` as a cfg expression: unexpected character ` ` in cfg, expected parens, a comma, an identifier, or a string"]);
123-
bad::<Cfg>("r #foo", str!["failed to parse `r #foo` as a cfg expression: unexpected content `#foo` found after cfg expression"]);
124-
bad::<Cfg>("r#\"foo\"", str![[r#"failed to parse `r#"foo"` as a cfg expression: unexpected character `"` in cfg, expected parens, a comma, an identifier, or a string"#]]);
125-
bad::<Cfg>("foo = r#\"\"", str![[r#"failed to parse `foo = r#""` as a cfg expression: unexpected character `"` in cfg, expected parens, a comma, an identifier, or a string"#]]);
126195
}
127196

128197
#[test]
@@ -145,6 +214,12 @@ fn cfg_expr() {
145214
good("all(a, )", e!(all(a)));
146215
good("not(a = \"b\")", e!(not(a = "b")));
147216
good("not(all(a))", e!(not(all(a))));
217+
bad::<Cfg>(
218+
"not(version(\"1.23.4\"))",
219+
str![[
220+
r#"failed to parse `not(version("1.23.4"))` as a cfg expression: unexpected content `(version("1.23.4"))` found after cfg expression"#
221+
]],
222+
);
148223
}
149224

150225
#[test]

tests/testsuite/cfg.rs

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,3 +817,162 @@ fn cfg_booleans_rustflags_no_effect() {
817817
"#]])
818818
.run();
819819
}
820+
821+
#[cargo_test]
822+
fn cfg_version() {
823+
let p = project()
824+
.file(
825+
"Cargo.toml",
826+
r#"
827+
[package]
828+
name = "a"
829+
edition = "2015"
830+
831+
[target.'cfg(version("1.87.0"))'.dependencies]
832+
b = { path = 'b' }
833+
"#,
834+
)
835+
.file("src/lib.rs", "extern crate b;")
836+
.file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
837+
.file("b/src/lib.rs", "")
838+
.build();
839+
p.cargo("check -v")
840+
.with_status(101)
841+
.with_stderr_data(str![[r#"
842+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
843+
844+
Caused by:
845+
failed to parse `version("1.87.0")` as a cfg expression: unexpected content `("1.87.0")` found after cfg expression
846+
847+
"#]])
848+
.run();
849+
}
850+
851+
#[cargo_test]
852+
fn cfg_version_short() {
853+
let p = project()
854+
.file(
855+
"Cargo.toml",
856+
r#"
857+
[package]
858+
name = "a"
859+
edition = "2015"
860+
861+
[target.'cfg(version("1.87"))'.dependencies]
862+
b = { path = 'b' }
863+
"#,
864+
)
865+
.file("src/lib.rs", "extern crate b;")
866+
.file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
867+
.file("b/src/lib.rs", "")
868+
.build();
869+
p.cargo("check -v")
870+
.with_status(101)
871+
.with_stderr_data(str![[r#"
872+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
873+
874+
Caused by:
875+
failed to parse `version("1.87")` as a cfg expression: unexpected content `("1.87")` found after cfg expression
876+
877+
"#]])
878+
.run();
879+
}
880+
881+
#[cargo_test]
882+
fn cfg_bad_version() {
883+
let p = project()
884+
.file(
885+
"Cargo.toml",
886+
r#"
887+
[package]
888+
name = "foo"
889+
edition = "2015"
890+
891+
[target.'cfg(version("1"))'.dependencies]
892+
b = { path = 'b' }
893+
"#,
894+
)
895+
.file("src/lib.rs", "extern crate b;")
896+
.file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
897+
.file("b/src/lib.rs", "")
898+
.build();
899+
900+
p.cargo("check")
901+
.with_status(101)
902+
.with_stderr_data(str![[r#"
903+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
904+
905+
Caused by:
906+
failed to parse `version("1")` as a cfg expression: unexpected content `("1")` found after cfg expression
907+
908+
"#]])
909+
.run();
910+
}
911+
912+
#[cargo_test]
913+
fn cfg_bad_version2() {
914+
let p = project()
915+
.file(
916+
"Cargo.toml",
917+
r#"
918+
[package]
919+
name = "foo"
920+
edition = "2015"
921+
922+
[target.'cfg(version(1.87.0))'.dependencies]
923+
b = { path = 'b' }
924+
"#,
925+
)
926+
.file("src/lib.rs", "extern crate b;")
927+
.file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
928+
.file("b/src/lib.rs", "")
929+
.build();
930+
931+
p.cargo("check")
932+
.with_status(101)
933+
.with_stderr_data(str![[r#"
934+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
935+
936+
Caused by:
937+
failed to parse `version(1.87.0)` as a cfg expression: unexpected content `(1.87.0)` found after cfg expression
938+
939+
"#]])
940+
.run();
941+
}
942+
943+
#[cargo_test]
944+
fn cfg_bad_version3() {
945+
let p = project()
946+
.file(
947+
"Cargo.toml",
948+
r#"
949+
[package]
950+
name = "foo"
951+
edition = "2015"
952+
953+
[target.'cfg(version = "1.87.0")'.dependencies]
954+
b = { path = 'b' }
955+
"#,
956+
)
957+
.file("src/lib.rs", "extern crate b;")
958+
.file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
959+
.file("b/src/lib.rs", "")
960+
.build();
961+
962+
p.cargo("check")
963+
.with_status(101)
964+
.with_stderr_data(str![[r#"
965+
[LOCKING] 1 package to latest compatible version
966+
[CHECKING] foo v0.0.0 ([ROOT]/foo)
967+
error[E0463]: can't find crate for `b`
968+
--> src/lib.rs:1:1
969+
|
970+
1 | extern crate b;
971+
| ^^^^^^^^^^^^^^^ can't find crate
972+
973+
For more information about this error, try `rustc --explain E0463`.
974+
[ERROR] could not compile `foo` (lib) due to 1 previous error
975+
976+
"#]])
977+
.run();
978+
}

0 commit comments

Comments
 (0)