forked from phoedos/pmd
[doc] Fix build/unit tests under windows
* expect line names with windows path normalized to unix paths * normalize line endings * File.separator might be a backslash and needs to be escaped in regex
This commit is contained in:
@ -12,6 +12,7 @@ import java.nio.file.Path;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
@ -26,6 +27,7 @@ import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -300,6 +302,10 @@ public class RuleDocGenerator {
|
||||
return description;
|
||||
}
|
||||
|
||||
private static List<String> toLines(String s) {
|
||||
return Arrays.asList(s.split("\r\n|\n"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates for each ruleset a page. The page contains the details for each rule.
|
||||
*
|
||||
@ -375,12 +381,12 @@ public class RuleDocGenerator {
|
||||
lines.add("");
|
||||
}
|
||||
|
||||
lines.add(stripIndentation(rule.getDescription()));
|
||||
lines.addAll(toLines(stripIndentation(rule.getDescription())));
|
||||
lines.add("");
|
||||
|
||||
if (rule instanceof XPathRule || rule instanceof RuleReference && ((RuleReference) rule).getRule() instanceof XPathRule) {
|
||||
lines.add("```");
|
||||
lines.add(StringUtils.stripToEmpty(rule.getProperty(XPathRule.XPATH_DESCRIPTOR)));
|
||||
lines.addAll(toLines(StringUtils.stripToEmpty(rule.getProperty(XPathRule.XPATH_DESCRIPTOR))));
|
||||
lines.add("```");
|
||||
lines.add("");
|
||||
} else {
|
||||
@ -396,7 +402,7 @@ public class RuleDocGenerator {
|
||||
lines.add("");
|
||||
for (String example : rule.getExamples()) {
|
||||
lines.add("``` " + mapLanguageForHighlighting(languageTersename));
|
||||
lines.add(StringUtils.stripToEmpty(example));
|
||||
lines.addAll(toLines(StringUtils.stripToEmpty(example)));
|
||||
lines.add("```");
|
||||
lines.add("");
|
||||
}
|
||||
@ -540,7 +546,8 @@ public class RuleDocGenerator {
|
||||
}
|
||||
|
||||
private String getRuleClassSourceFilepath(String ruleClass) throws IOException {
|
||||
final String relativeSourceFilename = ruleClass.replaceAll("\\.", File.separator) + ".java";
|
||||
final String relativeSourceFilename = ruleClass.replaceAll("\\.", Matcher.quoteReplacement(File.separator))
|
||||
+ ".java";
|
||||
final List<Path> foundPathResult = new LinkedList<>();
|
||||
|
||||
Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
|
||||
|
@ -15,8 +15,10 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.apache.commons.lang3.SystemUtils;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
|
||||
import org.yaml.snakeyaml.DumperOptions.LineBreak;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import net.sourceforge.pmd.RuleSet;
|
||||
@ -92,6 +94,9 @@ public class SidebarGenerator {
|
||||
public void writeSidebar(Map<String, Object> sidebar) throws IOException {
|
||||
DumperOptions options = new DumperOptions();
|
||||
options.setDefaultFlowStyle(FlowStyle.BLOCK);
|
||||
if (SystemUtils.IS_OS_WINDOWS) {
|
||||
options.setLineBreak(LineBreak.WIN);
|
||||
}
|
||||
Yaml yaml = new Yaml(options);
|
||||
writer.write(sidebarPath, Arrays.asList(yaml.dump(sidebar)));
|
||||
System.out.println("Generated " + sidebarPath);
|
||||
|
@ -8,15 +8,15 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@ -38,9 +38,14 @@ public class RuleDocGeneratorTest {
|
||||
|
||||
root = Files.createTempDirectory("pmd-ruledocgenerator-test");
|
||||
Files.createDirectories(root.resolve("docs/_data/sidebars"));
|
||||
try (Writer out = Files.newBufferedWriter(root.resolve("docs/_data/sidebars/pmd_sidebar.yml"), StandardCharsets.UTF_8)) {
|
||||
IOUtils.write("entries:\n- title: sidebar\n folders:\n - title: 1\n - title: 2\n - title: Rules\n", out);
|
||||
}
|
||||
List<String> mockedSidebar = Arrays.asList(
|
||||
"entries:",
|
||||
"- title: sidebar",
|
||||
" folders:",
|
||||
" - title: 1",
|
||||
" - title: 2",
|
||||
" - title: Rules");
|
||||
Files.write(root.resolve("docs/_data/sidebars/pmd_sidebar.yml"), mockedSidebar);
|
||||
}
|
||||
|
||||
@After
|
||||
@ -74,17 +79,17 @@ public class RuleDocGeneratorTest {
|
||||
|
||||
assertEquals(3, writer.getData().size());
|
||||
FileEntry languageIndex = writer.getData().get(0);
|
||||
assertTrue(languageIndex.getFilename().endsWith("docs/pages/pmd/rules/java.md"));
|
||||
assertTrue(FilenameUtils.normalize(languageIndex.getFilename(), true).endsWith("docs/pages/pmd/rules/java.md"));
|
||||
assertEquals(IOUtils.toString(RuleDocGeneratorTest.class.getResourceAsStream("/expected/java.md")),
|
||||
languageIndex.getContent());
|
||||
|
||||
FileEntry ruleSetIndex = writer.getData().get(1);
|
||||
assertTrue(ruleSetIndex.getFilename().endsWith("docs/pages/pmd/rules/java/sample.md"));
|
||||
assertTrue(FilenameUtils.normalize(ruleSetIndex.getFilename(), true).endsWith("docs/pages/pmd/rules/java/sample.md"));
|
||||
assertEquals(IOUtils.toString(RuleDocGeneratorTest.class.getResourceAsStream("/expected/sample.md")),
|
||||
ruleSetIndex.getContent());
|
||||
|
||||
FileEntry sidebar = writer.getData().get(2);
|
||||
assertTrue(sidebar.getFilename().endsWith("docs/_data/sidebars/pmd_sidebar.yml"));
|
||||
assertTrue(FilenameUtils.normalize(sidebar.getFilename(), true).endsWith("docs/_data/sidebars/pmd_sidebar.yml"));
|
||||
assertEquals(IOUtils.toString(RuleDocGeneratorTest.class.getResourceAsStream("/expected/pmd_sidebar.yml")),
|
||||
sidebar.getContent());
|
||||
}
|
||||
|
@ -15,10 +15,12 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.SystemUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
|
||||
import org.yaml.snakeyaml.DumperOptions.LineBreak;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import net.sourceforge.pmd.RuleSet;
|
||||
@ -47,6 +49,9 @@ public class SidebarGeneratorTest {
|
||||
|
||||
DumperOptions options = new DumperOptions();
|
||||
options.setDefaultFlowStyle(FlowStyle.BLOCK);
|
||||
if (SystemUtils.IS_OS_WINDOWS) {
|
||||
options.setLineBreak(LineBreak.WIN);
|
||||
}
|
||||
String yaml = new Yaml(options).dump(result);
|
||||
|
||||
assertEquals(IOUtils.toString(SidebarGeneratorTest.class.getResourceAsStream("sidebar.yml")), yaml);
|
||||
|
Reference in New Issue
Block a user