diff --git a/spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolver.java b/spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolver.java index 2ca3ea5f..521c0832 100644 --- a/spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolver.java +++ b/spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolver.java @@ -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 */ @@ -46,7 +45,7 @@ public File getSnippetsDirectory(Map attributes) { private File getMavenSnippetsDirectory(Map 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 @@ private Path findPom(Path docdir) { private File getGradleSnippetsDirectory(Map 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 attributes, String name) { diff --git a/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolverTests.java b/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolverTests.java index b6cc1b24..2bede597 100644 --- a/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolverTests.java +++ b/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolverTests.java @@ -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 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 void gradleProjectsUseBuildGeneratedSnippetsBeneathGradleProjectdir() { Map 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 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 @@ -86,7 +88,8 @@ public void gradleProjectsUseBuildGeneratedSnippetsBeneathProjectdirWhenGradlePr Map 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