backend.nim 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #
  2. #
  3. # The Nim Tester
  4. # (c) Copyright 2017 Andreas Rumpf
  5. #
  6. # Look at license.txt for more info.
  7. # All rights reserved.
  8. import strutils, os, osproc, json
  9. type
  10. MachineId* = distinct string
  11. CommitId = distinct string
  12. proc `$`*(id: MachineId): string {.borrow.}
  13. #proc `$`(id: CommitId): string {.borrow.} # not used
  14. var
  15. thisMachine: MachineId
  16. thisCommit: CommitId
  17. thisBranch: string
  18. proc getMachine*(): MachineId =
  19. var name = execProcess("hostname").string.strip
  20. if name.len == 0:
  21. name = when defined(posix): getEnv("HOSTNAME").string
  22. else: getEnv("COMPUTERNAME").string
  23. if name.len == 0:
  24. quit "cannot determine the machine name"
  25. result = MachineId(name)
  26. proc getCommit(): CommitId =
  27. const commLen = "commit ".len
  28. let hash = execProcess("git log -n 1").string.strip[commLen..commLen+10]
  29. thisBranch = execProcess("git symbolic-ref --short HEAD").string.strip
  30. if hash.len == 0 or thisBranch.len == 0: quit "cannot determine git HEAD"
  31. result = CommitId(hash)
  32. var
  33. results: File
  34. currentCategory: string
  35. entries: int
  36. proc writeTestResult*(name, category, target, action, result, expected, given: string) =
  37. createDir("testresults")
  38. if currentCategory != category:
  39. if currentCategory.len > 0:
  40. results.writeLine("]")
  41. close(results)
  42. currentCategory = category
  43. results = open("testresults" / category.addFileExt"json", fmWrite)
  44. results.writeLine("[")
  45. entries = 0
  46. let jentry = %*{"name": name, "category": category, "target": target,
  47. "action": action, "result": result, "expected": expected, "given": given,
  48. "machine": thisMachine.string, "commit": thisCommit.string, "branch": thisBranch}
  49. if entries > 0:
  50. results.writeLine(",")
  51. results.write($jentry)
  52. inc entries
  53. proc open*() =
  54. thisMachine = getMachine()
  55. thisCommit = getCommit()
  56. proc close*() =
  57. if currentCategory.len > 0:
  58. results.writeLine("]")
  59. close(results)