specs.nim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #
  2. #
  3. # Nim Tester
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. import sequtils, parseutils, strutils, os, streams, parsecfg,
  10. tables, hashes, sets
  11. type TestamentData* = ref object
  12. # better to group globals under 1 object; could group the other ones here too
  13. batchArg*: string
  14. testamentNumBatch*: int
  15. testamentBatch*: int
  16. let testamentData0* = TestamentData()
  17. var compilerPrefix* = findExe("nim")
  18. let isTravis* = existsEnv("TRAVIS")
  19. let isAppVeyor* = existsEnv("APPVEYOR")
  20. let isAzure* = existsEnv("TF_BUILD")
  21. var skips*: seq[string]
  22. type
  23. TTestAction* = enum
  24. actionRun = "run"
  25. actionCompile = "compile"
  26. actionReject = "reject"
  27. TOutputCheck* = enum
  28. ocIgnore = "ignore"
  29. ocEqual = "equal"
  30. ocSubstr = "substr"
  31. TResultEnum* = enum
  32. reNimcCrash, # nim compiler seems to have crashed
  33. reMsgsDiffer, # error messages differ
  34. reFilesDiffer, # expected and given filenames differ
  35. reLinesDiffer, # expected and given line numbers differ
  36. reOutputsDiffer,
  37. reExitcodesDiffer, # exit codes of program or of valgrind differ
  38. reTimeout,
  39. reInvalidPeg,
  40. reCodegenFailure,
  41. reCodeNotFound,
  42. reExeNotFound,
  43. reInstallFailed # package installation failed
  44. reBuildFailed # package building failed
  45. reDisabled, # test is disabled
  46. reJoined, # test is disabled because it was joined into the megatest
  47. reSuccess # test was successful
  48. reInvalidSpec # test had problems to parse the spec
  49. TTarget* = enum
  50. targetC = "c"
  51. targetCpp = "cpp"
  52. targetObjC = "objc"
  53. targetJS = "js"
  54. InlineError* = object
  55. kind*: string
  56. msg*: string
  57. line*, col*: int
  58. ValgrindSpec* = enum
  59. disabled, enabled, leaking
  60. TSpec* = object
  61. # xxx make sure `isJoinableSpec` takes into account each field here.
  62. action*: TTestAction
  63. file*, cmd*: string
  64. input*: string
  65. outputCheck*: TOutputCheck
  66. sortoutput*: bool
  67. output*: string
  68. line*, column*: int
  69. exitCode*: int
  70. msg*: string
  71. ccodeCheck*: seq[string]
  72. maxCodeSize*: int
  73. err*: TResultEnum
  74. inCurrentBatch*: bool
  75. targets*: set[TTarget]
  76. matrix*: seq[string]
  77. nimout*: string
  78. parseErrors*: string # when the spec definition is invalid, this is not empty.
  79. unjoinable*: bool
  80. unbatchable*: bool
  81. # whether this test can be batchable via `NIM_TESTAMENT_BATCH`; only very
  82. # few tests are not batchable; the ones that are not could be turned batchable
  83. # by making the dependencies explicit
  84. useValgrind*: ValgrindSpec
  85. timeout*: float # in seconds, fractions possible,
  86. # but don't rely on much precision
  87. inlineErrors*: seq[InlineError] # line information to error message
  88. proc getCmd*(s: TSpec): string =
  89. if s.cmd.len == 0:
  90. result = compilerPrefix & " $target --hints:on -d:testing --clearNimblePath --nimblePath:build/deps/pkgs $options $file"
  91. else:
  92. result = s.cmd
  93. const
  94. targetToExt*: array[TTarget, string] = ["nim.c", "nim.cpp", "nim.m", "js"]
  95. targetToCmd*: array[TTarget, string] = ["c", "cpp", "objc", "js"]
  96. proc defaultOptions*(a: TTarget): string =
  97. case a
  98. of targetJS: "-d:nodejs"
  99. # once we start testing for `nim js -d:nimbrowser` (eg selenium or similar),
  100. # we can adapt this logic; or a given js test can override with `-u:nodejs`.
  101. else: ""
  102. when not declared(parseCfgBool):
  103. # candidate for the stdlib:
  104. proc parseCfgBool(s: string): bool =
  105. case normalize(s)
  106. of "y", "yes", "true", "1", "on": result = true
  107. of "n", "no", "false", "0", "off": result = false
  108. else: raise newException(ValueError, "cannot interpret as a bool: " & s)
  109. const
  110. inlineErrorMarker = "#[tt."
  111. proc extractErrorMsg(s: string; i: int; line: var int; col: var int; spec: var TSpec): int =
  112. result = i + len(inlineErrorMarker)
  113. inc col, len(inlineErrorMarker)
  114. var kind = ""
  115. while result < s.len and s[result] in IdentChars:
  116. kind.add s[result]
  117. inc result
  118. inc col
  119. var caret = (line, -1)
  120. template skipWhitespace =
  121. while result < s.len and s[result] in Whitespace:
  122. if s[result] == '\n':
  123. col = 1
  124. inc line
  125. else:
  126. inc col
  127. inc result
  128. skipWhitespace()
  129. if result < s.len and s[result] == '^':
  130. caret = (line-1, col)
  131. inc result
  132. inc col
  133. skipWhitespace()
  134. var msg = ""
  135. while result < s.len-1:
  136. if s[result] == '\n':
  137. inc result
  138. inc line
  139. col = 1
  140. elif s[result] == ']' and s[result+1] == '#':
  141. while msg.len > 0 and msg[^1] in Whitespace:
  142. setLen msg, msg.len - 1
  143. inc result
  144. inc col, 2
  145. if kind == "Error": spec.action = actionReject
  146. spec.unjoinable = true
  147. spec.inlineErrors.add InlineError(kind: kind, msg: msg, line: caret[0], col: caret[1])
  148. break
  149. else:
  150. msg.add s[result]
  151. inc result
  152. inc col
  153. proc extractSpec(filename: string; spec: var TSpec): string =
  154. const
  155. tripleQuote = "\"\"\""
  156. var s = readFile(filename)
  157. var i = 0
  158. var a = -1
  159. var b = -1
  160. var line = 1
  161. var col = 1
  162. while i < s.len:
  163. if s.continuesWith(tripleQuote, i):
  164. if a < 0: a = i
  165. elif b < 0: b = i
  166. inc i, 2
  167. inc col
  168. elif s[i] == '\n':
  169. inc line
  170. col = 1
  171. elif s.continuesWith(inlineErrorMarker, i):
  172. i = extractErrorMsg(s, i, line, col, spec)
  173. else:
  174. inc col
  175. inc i
  176. # look for """ only in the first section
  177. if a >= 0 and b > a and a < 40:
  178. result = s.substr(a+3, b-1).replace("'''", tripleQuote)
  179. else:
  180. #echo "warning: file does not contain spec: " & filename
  181. result = ""
  182. proc parseTargets*(value: string): set[TTarget] =
  183. for v in value.normalize.splitWhitespace:
  184. case v
  185. of "c": result.incl(targetC)
  186. of "cpp", "c++": result.incl(targetCpp)
  187. of "objc": result.incl(targetObjC)
  188. of "js": result.incl(targetJS)
  189. else: raise newException(ValueError, "invalid target: '$#'" % v)
  190. proc addLine*(self: var string; a: string) =
  191. self.add a
  192. self.add "\n"
  193. proc addLine*(self: var string; a, b: string) =
  194. self.add a
  195. self.add b
  196. self.add "\n"
  197. proc initSpec*(filename: string): TSpec =
  198. result.file = filename
  199. proc isCurrentBatch*(testamentData: TestamentData; filename: string): bool =
  200. if testamentData.testamentNumBatch != 0:
  201. hash(filename) mod testamentData.testamentNumBatch == testamentData.testamentBatch
  202. else:
  203. true
  204. proc parseSpec*(filename: string): TSpec =
  205. result.file = filename
  206. let specStr = extractSpec(filename, result)
  207. var ss = newStringStream(specStr)
  208. var p: CfgParser
  209. open(p, ss, filename, 1)
  210. var flags: HashSet[string]
  211. while true:
  212. var e = next(p)
  213. case e.kind
  214. of cfgKeyValuePair:
  215. let key = e.key.normalize
  216. const whiteListMulti = ["disabled", "ccodecheck"]
  217. ## list of flags that are correctly handled when passed multiple times
  218. ## (instead of being overwritten)
  219. if key notin whiteListMulti:
  220. doAssert key notin flags, $(key, filename)
  221. flags.incl key
  222. case key
  223. of "action":
  224. case e.value.normalize
  225. of "compile":
  226. result.action = actionCompile
  227. of "run":
  228. result.action = actionRun
  229. of "reject":
  230. result.action = actionReject
  231. else:
  232. result.parseErrors.addLine "cannot interpret as action: ", e.value
  233. of "file":
  234. if result.msg.len == 0 and result.nimout.len == 0:
  235. result.parseErrors.addLine "errormsg or msg needs to be specified before file"
  236. result.file = e.value
  237. of "line":
  238. if result.msg.len == 0 and result.nimout.len == 0:
  239. result.parseErrors.addLine "errormsg, msg or nimout needs to be specified before line"
  240. discard parseInt(e.value, result.line)
  241. of "column":
  242. if result.msg.len == 0 and result.nimout.len == 0:
  243. result.parseErrors.addLine "errormsg or msg needs to be specified before column"
  244. discard parseInt(e.value, result.column)
  245. of "output":
  246. if result.outputCheck != ocSubstr:
  247. result.outputCheck = ocEqual
  248. result.output = e.value
  249. of "input":
  250. result.input = e.value
  251. of "outputsub":
  252. result.outputCheck = ocSubstr
  253. result.output = strip(e.value)
  254. of "sortoutput":
  255. try:
  256. result.sortoutput = parseCfgBool(e.value)
  257. except:
  258. result.parseErrors.addLine getCurrentExceptionMsg()
  259. of "exitcode":
  260. discard parseInt(e.value, result.exitCode)
  261. result.action = actionRun
  262. of "errormsg":
  263. result.msg = e.value
  264. result.action = actionReject
  265. of "nimout":
  266. result.nimout = e.value
  267. of "batchable":
  268. result.unbatchable = not parseCfgBool(e.value)
  269. of "joinable":
  270. result.unjoinable = not parseCfgBool(e.value)
  271. of "valgrind":
  272. when defined(linux) and sizeof(int) == 8:
  273. result.useValgrind = if e.value.normalize == "leaks": leaking
  274. else: ValgrindSpec(parseCfgBool(e.value))
  275. result.unjoinable = true
  276. if result.useValgrind != disabled:
  277. result.outputCheck = ocSubstr
  278. else:
  279. # Windows lacks valgrind. Silly OS.
  280. # Valgrind only supports OSX <= 17.x
  281. result.useValgrind = disabled
  282. of "disabled":
  283. case e.value.normalize
  284. of "y", "yes", "true", "1", "on": result.err = reDisabled
  285. of "n", "no", "false", "0", "off": discard
  286. of "win", "windows":
  287. when defined(windows): result.err = reDisabled
  288. of "linux":
  289. when defined(linux): result.err = reDisabled
  290. of "bsd":
  291. when defined(bsd): result.err = reDisabled
  292. of "osx", "macosx": # xxx remove `macosx` alias?
  293. when defined(osx): result.err = reDisabled
  294. of "unix":
  295. when defined(unix): result.err = reDisabled
  296. of "posix":
  297. when defined(posix): result.err = reDisabled
  298. of "travis":
  299. if isTravis: result.err = reDisabled
  300. of "appveyor":
  301. if isAppVeyor: result.err = reDisabled
  302. of "azure":
  303. if isAzure: result.err = reDisabled
  304. of "32bit":
  305. if sizeof(int) == 4:
  306. result.err = reDisabled
  307. of "freebsd":
  308. when defined(freebsd): result.err = reDisabled
  309. of "arm64":
  310. when defined(arm64): result.err = reDisabled
  311. of "i386":
  312. when defined(i386): result.err = reDisabled
  313. of "openbsd":
  314. when defined(openbsd): result.err = reDisabled
  315. of "netbsd":
  316. when defined(netbsd): result.err = reDisabled
  317. else:
  318. result.parseErrors.addLine "cannot interpret as a bool: ", e.value
  319. of "cmd":
  320. if e.value.startsWith("nim "):
  321. result.cmd = compilerPrefix & e.value[3..^1]
  322. else:
  323. result.cmd = e.value
  324. of "ccodecheck":
  325. result.ccodeCheck.add e.value
  326. of "maxcodesize":
  327. discard parseInt(e.value, result.maxCodeSize)
  328. of "timeout":
  329. try:
  330. result.timeout = parseFloat(e.value)
  331. except ValueError:
  332. result.parseErrors.addLine "cannot interpret as a float: ", e.value
  333. of "targets", "target":
  334. try:
  335. result.targets.incl parseTargets(e.value)
  336. except ValueError as e:
  337. result.parseErrors.addLine e.msg
  338. of "matrix":
  339. for v in e.value.split(';'):
  340. result.matrix.add(v.strip)
  341. else:
  342. result.parseErrors.addLine "invalid key for test spec: ", e.key
  343. of cfgSectionStart:
  344. result.parseErrors.addLine "section ignored: ", e.section
  345. of cfgOption:
  346. result.parseErrors.addLine "command ignored: ", e.key & ": " & e.value
  347. of cfgError:
  348. result.parseErrors.addLine e.msg
  349. of cfgEof:
  350. break
  351. close(p)
  352. if skips.anyIt(it in result.file):
  353. result.err = reDisabled
  354. result.inCurrentBatch = isCurrentBatch(testamentData0, filename) or result.unbatchable
  355. if not result.inCurrentBatch:
  356. result.err = reDisabled