Provide context when logging warnings in operation macro
See gh-699
This commit is contained in:
committed by
Andy Wilkinson
parent
0d8eb26ce0
commit
2cf5919632
@@ -1,5 +1,6 @@
|
||||
require 'asciidoctor/extensions'
|
||||
require 'stringio'
|
||||
require 'asciidoctor/logging'
|
||||
|
||||
# Spring REST Docs block macro to import multiple snippet of an operation at
|
||||
# once
|
||||
@@ -11,6 +12,7 @@ require 'stringio'
|
||||
class OperationBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
|
||||
use_dsl
|
||||
named :operation
|
||||
include Asciidoctor::Logging
|
||||
|
||||
def process(parent, operation, attributes)
|
||||
snippets_dir = parent.document.attributes['snippets'].to_s
|
||||
@@ -27,8 +29,9 @@ class OperationBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
|
||||
snippet_titles)
|
||||
snippets = snippets_to_include(snippet_names, snippets_dir, operation)
|
||||
if snippets.empty?
|
||||
warn "No snippets were found for operation #{operation} in"\
|
||||
"#{snippets_dir}"
|
||||
location = parent.document.reader.cursor_at_mark
|
||||
logger.warn message_with_context "No snippets were found for operation #{operation} in "\
|
||||
"#{snippets_dir}", source_location: location
|
||||
"No snippets found for operation::#{operation}"
|
||||
else
|
||||
do_read_snippets(snippets, parent, operation, snippet_titles)
|
||||
@@ -41,7 +44,7 @@ class OperationBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
|
||||
section_id = parent.id
|
||||
snippets.each do |snippet|
|
||||
append_snippet_block(content, snippet, section_id,
|
||||
operation, snippet_titles)
|
||||
operation, snippet_titles, parent)
|
||||
end
|
||||
content.string
|
||||
end
|
||||
@@ -87,17 +90,18 @@ class OperationBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
|
||||
end
|
||||
|
||||
def append_snippet_block(content, snippet, section_id,
|
||||
operation, snippet_titles)
|
||||
operation, snippet_titles, parent)
|
||||
write_title content, snippet, section_id, snippet_titles
|
||||
write_content content, snippet, operation
|
||||
write_content content, snippet, operation, parent
|
||||
end
|
||||
|
||||
def write_content(content, snippet, operation)
|
||||
def write_content(content, snippet, operation, parent)
|
||||
if File.file? snippet.path
|
||||
content.puts File.readlines(snippet.path, :encoding => 'UTF-8').join
|
||||
else
|
||||
warn "Snippet #{snippet.name} not found at #{snippet.path} for"\
|
||||
" operation #{operation}"
|
||||
location = parent.document.reader.cursor_at_mark
|
||||
logger.warn message_with_context "Snippet #{snippet.name} not found at #{snippet.path} for"\
|
||||
" operation #{operation}", source_location: location
|
||||
content.puts "Snippet #{snippet.name} not found for"\
|
||||
" operation::#{operation}"
|
||||
content.puts ''
|
||||
@@ -149,4 +153,4 @@ class OperationBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -170,6 +170,11 @@ public abstract class AbstractOperationBlockMacroTests {
|
||||
public void includingMissingSnippetAddsWarning() throws Exception {
|
||||
String result = this.asciidoctor.convert("operation::some-operation[snippets='missing-snippet']", this.options);
|
||||
assertThat(result).startsWith(getExpectedContentFromFile("missing-snippet"));
|
||||
assertThat(CapturingLogHandler.getLogRecords()).hasSize(1);
|
||||
assertThat(CapturingLogHandler.getLogRecords().get(0).getMessage())
|
||||
.contains("Snippet missing-snippet not found");
|
||||
assertThat(CapturingLogHandler.getLogRecords().get(0).getCursor().getLineNumber()).isEqualTo(1);
|
||||
CapturingLogHandler.getLogRecords().clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -182,6 +187,11 @@ public abstract class AbstractOperationBlockMacroTests {
|
||||
public void missingOperationIsHandledGracefully() throws Exception {
|
||||
String result = this.asciidoctor.convert("operation::missing-operation[]", this.options);
|
||||
assertThat(result).startsWith(getExpectedContentFromFile("missing-operation"));
|
||||
assertThat(CapturingLogHandler.getLogRecords()).hasSize(1);
|
||||
assertThat(CapturingLogHandler.getLogRecords().get(0).getMessage())
|
||||
.contains("No snippets were found for operation missing-operation");
|
||||
assertThat(CapturingLogHandler.getLogRecords().get(0).getCursor().getLineNumber()).isEqualTo(1);
|
||||
CapturingLogHandler.getLogRecords().clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user