-
Notifications
You must be signed in to change notification settings - Fork 481
/
DocTests.jl
497 lines (448 loc) · 18 KB
/
DocTests.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
"""
Provides the [`doctest`](@ref) function that makes sure that the `jldoctest` code blocks
in the documents and docstrings run and are up to date.
"""
module DocTests
using DocStringExtensions
import ..Documenter:
DocSystem,
DocMeta,
Documenter,
Documents,
Expanders,
Utilities,
IdDict
import Markdown, REPL
import .Utilities: Markdown2
import IOCapture
# Julia code block testing.
# -------------------------
mutable struct MutableMD2CodeBlock
language :: String
code :: String
end
MutableMD2CodeBlock(block :: Markdown2.CodeBlock) = MutableMD2CodeBlock(block.language, block.code)
struct DocTestContext
file :: String
doc :: Documents.Document
meta :: Dict{Symbol, Any}
DocTestContext(file::String, doc::Documents.Document) = new(file, doc, Dict())
end
"""
$(SIGNATURES)
Traverses the pages and modules in the documenter blueprint, searching and
executing doctests.
Will abort the document generation when an error is thrown. Use `doctest = false`
keyword in [`Documenter.makedocs`](@ref) to disable doctesting.
"""
function doctest(blueprint::Documents.DocumentBlueprint, doc::Documents.Document)
@debug "Running doctests."
# find all the doctest blocks in the pages
for (src, page) in blueprint.pages
doctest(page, doc)
end
# find all the doctest block in all the docstrings (within specified modules)
for mod in blueprint.modules
for (binding, multidoc) in DocSystem.getmeta(mod)
for signature in multidoc.order
doctest(multidoc.docs[signature], mod, doc)
end
end
end
end
function doctest(page::Documents.Page, doc::Documents.Document)
ctx = DocTestContext(page.source, doc) # FIXME
ctx.meta[:CurrentFile] = page.source
doctest(ctx, page.md2ast)
end
function doctest(docstr::Docs.DocStr, mod::Module, doc::Documents.Document)
md = DocSystem.parsedoc(docstr)
# Note: parsedocs / formatdoc in Base is weird. It double-wraps the docstring Markdown
# in a Markdown.MD object..
@assert isa(md, Markdown.MD) # relying on Julia internals here
while length(md.content) == 1 && isa(first(md.content), Markdown.MD)
md = first(md.content)
end
md2ast = try
Markdown2.convert(Markdown2.MD, md)
catch err
@error """
Markdown2 conversion error for a docstring in $(mod).
This is a bug — please report this on the Documenter issue tracker
""" docstr.data
rethrow(err)
end
ctx = DocTestContext(docstr.data[:path], doc)
merge!(ctx.meta, DocMeta.getdocmeta(mod))
ctx.meta[:CurrentFile] = get(docstr.data, :path, nothing)
doctest(ctx, md2ast)
end
function parse_metablock(ctx::DocTestContext, block::Markdown2.CodeBlock)
@assert startswith(block.language, "@meta")
meta = Dict{Symbol, Any}()
for (ex, str) in Utilities.parseblock(block.code, ctx.doc, ctx.file)
if Utilities.isassign(ex)
try
meta[ex.args[1]] = Core.eval(Main, ex.args[2])
catch err
push!(ctx.doc.internal.errors, :meta_block)
@warn "Failed to evaluate `$(strip(str))` in `@meta` block." err
end
end
end
return meta
end
function doctest(ctx::DocTestContext, md2ast::Markdown2.MD)
Markdown2.walk(md2ast) do node
isa(node, Markdown2.CodeBlock) || return true
if startswith(node.language, "jldoctest")
doctest(ctx, node)
elseif startswith(node.language, "@meta")
merge!(ctx.meta, parse_metablock(ctx, node))
else
return true
end
return false
end
end
function doctest(ctx::DocTestContext, block_immutable::Markdown2.CodeBlock)
lang = block_immutable.language
if startswith(lang, "jldoctest")
# Define new module or reuse an old one from this page if we have a named doctest.
name = match(r"jldoctest[ ]?(.*)$", split(lang, ';', limit = 2)[1])[1]
sym = isempty(name) ? gensym("doctest-") : Symbol("doctest-", name)
sandbox = get!(() -> Expanders.get_new_sandbox(sym), ctx.meta, sym)
# Normalise line endings.
block = MutableMD2CodeBlock(block_immutable)
block.code = replace(block.code, "\r\n" => "\n")
# parse keyword arguments to doctest
d = Dict()
idx = findfirst(c -> c == ';', lang)
if idx !== nothing
kwargs = Meta.parse("($(lang[nextind(lang, idx):end]),)")
for kwarg in kwargs.args
if !(isa(kwarg, Expr) && kwarg.head === :(=) && isa(kwarg.args[1], Symbol))
file = ctx.meta[:CurrentFile]
lines = Utilities.find_block_in_file(block.code, file)
@warn("""
invalid syntax for doctest keyword arguments in $(Utilities.locrepr(file, lines))
Use ```jldoctest name; key1 = value1, key2 = value2
```$(lang)
$(block.code)
```
""")
return false
end
d[kwarg.args[1]] = Core.eval(sandbox, kwarg.args[2])
end
end
ctx.meta[:LocalDocTestArguments] = d
for expr in [get(ctx.meta, :DocTestSetup, []); get(ctx.meta[:LocalDocTestArguments], :setup, [])]
Meta.isexpr(expr, :block) && (expr.head = :toplevel)
try
Core.eval(sandbox, expr)
catch e
push!(ctx.doc.internal.errors, :doctest)
@error("could not evaluate expression from doctest setup.",
expression = expr, exception = e)
return false
end
end
if occursin(r"^julia> "m, block.code)
eval_repl(block, sandbox, ctx.meta, ctx.doc, ctx.file)
elseif occursin(r"^# output$"m, block.code)
eval_script(block, sandbox, ctx.meta, ctx.doc, ctx.file)
else
push!(ctx.doc.internal.errors, :doctest)
file = ctx.meta[:CurrentFile]
lines = Utilities.find_block_in_file(block.code, file)
@warn("""
invalid doctest block in $(Utilities.locrepr(file, lines))
Requires `julia> ` or `# output`
```$(lang)
$(block.code)
```
""")
end
delete!(ctx.meta, :LocalDocTestArguments)
end
false
end
doctest(ctx::DocTestContext, block) = true
# Doctest evaluation.
mutable struct Result
block :: MutableMD2CodeBlock # The entire code block that is being tested.
input :: String # Part of `block.code` representing the current input.
output :: String # Part of `block.code` representing the current expected output.
file :: String # File in which the doctest is written. Either `.md` or `.jl`.
value :: Any # The value returned when evaluating `input`.
hide :: Bool # Semi-colon suppressing the output?
stdout :: IOBuffer # Redirected stdout/stderr gets sent here.
bt :: Vector # Backtrace when an error is thrown.
function Result(block, input, output, file)
new(block, input, rstrip(output, '\n'), file, nothing, false, IOBuffer())
end
end
function eval_repl(block, sandbox, meta::Dict, doc::Documents.Document, page)
for (input, output) in repl_splitter(block.code)
result = Result(block, input, output, meta[:CurrentFile])
for (ex, str) in Utilities.parseblock(input, doc, page; keywords = false, raise=false)
# Input containing a semi-colon gets suppressed in the final output.
result.hide = REPL.ends_with_semicolon(str)
if VERSION >= v"1.5.0-DEV.178"
# Use the REPL softscope for REPL jldoctests,
# see https://github.com/JuliaLang/julia/pull/33864
ex = REPL.softscope!(ex)
end
c = IOCapture.iocapture(throwerrors=false) do
Core.eval(sandbox, ex)
end
Core.eval(sandbox, Expr(:global, Expr(:(=), :ans, QuoteNode(c.value))))
result.value = c.value
print(result.stdout, c.output)
if c.error
result.bt = c.backtrace
end
# don't evaluate further if there is a parse error
isa(ex, Expr) && ex.head === :error && break
end
checkresult(sandbox, result, meta, doc)
end
end
function eval_script(block, sandbox, meta::Dict, doc::Documents.Document, page)
# TODO: decide whether to keep `# output` syntax for this. It's a bit ugly.
# Maybe use double blank lines, i.e.
#
#
# to mark `input`/`output` separation.
input, output = split(block.code, "# output\n", limit = 2)
input = rstrip(input, '\n')
output = lstrip(output, '\n')
result = Result(block, input, output, meta[:CurrentFile])
for (ex, str) in Utilities.parseblock(input, doc, page; keywords = false, raise=false)
c = IOCapture.iocapture(throwerrors=false) do
Core.eval(sandbox, ex)
end
result.value = c.value
print(result.stdout, c.output)
if c.error
result.bt = c.backtrace
break
end
end
checkresult(sandbox, result, meta, doc)
end
function filter_doctests(strings::NTuple{2, AbstractString},
doc::Documents.Document, meta::Dict)
meta_block_filters = get(meta, :DocTestFilters, [])
meta_block_filters == nothing && meta_block_filters == []
doctest_local_filters = get(meta[:LocalDocTestArguments], :filter, [])
for r in [doc.user.doctestfilters; meta_block_filters; doctest_local_filters]
if all(occursin.((r,), strings))
strings = replace.(strings, (r => "",))
end
end
return strings
end
# Regex used here to replace gensym'd module names could probably use improvements.
function checkresult(sandbox::Module, result::Result, meta::Dict, doc::Documents.Document)
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(outio, result.value, result.bt), mod_regex => "")
str = replace(str, mod_regex_nodot => "Main")
str, head = filter_doctests((str, head), doc, meta)
# Since checking for the prefix of an error won't catch the empty case we need
# to check that manually with `isempty`.
if isempty(head) || !startswith(str, head)
if doc.user.doctest === :fix
fix_doctest(result, str, doc)
else
report(result, str, doc)
@debug "Doctest metadata" meta
push!(doc.internal.errors, :doctest)
end
end
else
value = result.hide ? nothing : result.value # `;` hides output.
output = replace(rstrip(sanitise(IOBuffer(result.output))), 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)
if filteredstr != filteredoutput
if doc.user.doctest === :fix
fix_doctest(result, str, doc)
else
report(result, str, doc)
@debug "Doctest metadata" meta
push!(doc.internal.errors, :doctest)
end
end
end
return nothing
end
# Display doctesting results.
function result_to_string(buf, value)
value === nothing || Base.invokelatest(show, IOContext(buf, :limit => true), MIME"text/plain"(), value)
return sanitise(buf)
end
function error_to_string(buf, er, bt)
# Remove unimportant backtrace info.
bt = remove_common_backtrace(bt, backtrace())
# Remove everything below the last eval call (which should be the one in IOCapture.iocapture)
index = findlast(ptr -> Base.ip_matches_func(ptr, :eval), bt)
bt = (index === nothing) ? bt : bt[1:(index - 1)]
# Print a REPL-like error message.
print(buf, "ERROR: ")
Base.invokelatest(showerror, buf, er, bt)
return sanitise(buf)
end
function remove_common_backtrace(bt, reference_bt)
cutoff = nothing
# We'll start from the top of the backtrace (end of the array) and go down, checking
# if the backtraces agree
for ridx in 1:length(bt)
# Cancel search if we run out the reference BT or find a non-matching one frames:
if ridx > length(reference_bt) || bt[length(bt) - ridx + 1] != reference_bt[length(reference_bt) - ridx + 1]
cutoff = length(bt) - ridx + 1
break
end
end
# It's possible that the loop does not find anything, i.e. that all BT elements are in
# the reference_BT too. In that case we'll just return an empty BT.
bt[1:(cutoff === nothing ? 0 : cutoff)]
end
# Strip trailing whitespace from each line and return resulting string
function sanitise(buffer)
out = IOBuffer()
for line in eachline(seekstart(Base.unwrapcontext(buffer)[1]))
println(out, rstrip(line))
end
return rstrip(String(take!(out)), '\n')
end
import .Utilities.TextDiff
function report(result::Result, str, doc::Documents.Document)
diff = TextDiff.Diff{TextDiff.Words}(result.output, rstrip(str))
lines = Utilities.find_block_in_file(result.block.code, result.file)
@error("""
doctest failure in $(Utilities.locrepr(result.file, lines))
```$(result.block.language)
$(result.block.code)
```
Subexpression:
$(result.input)
Evaluated output:
$(rstrip(str))
Expected output:
$(result.output)
""", diff)
end
function fix_doctest(result::Result, str, doc::Documents.Document)
code = result.block.code
filename = Base.find_source_file(result.file)
# read the file containing the code block
content = read(filename, String)
# output stream
io = IOBuffer(sizehint = sizeof(content))
# first look for the entire code block
# make a regex of the code that matches leading whitespace
rcode = "(\\h*)" * replace(Utilities.regex_escape(code), "\\n" => "\\n\\h*")
r = Regex(rcode)
codeidx = findfirst(r, content)
if codeidx === nothing
@warn "could not find code block in source file"
return
end
# use the capture group to identify indentation
indent = match(r, content).captures[1]
# write everything up until the code block
write(io, content[1:prevind(content, first(codeidx))])
# next look for the particular input string in the given code block
# make a regex of the input that matches leading whitespace (for multiline input)
rinput = "\\h*" * replace(Utilities.regex_escape(result.input), "\\n" => "\\n\\h*")
r = Regex(rinput)
inputidx = findfirst(r, code)
if inputidx === nothing
@warn "could not find input line in code block"
return
end
# construct the new code-snippet (without indent)
# first part: everything up until the last index of the input string
newcode = code[1:last(inputidx)]
isempty(result.output) && (newcode *= '\n') # issue #772
# second part: the rest, with the old output replaced with the new one
if result.output == ""
# This works around a regression in Julia 1.5.0 (https://github.com/JuliaLang/julia/issues/36953)
# Technically, it is only necessary if VERSION >= v"1.5.0-DEV.826"
newcode *= str
newcode *= code[nextind(code, last(inputidx)):end]
else
newcode *= replace(code[nextind(code, last(inputidx)):end], result.output => str, count = 1)
end
# replace internal code block with the non-indented new code, needed if we come back
# looking to replace output in the same code block later
result.block.code = newcode
# write the new code snippet to the stream, with indent
newcode = replace(newcode, r"^(.+)$"m => Base.SubstitutionString(indent * "\\1"))
write(io, newcode)
# write rest of the file
write(io, content[nextind(content, last(codeidx)):end])
# write to file
write(filename, seekstart(io))
return
end
# REPL doctest splitter.
const PROMPT_REGEX = r"^julia> (.*)$"
const SOURCE_REGEX = r"^ (.*)$"
function repl_splitter(code)
lines = split(string(code, "\n"), '\n')
input = String[]
output = String[]
buffer = IOBuffer() # temporary buffer for doctest inputs and outputs
found_first_prompt = false
while !isempty(lines)
line = popfirst!(lines)
prompt = match(PROMPT_REGEX, line)
# We allow comments before the first julia> prompt
!found_first_prompt && startswith(line, '#') && continue
if prompt === nothing
source = match(SOURCE_REGEX, line)
if source === nothing
savebuffer!(input, buffer)
println(buffer, line)
takeuntil!(PROMPT_REGEX, buffer, lines)
else
println(buffer, source[1])
end
else
found_first_prompt = true
savebuffer!(output, buffer)
println(buffer, prompt[1])
end
end
savebuffer!(output, buffer)
zip(input, output)
end
function savebuffer!(out, buf)
n = bytesavailable(seekstart(buf))
n > 0 ? push!(out, rstrip(String(take!(buf)))) : out
end
function takeuntil!(r, buf, lines)
while !isempty(lines)
line = lines[1]
if !occursin(r, line)
println(buf, popfirst!(lines))
else
break
end
end
end
end