Skip to content
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

nim doc --backend:js, nim doc --doccmd:-d:foo, nim r --backend:js, --doccmd:skip + other improvements #14278

Merged
merged 9 commits into from
May 11, 2020
Prev Previous commit
Next Next commit
add tests
  • Loading branch information
timotheecour committed May 10, 2020
commit c4dcb85be5665bff5e6537ab3778cc8beb041ad9
2 changes: 0 additions & 2 deletions compiler/docgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ proc runAllExamples(d: PDoc) =
# This used to be: `let backend = if isDefined(d.conf, "js"): "js"` (etc), however
# using `-d:js` (etc) cannot work properly, eg would fail with `importjs`
# since semantics are affected by `config.backend`, not by isDefined(d.conf, "js")
echo0b docCmd, backend
if d.examples.len == 0 or docCmd == "skip": return
let outputDir = d.conf.getNimcacheDir / RelativeDir"runnableExamples"
let outp = outputDir / RelativeFile(extractFilename(d.filename.changeFileExt"" &
Expand All @@ -463,7 +462,6 @@ proc runAllExamples(d: PDoc) =
"file", quoteShell(outp),
"docCmd", docCmd,
]
echo0b cmd
if os.execShellCmd(cmd) != 0:
quit "[runnableExamples] failed: generated file: '$1' cmd: $2" % [outp.string, cmd]
else:
Expand Down
2 changes: 0 additions & 2 deletions compiler/extccomp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,6 @@ proc footprint(conf: ConfigRef; cfile: Cfile): SecureHash =
getCompileCFileCmd(conf, cfile))

proc externalFileChanged(conf: ConfigRef; cfile: Cfile): bool =
# PRTEMP nim doc?
echo0b (conf.backend,"D20200507T233616")
case conf.backend
of backendInvalid: doAssert false
of backendJs: return false # pre-existing behavior, but not sure it's good
Expand Down
1 change: 1 addition & 0 deletions compiler/vmops.nim
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ when defined(nimHasInvariant):
of linkOptions: result = conf.linkOptions
of compileOptions: result = conf.compileOptions
of ccompilerPath: result = conf.cCompilerPath
of backend: result = $conf.backend

proc querySettingSeqImpl(conf: ConfigRef, switch: BiggestInt): seq[string] =
template copySeq(field: untyped): untyped =
Expand Down
2 changes: 2 additions & 0 deletions lib/std/compilesettings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type
linkOptions, ## additional options passed to the linker
compileOptions, ## additional options passed to the C/C++ compiler
ccompilerPath ## the path to the C/C++ compiler
backend ## the backend (eg: c|cpp|objc|js); both `nim doc --backend:js`
## and `nim js` would imply backend=js

MultipleValueSetting* {.pure.} = enum ## \
## settings resulting in a seq of string values
Expand Down
36 changes: 36 additions & 0 deletions tests/nimdoc/m13129.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
when defined(cpp):
{.push header: "<vector>".}
type
Vector[T] {.importcpp: "std::vector".} = object
elif defined(js):
proc endsWith*(s, suffix: cstring): bool {.noSideEffect,importjs: "#.endsWith(#)".}
elif defined(c):
proc c_printf*(frmt: cstring): cint {.
importc: "printf", header: "<stdio.h>", varargs, discardable.}

proc main*() =
runnableExamples:
import std/compilesettings
doAssert not defined(m13129Foo1)
doAssert defined(m13129Foo2)
doAssert not defined(nimdoc)
echo "ok2: backend: " & querySetting(backend)
# echo defined(c), defined(js),

import std/compilesettings
when defined nimdoc:
# import std/compilesettings
static:
doAssert defined(m13129Foo1)
doAssert not defined(m13129Foo2)
echo "ok1:" & querySetting(backend)

