From 2d838a1a347d07a46888e8919469be1cdd254a04 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 27 Nov 2024 11:26:58 +0000 Subject: [PATCH] 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 --- .../asciidoctor/SnippetsDirectoryResolver.java | 8 ++++---- .../SnippetsDirectoryResolverTests.java | 17 ++++++++++------- 2 files changed, 14 insertions(+), 11 deletions(-) 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