tester.nim 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 versionCacheParam = "?v=" & $NimMajor & "." & $NimMinor & "." & $NimPatch
  49. let produced = expected.replace('\\', '/').replace("/expected/", "/$1/" % [docsDir])
  50. if not fileExists(produced):
  51. echo "FAILURE: files not found: ", produced
  52. inc failures
  53. let producedFile = readFile(produced).replace(versionCacheParam,"") #remove version cache param used for cache invalidation
  54. if readFile(expected) != producedFile:
  55. echo "FAILURE: files differ: ", produced
  56. echo diffFiles(expected, produced).output
  57. inc failures
  58. if fixup:
  59. writeFile(expected, producedFile)
  60. else:
  61. echo "SUCCESS: files identical: ", produced
  62. if failures == 0 and ((prjDir / docsDir) != prjDir):
  63. removeDir(prjDir / docsDir)
  64. # Test "nim doc --project --out:.. --index:on .."
  65. let
  66. test1PrjName = "testproject"
  67. test1Dir = baseDir / test1PrjName
  68. test1DocsDir = "htmldocs"
  69. test1Switches = NimSwitches(doc: @["--project",
  70. "--out:$1/$2" % [test1Dir, test1DocsDir],
  71. "--index:on",
  72. "$1/$2.nim" % [test1Dir, test1PrjName]],
  73. buildIndex: @["--out:$1/$2/theindex.html" % [test1Dir, test1DocsDir],
  74. "$1/$2" % [test1Dir, test1DocsDir]])
  75. testNimDoc(test1Dir, test1DocsDir, test1Switches, fixup)
  76. # Test "nim doc --out:.. --index:on .."
  77. let
  78. test2PrjDir = "test_out_index_dot_html"
  79. test2PrjName = "foo"
  80. test2Dir = baseDir / test2PrjDir
  81. test2DocsDir = "htmldocs"
  82. test2Switches = NimSwitches(doc: @["--out:$1/$2/index.html" % [test2Dir, test2DocsDir],
  83. "--index:on",
  84. "$1/$2.nim" % [test2Dir, test2PrjName]],
  85. buildIndex: @["--out:$1/$2/theindex.html" % [test2Dir, test2DocsDir],
  86. "$1/$2" % [test2Dir, test2DocsDir]])
  87. testNimDoc(test2Dir, test2DocsDir, test2Switches, fixup)
  88. # Test `nim doc` on file with `{.doctype.}` pragma
  89. let
  90. test3PrjDir = "test_doctype"
  91. test3PrjName = "test_doctype"
  92. test3Dir = baseDir / test3PrjDir
  93. test3DocsDir = "htmldocs"
  94. test3Switches = NimSwitches(doc: @["$1/$2.nim" % [test3Dir, test3PrjName]])
  95. testNimDoc(test3Dir, test3DocsDir, test3Switches, fixup)
  96. # Test concise external links (RFC#125) that work with `.idx` files.
  97. # extlinks
  98. # ├── project
  99. # │   ├── main.nim
  100. # │   ├── manual.md
  101. # │   └── sub
  102. # │   └── submodule.nim
  103. # └── util.nim
  104. #
  105. # `main.nim` imports `submodule.nim` and `../utils.nim`.
  106. # `main.nim`, `submodule.nim`, `manual.md` do importdoc and reference each other.
  107. let
  108. test4PrjName = "extlinks/project"
  109. test4Dir = baseDir / test4PrjName
  110. test4DirAbs = baseDirAbs / test4PrjName
  111. test4MainModule = "main"
  112. test4MarkupDoc = "doc" / "manual.md"
  113. test4DocsDir = "htmldocs"
  114. # 1st stage is with --index:only, 2nd is final
  115. test4Switches = NimSwitches(
  116. doc: @["--project",
  117. "--outdir:$1/$2" % [test4Dir, test4DocsDir],
  118. "--index:only",
  119. "$1/$2.nim" % [test4Dir, test4MainModule]],
  120. md2html:
  121. @["--outdir:$1/$2" % [test4Dir, test4DocsDir],
  122. "--docroot:$1" % [test4DirAbs],
  123. "--index:only",
  124. "$1/$2" % [test4Dir, test4MarkupDoc]],
  125. docStage2:
  126. @["--project",
  127. "--outdir:$1/$2" % [test4Dir, test4DocsDir],
  128. "$1/$2.nim" % [test4Dir, test4MainModule]],
  129. md2htmlStage2:
  130. @["--outdir:$1/$2" % [test4Dir, test4DocsDir],
  131. "--docroot:$1" % [test4DirAbs],
  132. "$1/$2" % [test4Dir, test4MarkupDoc]],
  133. )
  134. testNimDoc(test4Dir, test4DocsDir, test4Switches, fixup)
  135. if failures > 0:
  136. quit "$# failures occurred; see note in nimdoc/tester.nim regarding -d:nimTestsNimdocFixup" % $failures