* fix code-block test bugs: fix #17183, fix https://github.com/timotheecour/Nim/issues/620

* cleanup
This commit is contained in:
Timothee Cour 2021-02-26 07:05:20 -08:00 • committed by GitHub
commit ff3ace2232
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 2 deletions

View file

@ -210,7 +210,8 @@ proc newDocumentor*(filename: AbsoluteFile; cache: IdentCache; conf: ConfigRef,
var outp: AbsoluteFile var outp: AbsoluteFile
if filename.len == 0: if filename.len == 0:
let nameOnly = splitFile(d.filename).name let nameOnly = splitFile(d.filename).name
outp = getNimcacheDir(conf) / RelativeDir(nameOnly) / # "snippets" needed, refs bug #17183
outp = getNimcacheDir(conf) / "snippets".RelativeDir / RelativeDir(nameOnly) /
RelativeFile(nameOnly & "_snippet_" & $d.id & ".nim") RelativeFile(nameOnly & "_snippet_" & $d.id & ".nim")
elif isAbsolute(filename): elif isAbsolute(filename):
outp = AbsoluteFile(filename) outp = AbsoluteFile(filename)
@ -228,10 +229,14 @@ proc newDocumentor*(filename: AbsoluteFile; cache: IdentCache; conf: ConfigRef,
# backward compatibility hacks; interpolation commands should explicitly use `$` # backward compatibility hacks; interpolation commands should explicitly use `$`
if cmd.startsWith "nim ": result = "$nim " & cmd[4..^1] if cmd.startsWith "nim ": result = "$nim " & cmd[4..^1]
else: result = cmd else: result = cmd
# factor with D20210224T221756
result = result.replace("$1", "$options") % [ result = result.replace("$1", "$options") % [
"nim", os.getAppFilename().quoteShell, "nim", os.getAppFilename().quoteShell,
"libpath", quoteShell(d.conf.libpath),
"docCmd", d.conf.docCmd,
"backend", $d.conf.backend, "backend", $d.conf.backend,
"options", outp.quoteShell, "options", outp.quoteShell,
# xxx `quoteShell` seems buggy if user passes options = "-d:foo somefile.nim"
] ]
let cmd = cmd.interpSnippetCmd let cmd = cmd.interpSnippetCmd
rawMessage(conf, hintExecuting, cmd) rawMessage(conf, hintExecuting, cmd)
@ -475,6 +480,7 @@ proc runAllExamples(d: PDoc) =
writeFile(outp, group.code) writeFile(outp, group.code)
# most useful semantics is that `docCmd` comes after `rdoccmd`, so that we can (temporarily) override # most useful semantics is that `docCmd` comes after `rdoccmd`, so that we can (temporarily) override
# via command line # via command line
# D20210224T221756:here
let cmd = "$nim $backend -r --lib:$libpath --warning:UnusedImport:off --path:$path --nimcache:$nimcache $rdoccmd $docCmd $file" % [ let cmd = "$nim $backend -r --lib:$libpath --warning:UnusedImport:off --path:$path --nimcache:$nimcache $rdoccmd $docCmd $file" % [
"nim", quoteShell(os.getAppFilename()), "nim", quoteShell(os.getAppFilename()),
"backend", $d.conf.backend, "backend", $d.conf.backend,

View file

@ -920,8 +920,11 @@ proc parseCodeBlockField(d: PDoc, n: PRstNode, params: var CodeBlockParams) =
of "test": of "test":
params.testCmd = n.getFieldValue.strip params.testCmd = n.getFieldValue.strip
if params.testCmd.len == 0: if params.testCmd.len == 0:
params.testCmd = "$nim r --backend:$backend $options" # see `interpSnippetCmd` # factor with D20210224T221756. Note that `$docCmd` should appear before `$file`
# but after all other options, but currently `$options` merges both options and `$file` so it's tricky.
params.testCmd = "$nim r --backend:$backend --lib:$libpath $docCmd $options"
else: else:
# consider whether `$docCmd` should be appended here too
params.testCmd = unescape(params.testCmd) params.testCmd = unescape(params.testCmd)
of "status", "exitcode": of "status", "exitcode":
var status: int var status: int

View file

@ -120,3 +120,13 @@ runnableExamples:
# note: there are yet other examples where putting runnableExamples at module # note: there are yet other examples where putting runnableExamples at module
# scope is needed, for example when using an `include` before an `import`, etc. # scope is needed, for example when using an `include` before an `import`, etc.
##[
snippet:
.. code-block:: Nim
:test:
doAssert defined(testFooExternal)
]##