@@ -177,16 +177,7 @@ impl NewVersion {
177
177
178
178
fn validate_license ( & mut self , license_file : Option < & str > ) -> AppResult < ( ) > {
179
179
if let Some ( ref license) = self . license {
180
- for part in license. split ( '/' ) {
181
- license_exprs:: validate_license_expr ( part) . map_err ( |e| {
182
- cargo_err ( & format_args ! (
183
- "{}; see http://opensource.org/licenses \
184
- for options, and http://spdx.org/licenses/ \
185
- for their identifiers",
186
- e
187
- ) )
188
- } ) ?;
189
- }
180
+ validate_license_expr ( license) ?;
190
181
} else if license_file. is_some ( ) {
191
182
// If no license is given, but a license file is given, flag this
192
183
// crate as having a nonstandard license. Note that we don't
@@ -197,9 +188,19 @@ impl NewVersion {
197
188
}
198
189
}
199
190
191
+ fn validate_license_expr ( s : & str ) -> AppResult < ( ) > {
192
+ for part in s. split ( '/' ) {
193
+ license_exprs:: validate_license_expr ( part) . map_err ( |e| {
194
+ cargo_err ( & format_args ! ( "{}; see http://opensource.org/licenses for options, and http://spdx.org/licenses/ for their identifiers" , e) )
195
+ } ) ?;
196
+ }
197
+
198
+ Ok ( ( ) )
199
+ }
200
+
200
201
#[ cfg( test) ]
201
202
mod tests {
202
- use super :: TopVersions ;
203
+ use super :: { validate_license_expr , TopVersions } ;
203
204
use chrono:: NaiveDateTime ;
204
205
205
206
#[ track_caller]
@@ -269,4 +270,20 @@ mod tests {
269
270
}
270
271
) ;
271
272
}
273
+
274
+ #[ test]
275
+ fn licenses ( ) {
276
+ assert_ok ! ( validate_license_expr( "MIT" ) ) ;
277
+ assert_ok ! ( validate_license_expr( "MIT OR Apache-2.0" ) ) ;
278
+ assert_ok ! ( validate_license_expr( "MIT/Apache-2.0" ) ) ;
279
+ assert_ok ! ( validate_license_expr( "MIT AND Apache-2.0" ) ) ;
280
+
281
+ let error = assert_err ! ( validate_license_expr( "apache 2.0" ) ) ;
282
+ let error = format ! ( "{}" , error) ;
283
+ assert ! ( error. starts_with( "unknown license or other term: apache; see http" ) ) ;
284
+
285
+ let error = assert_err ! ( validate_license_expr( "MIT OR (Apache-2.0 AND MIT)" ) ) ;
286
+ let error = format ! ( "{}" , error) ;
287
+ assert ! ( error. starts_with( "unknown license or other term: (Apache-2.0; see http" ) ) ;
288
+ }
272
289
}
0 commit comments