Skip to content

Commit ee38010

Browse files
committed
Auto merge of #4292 - Turbo87:license-validation, r=Turbo87
Add tests for license expression validation related to: - #2595
2 parents ef0b0aa + 3d76b3b commit ee38010

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

src/models/version.rs

+28-11
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,7 @@ impl NewVersion {
177177

178178
fn validate_license(&mut self, license_file: Option<&str>) -> AppResult<()> {
179179
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)?;
190181
} else if license_file.is_some() {
191182
// If no license is given, but a license file is given, flag this
192183
// crate as having a nonstandard license. Note that we don't
@@ -197,9 +188,19 @@ impl NewVersion {
197188
}
198189
}
199190

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+
200201
#[cfg(test)]
201202
mod tests {
202-
use super::TopVersions;
203+
use super::{validate_license_expr, TopVersions};
203204
use chrono::NaiveDateTime;
204205

205206
#[track_caller]
@@ -269,4 +270,20 @@ mod tests {
269270
}
270271
);
271272
}
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+
}
272289
}

0 commit comments

Comments
 (0)