Ensure that default value for snippets attribute is an absolute path

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
This commit is contained in:
Andy Wilkinson
2024-11-27 11:26:58 +00:00
parent 7f0e6dde74
commit 2d838a1a34
2 changed files with 14 additions and 11 deletions

View File

@@ -25,8 +25,7 @@ import java.util.function.Supplier;
/**
* 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
*/
@@ -46,7 +45,7 @@ public class SnippetsDirectoryResolver {
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) {
@@ -63,7 +62,8 @@ public class SnippetsDirectoryResolver {
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) {

View File

@@ -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.
@@ -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
@@ -69,7 +69,8 @@ public class SnippetsDirectoryResolverTests {
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
@@ -78,7 +79,8 @@ public class SnippetsDirectoryResolverTests {
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
@@ -86,7 +88,8 @@ public class SnippetsDirectoryResolverTests {
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