when isMainModule:
when not defined(js):
import std/os
let cache = querySetting(nimcacheDir)
doAssert cache.len > 0
let app = getAppFilename()
doAssert app.isRelativeTo(cache)
doAssert querySetting(projectFull) == currentSourcePath
echo "ok3"
2 changes: 2 additions & 0 deletions tests/nimdoc/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
the html validation is tested by nimdoc/tester.nim
the runnableExamples + nim doc logic (across backend) is tested here
37 changes: 28 additions & 9 deletions tests/trunner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ discard """
## tests that don't quite fit the mold and are easier to handle via `execCmdEx`
## A few others could be added to here to simplify code.

import std/[strformat,os,osproc]
import std/[strformat,os,osproc,unittest]

const nim = getCurrentCompilerExe()

Expand All @@ -15,8 +15,9 @@ const mode =
elif defined(cpp): "cpp"
else: static: doAssert false

const testsDir = currentSourcePath().parentDir

proc runCmd(file, options = ""): auto =
const testsDir = currentSourcePath().parentDir
let fileabs = testsDir / file.unixToNativePath
doAssert fileabs.existsFile, fileabs
let cmd = fmt"{nim} {mode} {options} --hints:off {fileabs}"
Expand Down Expand Up @@ -55,11 +56,11 @@ ret=[s1:foobar s2:foobar age:25 pi:3.14]

else: # don't run twice the same test
import std/[strutils]
template check(msg) = doAssert msg in output, output
template check2(msg) = doAssert msg in output, output

block: # mstatic_assert
let (output, exitCode) = runCmd("ccgbugs/mstatic_assert.nim", "-d:caseBad")
check "sizeof(bool) == 2"
check2 "sizeof(bool) == 2"
doAssert exitCode != 0

block: # ABI checks
Expand All @@ -72,11 +73,11 @@ else: # don't run twice the same test
# on platforms that support _StaticAssert natively, errors will show full context, eg:
# error: static_assert failed due to requirement 'sizeof(unsigned char) == 8'
# "backend & Nim disagree on size for: BadImportcType{int64} [declared in mabi_check.nim(1, 6)]"
check "sizeof(unsigned char) == 8"
check "sizeof(struct Foo2) == 1"
check "sizeof(Foo5) == 16"
check "sizeof(Foo5) == 3"
check "sizeof(struct Foo6) == "
check2 "sizeof(unsigned char) == 8"
check2 "sizeof(struct Foo2) == 1"
check2 "sizeof(Foo5) == 16"
check2 "sizeof(Foo5) == 3"
check2 "sizeof(struct Foo6) == "
doAssert exitCode != 0

import streams
Expand All @@ -103,3 +104,21 @@ else: # don't run twice the same test
var (output, exitCode) = execCmdEx(cmd)
output.stripLineEnd
doAssert output == expected

block: # nim doc --backend:$backend --doccmd:$cmd
# test for https://github.com/nim-lang/Nim/issues/13129
# test for https://github.com/nim-lang/Nim/issues/13891
const buildDir = testsDir.parentDir / "build"
const nimcache = buildDir / "nimcacheTrunner"
# `querySetting(nimcacheDir)` would also be possible, but we thus
# avoid stomping on other parallel tests
let file = testsDir / "nimdoc/m13129.nim"
for backend in fmt"{mode} js".split:
let cmd = fmt"{nim} doc -b:{backend} --nimcache:{nimcache} -d:m13129Foo1 --doccmd:'-d:m13129Foo2 --hints:off' --usenimcache --hints:off {file}"
check execCmdEx(cmd) == (&"ok1:{backend}\nok2: backend: {backend}\n", 0)
# checks that --usenimcache works with `nim doc`
check fileExists(nimcache / "m13129.html")

block: # mak sure --backend works with `nim r`
let cmd = fmt"{nim} r --backend:{mode} --hints:off --nimcache:{nimcache} {file}"
check execCmdEx(cmd) == ("ok3\n", 0)
1 change: 1 addition & 0 deletions tests/vm/tcompilesetting.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ static:

doAssert "myNimCache" in nc
doAssert "myNimblePath" in np[0]
doAssert querySetting(backend) == "c"