elharo commented on code in PR #1737: URL: https://github.com/apache/maven-site/pull/1737#discussion_r4362002682
########## src/test/java/org/apache/maven/site/MarkdownDocsTest.java: ########## @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.site; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Stream; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +class MarkdownDocsTest { + private static final Pattern UNESCAPED_ISSUE_LINK = + Pattern.compile("\\[\\[([A-Z][A-Z0-9]*-[0-9]+)]]\\((https?://[^)\\s]+)\\)"); + + @Test + void markdownDocsEscapeIssueKeysInLinkLabels() throws IOException { + Path contentDirectory = Paths.get("content/markdown"); + List<String> violations = new ArrayList<>(); + + try (Stream<Path> files = Files.walk(contentDirectory)) { + files.filter(path -> { + String name = path.getFileName().toString(); + return name.endsWith(".md") || name.endsWith(".md.vm"); + }) + .sorted() + .forEach(path -> collectViolations(contentDirectory, path, violations)); + } + + assertTrue( + violations.isEmpty(), + () -> "Issue keys in link labels must have their square brackets escaped in Markdown docs, " + + "otherwise the generated link target is the issue key instead of the URL. " + + "Use [\\[MNG-1234\\]](https://issues.apache.org/jira/browse/MNG-1234).\n" + + String.join("\n", violations)); + } + + private static void collectViolations(Path contentDirectory, Path file, List<String> violations) { + List<String> lines; + try { + lines = Files.readAllLines(file); + } catch (IOException e) { + throw new IllegalStateException("Unable to read " + file, e); Review Comment: just bubble the IOException and let it fail the test; no need for try catch ########## src/test/java/org/apache/maven/site/MarkdownDocsTest.java: ########## @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.site; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Stream; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +class MarkdownDocsTest { + private static final Pattern UNESCAPED_ISSUE_LINK = + Pattern.compile("\\[\\[([A-Z][A-Z0-9]*-[0-9]+)]]\\((https?://[^)\\s]+)\\)"); + + @Test + void markdownDocsEscapeIssueKeysInLinkLabels() throws IOException { + Path contentDirectory = Paths.get("content/markdown"); + List<String> violations = new ArrayList<>(); + + try (Stream<Path> files = Files.walk(contentDirectory)) { + files.filter(path -> { + String name = path.getFileName().toString(); + return name.endsWith(".md") || name.endsWith(".md.vm"); + }) + .sorted() + .forEach(path -> collectViolations(contentDirectory, path, violations)); + } + + assertTrue( + violations.isEmpty(), + () -> "Issue keys in link labels must have their square brackets escaped in Markdown docs, " + + "otherwise the generated link target is the issue key instead of the URL. " + + "Use [\\[MNG-1234\\]](https://issues.apache.org/jira/browse/MNG-1234).\n" + + String.join("\n", violations)); + } + + private static void collectViolations(Path contentDirectory, Path file, List<String> violations) { + List<String> lines; + try { + lines = Files.readAllLines(file); + } catch (IOException e) { + throw new IllegalStateException("Unable to read " + file, e); + } + + for (int i = 0; i < lines.size(); i++) { + Matcher matcher = UNESCAPED_ISSUE_LINK.matcher(lines.get(i)); + while (matcher.find()) { + violations.add(String.format( Review Comment: This is very specific. Is it possible to do a more generic verification of well-formed markdown? -- 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]
