Skip to content

Commit

Permalink
Update and add test for isLicenseFileName() (#1116)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck authored Jul 6, 2024
1 parent affe1d0 commit 392a42a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ private List<LicenseInfo> addLicensesFromJar(FileObject jar, FileObject localFil
licenseInfos = addLicensesFromPom(localFile, POM_XML);
} else if (LicenseUtils.isManifestMfFileName(localFile)) {
licenseInfos = addLicensesFromBundleLicense(localFile);
} else if (LicenseUtils.isLicenseFileName(localFile)) {
} else if (LicenseUtils.isLicenseFileName(localFile.getName().getBaseName())) {
licenseInfos = addLicenseFromTextFile(jar, localFile);
} else {
licenseInfos = Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.vfs2.FileContent;
import org.apache.commons.vfs2.FileName;
import org.apache.commons.vfs2.FileObject;
import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.model.license.AnyLicenseInfo;
Expand Down Expand Up @@ -114,7 +113,7 @@ public final class LicenseUtils {

private static final String URL_MARKER = ":/";

private static final List<String> EXTENSIONS_TO_REMOVE = List.of(".html", ".php", ".txt");
private static final List<String> EXTENSIONS_TO_REMOVE = List.of(".html", ".md", ".php", ".txt");

private static final Pattern NAME_VERSION_PATTERN = Pattern
.compile("(?<name>[A-Z-a-z])[Vv]?(?<major>[1-9]+)(\\.(?<minor>[0-9]+))?");
Expand Down Expand Up @@ -535,7 +534,8 @@ public static boolean isManifestMfFileName(FileObject fileObject) {
}

/**
* Returns whether the given file object is a license text file. Matches files such as
* Returns whether the given file name is a license text file. Matches files such as
*
* <ul>
* <li>LICENSE.md</li>
* <li>LICENSE</li>
Expand All @@ -544,25 +544,16 @@ public static boolean isManifestMfFileName(FileObject fileObject) {
* <li>&lt;SPDX-LICENSE-ID&gt;.txt</li>
* </ul>
*
* @param fileObject the file object
* @return whether the given file object is a license text file
* @param fileName the file name
* @return whether the given file name is a license text file
*/
public static boolean isLicenseFileName(FileObject fileObject) {
FileName name = fileObject.getName();
String fileName = name.getBaseName();

public static boolean isLicenseFileName(String fileName) {
if (LICENSE_FILE_PATTERN.matcher(fileName).matches()) {
return true;
}

String extension = FilenameUtils.getExtension(fileName);

if (StringUtils.equalsAny(extension, "", "txt")) {
String baseName = StringUtils.removeEnd(fileName, extension);
return LicenseUtils.isKnownLicenseId(baseName);
}

return false;
String baseName = FilenameUtils.removeExtension(fileName);
return LicenseUtils.isKnownLicenseId(baseName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.model.license.InvalidLicenseStringException;
import org.spdx.library.model.license.LicenseInfoFactory;
Expand Down Expand Up @@ -319,4 +320,10 @@ void testGetMatchingLicense() throws IOException, InvalidSPDXAnalysisException {
assertThat(LicenseUtils.getMatchingLicense(fo)).isEqualTo("Apache-2.0");
}
}

@ParameterizedTest
@ValueSource(strings = { "LICENSE", "LICENSE.md", "LICENSE.txt", "MIT-LICENSE", "MIT", "MIT.md", "MIT.txt" })
void testIsLicenseFileName(String fileName) {
assertThat(LicenseUtils.isLicenseFileName(fileName)).isTrue();
}
}

0 comments on commit 392a42a

Please sign in to comment.