testutils.nim 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import std/private/miscdollars
  2. import std/strutils
  3. template flakyAssert*(cond: untyped, msg = "", notifySuccess = true) =
  4. ## API to deal with flaky or failing tests. This avoids disabling entire tests
  5. ## altogether so that at least the parts that are working are kept being
  6. ## tested. This also avoids making CI fail periodically for tests known to
  7. ## be flaky. Finally, for known failures, passing `notifySuccess = true` will
  8. ## log that the test succeeded, which may indicate that a bug was fixed
  9. ## "by accident" and should be looked into.
  10. const info = instantiationInfo(-1, true)
  11. const expr = astToStr(cond)
  12. if cond and not notifySuccess:
  13. discard # silent success
  14. else:
  15. var msg2 = ""
  16. toLocation(msg2, info.filename, info.line, info.column)
  17. if cond:
  18. # a flaky test is failing, we still report it but we don't fail CI
  19. msg2.add " FLAKY_SUCCESS "
  20. else:
  21. # a previously failing test is now passing, a pre-existing bug might've been
  22. # fixed by accidend
  23. msg2.add " FLAKY_FAILURE "
  24. msg2.add $expr & " " & msg
  25. echo msg2
  26. proc greedyOrderedSubsetLines*(lhs, rhs: string): bool =
  27. ## returns true if each stripped line in `lhs` appears in rhs, using a greedy matching.
  28. let rhs = rhs.strip
  29. var currentPos = 0
  30. for line in lhs.strip.splitLines:
  31. currentPos = rhs.find(line.strip, currentPos)
  32. if currentPos < 0:
  33. return false
  34. return true