This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-doxia-converter.git

commit cd9676c8aa59ee70094e4ac49e0d0c1b9a53e17e
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Thu Nov 5 02:27:49 2026 +0000

    Keep existing target-format documents in the shared source directory 
duplicate check
    
    A hand-written usage.md next to usage.apt was skipped before it reached the 
duplicate check, so the conversion overwrote it. The in-place warning now also 
covers nested input and output directories, and the tests use @TempDir.
---
 .../org/apache/maven/doxia/DefaultConverter.java   | 17 ++++++++---
 .../java/org/apache/maven/doxia/ConverterTest.java | 33 ++++++++++++++++------
 2 files changed, 37 insertions(+), 13 deletions(-)

diff --git a/src/main/java/org/apache/maven/doxia/DefaultConverter.java 
b/src/main/java/org/apache/maven/doxia/DefaultConverter.java
index 492c5a0..abea903 100644
--- a/src/main/java/org/apache/maven/doxia/DefaultConverter.java
+++ b/src/main/java/org/apache/maven/doxia/DefaultConverter.java
@@ -459,6 +459,12 @@ public class DefaultConverter implements Converter {
                     
SOURCE_EXTENSIONS.get(FileUtils.getExtension(name).toLowerCase(Locale.ENGLISH));
             if (format == output.getFormat()) {
                 LOGGER.debug("Skipping \"{}\", already in format {}", f, 
format);
+                // a document in the output format keeps its place, so no 
converted file may be written over it
+                File other = outputs.put(f.getAbsoluteFile(), f);
+                if (other != null) {
+                    throw new ConverterException("ConverterException: \"" + 
other + "\" and \"" + f
+                            + "\" would both be converted to \"" + f + "\"");
+                }
                 continue;
             }
             if (format.isXml()
@@ -489,11 +495,14 @@ public class DefaultConverter implements Converter {
                     new 
File(PathTool.getRelativeFilePath(input.getFile().getAbsolutePath(), 
f.getParent()));
             convert(f, input.getEncoding(), entry.getValue(), output, 
relativeOutputDirectory);
         }
-        if (postProcess == PostProcess.NONE
-                && 
input.getFile().getAbsoluteFile().equals(output.getFile().getAbsoluteFile())) {
+        Path inputPath = 
input.getFile().getAbsoluteFile().toPath().normalize();
+        Path outputPath = 
output.getFile().getAbsoluteFile().toPath().normalize();
+        if (postProcess == PostProcess.NONE && 
(outputPath.startsWith(inputPath) || inputPath.startsWith(outputPath))) {
             LOGGER.warn(
-                    "The converted documents sit next to their sources in {}; 
remove the sources, or Doxia Sitetools"
-                            + " reports each pair as a duplicate page",
+                    "The output directory {} and the input directory {} 
overlap, so converted documents sit next to"
+                            + " their sources; remove the sources, or Doxia 
Sitetools reports each pair as a"
+                            + " duplicate page",
+                    output.getFile(),
                     input.getFile());
         }
     }
diff --git a/src/test/java/org/apache/maven/doxia/ConverterTest.java 
b/src/test/java/org/apache/maven/doxia/ConverterTest.java
index 11404c6..4c51a34 100644
--- a/src/test/java/org/apache/maven/doxia/ConverterTest.java
+++ b/src/test/java/org/apache/maven/doxia/ConverterTest.java
@@ -46,6 +46,7 @@ import org.eclipse.sisu.launch.InjectedTest;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -492,9 +493,8 @@ class ConverterTest extends InjectedTest {
      * Markdown is converted next to its source, picking the parser by its 
extension.
      */
     @Test
-    void sharedSourceDirectoryInPlace() throws Exception {
-        File dir = new File(getBasedir(), "target/unit/shared/source");
-        FileUtils.deleteDirectory(dir);
+    void sharedSourceDirectoryInPlace(@TempDir File tempDir) throws Exception {
+        File dir = new File(tempDir, "source");
         String markdown = "# Index\n\nAlready Markdown.\n";
         write(new File(dir, "index.md"), markdown);
         write(new File(dir, "guide/usage.apt"), "Usage\n\n* APT\n\n  Text.\n");
@@ -518,14 +518,11 @@ class ConverterTest extends InjectedTest {
         assertTrue(readFile(new File(dir, 
"notes.md.vm")).contains("${project.version}"));
         assertEquals(markdown, readFile(new File(dir, "index.md")));
         assertFalse(new File(dir, "site.md").exists());
-
-        FileUtils.deleteDirectory(dir);
     }
 
     @Test
-    void sharedSourceDirectoryRejectsTwoSourcesForOneOutput() throws Exception 
{
-        File dir = new File(getBasedir(), 
"target/unit/shared-duplicate/source");
-        FileUtils.deleteDirectory(dir);
+    void sharedSourceDirectoryRejectsTwoSourcesForOneOutput(@TempDir File 
tempDir) throws Exception {
+        File dir = new File(tempDir, "source");
         write(new File(dir, "usage.apt"), "Usage\n\n* APT\n\n  Text.\n");
         write(new File(dir, "usage.xml"), "<document><body><section 
name=\"Usage\"/></body></document>");
 
@@ -536,8 +533,26 @@ class ConverterTest extends InjectedTest {
                         OutputFileWrapper.valueOf(dir.getPath(), 
DoxiaFormat.MARKDOWN, StandardCharsets.UTF_8.name())));
         assertTrue(e.getMessage().contains("usage.md"), e.getMessage());
         assertFalse(new File(dir, "usage.md").exists());
+    }
+
+    /**
+     * A hand-written document in the output format must not be overwritten by 
the conversion of a source that maps
+     * to the same file.
+     */
+    @Test
+    void sharedSourceDirectoryKeepsExistingTargetFormatDocument(@TempDir File 
tempDir) throws Exception {
+        File dir = new File(tempDir, "source");
+        String handWritten = "# Usage\n\nHand-written.\n";
+        write(new File(dir, "usage.md"), handWritten);
+        write(new File(dir, "usage.apt"), "Usage\n\n* APT\n\n  Text.\n");
 
-        FileUtils.deleteDirectory(dir);
+        ConverterException e = assertThrows(
+                ConverterException.class,
+                () -> converter.convert(
+                        InputFileWrapper.valueOf(dir.getPath(), null),
+                        OutputFileWrapper.valueOf(dir.getPath(), 
DoxiaFormat.MARKDOWN, StandardCharsets.UTF_8.name())));
+        assertTrue(e.getMessage().contains("usage.md"), e.getMessage());
+        assertEquals(handWritten, readFile(new File(dir, "usage.md")));
     }
 
     private static void write(File file, String content) throws IOException {

Reply via email to