From f3d35b9b151b5de7817e0556eea3201d9958df52 Mon Sep 17 00:00:00 2001 From: flaviut Date: Fri, 18 Jul 2014 15:52:51 -0400 Subject: [PATCH] Make it easy to test code --- testexamples.nim | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 testexamples.nim diff --git a/testexamples.nim b/testexamples.nim new file mode 100644 index 0000000..759a1e4 --- /dev/null +++ b/testexamples.nim @@ -0,0 +1,40 @@ +import md5, re, os, strutils, osproc + +let + markdownFilesGlob = getCurrentDir() / "content" / "*.md" + configFilePath = getCurrentDir() / "validcodehashes.txt" + +for filePath in walkFiles(markdownFilesGlob): + let file = open(filePath, fmRead) + let fileContents = file.readAll() + file.close() + + let sourceSegments = fileContents.findAll(re"""(?i)```\s*nimrod[\s\S]*?```""") + for match in sourceSegments: + let code = match.replacef(re"""(?i)```\s*nimrod[ \t]*\s([\s\S]*?)```""", "$1") + let codeHash = code.getMD5() + if not configFilePath.readFile().contains(codeHash): # Hasn't been verified yet + let tempCodePath = getTempDir() / "code.nim" + tempCodePath.writeFile(code) + let (output, exitCode) = execCmdEx("nimrod c -r --verbosity:0 " & tempCodePath) + + var printableCode = "" + for line in code.splitLines(): + printableCode.add(" " & line & "\n") + + var printableOutput = "" + if output != nil: + for line in output.splitLines(): + printableOutput.add("> " & line & "\n") + + echo filePath + stdout.write(printableCode & printableOutput) + + stdout.write("Everything ok? (y/n) ") + case stdin.readLine.toLower: + of "y": + configFilePath.writeFile(configFilePath.readFile() & "\n" & codeHash) + of "n": + discard + + echo "====================" \ No newline at end of file