tester.nim 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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:fixup
  4. import strutils, os
  5. var
  6. failures = 0
  7. const
  8. baseDir = "nimdoc"
  9. type
  10. NimSwitches = object
  11. doc: seq[string]
  12. buildIndex: seq[string]
  13. proc exec(cmd: string) =
  14. if execShellCmd(cmd) != 0:
  15. quit("FAILURE: " & cmd)
  16. proc testNimDoc(prjDir, docsDir: string; switches: NimSwitches; fixup = false) =
  17. let
  18. nimDocSwitches = switches.doc.join(" ")
  19. nimBuildIndexSwitches = switches.buildIndex.join(" ")
  20. putEnv("SOURCE_DATE_EPOCH", "100000")
  21. const nimExe = getCurrentCompilerExe() # so that `bin/nim_temp r nimdoc/tester.nim` works
  22. if nimDocSwitches != "":
  23. exec("$1 doc $2" % [nimExe, nimDocSwitches])
  24. if nimBuildIndexSwitches != "":
  25. exec("$1 buildIndex $2" % [nimExe, nimBuildIndexSwitches])
  26. for expected in walkDirRec(prjDir / "expected/"):
  27. let produced = expected.replace('\\', '/').replace("/expected/", "/$1/" % [docsDir])
  28. if not fileExists(produced):
  29. echo "FAILURE: files not found: ", produced
  30. inc failures
  31. elif readFile(expected) != readFile(produced):
  32. echo "FAILURE: files differ: ", produced
  33. discard execShellCmd("diff -uNdr " & expected & " " & produced)
  34. inc failures
  35. if fixup:
  36. copyFile(produced, expected)
  37. else:
  38. echo "SUCCESS: files identical: ", produced
  39. if failures == 0 and ((prjDir / docsDir) != prjDir):
  40. removeDir(prjDir / docsDir)
  41. # Test "nim doc --project --out:.. --index:on .."
  42. let
  43. test1PrjName = "testproject"
  44. test1Dir = baseDir / test1PrjName
  45. test1DocsDir = "htmldocs"
  46. test1Switches = NimSwitches(doc: @["--project",
  47. "--out:$1/$2" % [test1Dir, test1DocsDir],
  48. "--index:on",
  49. "$1/$2.nim" % [test1Dir, test1PrjName]],
  50. buildIndex: @["--out:$1/$2/theindex.html" % [test1Dir, test1DocsDir],
  51. "$1/$2" % [test1Dir, test1DocsDir]])
  52. testNimDoc(test1Dir, test1DocsDir, test1Switches, defined(fixup))
  53. # Test "nim doc --out:.. --index:on .."
  54. let
  55. test2PrjDir = "test_out_index_dot_html"
  56. test2PrjName = "foo"
  57. test2Dir = baseDir / test2PrjDir
  58. test2DocsDir = "htmldocs"
  59. test2Switches = NimSwitches(doc: @["--out:$1/$2/index.html" % [test2Dir, test2DocsDir],
  60. "--index:on",
  61. "$1/$2.nim" % [test2Dir, test2PrjName]],
  62. buildIndex: @["--out:$1/$2/theindex.html" % [test2Dir, test2DocsDir],
  63. "$1/$2" % [test2Dir, test2DocsDir]])
  64. testNimDoc(test2Dir, test2DocsDir, test2Switches, defined(fixup))
  65. # Check for failures
  66. if failures > 0: quit($failures & " failures occurred.")