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

fix #10731 ; runnableExamples "-b:cpp --run:off": code works #14384

Merged
merged 3 commits into from
May 20, 2020

Conversation

timotheecour
Copy link
Member

@timotheecour timotheecour commented May 17, 2020

example

proc fun*() =
  ## some comment
  runnableExamples: discard # same as before
  runnableExamples "-d:foo -b:cpp":
    import std/compilesettings
    doAssert querySetting(backend) == "cpp"
  runnableExamples "-r:off": ## this one is only compiled
     import std/browsers
     openDefaultBrowser "https://forum.nim-lang.org/"

more examples

see tests in tests/nimdoc/trunnableexamples.nim

features

  • plays well with --doccmd:cmd: --doccmd:cmd (if passed) is appended (not prepended) to rdoccmd so that we can override a test via cmdline; it'd be easy to add a prepend option if ever needed.

  • runnableExamples are grouped by unique rdoccmd so that we maintain the "megatest" speed advantages introduced in 5fc78d8

  • fixes runnableExamples that are only compiled #10731 as a special case: simply write:

runnableExamples "-r:off": ## this one is only compiled
   import std/browsers
   openDefaultBrowser "https://forum.nim-lang.org/"

workaround if false: was not good enough

it's uglier, eg:

import std/browsers
if false:
  openDefaultBrowser "https://forum.nim-lang.org/"

and doesn't work in many cases as a replacement for compile but not run:

  • eg if some import would cause module level code to run which would fail (bc of some required dependency)
  • or if it needs a dll that may not be available at runtime, which is a common problem, eg:
proc fun*() =
  # this would work fine with `runnableExamples: "-r:off"` and not require `if false:` hack
  runnableExamples:
    import sdl2, opengl
    if false: # this will still give a runtime failure in case sdl2 dll is not available at runtime
      discard sdl2.init(INIT_EVERYTHING)
      var window = createWindow("test", 100, 100, 640, 480, SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE)
      var context = window.glCreateContext()
      loadExtensions()
      var evt = sdl2.defaultEvent
      while true:
        while pollEvent(evt): break
        glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
        delay 1_000
      destroy window
  • furthermore it didn't handle at all the other use cases enabled by having custom command, eg -d:cpp --threads, or --compileOnly, etc.

bottom line: this makes it simple to have self contained examples in a single file, even if individual examples need different cmdline options

@timotheecour timotheecour marked this pull request as ready for review May 17, 2020 14:46
@timotheecour timotheecour changed the title runnableExamples "-b:cpp -r:off": code fix #10731 ; runnableExamples "-b:cpp --run:off": code works May 18, 2020
@timotheecour timotheecour requested a review from Araq May 18, 2020 09:34
@timotheecour
Copy link
Member Author

@Araq friendly ping on this (so I can avoid some workarounds in another PR)

@Araq Araq merged commit 3d20f14 into nim-lang:devel May 20, 2020
@timotheecour timotheecour deleted the pr_runnableExamples_doccmd branch May 20, 2020 08:04
@timotheecour
Copy link
Member Author

thanks!

EchoPouet pushed a commit to EchoPouet/Nim that referenced this pull request Jun 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

runnableExamples that are only compiled
3 participants