categories.nim 26 KB

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