Skip to content

Commit

Permalink
WIP tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsromero committed Mar 7, 2025
1 parent 45dd407 commit 2bf959a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.Set;
import java.util.logging.Logger;

import static org.asciidoctor.maven.process.SourceDirectoryFinder.DEFAULT_SOURCE_DIR;

import org.apache.commons.io.FilenameUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -35,8 +37,6 @@
import org.asciidoctor.maven.process.SourceDirectoryFinder;
import org.asciidoctor.maven.process.SourceDocumentFinder;

import static org.asciidoctor.maven.process.SourceDirectoryFinder.DEFAULT_SOURCE_DIR;


/**
* Basic maven plugin goal to convert AsciiDoc files using AsciidoctorJ.
Expand Down Expand Up @@ -241,7 +241,9 @@ public void processSources(List<File> sourceFiles, ResourcesProcessor resourcesP
Logger.getLogger("asciidoctor").setUseParentHandlers(false);

final Set<File> uniquePaths = new HashSet<>();
for (final File source : sourceFiles) {
for (int i = 0; i < sourceFiles.size(); i++) {
// for (final File source : sourceFiles) {
final File source = sourceFiles.get(i);
final Destination destination = setDestinationPaths(source, optionsBuilder, sourceDir, this);
final File destinationPath = destination.path;
if (!uniquePaths.add(destinationPath)) {
Expand All @@ -253,14 +255,9 @@ public void processSources(List<File> sourceFiles, ResourcesProcessor resourcesP
getLog().warn("Duplicated destination found: overwriting file: " + destinationFile);
}

convertFile(asciidoctor, optionsBuilder.build(), source);

if (logHandler.getFailFast()) {
processLogRecords(sourceDir, memoryLogHandler);
}
boolean lastFile = i == (sourceFiles.size() - 1);
convertFile(asciidoctor, optionsBuilder.build(), source, sourceDir, memoryLogHandler, lastFile);
}

// processLogRecords(sourceDir, memoryLogHandler);
}

private void processLogRecords(File sourceDir, MemoryLogHandler memoryLogHandler) throws MojoExecutionException {
Expand Down Expand Up @@ -357,9 +354,12 @@ protected List<File> findSourceFiles(File sourceDirectory) {
finder.find(sourceDirectoryPath, sourceDocumentExtensions);
}

protected void convertFile(Asciidoctor asciidoctor, Options options, File f) {
private void convertFile(Asciidoctor asciidoctor, Options options, File f, File sourceDir, MemoryLogHandler memoryLogHandler, boolean lastFile) throws MojoExecutionException {
asciidoctor.convertFile(f, options);
logConvertedFile(f);
if (logHandler.getFailFast() || lastFile) {
processLogRecords(sourceDir, memoryLogHandler);
}
}

protected void logConvertedFile(File f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.asciidoctor.maven.log.FailIf;
import org.asciidoctor.maven.log.LogHandler;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import static org.asciidoctor.log.Severity.ERROR;
Expand Down Expand Up @@ -393,6 +394,48 @@ void should_not_print_default_AsciidoctorJ_messages() throws MojoFailureExceptio
consoleHolder.release();
}

@Nested
class WithFailOn {

@Test
void should_print_messages_for_all_sources_when_failFast_is_false() throws MojoFailureException, MojoExecutionException {
// setup
final ConsoleHolder consoleHolder = ConsoleHolder.start();

File srcDir = new File(DEFAULT_SOURCE_DIRECTORY, "errors");
File outputDir = newOutputTestDirectory("logHandler");
LogHandler logHandler = new LogHandler();
logHandler.setOutputToConsole(true);

// when
AsciidoctorMojo mojo = mockAsciidoctorMojo(logHandler);
mojo.backend = "html";
mojo.sourceDirectory = srcDir;
mojo.outputDirectory = outputDir;
mojo.execute();

// then
List<String> asciidoctorMessages = Arrays.stream(consoleHolder.getOutput().split("\n"))
.filter(line -> line.contains("asciidoctor:"))
.collect(Collectors.toList());

assertThat(asciidoctorMessages)
.hasSize(6);
assertThat(asciidoctorMessages.get(0))
.contains(fixOsSeparator("[info] asciidoctor: ERROR: errors/document-with-missing-include.adoc: line 3: include file not found:"));
assertThat(asciidoctorMessages.get(1))
.contains(fixOsSeparator("[info] asciidoctor: ERROR: errors/document-with-missing-include.adoc: line 5: include file not found:"));
assertThat(asciidoctorMessages.get(2))
.contains(fixOsSeparator("[info] asciidoctor: ERROR: errors/document-with-missing-include.adoc: line 9: include file not found:"));
assertThat(asciidoctorMessages.get(3))
.contains(fixOsSeparator("[info] asciidoctor: WARN: errors/document-with-missing-include.adoc: line 25: no callout found for <1>"));

// cleanup
consoleHolder.release();
}

}

private List<String> getOutputInfoLines(ConsoleHolder consoleHolder) {
final String lineSeparator = lineSeparator();
return Arrays.stream(consoleHolder.getOutput().split(lineSeparator))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
= Document Title

This a valid document.

== Section A

No errors to be seen.

0 comments on commit 2bf959a

Please sign in to comment.