ottlinger commented on code in PR #252:
URL: https://github.com/apache/creadur-rat/pull/252#discussion_r1603972584
##########
apache-rat-core/src/test/java/org/apache/rat/ReportTest.java:
##########
@@ -145,11 +176,193 @@ public void LicensesOptionTest() throws Exception {
@Test
public void LicensesOptionNoDefaultsTest() throws Exception {
CommandLine cl = new DefaultParser().parse(Report.buildOptions(), new
String[] {"--no-default", "--licenses",
"target/test-classes/report/LicenseOne.xml", "--licenses",
"target/test-classes/report/LicenseTwo.xml"});
- ReportConfiguration config = Report.createConfiguration("", cl);
+ ReportConfiguration config =
Report.createConfiguration(DefaultLog.getInstance(), "", cl);
SortedSet<ILicense> set =
config.getLicenses(LicenseSetFactory.LicenseFilter.ALL);
assertEquals(2, set.size());
assertTrue(LicenseSetFactory.search("LiOne", "LiOne",
set).isPresent(), "LiOne");
assertFalse(LicenseSetFactory.search("LiOne", "LiTwo",
set).isPresent(), "LiOne/LiTwo");
assertTrue(LicenseSetFactory.search("LiTwo", "LiTwo",
set).isPresent(), "LiTwo");
}
+
+ @ParameterizedTest
+ @MethodSource("optionsProvider")
+ public void testOptionsUpdateConfig(String[] args,
Predicate<ReportConfiguration> test) throws Exception {
+ ReportConfiguration config = Report.parseCommands(args, (o)-> {},
true);
+ assertNotNull(config, "Did not create ReportConfiguration");
+ assertTrue(test.test(config));
+ }
+
+ static Stream<Arguments> optionsProvider() {
+
+ List<Arguments> lst = new ArrayList<>();
+ String[] args;
+ Predicate<ReportConfiguration> test;
+
+// Report.ARCHIVE
+ for (ReportConfiguration.Processing processing :
ReportConfiguration.Processing.values()) {
+ args = new String[]{longOpt(Report.ARCHIVE),
processing.name().toLowerCase()};
+ test = c -> c.getArchiveProcessing() == processing;
+ lst.add(Arguments.of(args, test));
+ }
+// Report.COPYRIGHT
+ args = new String[]{longOpt(Report.COPYRIGHT), "My copyright
statement"};
+ test = c -> c.getCopyrightMessage() == null;
+ lst.add(Arguments.of(args, test));
+ args = new String[]{longOpt(Report.COPYRIGHT), "My copyright
statement",
+ "--" + Report.ADD.getOptions().stream().filter(o ->
!o.isDeprecated()).findAny().get().getLongOpt()};
+ test = c -> "My copyright statement".equals(c.getCopyrightMessage());
+ lst.add(Arguments.of(args, test));
+
+// Report.DIR;
+
+// Report.DRY_RUN;
+ args = new String[]{"--" + Report.DRY_RUN.getLongOpt()};
+ test = ReportConfiguration::isDryRun;
+ lst.add(Arguments.of(args, test));
+
+// Report.EXCLUDE_CLI;
+ args = new String[]{"--" + Report.EXCLUDE_CLI.getLongOpt(),
"*.foo","[A-Z]\\.bar", "justbaz"};
+ test = c -> {
+ FilenameFilter f = c.getFilesToIgnore();
+
+ assertNotNull(f);
+ assertFalse(f.accept(tempDirectory, "some.foo" ), "some.foo");
+ assertFalse(f.accept(tempDirectory, "B.bar"), "B.bar");
+ assertFalse(f.accept(tempDirectory, "justbaz" ), "justbaz");
+ assertTrue(f.accept(tempDirectory, "notbaz"), "notbaz");
+ return true;
+ };
+ lst.add(Arguments.of(args, test));
+
+// Report.EXCLUDE_FILE_CLI;
+ File outputFile = new File(tempDirectory, "exclude.txt");
+ try (FileWriter fw = new FileWriter(outputFile)) {
+ fw.write("*.foo");
+ fw.write(System.lineSeparator());
+ fw.write("[A-Z]\\.bar");
+ fw.write(System.lineSeparator());
+ fw.write("justbaz");
+ fw.write(System.lineSeparator());
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ args = new String[]{"--" + Report.EXCLUDE_FILE_CLI.getLongOpt(),
outputFile.getPath()};
+ // same test as above
+ lst.add(Arguments.of(args, test));
+
+// Report.FORCE
+ args = new String[]{longOpt(Report.FORCE)};
+ test = ReportConfiguration::isAddingLicensesForced;
+ lst.add(Arguments.of(args, test.negate()));
+
+ args = new String[]{longOpt(Report.FORCE),
+ "--" + Report.ADD.getOptions().stream().filter(o ->
!o.isDeprecated()).findAny().get().getLongOpt()};
+ lst.add(Arguments.of(args, test));
+
+// Report.LICENSES -- tested in standalone test: testLicenseOption()
+
+// Report.LIST_LICENSES;
+ for (LicenseSetFactory.LicenseFilter f :
LicenseSetFactory.LicenseFilter.values()) {
+ args = new String[]{"--" + Report.LIST_LICENSES.getLongOpt(),
f.name().toLowerCase()};
+ test = c -> c.listLicenses() == f;
+ lst.add(Arguments.of(args, test));
+ }
+
+// Report.LIST_FAMILIES
+ for (LicenseSetFactory.LicenseFilter f :
LicenseSetFactory.LicenseFilter.values()) {
+ args = new String[]{"--" + Report.LIST_FAMILIES.getLongOpt(),
f.name().toLowerCase()};
+ test = c -> c.listFamilies() == f;
+ lst.add(Arguments.of(args, test));
+ }
+
+// Report LOG_LEVEL
+ DefaultLog log = (DefaultLog) DefaultLog.getInstance();
+ for (Log.Level l : Log.Level.values()) {
+ args = new String[]{"--" + Report.LOG_LEVEL.getLongOpt(),
l.name().toLowerCase()};
+ test = c -> log.getLevel() == l;
+ lst.add(Arguments.of(args, test));
+ }
+// Report.NO_DEFAULTS
+ args = new String[]{"--" + Report.NO_DEFAULTS.getLongOpt()};
+ test = c -> {
+ assertEquals(0,
c.getLicenses(LicenseSetFactory.LicenseFilter.ALL).size());
+ return true;
+ };
+ lst.add(Arguments.of(args, test));
+
+// Report.OUT;
+ File outFile = new File( tempDirectory, "outexample");
+ args = new String[] {"--"+Report.OUT.getLongOpt(),
outFile.getAbsolutePath()};
+ test = c -> {
+ try (OutputStream os = c.getOutput().get()) {
+ os.write("Hello world".getBytes());
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ try (BufferedReader reader = new BufferedReader( new
InputStreamReader( new FileInputStream(outFile)))) {
+ return "Hello world".equals(reader.readLine());
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ };
+ lst.add(Arguments.of(args, test));
+
+// Report.SCAN_HIDDEN_DIRECTORIES
+ args = new String[]{"--" +
Report.SCAN_HIDDEN_DIRECTORIES.getLongOpt()};
+ test = c -> c.getDirectoriesToIgnore() == FalseFileFilter.FALSE;
+ lst.add(Arguments.of(args, test));
+
+// Report.STYLESHEET_CLI
+ test = c -> c.getStyleSheet() != null;
+ for (String sheet : new String[]{"plain-rat", "missing-headers",
"unapproved-licenses"}) {
+ args = new String[]{"--" + Report.STYLESHEET_CLI.getLongOpt(),
sheet};
+ lst.add(Arguments.of(args, test));
+ }
+ URL url = ReportTest.class.getResource("MatcherContainerResource.txt");
+ args = new String[]{"--" + Report.STYLESHEET_CLI.getLongOpt(),
url.getFile()};
+ lst.add(Arguments.of(args, test));
+
+// Report.XML;
+ args = new String[]{longOpt(Report.XML)};
+ test = ReportConfiguration::isStyleReport;
+ lst.add(Arguments.of(args, test.negate()));
+ return lst.stream();
+ }
+
+ @Test
+ public void testDeprecatedUseLogged() throws IOException {
+ TestingLog log = new TestingLog();
+ try {
+ DefaultLog.setInstance(log);
+ String[] args = {longOpt(Report.DIR), "foo", "-a"};
+ ReportConfiguration config = Report.parseCommands(args, (o) -> {
+ }, true);
+
+ } finally {
+ DefaultLog.setInstance(null);
+ }
+ log.assertContains("WARN: Option [-d, --dir] used. Deprecated for
removal since 0.17.0: Use '--'");
Review Comment:
Can you change this to 0.17 as we have 2digit version numbers only at the
moment. This will make certain tests fail, but will be the correct version.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]