Skip to content

Commit 6198475

Browse files
committed
Reuse document's attributes when loading snippet content
Previously, when adding blocks for the snippets' content, the operation macro would call Asciidoctor to load the content with attributes that only included the projectdir. This resulted in a failure in environments that did not set the projectdir and instead relied on another attribute, such as gradle-projectdir as used by the latest versions of the Asciidoctor Gradle plugin. This commit updates the macro to reuse all of the main document's attributes when loading the snippets' content, thereby ensuring that any attributes available in the main document are also available during snipppet processing. Closes gh-624
1 parent 3dabf8e commit 6198475

File tree

4 files changed

+170
-20
lines changed

4 files changed

+170
-20
lines changed

spring-restdocs-asciidoctor-support/src/main/resources/extensions/operation_block_macro.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ def do_read_snippets(snippets, parent, operation, snippet_titles)
4747
end
4848

4949
def add_blocks(content, doc, parent)
50-
options = { safe: doc.options[:safe],
51-
attributes: { 'projectdir' => doc.attr(:projectdir) } }
50+
options = { safe: doc.options[:safe], attributes: doc.attributes }
5251
fragment = Asciidoctor.load content, options
5352
fragment.blocks.each do |b|
5453
b.parent = parent
Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2018 the original author or authors.
2+
* Copyright 2014-2019 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.
@@ -33,39 +33,48 @@
3333
import org.asciidoctor.Asciidoctor;
3434
import org.asciidoctor.Attributes;
3535
import org.asciidoctor.Options;
36+
import org.asciidoctor.OptionsBuilder;
37+
import org.asciidoctor.SafeMode;
3638
import org.junit.Before;
37-
import org.junit.BeforeClass;
39+
import org.junit.Rule;
3840
import org.junit.Test;
41+
import org.junit.rules.TemporaryFolder;
3942

4043
import org.springframework.util.FileSystemUtils;
4144

4245
import static org.assertj.core.api.Assertions.assertThat;
4346

4447
/**
45-
* Tests for Ruby operation block macro.
48+
* Base class for tests for the Ruby operation block macro.
4649
*
4750
* @author Gerrit Meier
4851
* @author Andy Wilkinson
4952
*/
50-
public class OperationBlockMacroTests {
53+
public abstract class AbstractOperationBlockMacroTests {
5154

52-
private final Options options = new Options();
55+
@Rule
56+
public TemporaryFolder temp = new TemporaryFolder();
57+
58+
private Options options;
5359

5460
private final Asciidoctor asciidoctor = Asciidoctor.Factory.create();
5561

56-
@BeforeClass
57-
public static void prepareOperationSnippets() throws IOException {
58-
File destination = new File("build/generated-snippets/some-operation");
62+
@Before
63+
public void setUp() throws IOException {
64+
prepareOperationSnippets(getBuildOutputLocation());
65+
this.options = OptionsBuilder.options().safe(SafeMode.UNSAFE)
66+
.baseDir(getSourceLocation()).get();
67+
this.options.setAttributes(getAttributes());
68+
}
69+
70+
public void prepareOperationSnippets(File buildOutputLocation) throws IOException {
71+
File destination = new File(buildOutputLocation,
72+
"generated-snippets/some-operation");
5973
destination.mkdirs();
6074
FileSystemUtils.copyRecursively(new File("src/test/resources/some-operation"),
6175
destination);
6276
}
6377

64-
@Before
65-
public void setUp() {
66-
this.options.setAttributes(getAttributes());
67-
}
68-
6978
@Test
7079
public void codeBlockSnippetInclude() throws Exception {
7180
String result = this.asciidoctor.convert(
@@ -208,11 +217,11 @@ private boolean isWindows() {
208217
return File.separatorChar == '\\';
209218
}
210219

211-
private Attributes getAttributes() {
212-
Attributes attributes = new Attributes();
213-
attributes.setAttribute("projectdir", new File(".").getAbsolutePath());
214-
return attributes;
215-
}
220+
protected abstract Attributes getAttributes();
221+
222+
protected abstract File getBuildOutputLocation();
223+
224+
protected abstract File getSourceLocation();
216225

217226
private File configurePdfOutput() {
218227
this.options.setBackend("pdf");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2014-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.restdocs.asciidoctor;
18+
19+
import java.io.File;
20+
21+
import org.asciidoctor.Attributes;
22+
import org.junit.runner.RunWith;
23+
import org.junit.runners.Parameterized;
24+
import org.junit.runners.Parameterized.Parameters;
25+
26+
/**
27+
* Tests for Ruby operation block macro when used in a Gradle build.
28+
*
29+
* @author Andy Wilkinson
30+
*/
31+
@RunWith(Parameterized.class)
32+
public class GradleOperationBlockMacroTests extends AbstractOperationBlockMacroTests {
33+
34+
private final String attributeName;
35+
36+
public GradleOperationBlockMacroTests(String attributeName) {
37+
this.attributeName = attributeName;
38+
}
39+
40+
@Parameters(name = "{0}")
41+
public static Object[] parameters() {
42+
return new Object[] { "projectdir", "gradle-projectdir" };
43+
}
44+
45+
protected Attributes getAttributes() {
46+
Attributes attributes = new Attributes();
47+
attributes.setAttribute(this.attributeName,
48+
new File(temp.getRoot(), "gradle-project").getAbsolutePath());
49+
return attributes;
50+
}
51+
52+
@Override
53+
protected File getBuildOutputLocation() {
54+
File outputLocation = new File(temp.getRoot(), "gradle-project/build");
55+
outputLocation.mkdirs();
56+
return outputLocation;
57+
}
58+
59+
@Override
60+
protected File getSourceLocation() {
61+
File sourceLocation = new File(temp.getRoot(),
62+
"gradle-project/src/docs/asciidoc");
63+
if (!sourceLocation.exists()) {
64+
sourceLocation.mkdirs();
65+
}
66+
return sourceLocation;
67+
}
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2014-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.restdocs.asciidoctor;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
22+
import org.asciidoctor.Attributes;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
26+
/**
27+
* Tests for Ruby operation block macro when used in a Maven build.
28+
*
29+
* @author Andy Wilkinson
30+
*/
31+
public class MavenOperationBlockMacroTests extends AbstractOperationBlockMacroTests {
32+
33+
@Before
34+
public void setMavenHome() {
35+
System.setProperty("maven.home", "maven-home");
36+
}
37+
38+
@After
39+
public void clearMavenHome() {
40+
System.clearProperty("maven.home");
41+
}
42+
43+
protected Attributes getAttributes() {
44+
try {
45+
File sourceLocation = getSourceLocation();
46+
new File(sourceLocation.getParentFile().getParentFile().getParentFile(),
47+
"pom.xml").createNewFile();
48+
Attributes attributes = new Attributes();
49+
attributes.setAttribute("docdir", sourceLocation.getAbsolutePath());
50+
return attributes;
51+
}
52+
catch (IOException ex) {
53+
throw new RuntimeException(ex);
54+
}
55+
}
56+
57+
@Override
58+
protected File getBuildOutputLocation() {
59+
File outputLocation = new File(temp.getRoot(), "maven-project/target");
60+
outputLocation.mkdirs();
61+
return outputLocation;
62+
}
63+
64+
@Override
65+
protected File getSourceLocation() {
66+
File sourceLocation = new File(temp.getRoot(), "maven-project/src/main/asciidoc");
67+
if (!sourceLocation.exists()) {
68+
sourceLocation.mkdirs();
69+
}
70+
return sourceLocation;
71+
}
72+
73+
}

0 commit comments

Comments
 (0)