Skip to content

Commit

Permalink
Ensure that default value for snippets attribute is an absolute path
Browse files Browse the repository at this point in the history
Previously, the snippets attribute was absolute when using Gradle and
relative to the docdir when using Maven. The relative path that was
used with Maven only worked as long as the working directory when
invoking Asciidoctor was the same as the docdir. This was the case
until 3.1.0 of the Asciidoctor Maven Plugin when the working
directory and docdir diverged at which point the snippets could no
longer be found.

This commit updates the snippets directory resolver to return an
absolute file when using Maven, just has it already does when using
Gradle. The resolution for Gradle has also been updated to explicity
make the File absolute rather than relying on the gradle-projectdir
or projectdir attributes having an absolute value.

Fixes gh-950
  • Loading branch information
wilkinsona committed Nov 27, 2024
1 parent 7f0e6dd commit 2d838a1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@

/**
* Resolves the directory from which snippets can be read for inclusion in an Asciidoctor
* document. The resolved directory is relative to the {@code docdir} of the Asciidoctor
* document that it being rendered.
* document. The resolved directory is absolute.
*
* @author Andy Wilkinson
*/
Expand All @@ -46,7 +45,7 @@ public File getSnippetsDirectory(Map<String, Object> attributes) {

private File getMavenSnippetsDirectory(Map<String, Object> attributes) {
Path docdir = Paths.get(getRequiredAttribute(attributes, "docdir"));
return new File(docdir.relativize(findPom(docdir).getParent()).toFile(), "target/generated-snippets");
return new File(findPom(docdir).getParent().toFile(), "target/generated-snippets").getAbsoluteFile();
}

private Path findPom(Path docdir) {
Expand All @@ -63,7 +62,8 @@ private Path findPom(Path docdir) {

private File getGradleSnippetsDirectory(Map<String, Object> attributes) {
return new File(getRequiredAttribute(attributes, "gradle-projectdir",
() -> getRequiredAttribute(attributes, "projectdir")), "build/generated-snippets");
() -> getRequiredAttribute(attributes, "projectdir")), "build/generated-snippets")
.getAbsoluteFile();
}

private String getRequiredAttribute(Map<String, Object> attributes, String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,13 +39,13 @@ public class SnippetsDirectoryResolverTests {
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
public void mavenProjectsUseTargetGeneratedSnippetsRelativeToDocdir() throws IOException {
public void mavenProjectsUseTargetGeneratedSnippets() throws IOException {
this.temporaryFolder.newFile("pom.xml");
Map<String, Object> attributes = new HashMap<>();
attributes.put("docdir", new File(this.temporaryFolder.getRoot(), "src/main/asciidoc").getAbsolutePath());
File snippetsDirectory = getMavenSnippetsDirectory(attributes);
assertThat(snippetsDirectory).isRelative();
assertThat(snippetsDirectory).isEqualTo(new File("../../../target/generated-snippets"));
assertThat(snippetsDirectory).isAbsolute();
assertThat(snippetsDirectory).isEqualTo(new File(this.temporaryFolder.getRoot(), "target/generated-snippets"));
}

@Test
Expand All @@ -69,7 +69,8 @@ public void gradleProjectsUseBuildGeneratedSnippetsBeneathGradleProjectdir() {
Map<String, Object> attributes = new HashMap<>();
attributes.put("gradle-projectdir", "project/dir");
File snippetsDirectory = new SnippetsDirectoryResolver().getSnippetsDirectory(attributes);
assertThat(snippetsDirectory).isEqualTo(new File("project/dir/build/generated-snippets"));
assertThat(snippetsDirectory).isAbsolute();
assertThat(snippetsDirectory).isEqualTo(new File("project/dir/build/generated-snippets").getAbsoluteFile());
}

@Test
Expand All @@ -78,15 +79,17 @@ public void gradleProjectsUseBuildGeneratedSnippetsBeneathGradleProjectdirWhenBo
attributes.put("gradle-projectdir", "project/dir");
attributes.put("projectdir", "fallback/dir");
File snippetsDirectory = new SnippetsDirectoryResolver().getSnippetsDirectory(attributes);
assertThat(snippetsDirectory).isEqualTo(new File("project/dir/build/generated-snippets"));
assertThat(snippetsDirectory).isAbsolute();
assertThat(snippetsDirectory).isEqualTo(new File("project/dir/build/generated-snippets").getAbsoluteFile());
}

@Test
public void gradleProjectsUseBuildGeneratedSnippetsBeneathProjectdirWhenGradleProjectdirIsNotSet() {
Map<String, Object> attributes = new HashMap<>();
attributes.put("projectdir", "project/dir");
File snippetsDirectory = new SnippetsDirectoryResolver().getSnippetsDirectory(attributes);
assertThat(snippetsDirectory).isEqualTo(new File("project/dir/build/generated-snippets"));
assertThat(snippetsDirectory).isAbsolute();
assertThat(snippetsDirectory).isEqualTo(new File("project/dir/build/generated-snippets").getAbsoluteFile());
}

@Test
Expand Down

0 comments on commit 2d838a1

Please sign in to comment.