read license from manifest first
@@ -28,6 +28,8 @@ impl Detector {
}
fn get_license(&self, dir: &Path, manifest: Option<&Manifest>) -> Result<String> {
+ let license_from_manifest = manifest.and_then(|m| m.license.clone()).unwrap_or_default();
+ if license_from_manifest.is_empty() {
let mut output = fs::read_dir(dir)?
.filter_map(std::result::Result::ok)
.map(|entry| entry.path())
@@ -48,13 +50,9 @@ impl Detector {
output.sort();
output.dedup();
let license = output.join(", ");
-
- if license.is_empty() {
- let license_from_manifest =
- manifest.and_then(|m| m.license.clone()).unwrap_or_default();
- Ok(license_from_manifest)
- } else {
Ok(license)
+ } else {
+ Ok(license_from_manifest)
}
}
@@ -100,6 +98,8 @@ impl InfoField for LicenseInfo {
#[cfg(test)]
mod test {
+ use onefetch_manifest::ManifestType;
+
use super::*;
#[test]
@@ -126,4 +126,21 @@ mod test {
assert_eq!(license, Some("MIT".into()));
Ok(())
}
+
+ #[test]
+ fn should_read_from_manifest_first() -> Result<()> {
+ let license_info = LicenseInfo::new(
+ Path::new("."),
+ Some(&Manifest {
+ manifest_type: ManifestType::Cargo,
+ name: String::new(),
+ description: None,
+ number_of_dependencies: 0,
+ version: String::new(),
+ license: Some("LICENSE".into()),
+ }),
+ )?;
+ assert_eq!(license_info.value(), "LICENSE");
+ Ok(())
+ }
}
