Skip to content

Commit

Permalink
add unit tests for license.rs #700
Browse files Browse the repository at this point in the history
  • Loading branch information
o2sh committed Aug 6, 2022
1 parent b197c69 commit edc27d8
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions src/info/license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ impl Detector {
}

pub fn get_license(&self, dir: &Path) -> Result<String> {
fn is_license_file<S: AsRef<str>>(file_name: S) -> bool {
LICENSE_FILES
.iter()
.any(|&name| file_name.as_ref().starts_with(name))
}

let mut output = fs::read_dir(dir)?
.filter_map(std::result::Result::ok)
.map(|entry| entry.path())
Expand Down Expand Up @@ -64,3 +58,38 @@ impl Detector {
}
}
}

fn is_license_file<S: AsRef<str>>(file_name: S) -> bool {
LICENSE_FILES
.iter()
.any(|&name| file_name.as_ref().starts_with(name))
}

#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_get_license() {
let license = Detector::new()
.unwrap()
.get_license(Path::new("."))
.unwrap();
assert_eq!(license, "MIT");
}

#[test]
fn test_is_license_file() {
for file_name in LICENSE_FILES.iter() {
assert_eq!(is_license_file(file_name), true);
}
assert_eq!(is_license_file("NOT_LICENSE"), false);
}

#[test]
fn test_analyze() {
let license = Detector::new()
.unwrap()
.analyze(&fs::read_to_string(Path::new("LICENSE.md")).unwrap());
assert_eq!(license, Some(String::from("MIT")));
}
}

0 comments on commit edc27d8

Please sign in to comment.