Skip to content

Commit 2d838a1

Browse files
committed
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
1 parent 7f0e6dd commit 2d838a1

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolver.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525

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

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

5251
private Path findPom(Path docdir) {
@@ -63,7 +62,8 @@ private Path findPom(Path docdir) {
6362

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

6969
private String getRequiredAttribute(Map<String, Object> attributes, String name) {

spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolverTests.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2023 the original author or authors.
2+
* Copyright 2014-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,13 +39,13 @@ public class SnippetsDirectoryResolverTests {
3939
public TemporaryFolder temporaryFolder = new TemporaryFolder();
4040

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

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

7576
@Test
@@ -78,15 +79,17 @@ public void gradleProjectsUseBuildGeneratedSnippetsBeneathGradleProjectdirWhenBo
7879
attributes.put("gradle-projectdir", "project/dir");
7980
attributes.put("projectdir", "fallback/dir");
8081
File snippetsDirectory = new SnippetsDirectoryResolver().getSnippetsDirectory(attributes);
81-
assertThat(snippetsDirectory).isEqualTo(new File("project/dir/build/generated-snippets"));
82+
assertThat(snippetsDirectory).isAbsolute();
83+
assertThat(snippetsDirectory).isEqualTo(new File("project/dir/build/generated-snippets").getAbsoluteFile());
8284
}
8385

8486
@Test
8587
public void gradleProjectsUseBuildGeneratedSnippetsBeneathProjectdirWhenGradleProjectdirIsNotSet() {
8688
Map<String, Object> attributes = new HashMap<>();
8789
attributes.put("projectdir", "project/dir");
8890
File snippetsDirectory = new SnippetsDirectoryResolver().getSnippetsDirectory(attributes);
89-
assertThat(snippetsDirectory).isEqualTo(new File("project/dir/build/generated-snippets"));
91+
assertThat(snippetsDirectory).isAbsolute();
92+
assertThat(snippetsDirectory).isEqualTo(new File("project/dir/build/generated-snippets").getAbsoluteFile());
9093
}
9194

9295
@Test

0 commit comments

Comments
 (0)