tester.nim 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. let
  12. baseDirAbs = getCurrentDir() / baseDir
  13. type
  14. NimSwitches = object
  15. doc: seq[string]
  16. docStage2: seq[string]
  17. buildIndex: seq[string]
  18. md2html: seq[string]
  19. md2htmlStage2: seq[string]
  20. proc exec(cmd: string) =
  21. if execShellCmd(cmd) != 0:
  22. quit("FAILURE: " & cmd)
  23. proc testNimDoc(prjDir, docsDir: string; switches: NimSwitches; fixup = false) =
  24. let
  25. nimDocSwitches = switches.doc.join(" ")
  26. nimDocStage2Switches = switches.docStage2.join(" ")
  27. nimMd2HtmlSwitches = switches.md2html.join(" ")
  28. nimMd2HtmlStage2Switches = switches.md2htmlStage2.join(" ")
  29. nimBuildIndexSwitches = switches.buildIndex.join(" ")
  30. putEnv("SOURCE_DATE_EPOCH", "100000")
  31. const nimExe = getCurrentCompilerExe() # so that `bin/nim_temp r nimdoc/tester.nim` works
  32. if nimDocSwitches != "":
  33. exec("$1 doc $2" % [nimExe, nimDocSwitches])
  34. echo("$1 doc $2" % [nimExe, nimDocSwitches])
  35. if nimMd2HtmlSwitches != "":
  36. exec("$1 md2html $2" % [nimExe, nimMd2HtmlSwitches])
  37. echo("$1 md2html $2" % [nimExe, nimMd2HtmlSwitches])
  38. if nimDocStage2Switches != "":
  39. exec("$1 doc $2" % [nimExe, nimDocStage2Switches])
  40. echo("$1 doc $2" % [nimExe, nimDocStage2Switches])
  41. if nimMd2HtmlStage2Switches != "":
  42. exec("$1 md2html $2" % [nimExe, nimMd2HtmlStage2Switches])
  43. echo("$1 md2html $2" % [nimExe, nimMd2HtmlStage2Switches])
  44. if nimBuildIndexSwitches != "":
  45. exec("$1 buildIndex $2" % [nimExe, nimBuildIndexSwitches])
  46. echo("$1 buildIndex $2" % [nimExe, nimBuildIndexSwitches])
  47. for expected in walkDirRec(prjDir / "expected/", checkDir=true):
  48. let produced = expected.replace('\\', '/').replace("/expected/", "/$1/" % [docsDir])
  49. if not fileExists(produced):
  50. echo "FAILURE: files not found: ", produced
  51. inc failures
  52. elif readFile(expected) != readFile(produced):
  53. echo "FAILURE: files differ: ", produced
  54. echo diffFiles(expected, produced).output
  55. inc failures
  56. if fixup:
  57. copyFile(produced, expected)
  58. else:
  59. echo "SUCCESS: files identical: ", produced
  60. if failures == 0 and ((prjDir / docsDir) != prjDir):
  61. removeDir(prjDir / docsDir)
  62. # Test "nim doc --project --out:.. --index:on .."
  63. let
  64. test1PrjName = "testproject"
  65. test1Dir = baseDir / test1PrjName
  66. test1DocsDir = "htmldocs"
  67. test1Switches = NimSwitches(doc: @["--project",
  68. "--out:$1/$2" % [test1Dir, test1DocsDir],
  69. "--index:on",
  70. "$1/$2.nim" % [test1Dir, test1PrjName]],
  71. buildIndex: @["--out:$1/$2/theindex.html" % [test1Dir, test1DocsDir],
  72. "$1/$2" % [test1Dir, test1DocsDir]])
  73. testNimDoc(test1Dir, test1DocsDir, test1Switches, fixup)
  74. # Test "nim doc --out:.. --index:on .."
  75. let
  76. test2PrjDir = "test_out_index_dot_html"
  77. test2PrjName = "foo"
  78. test2Dir = baseDir / test2PrjDir
  79. test2DocsDir = "htmldocs"
  80. test2Switches = NimSwitches(doc: @["--out:$1/$2/index.html" % [test2Dir, test2DocsDir],
  81. "--index:on",
  82. "$1/$2.nim" % [test2Dir, test2PrjName]],
  83. buildIndex: @["--out:$1/$2/theindex.html" % [test2Dir, test2DocsDir],
  84. "$1/$2" % [test2Dir, test2DocsDir]])
  85. testNimDoc(test2Dir, test2DocsDir, test2Switches, fixup)
  86. # Test `nim doc` on file with `{.doctype.}` pragma
  87. let
  88. test3PrjDir = "test_doctype"
  89. test3PrjName = "test_doctype"
  90. test3Dir = baseDir / test3PrjDir
  91. test3DocsDir = "htmldocs"
  92. test3Switches = NimSwitches(doc: @["$1/$2.nim" % [test3Dir, test3PrjName]])
  93. testNimDoc(test3Dir, test3DocsDir, test3Switches, fixup)
  94. # Test concise external links (RFC#125) that work with `.idx` files.
  95. # extlinks
  96. # ├── project
  97. # │   ├── main.nim
  98. # │   ├── manual.md
  99. # │   └── sub
  100. # │   └── submodule.nim
  101. # └── util.nim
  102. #
  103. # `main.nim` imports `submodule.nim` and `../utils.nim`.
  104. # `main.nim`, `submodule.nim`, `manual.md` do importdoc and reference each other.
  105. let
  106. test4PrjName = "extlinks/project"
  107. test4Dir = baseDir / test4PrjName
  108. test4DirAbs = baseDirAbs / test4PrjName
  109. test4MainModule = "main"
  110. test4MarkupDoc = "doc" / "manual.md"
  111. test4DocsDir = "htmldocs"
  112. # 1st stage is with --index:only, 2nd is final
  113. test4Switches = NimSwitches(
  114. doc: @["--project",
  115. "--outdir:$1/$2" % [test4Dir, test4DocsDir],
  116. "--index:only",
  117. "$1/$2.nim" % [test4Dir, test4MainModule]],
  118. md2html:
  119. @["--outdir:$1/$2" % [test4Dir, test4DocsDir],
  120. "--docroot:$1" % [test4DirAbs],
  121. "--index:only",
  122. "$1/$2" % [test4Dir, test4MarkupDoc]],
  123. docStage2:
  124. @["--project",
  125. "--outdir:$1/$2" % [test4Dir, test4DocsDir],
  126. "$1/$2.nim" % [test4Dir, test4MainModule]],
  127. md2htmlStage2:
  128. @["--outdir:$1/$2" % [test4Dir, test4DocsDir],
  129. "--docroot:$1" % [test4DirAbs],
  130. "$1/$2" % [test4Dir, test4MarkupDoc]],
  131. )
  132. testNimDoc(test4Dir, test4DocsDir, test4Switches, fixup)
  133. if failures > 0:
  134. quit "$# failures occurred; see note in nimdoc/tester.nim regarding -d:nimTestsNimdocFixup" % $failures