diff --git a/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java b/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java index 95782c7a..71e9f627 100644 --- a/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java +++ b/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java @@ -244,8 +244,9 @@ public void processSources(List sourceFiles, ResourcesProcessor resourcesP final Set uniquePaths = new HashSet<>(); for (final File source : sourceFiles) { final File destinationPath = setDestinationPaths(source, optionsBuilder, sourceDir, this); - if (!uniquePaths.add(destinationPath)) + if (!uniquePaths.add(destinationPath)) { getLog().warn("Duplicated destination found: overwriting file: " + destinationPath.getAbsolutePath()); + } convertFile(asciidoctor, optionsBuilder.asMap(), source); @@ -379,9 +380,8 @@ public File setDestinationPaths(final File sourceFile, final OptionsBuilder opti optionsBuilder.toDir(outputDir).destinationDir(outputDir); } final File outputFile = configuration.getOutputFile(); - final String destinationDir = (String) optionsBuilder.asMap().get(Options.DESTINATION_DIR); + final String destinationDir = getDestinationDir(optionsBuilder); if (outputFile != null) { - // allow overriding the output file name optionsBuilder.toFile(outputFile); return outputFile.isAbsolute() ? outputFile : new File(destinationDir, outputFile.getPath()); } else { @@ -392,6 +392,12 @@ public File setDestinationPaths(final File sourceFile, final OptionsBuilder opti } } + private static String getDestinationDir(OptionsBuilder optionsBuilder) { + final Map options = optionsBuilder.asMap(); + return (String) Optional.ofNullable(options.get(Options.DESTINATION_DIR)) + .orElse(options.get(Options.TO_DIR)); + } + protected Asciidoctor getAsciidoctorInstance(String gemPath) throws MojoExecutionException { Asciidoctor asciidoctor = null; if (gemPath == null) { diff --git a/src/test/java/org/asciidoctor/maven/AsciidoctorMojoTest.java b/src/test/java/org/asciidoctor/maven/AsciidoctorMojoTest.java index d6342af5..31d840e7 100644 --- a/src/test/java/org/asciidoctor/maven/AsciidoctorMojoTest.java +++ b/src/test/java/org/asciidoctor/maven/AsciidoctorMojoTest.java @@ -5,12 +5,17 @@ import org.apache.maven.model.Resource; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.asciidoctor.Options; +import org.asciidoctor.OptionsBuilder; import org.asciidoctor.maven.extensions.ExtensionConfiguration; import org.asciidoctor.maven.io.AsciidoctorFileScanner; import org.asciidoctor.maven.io.ConsoleHolder; import org.asciidoctor.maven.test.processors.RequireCheckerTreeprocessor; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; +import org.junit.jupiter.params.provider.ValueSource; import java.io.*; import java.nio.file.Files; @@ -1069,4 +1074,22 @@ public void should_not_show_message_when_overwriting_files_using_outputFile_and_ consoleHolder.release(); } + @ParameterizedTest + @NullAndEmptySource + @ValueSource(strings = {"output.html"}) + public void should_return_absolute_path_when_calculating_destination(String outputFile) throws MojoExecutionException { + // given + final AsciidoctorMojo mojo = mockAsciidoctorMojo(); + mojo.setOutputDirectory(newOutputTestDirectory("overlapping-outputFile")); + if (outputFile != null) mojo.setOutputFile(outputFile); + final File source = new File("source.adoc"); + final OptionsBuilder builder = Options.builder(); + final File sourceDir = new File("."); + + // when + File file = mojo.setDestinationPaths(source, builder, sourceDir, mojo); + + // then + Assertions.assertThat(file).isAbsolute(); + } }