Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions apache-rat-core/src/main/java/org/apache/rat/OptionCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -88,11 +85,32 @@ private static String asString(final Object[] args) {
* @param helpCmd the help command to run when necessary.
* @return a ReportConfiguration or {@code null} if Help was printed.
* @throws IOException on error.
* @throws ParseException on option parsing error.
*/
public static ReportConfiguration parseCommands(final File workingDirectory, final String[] args, final Consumer<Options> helpCmd) throws IOException {
public static ReportConfiguration parseCommands(final File workingDirectory, final String[] args, final Consumer<Options> helpCmd)
throws IOException, ParseException {
return parseCommands(workingDirectory, args, helpCmd, false);
}

/**
* Parse the options into the command line.
* @param opts the option definitions.
* @param args the argument to apply the definitions to.
* @return the CommandLine
* @throws ParseException on option parsing error.
*/
//@VisibleForTesting
static CommandLine parseCommandLine(final Options opts, final String[] args) throws ParseException {
try {
return DefaultParser.builder().setDeprecatedHandler(DeprecationReporter.getLogReporter())
.setAllowPartialMatching(true).build().parse(opts, args);
} catch (ParseException e) {
DefaultLog.getInstance().error(e.getMessage());
DefaultLog.getInstance().error("Please use the \"--help\" option to see a list of valid commands and options.", e);
throw e;
}
}

/**
* Parses the standard options to create a ReportConfiguration.
*
Expand All @@ -104,20 +122,11 @@ public static ReportConfiguration parseCommands(final File workingDirectory, fin
* @throws IOException on error.
*/
public static ReportConfiguration parseCommands(final File workingDirectory, final String[] args,
final Consumer<Options> helpCmd, final boolean noArgs) throws IOException {
final Consumer<Options> helpCmd, final boolean noArgs) throws IOException, ParseException {

Options opts = buildOptions();
CommandLine commandLine;
try {
commandLine = DefaultParser.builder().setDeprecatedHandler(DeprecationReporter.getLogReporter())
.setAllowPartialMatching(true).build().parse(opts, args);
} catch (ParseException e) {
DefaultLog.getInstance().error(e.getMessage());
DefaultLog.getInstance().error("Please use the \"--help\" option to see a list of valid commands and options.", e);
System.exit(1);
return null; // dummy return (won't be reached) to avoid Eclipse complaint about possible NPE
// for "commandLine"
}
CommandLine commandLine = parseCommandLine(buildOptions(), args);


Arg.processLogLevel(commandLine);

Expand Down Expand Up @@ -157,13 +166,6 @@ static ReportConfiguration createConfiguration(final ArgumentContext argumentCon
argumentContext.processArgs();
final ReportConfiguration configuration = argumentContext.getConfiguration();
final CommandLine commandLine = argumentContext.getCommandLine();
if (Arg.DIR.isSelected()) {
try {
configuration.addSource(getReportable(commandLine.getParsedOptionValue(Arg.DIR.getSelected()), configuration));
} catch (ParseException e) {
throw new ConfigurationException("Unable to set parse " + Arg.DIR.getSelected(), e);
}
}
for (String s : commandLine.getArgs()) {
IReportable reportable = getReportable(new File(s), configuration);
if (reportable != null) {
Expand Down
27 changes: 16 additions & 11 deletions apache-rat-core/src/main/java/org/apache/rat/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.File;

import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.rat.document.RatDocumentAnalysisException;
import org.apache.rat.help.Help;
import org.apache.rat.utils.DefaultLog;
Expand Down Expand Up @@ -48,19 +49,23 @@ public static void main(final String[] args) throws Exception {
System.exit(0);
}

ReportConfiguration configuration = OptionCollection.parseCommands(new File("."), args, Report::printUsage);
if (configuration != null) {
configuration.validate(DefaultLog.getInstance()::error);
Reporter reporter = new Reporter(configuration);
reporter.output();
reporter.writeSummary(DefaultLog.getInstance().asWriter());
try {
ReportConfiguration configuration = OptionCollection.parseCommands(new File("."), args, Report::printUsage);
if (configuration != null) {
configuration.validate(DefaultLog.getInstance()::error);
Reporter reporter = new Reporter(configuration);
reporter.output();
reporter.writeSummary(DefaultLog.getInstance().asWriter());

if (configuration.getClaimValidator().hasErrors()) {
configuration.getClaimValidator().logIssues(reporter.getClaimsStatistic());
throw new RatDocumentAnalysisException(format("Issues with %s",
String.join(", ",
configuration.getClaimValidator().listIssues(reporter.getClaimsStatistic()))));
if (configuration.getClaimValidator().hasErrors()) {
configuration.getClaimValidator().logIssues(reporter.getClaimsStatistic());
throw new RatDocumentAnalysisException(format("Issues with %s",
String.join(", ",
configuration.getClaimValidator().listIssues(reporter.getClaimsStatistic()))));
}
}
} catch (ParseException e) {
System.exit(1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import java.util.TreeMap;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.DeprecatedAttributes;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.rat.commandline.ArgumentContext;
Expand Down Expand Up @@ -177,38 +179,24 @@ void cleanUp() {
}

@Test
public void testDeprecatedUseLogged() throws IOException {
TestingLog log = new TestingLog();
try {
DefaultLog.setInstance(log);
String[] args = {"--dir", "target", "-a"};
ReportConfiguration config = OptionCollection.parseCommands(testPath.toFile(), args, o -> fail("Help printed"), true);
assertThat(config).isNotNull();
} finally {
DefaultLog.setInstance(null);
}
log.assertContainsExactly(1, "WARN: Option [-d, --dir] used. Deprecated for removal since 0.17: Use the standard '--'");
log.assertContainsExactly(1, "WARN: Option [-a] used. Deprecated for removal since 0.17: Use --edit-license");
}
public void testDeprecatedUseLogged() throws ParseException {
Options opts = OptionCollection.buildOptions();
opts.addOption(Option.builder().longOpt("deprecated-option").deprecated(DeprecatedAttributes.builder().setDescription("Deprecation reason.")
.setForRemoval(true).setSince("1.0.0-SNAPSHOT").get()).build());

@Test
public void testDirOptionCapturesDirectoryToScan() throws IOException {
TestingLog log = new TestingLog();
ReportConfiguration config;
try {
DefaultLog.setInstance(log);
String[] args = {"--dir", testPath.toFile().getAbsolutePath()};
config = OptionCollection.parseCommands(testPath.toFile(), args, (o) -> {
}, true);
CommandLine commandLine = OptionCollection.parseCommandLine(opts, new String[]{"--deprecated-option", "."});
commandLine.hasOption("--deprecated-option");
} finally {
DefaultLog.setInstance(null);
}
assertThat(config).isNotNull();
log.assertContainsExactly(1,"WARN: Option [-d, --dir] used. Deprecated for removal since 0.17: Use the standard '--'");
log.assertContainsExactly(1, "WARN: Option [--deprecated-option] used. Deprecated for removal since 1.0.0-SNAPSHOT: Deprecation reason.");
}

@Test
public void testShortenedOptions() throws IOException {
public void testShortenedOptions() throws IOException, ParseException {
String[] args = {"--output-lic", "ALL"};
ReportConfiguration config = OptionCollection.parseCommands(testPath.toFile(), args, (o) -> {
}, true);
Expand All @@ -227,7 +215,7 @@ public void testDefaultConfiguration() throws ParseException {

@ParameterizedTest
@ValueSource(strings = { ".", "./", "target", "./target" })
public void getReportableTest(String fName) throws IOException {
public void getReportableTest(String fName) throws IOException, ParseException {
File base = new File(fName);
String expected = DocumentName.FSInfo.getDefault().normalize(base.getAbsolutePath());
ReportConfiguration config = OptionCollection.parseCommands(testPath.toFile(), new String[]{fName}, o -> fail("Help called"), false);
Expand Down Expand Up @@ -263,7 +251,7 @@ public void helpTest() {
ReportConfiguration config = OptionCollection.parseCommands(testPath.toFile(), args, o -> helpCalled.set(true), true);
assertThat(config).as("Should not have config").isNull();
assertThat(helpCalled.get()).as("Help was not called").isTrue();
} catch (IOException e) {
} catch (IOException | ParseException e) {
fail(e.getMessage());
}
}
Expand All @@ -282,7 +270,7 @@ public CliOptionsProvider() {
* @return A ReportConfiguration
* @throws IOException on critical error.
*/
protected final ReportConfiguration generateConfig(List<Pair<Option, String[]>> args) throws IOException {
protected final ReportConfiguration generateConfig(List<Pair<Option, String[]>> args) throws IOException, ParseException {
helpCalled.set(false);
List<String> sArgs = new ArrayList<>();
for (Pair<Option, String[]> pair : args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.SortedSet;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.rat.ReportConfiguration.NoCloseOutputStream;
Expand Down Expand Up @@ -662,52 +664,54 @@ public void licenseDuplicateOptionsTest() {
.isExactlyInstanceOf(IllegalArgumentException.class);
}


/**
* Validates that the configuration contains the default approved licenses.
* @param config The configuration to test.
*/
public static void validateDefaultApprovedLicenses(ReportConfiguration config) {
validateDefaultApprovedLicenses(config, 0);
public static void validateDefaultApprovedLicenses(ReportConfiguration config, String... additionalIds) {
validateLicenses(config, Arrays.asList(additionalIds), LicenseFilter.APPROVED, XMLConfigurationReaderTest.APPROVED_LICENSES);
}

/**
* Validates that the configuration contains the default approved licenses.
* @param config The configuration to test.
* Validates that the configuration contains all the default licenses along with any addiitonal licenses
* @param config the configuration to test.
* @param additionalLicenses Additional licence IDs that are expected.
*/
public static void validateDefaultApprovedLicenses(ReportConfiguration config, int additionalIdCount) {
assertThat(config.getLicenseCategories(LicenseFilter.APPROVED)).hasSize(XMLConfigurationReaderTest.APPROVED_IDS.length + additionalIdCount);
for (String s : XMLConfigurationReaderTest.APPROVED_IDS) {
assertThat(config.getLicenseCategories(LicenseFilter.APPROVED)).contains(ILicenseFamily.makeCategory(s));
}
public static void validateDefaultLicenses(ReportConfiguration config, String...additionalLicenses) {
validateLicenses(config, Arrays.asList(additionalLicenses), LicenseFilter.ALL, XMLConfigurationReaderTest.EXPECTED_LICENSES);
}

private static void validateLicenses(ReportConfiguration config, List<String> additionalIds, LicenseFilter filter, String[] approvedIds) {
List<String> expected = new ArrayList<>(Arrays.asList(approvedIds));
expected.addAll(additionalIds);
assertThat(config.getLicenses(filter).stream().map(ILicense::getId).collect(Collectors.toSet())).containsExactlyInAnyOrderElementsOf(expected);
}


/**
* Validates that the configuration contains the default license families.
* @param config the configuration to test.
*/
public static void validateDefaultLicenseFamilies(ReportConfiguration config, String...additionalIds) {
assertThat(config.getLicenseFamilies(LicenseFilter.ALL)).hasSize(XMLConfigurationReaderTest.EXPECTED_IDS.length + additionalIds.length);
List<String> expected = new ArrayList<>();
expected.addAll(Arrays.asList(XMLConfigurationReaderTest.EXPECTED_IDS));
expected.addAll(Arrays.asList(additionalIds));
for (ILicenseFamily family : config.getLicenseFamilies(LicenseFilter.ALL)) {
assertThat(expected).contains(family.getFamilyCategory().trim());
}
validateLicenseFamilies(config, Arrays.asList(additionalIds), LicenseFilter.ALL, XMLConfigurationReaderTest.EXPECTED_IDS);
}

/**
* Validates that the configuration contains the default licenses.
* Validates that the configuration contains the default license families.
* @param config the configuration to test.
*/
public static void validateDefaultLicenses(ReportConfiguration config, String...additionalLicenses) {
assertThat(config.getLicenses(LicenseFilter.ALL)).hasSize(XMLConfigurationReaderTest.EXPECTED_LICENSES.length + additionalLicenses.length);
List<String> expected = new ArrayList<>();
expected.addAll(Arrays.asList(XMLConfigurationReaderTest.EXPECTED_LICENSES));
expected.addAll(Arrays.asList(additionalLicenses));
for (ILicense license : config.getLicenses(LicenseFilter.ALL)) {
assertThat(expected).contains(license.getId());
}
public static void validateDefaultApprovedLicenseFamilies(ReportConfiguration config, String...additionalIds) {
validateLicenseFamilies(config, Arrays.asList(additionalIds), LicenseFilter.APPROVED, XMLConfigurationReaderTest.APPROVED_IDS);
}

private static void validateLicenseFamilies(ReportConfiguration config, List<String> additionalIds, LicenseFilter filter, String[] approvedIds) {
List<String> expected = new ArrayList<>(Arrays.asList(approvedIds));
expected.addAll(additionalIds);
assertThat(config.getLicenseFamilies(filter).stream().map(lf -> lf.getFamilyCategory().trim())
.collect(Collectors.toSet())).containsExactlyInAnyOrderElementsOf(expected);
}


/**
* Validates that the configuration matches the default.
Expand All @@ -719,9 +723,10 @@ public static void validateDefault(ReportConfiguration config) {
assertThat(config.getCopyrightMessage()).isNull();
assertThat(config.getStyleSheet()).withFailMessage("Stylesheet should not be null").isNotNull();

validateDefaultApprovedLicenses(config);
validateDefaultLicenseFamilies(config);
validateDefaultApprovedLicenseFamilies(config);
validateDefaultLicenses(config);
validateDefaultApprovedLicenses(config);
}

/**
Expand Down
Loading
Loading