Skip to content

Commit

Permalink
set IO context for doctest output relative to sandbox module (#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson authored and mortenpi committed Oct 2, 2018
1 parent b2eda90 commit 98d5c59
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/DocTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,14 @@ function checkresult(sandbox::Module, result::Result, meta::Dict, doc::Documents
sandbox_name = nameof(sandbox)
mod_regex = Regex("(Main\\.)?(Symbol\\(\"$(sandbox_name)\"\\)|$(sandbox_name))[,.]")
mod_regex_nodot = Regex(("(Main\\.)?$(sandbox_name)"))
outio = IOContext(result.stdout, :module => sandbox)
if isdefined(result, :bt) # An error was thrown and we have a backtrace.
# To avoid dealing with path/line number issues in backtraces we use `[...]` to
# mark ignored output from an error message. Only the text prior to it is used to
# test for doctest success/failure.
head = replace(split(result.output, "\n[...]"; limit = 2)[1], mod_regex => "")
head = replace(head, mod_regex_nodot => "Main")
str = replace(error_to_string(result.stdout, result.value, result.bt), mod_regex => "")
str = replace(error_to_string(outio, result.value, result.bt), mod_regex => "")
str = replace(str, mod_regex_nodot => "Main")

str, head = filter_doctests((str, head), doc, meta)
Expand All @@ -247,7 +248,7 @@ function checkresult(sandbox::Module, result::Result, meta::Dict, doc::Documents
else
value = result.hide ? nothing : result.value # `;` hides output.
output = replace(rstrip(sanitise(IOBuffer(result.output))), mod_regex => "")
str = replace(result_to_string(result.stdout, value), mod_regex => "")
str = replace(result_to_string(outio, value), mod_regex => "")
# Replace a standalone module name with `Main`.
str = rstrip(replace(str, mod_regex_nodot => "Main"))
filteredstr, filteredoutput = filter_doctests((str, output), doc, meta)
Expand Down Expand Up @@ -291,7 +292,7 @@ end
# Strip trailing whitespace and remove terminal colors.
function sanitise(buffer)
out = IOBuffer()
for line in eachline(seekstart(buffer))
for line in eachline(seekstart(Base.unwrapcontext(buffer)[1]))
println(out, rstrip(line))
end
remove_term_colors(rstrip(String(take!(out)), '\n'))
Expand Down

0 comments on commit 98d5c59

Please sign in to comment.