backend.nim 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.}
  14. var
  15. thisMachine: MachineId
  16. thisCommit: CommitId
  17. thisBranch: string
  18. {.experimental.}
  19. proc `()`(cmd: string{lit}): string = cmd.execProcess.string.strip
  20. proc getMachine*(): MachineId =
  21. var name = "hostname"()
  22. if name.len == 0:
  23. name = when defined(posix): getenv"HOSTNAME".string
  24. else: getenv"COMPUTERNAME".string
  25. if name.len == 0:
  26. quit "cannot determine the machine name"
  27. result = MachineId(name)
  28. proc getCommit(): CommitId =
  29. const commLen = "commit ".len
  30. let hash = "git log -n 1"()[commLen..commLen+10]
  31. thisBranch = "git symbolic-ref --short HEAD"()
  32. if hash.len == 0 or thisBranch.len == 0: quit "cannot determine git HEAD"
  33. result = CommitId(hash)
  34. var
  35. results: File
  36. currentCategory: string
  37. entries: int
  38. proc writeTestResult*(name, category, target,
  39. action, result, expected, given: string) =
  40. createDir("testresults")
  41. if currentCategory != category:
  42. if currentCategory.len > 0:
  43. results.writeLine("]")
  44. close(results)
  45. currentCategory = category
  46. results = open("testresults" / category.addFileExt"json", fmWrite)
  47. results.writeLine("[")
  48. entries = 0
  49. let jentry = %*{"name": name, "category": category, "target": target,
  50. "action": action, "result": result, "expected": expected, "given": given,
  51. "machine": thisMachine.string, "commit": thisCommit.string, "branch": thisBranch}
  52. if entries > 0:
  53. results.writeLine(",")
  54. results.write($jentry)
  55. inc entries
  56. proc open*() =
  57. thisMachine = getMachine()
  58. thisCommit = getCommit()
  59. proc close*() =
  60. if currentCategory.len > 0:
  61. results.writeLine("]")
  62. close(results)