diff --git a/spring-restdocs-asciidoctor-support/src/main/resources/extensions/operation_block_macro.rb b/spring-restdocs-asciidoctor-support/src/main/resources/extensions/operation_block_macro.rb index 4d94c40e..b3f3a0dd 100644 --- a/spring-restdocs-asciidoctor-support/src/main/resources/extensions/operation_block_macro.rb +++ b/spring-restdocs-asciidoctor-support/src/main/resources/extensions/operation_block_macro.rb @@ -47,8 +47,7 @@ class OperationBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor end def add_blocks(content, doc, parent) - options = { safe: doc.options[:safe], - attributes: { 'projectdir' => doc.attr(:projectdir) } } + options = { safe: doc.options[:safe], attributes: doc.attributes } fragment = Asciidoctor.load content, options fragment.blocks.each do |b| b.parent = parent diff --git a/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/OperationBlockMacroTests.java b/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/AbstractOperationBlockMacroTests.java similarity index 89% rename from spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/OperationBlockMacroTests.java rename to spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/AbstractOperationBlockMacroTests.java index 2d72b924..96be6cff 100644 --- a/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/OperationBlockMacroTests.java +++ b/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/AbstractOperationBlockMacroTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2018 the original author or authors. + * Copyright 2014-2019 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. @@ -33,39 +33,48 @@ import org.apache.pdfbox.pdmodel.PDDocument; import org.asciidoctor.Asciidoctor; import org.asciidoctor.Attributes; import org.asciidoctor.Options; +import org.asciidoctor.OptionsBuilder; +import org.asciidoctor.SafeMode; import org.junit.Before; -import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.springframework.util.FileSystemUtils; import static org.assertj.core.api.Assertions.assertThat; /** - * Tests for Ruby operation block macro. + * Base class for tests for the Ruby operation block macro. * * @author Gerrit Meier * @author Andy Wilkinson */ -public class OperationBlockMacroTests { +public abstract class AbstractOperationBlockMacroTests { - private final Options options = new Options(); + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + private Options options; private final Asciidoctor asciidoctor = Asciidoctor.Factory.create(); - @BeforeClass - public static void prepareOperationSnippets() throws IOException { - File destination = new File("build/generated-snippets/some-operation"); + @Before + public void setUp() throws IOException { + prepareOperationSnippets(getBuildOutputLocation()); + this.options = OptionsBuilder.options().safe(SafeMode.UNSAFE) + .baseDir(getSourceLocation()).get(); + this.options.setAttributes(getAttributes()); + } + + public void prepareOperationSnippets(File buildOutputLocation) throws IOException { + File destination = new File(buildOutputLocation, + "generated-snippets/some-operation"); destination.mkdirs(); FileSystemUtils.copyRecursively(new File("src/test/resources/some-operation"), destination); } - @Before - public void setUp() { - this.options.setAttributes(getAttributes()); - } - @Test public void codeBlockSnippetInclude() throws Exception { String result = this.asciidoctor.convert( @@ -208,11 +217,11 @@ public class OperationBlockMacroTests { return File.separatorChar == '\\'; } - private Attributes getAttributes() { - Attributes attributes = new Attributes(); - attributes.setAttribute("projectdir", new File(".").getAbsolutePath()); - return attributes; - } + protected abstract Attributes getAttributes(); + + protected abstract File getBuildOutputLocation(); + + protected abstract File getSourceLocation(); private File configurePdfOutput() { this.options.setBackend("pdf"); diff --git a/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/GradleOperationBlockMacroTests.java b/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/GradleOperationBlockMacroTests.java new file mode 100644 index 00000000..3287b44c --- /dev/null +++ b/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/GradleOperationBlockMacroTests.java @@ -0,0 +1,69 @@ +/* + * Copyright 2014-2019 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.restdocs.asciidoctor; + +import java.io.File; + +import org.asciidoctor.Attributes; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +/** + * Tests for Ruby operation block macro when used in a Gradle build. + * + * @author Andy Wilkinson + */ +@RunWith(Parameterized.class) +public class GradleOperationBlockMacroTests extends AbstractOperationBlockMacroTests { + + private final String attributeName; + + public GradleOperationBlockMacroTests(String attributeName) { + this.attributeName = attributeName; + } + + @Parameters(name = "{0}") + public static Object[] parameters() { + return new Object[] { "projectdir", "gradle-projectdir" }; + } + + protected Attributes getAttributes() { + Attributes attributes = new Attributes(); + attributes.setAttribute(this.attributeName, + new File(temp.getRoot(), "gradle-project").getAbsolutePath()); + return attributes; + } + + @Override + protected File getBuildOutputLocation() { + File outputLocation = new File(temp.getRoot(), "gradle-project/build"); + outputLocation.mkdirs(); + return outputLocation; + } + + @Override + protected File getSourceLocation() { + File sourceLocation = new File(temp.getRoot(), + "gradle-project/src/docs/asciidoc"); + if (!sourceLocation.exists()) { + sourceLocation.mkdirs(); + } + return sourceLocation; + } + +} diff --git a/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/MavenOperationBlockMacroTests.java b/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/MavenOperationBlockMacroTests.java new file mode 100644 index 00000000..ad711d7d --- /dev/null +++ b/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/MavenOperationBlockMacroTests.java @@ -0,0 +1,73 @@ +/* + * Copyright 2014-2019 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.restdocs.asciidoctor; + +import java.io.File; +import java.io.IOException; + +import org.asciidoctor.Attributes; +import org.junit.After; +import org.junit.Before; + +/** + * Tests for Ruby operation block macro when used in a Maven build. + * + * @author Andy Wilkinson + */ +public class MavenOperationBlockMacroTests extends AbstractOperationBlockMacroTests { + + @Before + public void setMavenHome() { + System.setProperty("maven.home", "maven-home"); + } + + @After + public void clearMavenHome() { + System.clearProperty("maven.home"); + } + + protected Attributes getAttributes() { + try { + File sourceLocation = getSourceLocation(); + new File(sourceLocation.getParentFile().getParentFile().getParentFile(), + "pom.xml").createNewFile(); + Attributes attributes = new Attributes(); + attributes.setAttribute("docdir", sourceLocation.getAbsolutePath()); + return attributes; + } + catch (IOException ex) { + throw new RuntimeException(ex); + } + } + + @Override + protected File getBuildOutputLocation() { + File outputLocation = new File(temp.getRoot(), "maven-project/target"); + outputLocation.mkdirs(); + return outputLocation; + } + + @Override + protected File getSourceLocation() { + File sourceLocation = new File(temp.getRoot(), "maven-project/src/main/asciidoc"); + if (!sourceLocation.exists()) { + sourceLocation.mkdirs(); + } + return sourceLocation; + } + +}