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 {
@@ -89,42 +102,12 @@ fn cfg_syntax() {
89
102
good ( " foo=\" 3\" " , c ! ( foo = "3" ) ) ;
90
103
good ( "foo = \" 3 e\" " , c ! ( foo = "3 e" ) ) ;
91
104
good ( " r#foo = \" 3 e\" " , c ! ( r # foo = "3 e" ) ) ;
92
- bad :: < Cfg > (
93
- "version(\" 1.23.4\" )" ,
94
- str![ [
95
- r#"failed to parse `version("1.23.4")` as a cfg expression: unexpected content `("1.23.4")` found after cfg expression"#
96
- ] ] ,
97
- ) ;
98
- bad :: < Cfg > (
99
- "version(\" 1.23\" )" ,
100
- str![ [
101
- r#"failed to parse `version("1.23")` as a cfg expression: unexpected content `("1.23")` found after cfg expression"#
102
- ] ] ,
103
- ) ;
104
- bad :: < Cfg > (
105
- "version(\" 1.234.56\" )" ,
106
- str![ [
107
- r#"failed to parse `version("1.234.56")` as a cfg expression: unexpected content `("1.234.56")` found after cfg expression"#
108
- ] ] ,
109
- ) ;
110
- bad :: < Cfg > (
111
- " version(\" 1.23.4\" )" ,
112
- str![ [
113
- r#"failed to parse ` version("1.23.4")` as a cfg expression: unexpected content `("1.23.4")` found after cfg expression"#
114
- ] ] ,
115
- ) ;
116
- bad :: < Cfg > (
117
- "version(\" 1.23.4\" ) " ,
118
- str![ [
119
- r#"failed to parse `version("1.23.4") ` as a cfg expression: unexpected content `("1.23.4") ` found after cfg expression"#
120
- ] ] ,
121
- ) ;
122
- bad :: < Cfg > (
123
- " version(\" 1.23.4\" ) " ,
124
- str![ [
125
- r#"failed to parse ` version("1.23.4") ` as a cfg expression: unexpected content `("1.23.4") ` found after cfg expression"#
126
- ] ] ,
127
- ) ;
105
+ good ( "version(\" 1.23.4\" )" , c ! ( version( 23 , 4 ) ) ) ;
106
+ good ( "version(\" 1.23\" )" , c ! ( version( 23 ) ) ) ;
107
+ good ( "version(\" 1.234.56\" )" , c ! ( version( 234 , 56 ) ) ) ;
108
+ good ( " version(\" 1.23.4\" )" , c ! ( version( 23 , 4 ) ) ) ;
109
+ good ( "version(\" 1.23.4\" ) " , c ! ( version( 23 , 4 ) ) ) ;
110
+ good ( " version(\" 1.23.4\" ) " , c ! ( version( 23 , 4 ) ) ) ;
128
111
good ( "version = \" 1.23.4\" " , c ! ( version = "1.23.4" ) ) ;
129
112
}
130
113
@@ -154,43 +137,43 @@ fn cfg_syntax_bad() {
154
137
bad :: < Cfg > (
155
138
"version(\" 1\" )" ,
156
139
str![ [
157
- r#"failed to parse `version("1")` as a cfg expression: unexpected content ` ("1")` found after cfg expression "#
140
+ r#"failed to parse `version("1")` as a cfg expression: invalid Rust cfg version, expected format `version ("1.23.4 ")` or `version("1.23")` "#
158
141
] ] ,
159
142
) ;
160
143
bad :: < Cfg > (
161
144
"version(\" 1.\" )" ,
162
145
str![ [
163
- r#"failed to parse `version("1.")` as a cfg expression: unexpected content ` ("1.")` found after cfg expression "#
146
+ r#"failed to parse `version("1.")` as a cfg expression: invalid Rust cfg version, expected format `version ("1.23.4 ")` or `version("1.23")` "#
164
147
] ] ,
165
148
) ;
166
149
bad :: < Cfg > (
167
150
"version(\" 1.2.\" )" ,
168
151
str![ [
169
- r#"failed to parse `version("1.2.")` as a cfg expression: unexpected content ` ("1.2. ")` found after cfg expression "#
152
+ 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")` "#
170
153
] ] ,
171
154
) ;
172
155
bad :: < Cfg > (
173
156
"version(\" 1.2.3.\" )" ,
174
157
str![ [
175
- r#"failed to parse `version("1.2.3.")` as a cfg expression: unexpected content ` ("1.2.3. ")` found after cfg expression "#
158
+ 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")` "#
176
159
] ] ,
177
160
) ;
178
161
bad :: < Cfg > (
179
162
"version(\" 1.2.3-stable\" )" ,
180
163
str![ [
181
- r#"failed to parse `version("1.2.3-stable")` as a cfg expression: unexpected content ` ("1.2.3-stable ")` found after cfg expression "#
164
+ 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")` "#
182
165
] ] ,
183
166
) ;
184
167
bad :: < Cfg > (
185
168
"version(\" 2.3\" )" ,
186
169
str![ [
187
- r#"failed to parse `version("2.3")` as a cfg expression: unexpected content `("2.3 ")` found after cfg expression "#
170
+ 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")` "#
188
171
] ] ,
189
172
) ;
190
173
bad :: < Cfg > (
191
174
"version(\" 0.99.9\" )" ,
192
175
str![ [
193
- r#"failed to parse `version("0.99.9")` as a cfg expression: unexpected content `("0.99.9 ")` found after cfg expression "#
176
+ 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")` "#
194
177
] ] ,
195
178
) ;
196
179
}
@@ -215,12 +198,7 @@ fn cfg_expr() {
215
198
good ( "all(a, )" , e ! ( all( a) ) ) ;
216
199
good ( "not(a = \" b\" )" , e ! ( not( a = "b" ) ) ) ;
217
200
good ( "not(all(a))" , e ! ( not( all( a) ) ) ) ;
218
- bad :: < Cfg > (
219
- "not(version(\" 1.23.4\" ))" ,
220
- str![ [
221
- r#"failed to parse `not(version("1.23.4"))` as a cfg expression: unexpected content `(version("1.23.4"))` found after cfg expression"#
222
- ] ] ,
223
- ) ;
201
+ good ( "not(version(\" 1.23.4\" ))" , e ! ( not( version( 23 , 4 ) ) ) ) ;
224
202
}
225
203
226
204
#[ test]
@@ -280,6 +258,28 @@ fn cfg_matches() {
280
258
assert ! ( !e!( not( bar) ) . matches( & [ c!( bar) ] , & v87) ) ;
281
259
assert ! ( !e!( not( bar) ) . matches( & [ c!( baz) , c!( bar) ] , & v87) ) ;
282
260
assert ! ( !e!( any( ( not( foo) ) , ( all( foo, bar) ) ) ) . matches( & [ c!( foo) ] , & v87) ) ;
261
+
262
+ assert ! ( e!( version( 87 ) ) . matches( & [ ] , & v87) ) ;
263
+ assert ! ( e!( version( 87 , 0 ) ) . matches( & [ ] , & v87) ) ;
264
+ assert ! ( e!( version( 86 ) ) . matches( & [ ] , & v87) ) ;
265
+ assert ! ( e!( version( 86 , 1 ) ) . matches( & [ ] , & v87) ) ;
266
+ assert ! ( !e!( version( 87 , 1 ) ) . matches( & [ ] , & v87) ) ;
267
+ assert ! ( !e!( version( 88 ) ) . matches( & [ ] , & v87) ) ;
268
+ assert ! ( !e!( version( 88 , 1 ) ) . matches( & [ ] , & v87) ) ;
269
+ assert ! ( e!( not( version( 88 ) ) ) . matches( & [ ] , & v87) ) ;
270
+ assert ! ( e!( not( version( 88 , 1 ) ) ) . matches( & [ ] , & v87) ) ;
271
+
272
+ let v89_nightly = semver:: Version {
273
+ major : 1 ,
274
+ minor : 89 ,
275
+ patch : 0 ,
276
+ pre : Prerelease :: new ( "nightly" ) . unwrap ( ) ,
277
+ build : BuildMetadata :: EMPTY ,
278
+ } ;
279
+ assert ! ( e!( version( 89 ) ) . matches( & [ ] , & v89_nightly) ) ;
280
+ assert ! ( !e!( version( 89 , 0 ) ) . matches( & [ ] , & v89_nightly) ) ;
281
+ assert ! ( e!( version( 88 ) ) . matches( & [ ] , & v89_nightly) ) ;
282
+ assert ! ( e!( version( 88 , 0 ) ) . matches( & [ ] , & v89_nightly) ) ;
283
283
}
284
284
285
285
#[ test]
0 commit comments