Skip to content

Warnings logged by the operation block macro lack context #699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,6 +12,7 @@
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
Expand All @@ -27,8 +29,9 @@ def read_snippets(snippets_dir, snippet_names, parent, operation,
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)
Expand All @@ -41,7 +44,7 @@ def do_read_snippets(snippets, parent, operation, snippet_titles)
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
Expand Down Expand Up @@ -87,17 +90,18 @@ def all_snippets(snippets_dir, operation)
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 ''
Expand Down Expand Up @@ -149,4 +153,4 @@ def title_for_snippet(snippet)
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ public void useMacroWithEmptySnippetAttributeAddsAllSnippets() throws Exception
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
Expand All @@ -182,6 +187,11 @@ public void defaultTitleIsProvidedForCustomSnippet() throws Exception {
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
Expand Down