tester.nim 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Small program that runs the test cases for 'nim doc'.
  2. # To run this, cd to the git repo root, and run "nim r nimdoc/tester.nim".
  3. # to change expected results (after carefully verifying everything), use -d:nimTestsNimdocFixup
  4. import strutils, os
  5. from std/private/gitutils import diffFiles
  6. const fixup = defined(nimTestsNimdocFixup)
  7. var
  8. failures = 0
  9. const
  10. baseDir = "nimdoc"
  11. type
  12. NimSwitches = object
  13. doc: seq[string]
  14. buildIndex: seq[string]
  15. proc exec(cmd: string) =
  16. if execShellCmd(cmd) != 0:
  17. quit("FAILURE: " & cmd)
  18. proc testNimDoc(prjDir, docsDir: string; switches: NimSwitches; fixup = false) =
  19. let
  20. nimDocSwitches = switches.doc.join(" ")
  21. nimBuildIndexSwitches = switches.buildIndex.join(" ")
  22. putEnv("SOURCE_DATE_EPOCH", "100000")
  23. const nimExe = getCurrentCompilerExe() # so that `bin/nim_temp r nimdoc/tester.nim` works
  24. if nimDocSwitches != "":
  25. exec("$1 doc $2" % [nimExe, nimDocSwitches])
  26. if nimBuildIndexSwitches != "":
  27. exec("$1 buildIndex $2" % [nimExe, nimBuildIndexSwitches])
  28. for expected in walkDirRec(prjDir / "expected/"):
  29. let produced = expected.replace('\\', '/').replace("/expected/", "/$1/" % [docsDir])
  30. if not fileExists(produced):
  31. echo "FAILURE: files not found: ", produced
  32. inc failures
  33. elif readFile(expected) != readFile(produced):
  34. echo "FAILURE: files differ: ", produced
  35. echo diffFiles(expected, produced).output
  36. inc failures
  37. if fixup:
  38. copyFile(produced, expected)
  39. else:
  40. echo "SUCCESS: files identical: ", produced
  41. if failures == 0 and ((prjDir / docsDir) != prjDir):
  42. removeDir(prjDir / docsDir)
  43. # Test "nim doc --project --out:.. --index:on .."
  44. let
  45. test1PrjName = "testproject"
  46. test1Dir = baseDir / test1PrjName
  47. test1DocsDir = "htmldocs"
  48. test1Switches = NimSwitches(doc: @["--project",
  49. "--out:$1/$2" % [test1Dir, test1DocsDir],
  50. "--index:on",
  51. "$1/$2.nim" % [test1Dir, test1PrjName]],
  52. buildIndex: @["--out:$1/$2/theindex.html" % [test1Dir, test1DocsDir],
  53. "$1/$2" % [test1Dir, test1DocsDir]])
  54. testNimDoc(test1Dir, test1DocsDir, test1Switches, fixup)
  55. # Test "nim doc --out:.. --index:on .."
  56. let
  57. test2PrjDir = "test_out_index_dot_html"
  58. test2PrjName = "foo"
  59. test2Dir = baseDir / test2PrjDir
  60. test2DocsDir = "htmldocs"
  61. test2Switches = NimSwitches(doc: @["--out:$1/$2/index.html" % [test2Dir, test2DocsDir],
  62. "--index:on",
  63. "$1/$2.nim" % [test2Dir, test2PrjName]],
  64. buildIndex: @["--out:$1/$2/theindex.html" % [test2Dir, test2DocsDir],
  65. "$1/$2" % [test2Dir, test2DocsDir]])
  66. testNimDoc(test2Dir, test2DocsDir, test2Switches, fixup)
  67. if failures > 0:
  68. quit "$# failures occurred; see note in nimdoc/tester.nim regarding -d:nimTestsNimdocFixup" % $failures