categories.nim 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. ## Include for the tester that contains test suites that test special features
  10. ## of the compiler.
  11. # included from testament.nim
  12. import important_packages
  13. const
  14. specialCategories = [
  15. "assert",
  16. "async",
  17. "debugger",
  18. "dll",
  19. "examples",
  20. "flags",
  21. "gc",
  22. "io",
  23. "js",
  24. "ic",
  25. "lib",
  26. "longgc",
  27. "manyloc",
  28. "nimble-packages-1",
  29. "nimble-packages-2",
  30. "niminaction",
  31. "threads",
  32. "untestable",
  33. "testdata",
  34. "nimcache",
  35. "coroutines",
  36. "osproc",
  37. "shouldfail",
  38. "dir with space",
  39. "destructor"
  40. ]
  41. proc isTestFile*(file: string): bool =
  42. let (_, name, ext) = splitFile(file)
  43. result = ext == ".nim" and name.startsWith("t")
  44. # ---------------- IC tests ---------------------------------------------
  45. proc icTests(r: var TResults; testsDir: string, cat: Category, options: string) =
  46. const
  47. tooltests = ["compiler/nim.nim", "tools/nimgrep.nim"]
  48. writeOnly = " --incremental:writeonly "
  49. readOnly = " --incremental:readonly "
  50. incrementalOn = " --incremental:on "
  51. template test(x: untyped) =
  52. testSpecWithNimcache(r, makeRawTest(file, x & options, cat), nimcache)
  53. template editedTest(x: untyped) =
  54. var test = makeTest(file, x & options, cat)
  55. test.spec.targets = {getTestSpecTarget()}
  56. testSpecWithNimcache(r, test, nimcache)
  57. const tempExt = "_temp.nim"
  58. for it in walkDirRec(testsDir / "ic"):
  59. if isTestFile(it) and not it.endsWith(tempExt):
  60. let nimcache = nimcacheDir(it, options, getTestSpecTarget())
  61. removeDir(nimcache)
  62. let content = readFile(it)
  63. for fragment in content.split("#!EDIT!#"):
  64. let file = it.replace(".nim", tempExt)
  65. writeFile(file, fragment)
  66. let oldPassed = r.passed
  67. editedTest incrementalOn
  68. if r.passed != oldPassed+1: break
  69. for file in tooltests:
  70. let nimcache = nimcacheDir(file, options, getTestSpecTarget())
  71. removeDir(nimcache)
  72. let oldPassed = r.passed
  73. test writeOnly
  74. if r.passed == oldPassed+1:
  75. test readOnly
  76. if r.passed == oldPassed+2:
  77. test readOnly & "-d:nimBackendAssumesChange "
  78. # --------------------- flags tests -------------------------------------------
  79. proc flagTests(r: var TResults, cat: Category, options: string) =
  80. # --genscript
  81. const filename = testsDir/"flags"/"tgenscript"
  82. const genopts = " --genscript"
  83. let nimcache = nimcacheDir(filename, genopts, targetC)
  84. testSpec r, makeTest(filename, genopts, cat)
  85. when defined(windows):
  86. testExec r, makeTest(filename, " cmd /c cd " & nimcache &
  87. " && compile_tgenscript.bat", cat)
  88. elif defined(posix):
  89. testExec r, makeTest(filename, " sh -c \"cd " & nimcache &
  90. " && sh compile_tgenscript.sh\"", cat)
  91. # Run
  92. testExec r, makeTest(filename, " " & nimcache / "tgenscript", cat)
  93. # --------------------- DLL generation tests ----------------------------------
  94. proc runBasicDLLTest(c, r: var TResults, cat: Category, options: string) =
  95. const rpath = when defined(macosx):
  96. " --passL:-rpath --passL:@loader_path"
  97. else:
  98. ""
  99. var test1 = makeTest("lib/nimrtl.nim", options & " --outdir:tests/dll", cat)
  100. test1.spec.action = actionCompile
  101. testSpec c, test1
  102. var test2 = makeTest("tests/dll/server.nim", options & " --threads:on" & rpath, cat)
  103. test2.spec.action = actionCompile
  104. testSpec c, test2
  105. var test3 = makeTest("lib/nimhcr.nim", options & " --outdir:tests/dll" & rpath, cat)
  106. test3.spec.action = actionCompile
  107. testSpec c, test3
  108. var test4 = makeTest("tests/dll/visibility.nim", options & " --app:lib" & rpath, cat)
  109. test4.spec.action = actionCompile
  110. testSpec c, test4
  111. # windows looks in the dir of the exe (yay!):
  112. when not defined(Windows):
  113. # posix relies on crappy LD_LIBRARY_PATH (ugh!):
  114. const libpathenv = when defined(haiku): "LIBRARY_PATH"
  115. else: "LD_LIBRARY_PATH"
  116. var libpath = getEnv(libpathenv).string
  117. # Temporarily add the lib directory to LD_LIBRARY_PATH:
  118. putEnv(libpathenv, "tests/dll" & (if libpath.len > 0: ":" & libpath else: ""))
  119. defer: putEnv(libpathenv, libpath)
  120. testSpec r, makeTest("tests/dll/client.nim", options & " --threads:on" & rpath, cat)
  121. testSpec r, makeTest("tests/dll/nimhcr_unit.nim", options & rpath, cat)
  122. testSpec r, makeTest("tests/dll/visibility.nim", options & rpath, cat)
  123. if "boehm" notin options:
  124. # force build required - see the comments in the .nim file for more details
  125. var hcri = makeTest("tests/dll/nimhcr_integration.nim",
  126. options & " --forceBuild --hotCodeReloading:on" & rpath, cat)
  127. let nimcache = nimcacheDir(hcri.name, hcri.options, getTestSpecTarget())
  128. hcri.args = prepareTestArgs(hcri.spec.getCmd, hcri.name,
  129. hcri.options, nimcache, getTestSpecTarget())
  130. testSpec r, hcri
  131. proc dllTests(r: var TResults, cat: Category, options: string) =
  132. # dummy compile result:
  133. var c = initResults()
  134. runBasicDLLTest c, r, cat, options
  135. runBasicDLLTest c, r, cat, options & " -d:release"
  136. when not defined(windows):
  137. # still cannot find a recent Windows version of boehm.dll:
  138. runBasicDLLTest c, r, cat, options & " --gc:boehm"
  139. runBasicDLLTest c, r, cat, options & " -d:release --gc:boehm"
  140. # ------------------------------ GC tests -------------------------------------
  141. proc gcTests(r: var TResults, cat: Category, options: string) =
  142. template testWithoutMs(filename: untyped) =
  143. testSpec r, makeTest("tests/gc" / filename, options, cat)
  144. testSpec r, makeTest("tests/gc" / filename, options &
  145. " -d:release -d:useRealtimeGC", cat)
  146. when filename != "gctest":
  147. testSpec r, makeTest("tests/gc" / filename, options &
  148. " --gc:orc", cat)
  149. testSpec r, makeTest("tests/gc" / filename, options &
  150. " --gc:orc -d:release", cat)
  151. template testWithoutBoehm(filename: untyped) =
  152. testWithoutMs filename
  153. testSpec r, makeTest("tests/gc" / filename, options &
  154. " --gc:markAndSweep", cat)
  155. testSpec r, makeTest("tests/gc" / filename, options &
  156. " -d:release --gc:markAndSweep", cat)
  157. template test(filename: untyped) =
  158. testWithoutBoehm filename
  159. when not defined(windows) and not defined(android):
  160. # AR: cannot find any boehm.dll on the net, right now, so disabled
  161. # for windows:
  162. testSpec r, makeTest("tests/gc" / filename, options &
  163. " --gc:boehm", cat)
  164. testSpec r, makeTest("tests/gc" / filename, options &
  165. " -d:release --gc:boehm", cat)
  166. testWithoutBoehm "foreign_thr"
  167. test "gcemscripten"
  168. test "growobjcrash"
  169. test "gcbench"
  170. test "gcleak"
  171. test "gcleak2"
  172. testWithoutBoehm "gctest"
  173. test "gcleak3"
  174. test "gcleak4"
  175. # Disabled because it works and takes too long to run:
  176. #test "gcleak5"
  177. testWithoutBoehm "weakrefs"
  178. test "cycleleak"
  179. testWithoutBoehm "closureleak"
  180. testWithoutMs "refarrayleak"
  181. testWithoutBoehm "tlists"
  182. testWithoutBoehm "thavlak"
  183. test "stackrefleak"
  184. test "cyclecollector"
  185. proc longGCTests(r: var TResults, cat: Category, options: string) =
  186. when defined(windows):
  187. let cOptions = "-ldl -DWIN"
  188. else:
  189. let cOptions = "-ldl"
  190. var c = initResults()
  191. # According to ioTests, this should compile the file
  192. testSpec c, makeTest("tests/realtimeGC/shared", options, cat)
  193. # ^- why is this not appended to r? Should this be discarded?
  194. testC r, makeTest("tests/realtimeGC/cmain", cOptions, cat), actionRun
  195. testSpec r, makeTest("tests/realtimeGC/nmain", options & "--threads: on", cat)
  196. # ------------------------- threading tests -----------------------------------
  197. proc threadTests(r: var TResults, cat: Category, options: string) =
  198. template test(filename: untyped) =
  199. testSpec r, makeTest(filename, options, cat)
  200. testSpec r, makeTest(filename, options & " -d:release", cat)
  201. testSpec r, makeTest(filename, options & " --tlsEmulation:on", cat)
  202. for t in os.walkFiles("tests/threads/t*.nim"):
  203. test(t)
  204. # ------------------------- IO tests ------------------------------------------
  205. proc ioTests(r: var TResults, cat: Category, options: string) =
  206. # We need readall_echo to be compiled for this test to run.
  207. # dummy compile result:
  208. var c = initResults()
  209. testSpec c, makeTest("tests/system/helpers/readall_echo", options, cat)
  210. testSpec r, makeTest("tests/system/tio", options, cat)
  211. # ------------------------- async tests ---------------------------------------
  212. proc asyncTests(r: var TResults, cat: Category, options: string) =
  213. template test(filename: untyped) =
  214. testSpec r, makeTest(filename, options, cat)
  215. for t in os.walkFiles("tests/async/t*.nim"):
  216. test(t)
  217. # ------------------------- debugger tests ------------------------------------
  218. proc debuggerTests(r: var TResults, cat: Category, options: string) =
  219. if fileExists("tools/nimgrep.nim"):
  220. var t = makeTest("tools/nimgrep", options & " --debugger:on", cat)
  221. t.spec.action = actionCompile
  222. testSpec r, t
  223. # ------------------------- JS tests ------------------------------------------
  224. proc jsTests(r: var TResults, cat: Category, options: string) =
  225. template test(filename: untyped) =
  226. testSpec r, makeTest(filename, options, cat), {targetJS}
  227. testSpec r, makeTest(filename, options & " -d:release", cat), {targetJS}
  228. for t in os.walkFiles("tests/js/t*.nim"):
  229. test(t)
  230. for testfile in ["exception/texceptions", "exception/texcpt1",
  231. "exception/texcsub", "exception/tfinally",
  232. "exception/tfinally2", "exception/tfinally3",
  233. "actiontable/tactiontable", "method/tmultimjs",
  234. "varres/tvarres0", "varres/tvarres3", "varres/tvarres4",
  235. "varres/tvartup", "misc/tints", "misc/tunsignedinc",
  236. "async/tjsandnativeasync"]:
  237. test "tests/" & testfile & ".nim"
  238. for testfile in ["strutils", "json", "random", "times", "logging"]:
  239. test "lib/pure/" & testfile & ".nim"
  240. # ------------------------- nim in action -----------
  241. proc testNimInAction(r: var TResults, cat: Category, options: string) =
  242. let options = options & " --nilseqs:on"
  243. template test(filename: untyped) =
  244. testSpec r, makeTest(filename, options, cat)
  245. template testJS(filename: untyped) =
  246. testSpec r, makeTest(filename, options, cat), {targetJS}
  247. template testCPP(filename: untyped) =
  248. testSpec r, makeTest(filename, options, cat), {targetCpp}
  249. let tests = [
  250. "niminaction/Chapter1/various1",
  251. "niminaction/Chapter2/various2",
  252. "niminaction/Chapter2/resultaccept",
  253. "niminaction/Chapter2/resultreject",
  254. "niminaction/Chapter2/explicit_discard",
  255. "niminaction/Chapter2/no_def_eq",
  256. "niminaction/Chapter2/no_iterator",
  257. "niminaction/Chapter2/no_seq_type",
  258. "niminaction/Chapter3/ChatApp/src/server",
  259. "niminaction/Chapter3/ChatApp/src/client",
  260. "niminaction/Chapter3/various3",
  261. "niminaction/Chapter6/WikipediaStats/concurrency_regex",
  262. "niminaction/Chapter6/WikipediaStats/concurrency",
  263. "niminaction/Chapter6/WikipediaStats/naive",
  264. "niminaction/Chapter6/WikipediaStats/parallel_counts",
  265. "niminaction/Chapter6/WikipediaStats/race_condition",
  266. "niminaction/Chapter6/WikipediaStats/sequential_counts",
  267. "niminaction/Chapter6/WikipediaStats/unguarded_access",
  268. "niminaction/Chapter7/Tweeter/src/tweeter",
  269. "niminaction/Chapter7/Tweeter/src/createDatabase",
  270. "niminaction/Chapter7/Tweeter/tests/database_test",
  271. "niminaction/Chapter8/sdl/sdl_test"
  272. ]
  273. when false:
  274. # Verify that the files have not been modified. Death shall fall upon
  275. # whoever edits these hashes without dom96's permission, j/k. But please only
  276. # edit when making a conscious breaking change, also please try to make your
  277. # commit message clear and notify me so I can easily compile an errata later.
  278. # ---------------------------------------------------------
  279. # Hash-checks are disabled for Nim 1.1 and beyond
  280. # since we needed to fix the deprecated unary '<' operator.
  281. const refHashes = @[
  282. "51afdfa84b3ca3d810809d6c4e5037ba",
  283. "30f07e4cd5eaec981f67868d4e91cfcf",
  284. "d14e7c032de36d219c9548066a97e846",
  285. "b335635562ff26ec0301bdd86356ac0c",
  286. "6c4add749fbf50860e2f523f548e6b0e",
  287. "76de5833a7cc46f96b006ce51179aeb1",
  288. "705eff79844e219b47366bd431658961",
  289. "a1e87b881c5eb161553d119be8b52f64",
  290. "2d706a6ec68d2973ec7e733e6d5dce50",
  291. "c11a013db35e798f44077bc0763cc86d",
  292. "3e32e2c5e9a24bd13375e1cd0467079c",
  293. "a5452722b2841f0c1db030cf17708955",
  294. "dc6c45eb59f8814aaaf7aabdb8962294",
  295. "69d208d281a2e7bffd3eaf4bab2309b1",
  296. "ec05666cfb60211bedc5e81d4c1caf3d",
  297. "da520038c153f4054cb8cc5faa617714",
  298. "59906c8cd819cae67476baa90a36b8c1",
  299. "9a8fe78c588d08018843b64b57409a02",
  300. "8b5d28e985c0542163927d253a3e4fc9",
  301. "783299b98179cc725f9c46b5e3b5381f",
  302. "1a2b3fba1187c68d6a9bfa66854f3318",
  303. "391ff57b38d9ea6f3eeb3fe69ab539d3"
  304. ]
  305. for i, test in tests:
  306. let filename = testsDir / test.addFileExt("nim")
  307. let testHash = getMD5(readFile(filename).string)
  308. doAssert testHash == refHashes[i], "Nim in Action test " & filename &
  309. " was changed: " & $(i: i, testHash: testHash, refHash: refHashes[i])
  310. # Run the tests.
  311. for testfile in tests:
  312. test "tests/" & testfile & ".nim"
  313. let jsFile = "tests/niminaction/Chapter8/canvas/canvas_test.nim"
  314. testJS jsFile
  315. let cppFile = "tests/niminaction/Chapter8/sfml/sfml_test.nim"
  316. testCPP cppFile
  317. # ------------------------- manyloc -------------------------------------------
  318. proc findMainFile(dir: string): string =
  319. # finds the file belonging to ".nim.cfg"; if there is no such file
  320. # it returns the some ".nim" file if there is only one:
  321. const cfgExt = ".nim.cfg"
  322. result = ""
  323. var nimFiles = 0
  324. for kind, file in os.walkDir(dir):
  325. if kind == pcFile:
  326. if file.endsWith(cfgExt): return file[.. ^(cfgExt.len+1)] & ".nim"
  327. elif file.endsWith(".nim"):
  328. if result.len == 0: result = file
  329. inc nimFiles
  330. if nimFiles != 1: result.setLen(0)
  331. proc manyLoc(r: var TResults, cat: Category, options: string) =
  332. for kind, dir in os.walkDir("tests/manyloc"):
  333. if kind == pcDir:
  334. when defined(windows):
  335. if dir.endsWith"nake": continue
  336. if dir.endsWith"named_argument_bug": continue
  337. let mainfile = findMainFile(dir)
  338. if mainfile != "":
  339. var test = makeTest(mainfile, options, cat)
  340. test.spec.action = actionCompile
  341. testSpec r, test
  342. proc compileExample(r: var TResults, pattern, options: string, cat: Category) =
  343. for test in os.walkFiles(pattern):
  344. var test = makeTest(test, options, cat)
  345. test.spec.action = actionCompile
  346. testSpec r, test
  347. proc testStdlib(r: var TResults, pattern, options: string, cat: Category) =
  348. var files: seq[string]
  349. proc isValid(file: string): bool =
  350. for dir in parentDirs(file, inclusive = false):
  351. if dir.lastPathPart in ["includes", "nimcache"]:
  352. # eg: lib/pure/includes/osenv.nim gives: Error: This is an include file for os.nim!
  353. return false
  354. let name = extractFilename(file)
  355. if name.splitFile.ext != ".nim": return false
  356. for namei in disabledFiles:
  357. # because of `LockFreeHash.nim` which has case
  358. if namei.cmpPaths(name) == 0: return false
  359. return true
  360. for testFile in os.walkDirRec(pattern):
  361. if isValid(testFile):
  362. files.add testFile
  363. files.sort # reproducible order
  364. for testFile in files:
  365. let contents = readFile(testFile).string
  366. var testObj = makeTest(testFile, options, cat)
  367. #[
  368. todo:
  369. this logic is fragile:
  370. false positives (if appears in a comment), or false negatives, eg
  371. `when defined(osx) and isMainModule`.
  372. Instead of fixing this, see https://github.com/nim-lang/Nim/issues/10045
  373. for a much better way.
  374. ]#
  375. if "when isMainModule" notin contents:
  376. testObj.spec.action = actionCompile
  377. testSpec r, testObj
  378. # ----------------------------- nimble ----------------------------------------
  379. var nimbleDir = getEnv("NIMBLE_DIR").string
  380. if nimbleDir.len == 0: nimbleDir = getHomeDir() / ".nimble"
  381. let
  382. nimbleExe = findExe("nimble")
  383. packageIndex = nimbleDir / "packages_official.json"
  384. type
  385. PkgPart = enum
  386. ppOne
  387. ppTwo
  388. iterator listPackages(part: PkgPart): tuple[name, url, cmd: string, hasDeps: bool, useHead: bool] =
  389. let packageList = parseFile(packageIndex)
  390. let importantList =
  391. case part
  392. of ppOne: important_packages.packages1
  393. of ppTwo: important_packages.packages2
  394. for n, cmd, hasDeps, url, useHead in importantList.items:
  395. if url.len != 0:
  396. yield (n, url, cmd, hasDeps, useHead)
  397. else:
  398. var found = false
  399. for package in packageList.items:
  400. let name = package["name"].str
  401. if name == n:
  402. found = true
  403. let pUrl = package["url"].str
  404. yield (name, pUrl, cmd, hasDeps, useHead)
  405. break
  406. if not found:
  407. raise newException(ValueError, "Cannot find package '$#'." % n)
  408. proc makeSupTest(test, options: string, cat: Category): TTest =
  409. result.cat = cat
  410. result.name = test
  411. result.options = options
  412. result.startTime = epochTime()
  413. proc actionRetry(maxRetry: int, backoffDuration: float, action: proc: bool): bool =
  414. ## retry `action` up to `maxRetry` times with exponential backoff and initial
  415. ## duraton of `backoffDuration` seconds
  416. var t = backoffDuration
  417. for i in 0..<maxRetry:
  418. if action(): return true
  419. if i == maxRetry - 1: break
  420. sleep(int(t * 1000))
  421. t *= 2 # exponential backoff
  422. proc testNimblePackages(r: var TResults; cat: Category; packageFilter: string, part: PkgPart) =
  423. if nimbleExe == "":
  424. echo "[Warning] - Cannot run nimble tests: Nimble binary not found."
  425. return
  426. if execCmd("$# update" % nimbleExe) == QuitFailure:
  427. echo "[Warning] - Cannot run nimble tests: Nimble update failed."
  428. return
  429. let packageFileTest = makeSupTest("PackageFileParsed", "", cat)
  430. let packagesDir = "pkgstemp"
  431. var errors = 0
  432. try:
  433. for name, url, cmd, hasDep, useHead in listPackages(part):
  434. if packageFilter notin name:
  435. continue
  436. inc r.total
  437. var test = makeSupTest(url, "", cat)
  438. let buildPath = packagesDir / name
  439. if not dirExists(buildPath):
  440. if useHead:
  441. let (installCmdLine, installOutput, installStatus) = execCmdEx2("git", ["clone", url, buildPath])
  442. if installStatus != QuitSuccess:
  443. let message = "git clone failed:\n$ " & installCmdLine & "\n" & installOutput
  444. r.addResult(test, targetC, "", message, reInstallFailed)
  445. continue
  446. if hasDep:
  447. var message: string
  448. if not actionRetry(maxRetry = 3, backoffDuration = 1.0,
  449. proc: bool =
  450. let (outp, status) = execCmdEx("nimble install -y", workingDir = buildPath)
  451. if status != 0:
  452. message = "'$1' failed:\n$2" % [cmd, outp]
  453. false
  454. else: true
  455. ):
  456. r.addResult(test, targetC, "", message, reInstallFailed)
  457. continue
  458. else:
  459. let (installCmdLine, installOutput, installStatus) = execCmdEx2("nimble", ["develop", name, "-y"])
  460. if installStatus != QuitSuccess:
  461. let message = "nimble develop failed:\n$ " & installCmdLine & "\n" & installOutput
  462. r.addResult(test, targetC, "", message, reInstallFailed)
  463. continue
  464. let cmdArgs = parseCmdLine(cmd)
  465. let (buildCmdLine, buildOutput, buildStatus) = execCmdEx2(cmdArgs[0], cmdArgs[1..^1], workingDir=buildPath)
  466. if buildStatus != QuitSuccess:
  467. let message = "package test failed\n$ " & buildCmdLine & "\n" & buildOutput
  468. r.addResult(test, targetC, "", message, reBuildFailed)
  469. else:
  470. inc r.passed
  471. r.addResult(test, targetC, "", "", reSuccess)
  472. errors = r.total - r.passed
  473. if errors == 0:
  474. r.addResult(packageFileTest, targetC, "", "", reSuccess)
  475. else:
  476. r.addResult(packageFileTest, targetC, "", "", reBuildFailed)
  477. except JsonParsingError:
  478. echo "[Warning] - Cannot run nimble tests: Invalid package file."
  479. r.addResult(packageFileTest, targetC, "", "Invalid package file", reBuildFailed)
  480. except ValueError:
  481. echo "[Warning] - $#" % getCurrentExceptionMsg()
  482. r.addResult(packageFileTest, targetC, "", "Unknown package", reBuildFailed)
  483. finally:
  484. if errors == 0: removeDir(packagesDir)
  485. # ----------------------------------------------------------------------------
  486. const AdditionalCategories = ["debugger", "examples", "lib", "ic"]
  487. const MegaTestCat = "megatest"
  488. proc `&.?`(a, b: string): string =
  489. # candidate for the stdlib?
  490. result = if b.startsWith(a): b else: a & b
  491. proc processSingleTest(r: var TResults, cat: Category, options, test: string) =
  492. let test = testsDir &.? cat.string / test
  493. let target = if cat.string.normalize == "js": targetJS else: targetC
  494. if fileExists(test):
  495. testSpec r, makeTest(test, options, cat), {target}
  496. else:
  497. doAssert false, test & " test does not exist"
  498. proc isJoinableSpec(spec: TSpec): bool =
  499. result = not spec.sortoutput and
  500. spec.action == actionRun and
  501. not fileExists(spec.file.changeFileExt("cfg")) and
  502. not fileExists(spec.file.changeFileExt("nims")) and
  503. not fileExists(parentDir(spec.file) / "nim.cfg") and
  504. not fileExists(parentDir(spec.file) / "config.nims") and
  505. spec.cmd.len == 0 and
  506. spec.err != reDisabled and
  507. not spec.unjoinable and
  508. spec.exitCode == 0 and
  509. spec.input.len == 0 and
  510. spec.nimout.len == 0 and
  511. spec.outputCheck != ocSubstr and
  512. spec.ccodeCheck.len == 0 and
  513. (spec.targets == {} or spec.targets == {targetC})
  514. if result:
  515. if spec.file.readFile.contains "when isMainModule":
  516. result = false
  517. proc norm(s: var string) =
  518. while true:
  519. let tmp = s.replace("\n\n", "\n")
  520. if tmp == s: break
  521. s = tmp
  522. s = s.strip
  523. proc quoted(a: string): string =
  524. # todo: consider moving to system.nim
  525. result.addQuoted(a)
  526. proc runJoinedTest(r: var TResults, cat: Category, testsDir: string) =
  527. ## returns a list of tests that have problems
  528. var specs: seq[TSpec] = @[]
  529. for kind, dir in walkDir(testsDir):
  530. assert testsDir.startsWith(testsDir)
  531. let cat = dir[testsDir.len .. ^1]
  532. if kind == pcDir and cat notin specialCategories:
  533. for file in walkDirRec(testsDir / cat):
  534. if isTestFile(file):
  535. let spec = parseSpec(file)
  536. if isJoinableSpec(spec):
  537. specs.add spec
  538. proc cmp(a: TSpec, b:TSpec): auto = cmp(a.file, b.file)
  539. sort(specs, cmp=cmp) # reproducible order
  540. echo "joinable specs: ", specs.len
  541. if simulate:
  542. var s = "runJoinedTest: "
  543. for a in specs: s.add a.file & " "
  544. echo s
  545. return
  546. var megatest: string
  547. # xxx (minor) put outputExceptedFile, outputGottenFile, megatestFile under here or `buildDir`
  548. var outDir = nimcacheDir(testsDir / "megatest", "", targetC)
  549. const marker = "megatest:processing: "
  550. for i, runSpec in specs:
  551. let file = runSpec.file
  552. let file2 = outDir / ("megatest_$1.nim" % $i)
  553. # `include` didn't work with `trecmod2.nim`, so using `import`
  554. let code = "echo \"$1\", $2\n" % [marker, quoted(file)]
  555. createDir(file2.parentDir)
  556. writeFile(file2, code)
  557. megatest.add "import $1\nimport $2\n" % [quoted(file2), quoted(file)]
  558. let megatestFile = testsDir / "megatest.nim" # so it uses testsDir / "config.nims"
  559. writeFile(megatestFile, megatest)
  560. let root = getCurrentDir()
  561. let args = ["c", "--nimCache:" & outDir, "-d:testing", "--listCmd", "--path:" & root, megatestFile]
  562. var (cmdLine, buf, exitCode) = execCmdEx2(command = compilerPrefix, args = args, input = "")
  563. if exitCode != 0:
  564. echo "$ " & cmdLine & "\n" & buf.string
  565. quit(failString & "megatest compilation failed")
  566. (buf, exitCode) = execCmdEx(megatestFile.changeFileExt(ExeExt).dup normalizeExe)
  567. if exitCode != 0:
  568. echo buf.string
  569. quit(failString & "megatest execution failed")
  570. norm buf.string
  571. const outputExceptedFile = "outputExpected.txt"
  572. const outputGottenFile = "outputGotten.txt"
  573. writeFile(outputGottenFile, buf.string)
  574. var outputExpected = ""
  575. for i, runSpec in specs:
  576. outputExpected.add marker & runSpec.file & "\n"
  577. outputExpected.add runSpec.output.strip
  578. outputExpected.add '\n'
  579. norm outputExpected
  580. if buf.string != outputExpected:
  581. writeFile(outputExceptedFile, outputExpected)
  582. discard execShellCmd("diff -uNdr $1 $2" % [outputExceptedFile, outputGottenFile])
  583. echo failString & "megatest output different!"
  584. # outputGottenFile, outputExceptedFile not removed on purpose for debugging.
  585. quit 1
  586. else:
  587. echo "megatest output OK"
  588. when false: # no point removing those, always good for debugging
  589. removeFile(outputGottenFile)
  590. removeFile(megatestFile) # keep it around
  591. #testSpec r, makeTest("megatest", options, cat)
  592. # ---------------------------------------------------------------------------
  593. proc processCategory(r: var TResults, cat: Category,
  594. options, testsDir: string,
  595. runJoinableTests: bool) =
  596. case cat.string.normalize
  597. of "ic":
  598. when false:
  599. icTests(r, testsDir, cat, options)
  600. of "js":
  601. # only run the JS tests on Windows or Linux because Travis is bad
  602. # and other OSes like Haiku might lack nodejs:
  603. if not defined(linux) and isTravis:
  604. discard
  605. else:
  606. jsTests(r, cat, options)
  607. of "dll":
  608. dllTests(r, cat, options)
  609. of "flags":
  610. flagTests(r, cat, options)
  611. of "gc":
  612. gcTests(r, cat, options)
  613. of "longgc":
  614. longGCTests(r, cat, options)
  615. of "debugger":
  616. debuggerTests(r, cat, options)
  617. of "manyloc":
  618. manyLoc r, cat, options
  619. of "threads":
  620. threadTests r, cat, options & " --threads:on"
  621. of "io":
  622. ioTests r, cat, options
  623. of "async":
  624. asyncTests r, cat, options
  625. of "lib":
  626. testStdlib(r, "lib/pure/", options, cat)
  627. testStdlib(r, "lib/packages/docutils/", options, cat)
  628. of "examples":
  629. compileExample(r, "examples/*.nim", options, cat)
  630. compileExample(r, "examples/gtk/*.nim", options, cat)
  631. compileExample(r, "examples/talk/*.nim", options, cat)
  632. of "nimble-packages-1":
  633. testNimblePackages(r, cat, options, ppOne)
  634. of "nimble-packages-2":
  635. testNimblePackages(r, cat, options, ppTwo)
  636. of "niminaction":
  637. testNimInAction(r, cat, options)
  638. of "untestable":
  639. # We can't test it because it depends on a third party.
  640. discard # TODO: Move untestable tests to someplace else, i.e. nimble repo.
  641. of "megatest":
  642. runJoinedTest(r, cat, testsDir)
  643. else:
  644. var testsRun = 0
  645. var files: seq[string]
  646. for file in walkDirRec(testsDir &.? cat.string):
  647. if isTestFile(file): files.add file
  648. files.sort # give reproducible order
  649. for i, name in files:
  650. var test = makeTest(name, options, cat)
  651. if runJoinableTests or not isJoinableSpec(test.spec) or cat.string in specialCategories:
  652. discard "run the test"
  653. else:
  654. test.spec.err = reJoined
  655. testSpec r, test
  656. inc testsRun
  657. if testsRun == 0:
  658. echo "[Warning] - Invalid category specified \"", cat.string, "\", no tests were run"
  659. proc processPattern(r: var TResults, pattern, options: string; simulate: bool) =
  660. var testsRun = 0
  661. if dirExists(pattern):
  662. for k, name in walkDir(pattern):
  663. if k in {pcFile, pcLinkToFile} and name.endsWith(".nim"):
  664. if simulate:
  665. echo "Detected test: ", name
  666. else:
  667. var test = makeTest(name, options, Category"pattern")
  668. testSpec r, test
  669. inc testsRun
  670. else:
  671. for name in walkPattern(pattern):
  672. if simulate:
  673. echo "Detected test: ", name
  674. else:
  675. var test = makeTest(name, options, Category"pattern")
  676. testSpec r, test
  677. inc testsRun
  678. if testsRun == 0:
  679. echo "no tests were found for pattern: ", pattern