Skip to content

Commit

Permalink
Issue checkstyle#14759: Updated Command Line Options in table format
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoutsilis authored and rdiachenko committed Oct 10, 2024
1 parent 5b301c7 commit c904143
Show file tree
Hide file tree
Showing 2 changed files with 365 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -46,14 +47,9 @@ public class CliOptionsXdocsSyncTest {

@Test
public void validateCliDocSections() throws Exception {
final Map<String, String> cmdDesc = new HashMap<>();

final NodeList sections = getSectionsFromXdoc("src/xdocs/cmdline.xml.vm");
final Set<String> cmdOptions = getListById(sections.item(2), "CLI_Options");
for (String option : cmdOptions) {
final String text = option.trim().replaceAll("\\s+", " ");
cmdDesc.put(text.substring(0, 2), text.substring(text.indexOf(" - ") + 3));
}
final Node cmdUsageSection = sections.item(2);
final Map<String, String> cmdOptions = getOptions(cmdUsageSection);

final Class<?> cliOptions = Class.forName("com.puppycrawl.tools.checkstyle"
+ ".Main$CliOptions");
Expand All @@ -63,24 +59,24 @@ public void validateCliDocSections() throws Exception {
for (OptionSpec opt : optionSpecList) {
final String option = opt.names()[0];
if ("-h".equals(option) || "-V".equals(option)) {
cmdDesc.remove(option);
cmdOptions.remove(option);
continue;
}
final String descXdoc = cmdDesc.get(option);
final String descXdoc = cmdOptions.get(option);
final String descMain = opt.description()[0];
assertWithMessage("CLI Option: " + option + " present in "
+ "Main.java but not documented in cmdline.xml.vm")
.that(descXdoc)
.isNotNull();
assertWithMessage("CLI options descriptions in xdoc: "
+ " should match that of in Main.java")
.that(descMain)
.isEqualTo(descXdoc);
cmdDesc.remove(option);
.that(descMain)
.isEqualTo(descXdoc);
cmdOptions.remove(option);
}
assertWithMessage("CLI Options: %s present in cmdline.xml.vm, not documented in Main.java",
cmdDesc)
.that(cmdDesc)
cmdOptions)
.that(cmdOptions)
.isEmpty();
}

Expand Down Expand Up @@ -133,14 +129,26 @@ private static NodeList getSectionsFromXdoc(String xdocPath) throws Exception {
return document.getElementsByTagName("section");
}

private static Set<String> getListById(Node subSection, String id) {
Set<String> result = null;
final Node node = XmlUtil.findChildElementById(subSection, id);
if (node != null) {
result = XmlUtil.getChildrenElements(node)
.stream()
.map(Node::getTextContent)
.collect(Collectors.toUnmodifiableSet());
private static Map<String, String> getOptions(Node subSection) {
final Map<String, String> result = new HashMap<>();
final Node subsectionNode = XmlUtil.findChildElementById(subSection,
"Command_line_usage_Command_Line_Options");
if (subsectionNode != null) {
final Node divNode = XmlUtil.getFirstChildElement(subsectionNode);
final Node tableNode = XmlUtil.getFirstChildElement(divNode);
final Node tbodyNode = XmlUtil.findChildElementById(tableNode, "body");
final Set<Node> rows = XmlUtil.findChildElementsByTag(tbodyNode, "tr");
final List<List<Node>> columns = rows.stream()
.map(row -> new ArrayList<>(XmlUtil.getChildrenElements(row)))
.collect(Collectors.toUnmodifiableList());
for (List<Node> column : columns) {
final Node command = column.get(1);
final Node description = column.get(2);
if (command != null && description != null) {
result.put(command.getTextContent().trim().substring(0, 2),
description.getTextContent().trim().replaceAll("\\s+", " "));
}
}
}
return result;
}
Expand Down
Loading

0 comments on commit c904143

Please sign in to comment.