Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ParseException seems to be missing in Javadoc.


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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we log something before exiting with status 1?

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 IOException, 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
Loading
Loading