backend.nim 1.8 KB

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