Skip to content

Commit de7b151

Browse files
committed
Merge pull request #699 from ahus1
* gh-699: Provide context when logging warnings in operation macro Closes gh-699
2 parents 0d8eb26 + 2cf5919 commit de7b151

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'asciidoctor/extensions'
22
require 'stringio'
3+
require 'asciidoctor/logging'
34

45
# Spring REST Docs block macro to import multiple snippet of an operation at
56
# once
@@ -11,6 +12,7 @@
1112
class OperationBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
1213
use_dsl
1314
named :operation
15+
include Asciidoctor::Logging
1416

1517
def process(parent, operation, attributes)
1618
snippets_dir = parent.document.attributes['snippets'].to_s
@@ -27,8 +29,9 @@ def read_snippets(snippets_dir, snippet_names, parent, operation,
2729
snippet_titles)
2830
snippets = snippets_to_include(snippet_names, snippets_dir, operation)
2931
if snippets.empty?
30-
warn "No snippets were found for operation #{operation} in"\
31-
"#{snippets_dir}"
32+
location = parent.document.reader.cursor_at_mark
33+
logger.warn message_with_context "No snippets were found for operation #{operation} in "\
34+
"#{snippets_dir}", source_location: location
3235
"No snippets found for operation::#{operation}"
3336
else
3437
do_read_snippets(snippets, parent, operation, snippet_titles)
@@ -41,7 +44,7 @@ def do_read_snippets(snippets, parent, operation, snippet_titles)
4144
section_id = parent.id
4245
snippets.each do |snippet|
4346
append_snippet_block(content, snippet, section_id,
44-
operation, snippet_titles)
47+
operation, snippet_titles, parent)
4548
end
4649
content.string
4750
end
@@ -87,17 +90,18 @@ def all_snippets(snippets_dir, operation)
8790
end
8891

8992
def append_snippet_block(content, snippet, section_id,
90-
operation, snippet_titles)
93+
operation, snippet_titles, parent)
9194
write_title content, snippet, section_id, snippet_titles
92-
write_content content, snippet, operation
95+
write_content content, snippet, operation, parent
9396
end
9497

95-
def write_content(content, snippet, operation)
98+
def write_content(content, snippet, operation, parent)
9699
if File.file? snippet.path
97100
content.puts File.readlines(snippet.path, :encoding => 'UTF-8').join
98101
else
99-
warn "Snippet #{snippet.name} not found at #{snippet.path} for"\
100-
" operation #{operation}"
102+
location = parent.document.reader.cursor_at_mark
103+
logger.warn message_with_context "Snippet #{snippet.name} not found at #{snippet.path} for"\
104+
" operation #{operation}", source_location: location
101105
content.puts "Snippet #{snippet.name} not found for"\
102106
" operation::#{operation}"
103107
content.puts ''
@@ -149,4 +153,4 @@ def title_for_snippet(snippet)
149153
end
150154
end
151155
end
152-
end
156+
end

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ public void useMacroWithEmptySnippetAttributeAddsAllSnippets() throws Exception
170170
public void includingMissingSnippetAddsWarning() throws Exception {
171171
String result = this.asciidoctor.convert("operation::some-operation[snippets='missing-snippet']", this.options);
172172
assertThat(result).startsWith(getExpectedContentFromFile("missing-snippet"));
173+
assertThat(CapturingLogHandler.getLogRecords()).hasSize(1);
174+
assertThat(CapturingLogHandler.getLogRecords().get(0).getMessage())
175+
.contains("Snippet missing-snippet not found");
176+
assertThat(CapturingLogHandler.getLogRecords().get(0).getCursor().getLineNumber()).isEqualTo(1);
177+
CapturingLogHandler.getLogRecords().clear();
173178
}
174179

175180
@Test
@@ -182,6 +187,11 @@ public void defaultTitleIsProvidedForCustomSnippet() throws Exception {
182187
public void missingOperationIsHandledGracefully() throws Exception {
183188
String result = this.asciidoctor.convert("operation::missing-operation[]", this.options);
184189
assertThat(result).startsWith(getExpectedContentFromFile("missing-operation"));
190+
assertThat(CapturingLogHandler.getLogRecords()).hasSize(1);
191+
assertThat(CapturingLogHandler.getLogRecords().get(0).getMessage())
192+
.contains("No snippets were found for operation missing-operation");
193+
assertThat(CapturingLogHandler.getLogRecords().get(0).getCursor().getLineNumber()).isEqualTo(1);
194+
CapturingLogHandler.getLogRecords().clear();
185195
}
186196

187197
@Test

0 commit comments

Comments
 (0)