Skip to content

Commit

Permalink
Check for both destinationDir and toDir when calculating destination
Browse files Browse the repository at this point in the history
Newer versions of AsciidoctorJ deprecated destinationDir and only use
toDir, which caused the plugin to report paths being overwritten incorrectly.

Closes asciidoctor#728
  • Loading branch information
abelsromero committed Jan 5, 2024
1 parent 5b67431 commit 533238f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,9 @@ public void processSources(List<File> sourceFiles, ResourcesProcessor resourcesP
final Set<File> 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);

Expand Down Expand Up @@ -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 {
Expand All @@ -392,6 +392,12 @@ public File setDestinationPaths(final File sourceFile, final OptionsBuilder opti
}
}

private static String getDestinationDir(OptionsBuilder optionsBuilder) {
final Map<String, Object> 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) {
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/org/asciidoctor/maven/AsciidoctorMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 533238f

Please sign in to comment.