summary refs log tree commit diff stats
path: root/nimdoc/rsttester.nim
blob: be2b56c67831ad8f50ab4ff5bf58895c5389b662 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# To run this, cd to the git repo root, and run "nim r nimdoc/rsttester.nim".
# to change expected results (after carefully verifying everything), use -d:nimTestsNimdocFixup

import os, strutils
from std/private/gitutils import diffFiles

const
  baseDir = "nimdoc/rst2html"

const fixup = defined(nimTestsNimdocFixup)

var failures = 0

proc exec(cmd: string) =
  if execShellCmd(cmd) != 0:
    quit("FAILURE: " & cmd)

proc testRst2Html(fixup = false) =
  putEnv("SOURCE_DATE_EPOCH", "100000")
  const nimExe = getCurrentCompilerExe() # so that `bin/nim_temp r nimdoc/tester.nim` works

  for expectedHtml in walkDir(baseDir / "expected"):
    let expectedHtml = expectedHtml.path
    let sourceFile = expectedHtml.replace('\\', '/').replace("/expected/", "/source/").replace(".html", ".rst")
    exec("$1 rst2html $2" % [nimExe, sourceFile])
    let producedHtml = expectedHtml.replace('\\', '/').replace("/expected/", "/source/htmldocs/")
    let versionCacheParam = "?v=" & $NimMajor & "." & $NimMinor & "." & $NimPatch
    let producedFile = readFile(producedHtml).replace(versionCacheParam,"") #remove version cache param used for cache invalidation
    if readFile(expectedHtml) != producedFile:
      echo diffFiles(expectedHtml, producedHtml).output
      inc failures
      if fixup:
        writeFile(expectedHtml, producedFile)
    else:
      echo "SUCCESS: files identical: ", producedHtml
    if failures == 0:
      removeDir(baseDir / "source/htmldocs")

testRst2Html(fixup)

# Check for failures
if failures > 0: quit($failures & " failures occurred.")