From 5c3191a0c16f114a255531f0fd5a09898b425c79 Mon Sep 17 00:00:00 2001 From: Abel Salgado Romero Date: Sat, 24 Jul 2021 17:55:46 +0200 Subject: [PATCH] Fix excluding sources when enclosing parent path started with _ Fixes #545 --- .../asciidoctor/maven/AsciidoctorMojo.java | 8 +-- .../maven/process/SourceDocumentFinder.java | 28 +++++----- .../process/SourceDocumentFinderTest.java | 55 ++++++++++++------- .../source-finder/_enclosing/src/simple.adoc | 3 + 4 files changed, 55 insertions(+), 39 deletions(-) create mode 100644 src/test/resources/src/asciidoctor/source-finder/_enclosing/src/simple.adoc diff --git a/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java b/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java index 53184f18..47de8269 100644 --- a/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java +++ b/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java @@ -190,7 +190,7 @@ public void processSources(List sourceFiles, ResourcesProcessor resourcesP } if (sourceFiles == null) { - sourceFiles = calculateSourceFiles(sourceDirectoryCandidate.get()); + sourceFiles = findSourceFiles(sourceDirectoryCandidate.get()); } if (sourceFiles.isEmpty()) { getLog().info("No sources found. Skipping processing"); @@ -435,7 +435,7 @@ protected Asciidoctor getAsciidoctorInstance(String gemPath) throws MojoExecutio return asciidoctor; } - protected List calculateSourceFiles(File sourceDirectory) { + protected List findSourceFiles(File sourceDirectory) { if (sourceDocumentName != null) return Arrays.asList(new File(sourceDirectory, sourceDocumentName)); @@ -443,8 +443,8 @@ protected List calculateSourceFiles(File sourceDirectory) { SourceDocumentFinder finder = new SourceDocumentFinder(); return sourceDocumentExtensions.isEmpty() ? - finder.find(sourceDirectoryPath) : - finder.find(sourceDirectoryPath, sourceDocumentExtensions); + finder.find(sourceDirectoryPath) : + finder.find(sourceDirectoryPath, sourceDocumentExtensions); } protected void convertFile(Asciidoctor asciidoctor, Map options, File f) { diff --git a/src/main/java/org/asciidoctor/maven/process/SourceDocumentFinder.java b/src/main/java/org/asciidoctor/maven/process/SourceDocumentFinder.java index 2287385f..144c00de 100644 --- a/src/main/java/org/asciidoctor/maven/process/SourceDocumentFinder.java +++ b/src/main/java/org/asciidoctor/maven/process/SourceDocumentFinder.java @@ -47,27 +47,27 @@ public List find(Path sourceDirectory) { * @return the list of all matching source documents. */ public List find(Path sourceDirectory, List sourceDocumentExtensions) { - String extensionPattern = sourceDocumentExtensions.stream().collect(Collectors - .joining("|", CUSTOM_FILE_EXTENSIONS_PATTERN_PREFIX, CUSTOM_FILE_EXTENSIONS_PATTERN_SUFFIX)); + String extensionPattern = sourceDocumentExtensions.stream() + .collect(Collectors.joining("|", CUSTOM_FILE_EXTENSIONS_PATTERN_PREFIX, CUSTOM_FILE_EXTENSIONS_PATTERN_SUFFIX)); return find(sourceDirectory, Pattern.compile(extensionPattern)); } private List find(Path sourceDirectory, Pattern sourceDocumentPattern) { try (Stream sourceDocumentCandidates = Files.walk(sourceDirectory)) { - return sourceDocumentCandidates.filter(Files::isRegularFile) - // Filter all documents that don't match the file extension pattern. - .filter(p -> sourceDocumentPattern.matcher(p.getFileName().toString()).matches()) - // Filter all documents that are part of ignored directories. - .filter(p -> { - for (Path part : p) { - if (part.toString().startsWith("_")) { - return false; + return sourceDocumentCandidates + .filter(Files::isRegularFile) + .filter(path -> sourceDocumentPattern.matcher(path.getFileName().toString()).matches()) + .filter(path -> { + for (Path part : sourceDirectory.relativize(path)) { + if (part.toString().startsWith("_")) { + return false; + } } - } - return true; - }).map(Path::toFile).collect(Collectors.toList()); + return true; + }) + .map(Path::toFile) + .collect(Collectors.toList()); } catch (IOException e) { - // Can't access the source directory. return Collections.emptyList(); } } diff --git a/src/test/java/org/asciidoctor/maven/process/SourceDocumentFinderTest.java b/src/test/java/org/asciidoctor/maven/process/SourceDocumentFinderTest.java index 0c8abc7d..95d53705 100644 --- a/src/test/java/org/asciidoctor/maven/process/SourceDocumentFinderTest.java +++ b/src/test/java/org/asciidoctor/maven/process/SourceDocumentFinderTest.java @@ -1,13 +1,13 @@ package org.asciidoctor.maven.process; +import org.junit.jupiter.api.Test; + import java.io.File; import java.nio.file.Paths; -import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; public class SourceDocumentFinderTest { @@ -32,10 +32,9 @@ public void should_match_standard_file_extensions() { // then assertThat(files) - .isNotEmpty() - .map(File::getName) - .allMatch(name -> name.endsWith("ad") || name.endsWith("adoc") || name.endsWith("asc") || - name.endsWith("asciidoc")); + .hasSize(4) + .map(File::getName) + .allMatch(name -> name.endsWith("ad") || name.endsWith("adoc") || name.endsWith("asc") || name.endsWith("asciidoc")); } @Test @@ -48,26 +47,24 @@ public void should_match_custom_file_extension() { // then assertThat(files) - .isNotEmpty() - .allMatch(file -> file.getName().endsWith("my-adoc")); + .hasSize(1) + .allMatch(file -> file.getName().endsWith("my-adoc")); } @Test public void should_match_custom_file_extensions() { // given final String rootDirectory = DEFAULT_SOURCE_DIRECTORY + "/file-extensions"; - List customFileExtensions = new ArrayList<>(); - customFileExtensions.add("my-adoc"); - customFileExtensions.add("adoc"); + List customFileExtensions = Arrays.asList("my-adoc", "adoc"); // when List files = new SourceDocumentFinder().find(Paths.get(rootDirectory), customFileExtensions); // then assertThat(files) - .isNotEmpty() - .map(File::getName) - .allMatch(name -> name.endsWith("my-adoc") || name.endsWith("adoc")); + .hasSize(2) + .map(File::getName) + .allMatch(name -> name.endsWith("my-adoc") || name.endsWith("adoc")); } @Test @@ -80,7 +77,7 @@ public void should_not_match_custom_empty_file_extensions() { // then assertThat(files) - .isEmpty(); + .isEmpty(); } @Test @@ -93,11 +90,11 @@ public void should_return_empty_list_if_wrong_source_directory() { // then assertThat(files) - .isEmpty(); + .isEmpty(); } @Test - public void should_exclude_internal_sources() { + public void should_exclude_hidden_sources() { // given final String rootDirectory = DEFAULT_SOURCE_DIRECTORY + "/relative-path-treatment"; final List fileExtensions = Collections.singletonList("adoc"); @@ -107,12 +104,28 @@ public void should_exclude_internal_sources() { // then assertThat(files) - .isNotEmpty() + .hasSize(6) .allMatch(file -> !file.getName().startsWith("_")); } @Test - public void should_exclude_internal_directories() { + public void should_not_treat_enclosing_parent_paths_as_hidden() { + // given + final String rootDirectory = DEFAULT_SOURCE_DIRECTORY + "/source-finder/_enclosing/src/"; + final List fileExtensions = Collections.singletonList("adoc"); + + // when + List files = new SourceDocumentFinder().find(Paths.get(rootDirectory), fileExtensions); + + // then + assertThat(files) + .hasSize(1); + assertThat(files.get(0)) + .hasName("simple.adoc"); + } + + @Test + public void should_exclude_hidden_directories() { // given final String rootDirectory = DEFAULT_SOURCE_DIRECTORY + "/relative-path-treatment"; final List fileExtensions = Collections.singletonList("adoc"); @@ -122,7 +135,7 @@ public void should_exclude_internal_directories() { // then assertThat(files) - .isNotEmpty() + .hasSize(6) .allMatch(file -> !isContainedInInternalDirectory(file)); } diff --git a/src/test/resources/src/asciidoctor/source-finder/_enclosing/src/simple.adoc b/src/test/resources/src/asciidoctor/source-finder/_enclosing/src/simple.adoc new file mode 100644 index 00000000..753cadd5 --- /dev/null +++ b/src/test/resources/src/asciidoctor/source-finder/_enclosing/src/simple.adoc @@ -0,0 +1,3 @@ += Document Title + +Preamble paragraph.