summary refs log tree commit diff stats
path: root/testament/backend.nim
diff options
context:
space:
mode:
authorJacek Sieka <arnetheduck@gmail.com>2018-10-12 09:27:47 -0600
committerAndreas Rumpf <rumpf_a@web.de>2018-10-12 17:27:47 +0200
commit97738a4f2842c88b7b63a579565cd860a7b28c4e (patch)
tree32c32ac85b9e91e7ba37684002c2054e28a1d269 /testament/backend.nim
parentc492a7fd839175244abb7d4b40d189ec10d53aed (diff)
downloadNim-97738a4f2842c88b7b63a579565cd860a7b28c4e.tar.gz
Testament pre parallel (#9137)
* testament: move to root dir (it's not a test)

* osproc: fix process index passed to afterRunEvent for parallel runs

it was passing the index of the process, not index of all commands

* testament: complete file move
Diffstat (limited to 'testament/backend.nim')
-rw-r--r--testament/backend.nim75
1 files changed, 75 insertions, 0 deletions
diff --git a/testament/backend.nim b/testament/backend.nim
new file mode 100644
index 000000000..385f1171c
--- /dev/null
+++ b/testament/backend.nim
@@ -0,0 +1,75 @@
+#
+#
+#              The Nim Tester
+#        (c) Copyright 2017 Andreas Rumpf
+#
+#    Look at license.txt for more info.
+#    All rights reserved.
+
+import strutils, os, osproc, json
+
+type
+  MachineId* = distinct string
+  CommitId = distinct string
+
+proc `$`*(id: MachineId): string {.borrow.}
+#proc `$`(id: CommitId): string {.borrow.} # not used
+
+var
+  thisMachine: MachineId
+  thisCommit: CommitId
+  thisBranch: string
+
+{.experimental.}
+proc `()`(cmd: string{lit}): string = cmd.execProcess.string.strip
+
+proc getMachine*(): MachineId =
+  var name = "hostname"()
+  if name.len == 0:
+    name = when defined(posix): getenv"HOSTNAME".string
+           else: getenv"COMPUTERNAME".string
+  if name.len == 0:
+    quit "cannot determine the machine name"
+
+  result = MachineId(name)
+
+proc getCommit(): CommitId =
+  const commLen = "commit ".len
+  let hash = "git log -n 1"()[commLen..commLen+10]
+  thisBranch = "git symbolic-ref --short HEAD"()
+  if hash.len == 0 or thisBranch.len == 0: quit "cannot determine git HEAD"
+  result = CommitId(hash)
+
+var
+  results: File
+  currentCategory: string
+  entries: int
+
+proc writeTestResult*(name, category, target,
+                      action, result, expected, given: string) =
+  createDir("testresults")
+  if currentCategory != category:
+    if currentCategory.len > 0:
+      results.writeLine("]")
+      close(results)
+    currentCategory = category
+    results = open("testresults" / category.addFileExt"json", fmWrite)
+    results.writeLine("[")
+    entries = 0
+
+  let jentry = %*{"name": name, "category": category, "target": target,
+    "action": action, "result": result, "expected": expected, "given": given,
+    "machine": thisMachine.string, "commit": thisCommit.string, "branch": thisBranch}
+  if entries > 0:
+    results.writeLine(",")
+  results.write($jentry)
+  inc entries
+
+proc open*() =
+  thisMachine = getMachine()
+  thisCommit = getCommit()
+
+proc close*() =
+  if currentCategory.len > 0:
+    results.writeLine("]")
+    close(results)