tester.nim 2.7 KB

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