elharo commented on code in PR #1737: URL: https://github.com/apache/maven-site/pull/1737#discussion_r4376972864
########## 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: Updated: the test now collects the file list first and lets `collectViolations` propagate `IOException`, so a read failure fails the test directly. ########## 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: Updated: the test now parses each document with flexmark (the parser used by Doxia's Markdown module) and flags any wiki link immediately followed by a parenthesized destination. This catches `[[...]](...)` for any label, not just JIRA keys, and fenced code blocks are skipped by the parser. ########## 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)); + } Review Comment: Updated the PR description. The test intentionally covers all Markdown sources under `content/markdown` (`*.md` and `*.md.vm`). ########## 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: The test now reads files with `StandardCharsets.UTF_8`. -- 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]
