backend.nim 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #
  2. #
  3. # The Nim Tester
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # Look at license.txt for more info.
  7. # All rights reserved.
  8. import strutils, db_sqlite, os, osproc
  9. var db: DbConn
  10. proc createDb() =
  11. db.exec(sql"""
  12. create table if not exists Machine(
  13. id integer primary key,
  14. name varchar(100) not null,
  15. os varchar(20) not null,
  16. cpu varchar(20) not null
  17. );""")
  18. db.exec(sql"""
  19. create table if not exists [Commit](
  20. id integer primary key,
  21. hash varchar(256) not null,
  22. branch varchar(50) not null
  23. );""")
  24. db.exec(sql"""
  25. create table if not exists TestResult(
  26. id integer primary key,
  27. name varchar(100) not null,
  28. category varchar(100) not null,
  29. target varchar(20) not null,
  30. action varchar(10) not null,
  31. result varchar(30) not null,
  32. [commit] int not null,
  33. machine int not null,
  34. expected varchar(10000) not null,
  35. given varchar(10000) not null,
  36. created timestamp not null default (DATETIME('now')),
  37. foreign key ([commit]) references [commit](id),
  38. foreign key (machine) references machine(id)
  39. );""")
  40. #db.exec(sql"""
  41. # --create unique index if not exists TsstNameIx on TestResult(name);
  42. # """, [])
  43. type
  44. MachineId* = distinct int64
  45. CommitId = distinct int64
  46. proc `$`*(id: MachineId): string {.borrow.}
  47. proc `$`(id: CommitId): string {.borrow.}
  48. var
  49. thisMachine: MachineId
  50. thisCommit: CommitId
  51. {.experimental.}
  52. proc `()`(cmd: string{lit}): string = cmd.execProcess.string.strip
  53. proc getMachine*(db: DbConn): MachineId =
  54. var name = "hostname"()
  55. if name.len == 0:
  56. name = when defined(posix): getenv"HOSTNAME".string
  57. else: getenv"COMPUTERNAME".string
  58. if name.len == 0:
  59. quit "cannot determine the machine name"
  60. let id = db.getValue(sql"select id from Machine where name = ?", name)
  61. if id.len > 0:
  62. result = id.parseInt.MachineId
  63. else:
  64. result = db.insertId(sql"insert into Machine(name, os, cpu) values (?,?,?)",
  65. name, system.hostOS, system.hostCPU).MachineId
  66. proc getCommit(db: DbConn): CommitId =
  67. const commLen = "commit ".len
  68. let hash = "git log -n 1"()[commLen..commLen+10]
  69. let branch = "git symbolic-ref --short HEAD"()
  70. if hash.len == 0 or branch.len == 0: quit "cannot determine git HEAD"
  71. let id = db.getValue(sql"select id from [Commit] where hash = ? and branch = ?",
  72. hash, branch)
  73. if id.len > 0:
  74. result = id.parseInt.CommitId
  75. else:
  76. result = db.insertId(sql"insert into [Commit](hash, branch) values (?, ?)",
  77. hash, branch).CommitId
  78. proc writeTestResult*(name, category, target,
  79. action, result, expected, given: string) =
  80. let id = db.getValue(sql"""select id from TestResult
  81. where name = ? and category = ? and target = ? and
  82. machine = ? and [commit] = ?""",
  83. name, category, target,
  84. thisMachine, thisCommit)
  85. if id.len > 0:
  86. db.exec(sql"""update TestResult
  87. set action = ?, result = ?, expected = ?, given = ?
  88. where id = ?""", action, result, expected, given, id)
  89. else:
  90. db.exec(sql"""insert into TestResult(name, category, target,
  91. action,
  92. result, expected, given,
  93. [commit], machine)
  94. values (?,?,?,?,?,?,?,?,?) """, name, category, target,
  95. action,
  96. result, expected, given,
  97. thisCommit, thisMachine)
  98. proc open*() =
  99. let dbFile = if existsEnv("TRAVIS") or existsEnv("APPVEYOR"): ":memory:" else: "testament.db"
  100. db = open(connection=dbFile, user="testament", password="",
  101. database="testament")
  102. createDb()
  103. thisMachine = getMachine(db)
  104. thisCommit = getCommit(db)
  105. proc close*() = close(db)