Skip to content

Commit

Permalink
Fix excluding sources when enclosing parent path started with _
Browse files Browse the repository at this point in the history
Fixes #545
  • Loading branch information
abelsromero committed Jul 24, 2021
1 parent 9b18ae0 commit 5c3191a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 39 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public void processSources(List<File> sourceFiles, ResourcesProcessor resourcesP
}

if (sourceFiles == null) {
sourceFiles = calculateSourceFiles(sourceDirectoryCandidate.get());
sourceFiles = findSourceFiles(sourceDirectoryCandidate.get());
}
if (sourceFiles.isEmpty()) {
getLog().info("No sources found. Skipping processing");
Expand Down Expand Up @@ -435,16 +435,16 @@ protected Asciidoctor getAsciidoctorInstance(String gemPath) throws MojoExecutio
return asciidoctor;
}

protected List<File> calculateSourceFiles(File sourceDirectory) {
protected List<File> findSourceFiles(File sourceDirectory) {
if (sourceDocumentName != null)
return Arrays.asList(new File(sourceDirectory, sourceDocumentName));

Path sourceDirectoryPath = sourceDirectory.toPath();
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<String, Object> options, File f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,27 @@ public List<File> find(Path sourceDirectory) {
* @return the list of all matching source documents.
*/
public List<File> find(Path sourceDirectory, List<String> 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<File> find(Path sourceDirectory, Pattern sourceDocumentPattern) {
try (Stream<Path> 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();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand All @@ -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<String> customFileExtensions = new ArrayList<>();
customFileExtensions.add("my-adoc");
customFileExtensions.add("adoc");
List<String> customFileExtensions = Arrays.asList("my-adoc", "adoc");

// when
List<File> 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
Expand All @@ -80,7 +77,7 @@ public void should_not_match_custom_empty_file_extensions() {

// then
assertThat(files)
.isEmpty();
.isEmpty();
}

@Test
Expand All @@ -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<String> fileExtensions = Collections.singletonList("adoc");
Expand All @@ -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<String> fileExtensions = Collections.singletonList("adoc");

// when
List<File> 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<String> fileExtensions = Collections.singletonList("adoc");
Expand All @@ -122,7 +135,7 @@ public void should_exclude_internal_directories() {

// then
assertThat(files)
.isNotEmpty()
.hasSize(6)
.allMatch(file -> !isContainedInInternalDirectory(file));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= Document Title

Preamble paragraph.

0 comments on commit 5c3191a

Please sign in to comment.