1
1
use std:: fmt;
2
2
use std:: str:: FromStr ;
3
3
4
- use cargo_platform:: { Cfg , CfgExpr , Ident , Platform } ;
4
+ use cargo_platform:: { Cfg , CfgExpr , CfgRustVersion , Ident , Platform } ;
5
+ use semver:: { BuildMetadata , Prerelease } ;
5
6
use snapbox:: assert_data_eq;
6
7
use snapbox:: prelude:: * ;
7
8
use snapbox:: str;
@@ -37,6 +38,18 @@ macro_rules! c {
37
38
$e. to_string( ) ,
38
39
)
39
40
} ;
41
+ ( version( $minor: literal) ) => {
42
+ Cfg :: Version ( CfgRustVersion {
43
+ minor: $minor,
44
+ patch: None ,
45
+ } )
46
+ } ;
47
+ ( version( $minor: literal, $patch: literal) ) => {
48
+ Cfg :: Version ( CfgRustVersion {
49
+ minor: $minor,
50
+ patch: Some ( $patch) ,
51
+ } )
52
+ } ;
40
53
}
41
54
42
55
macro_rules! e {
@@ -88,42 +101,12 @@ fn cfg_syntax() {
88
101
good ( " foo=\" 3\" " , c ! ( foo = "3" ) ) ;
89
102
good ( "foo = \" 3 e\" " , c ! ( foo = "3 e" ) ) ;
90
103
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
- ) ;
104
+ good ( "version(\" 1.23.4\" )" , c ! ( version( 23 , 4 ) ) ) ;
105
+ good ( "version(\" 1.23\" )" , c ! ( version( 23 ) ) ) ;
106
+ good ( "version(\" 1.234.56\" )" , c ! ( version( 234 , 56 ) ) ) ;
107
+ good ( " version(\" 1.23.4\" )" , c ! ( version( 23 , 4 ) ) ) ;
108
+ good ( "version(\" 1.23.4\" ) " , c ! ( version( 23 , 4 ) ) ) ;
109
+ good ( " version(\" 1.23.4\" ) " , c ! ( version( 23 , 4 ) ) ) ;
127
110
good ( "version = \" 1.23.4\" " , c ! ( version = "1.23.4" ) ) ;
128
111
}
129
112
@@ -153,43 +136,43 @@ fn cfg_syntax_bad() {
153
136
bad :: < Cfg > (
154
137
"version(\" 1\" )" ,
155
138
str![ [
156
- r#"failed to parse `version("1")` as a cfg expression: unexpected content ` ("1")` found after cfg expression "#
139
+ r#"failed to parse `version("1")` as a cfg expression: invalid Rust cfg version, expected format `version ("1.23.4 ")` or `version("1.23")` "#
157
140
] ] ,
158
141
) ;
159
142
bad :: < Cfg > (
160
143
"version(\" 1.\" )" ,
161
144
str![ [
162
- r#"failed to parse `version("1.")` as a cfg expression: unexpected content ` ("1.")` found after cfg expression "#
145
+ r#"failed to parse `version("1.")` as a cfg expression: invalid Rust cfg version, expected format `version ("1.23.4 ")` or `version("1.23")` "#
163
146
] ] ,
164
147
) ;
165
148
bad :: < Cfg > (
166
149
"version(\" 1.2.\" )" ,
167
150
str![ [
168
- r#"failed to parse `version("1.2.")` as a cfg expression: unexpected content ` ("1.2. ")` found after cfg expression "#
151
+ r#"failed to parse `version("1.2.")` as a cfg expression: invalid Rust cfg version, expected format `version ("1.23.4 ")` or `version("1.23")` "#
169
152
] ] ,
170
153
) ;
171
154
bad :: < Cfg > (
172
155
"version(\" 1.2.3.\" )" ,
173
156
str![ [
174
- r#"failed to parse `version("1.2.3.")` as a cfg expression: unexpected content ` ("1.2.3. ")` found after cfg expression "#
157
+ r#"failed to parse `version("1.2.3.")` as a cfg expression: invalid Rust cfg version, expected format `version ("1.23.4 ")` or `version("1.23")` "#
175
158
] ] ,
176
159
) ;
177
160
bad :: < Cfg > (
178
161
"version(\" 1.2.3-stable\" )" ,
179
162
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 "#
163
+ r#"failed to parse `version("1.2.3-stable")` as a cfg expression: invalid Rust cfg version, expected format `version ("1.23.4 ")` or `version("1.23")` "#
181
164
] ] ,
182
165
) ;
183
166
bad :: < Cfg > (
184
167
"version(\" 2.3\" )" ,
185
168
str![ [
186
- r#"failed to parse `version("2.3")` as a cfg expression: unexpected content `("2.3 ")` found after cfg expression "#
169
+ r#"failed to parse `version("2.3")` as a cfg expression: invalid Rust cfg version, expected format `version("1.23.4 ")` or `version("1.23")` "#
187
170
] ] ,
188
171
) ;
189
172
bad :: < Cfg > (
190
173
"version(\" 0.99.9\" )" ,
191
174
str![ [
192
- r#"failed to parse `version("0.99.9")` as a cfg expression: unexpected content `("0.99.9 ")` found after cfg expression "#
175
+ r#"failed to parse `version("0.99.9")` as a cfg expression: invalid Rust cfg version, expected format `version("1.23.4 ")` or `version("1.23")` "#
193
176
] ] ,
194
177
) ;
195
178
}
@@ -214,12 +197,7 @@ fn cfg_expr() {
214
197
good ( "all(a, )" , e ! ( all( a) ) ) ;
215
198
good ( "not(a = \" b\" )" , e ! ( not( a = "b" ) ) ) ;
216
199
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
- ) ;
200
+ good ( "not(version(\" 1.23.4\" ))" , e ! ( not( version( 23 , 4 ) ) ) ) ;
223
201
}
224
202
225
203
#[ test]
@@ -279,6 +257,28 @@ fn cfg_matches() {
279
257
assert ! ( !e!( not( bar) ) . matches( & [ c!( bar) ] , & v87) ) ;
280
258
assert ! ( !e!( not( bar) ) . matches( & [ c!( baz) , c!( bar) ] , & v87) ) ;
281
259
assert ! ( !e!( any( ( not( foo) ) , ( all( foo, bar) ) ) ) . matches( & [ c!( foo) ] , & v87) ) ;
260
+
261
+ assert ! ( e!( version( 87 ) ) . matches( & [ ] , & v87) ) ;
262
+ assert ! ( e!( version( 87 , 0 ) ) . matches( & [ ] , & v87) ) ;
263
+ assert ! ( e!( version( 86 ) ) . matches( & [ ] , & v87) ) ;
264
+ assert ! ( e!( version( 86 , 1 ) ) . matches( & [ ] , & v87) ) ;
265
+ assert ! ( !e!( version( 87 , 1 ) ) . matches( & [ ] , & v87) ) ;
266
+ assert ! ( !e!( version( 88 ) ) . matches( & [ ] , & v87) ) ;
267
+ assert ! ( !e!( version( 88 , 1 ) ) . matches( & [ ] , & v87) ) ;
268
+ assert ! ( e!( not( version( 88 ) ) ) . matches( & [ ] , & v87) ) ;
269
+ assert ! ( e!( not( version( 88 , 1 ) ) ) . matches( & [ ] , & v87) ) ;
270
+
271
+ let v89_nightly = semver:: Version {
272
+ major : 1 ,
273
+ minor : 89 ,
274
+ patch : 0 ,
275
+ pre : Prerelease :: new ( "nightly" ) . unwrap ( ) ,
276
+ build : BuildMetadata :: EMPTY ,
277
+ } ;
278
+ assert ! ( e!( version( 89 ) ) . matches( & [ ] , & v89_nightly) ) ;
279
+ assert ! ( !e!( version( 89 , 0 ) ) . matches( & [ ] , & v89_nightly) ) ;
280
+ assert ! ( e!( version( 88 ) ) . matches( & [ ] , & v89_nightly) ) ;
281
+ assert ! ( e!( version( 88 , 0 ) ) . matches( & [ ] , & v89_nightly) ) ;
282
282
}
283
283
284
284
#[ test]
0 commit comments