categories.nim 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. import sequtils
  13. const
  14. specialCategories = [
  15. "assert",
  16. "async",
  17. "debugger",
  18. "dll",
  19. "examples",
  20. "flags",
  21. "gc",
  22. "io",
  23. "js",
  24. "lib",
  25. "longgc",
  26. "manyloc",
  27. "nimble-packages",
  28. "niminaction",
  29. "rodfiles",
  30. "threads",
  31. "untestable",
  32. "stdlib",
  33. "testdata",
  34. "nimcache",
  35. "coroutines",
  36. "osproc",
  37. "shouldfail",
  38. "dir with space"
  39. ]
  40. # included from tester.nim
  41. # ---------------- ROD file tests ---------------------------------------------
  42. const
  43. rodfilesDir = "tests/rodfiles"
  44. proc delNimCache(filename, options: string) =
  45. for target in low(TTarget)..high(TTarget):
  46. let dir = nimcacheDir(filename, options, target)
  47. try:
  48. removeDir(dir)
  49. except OSError:
  50. echo "[Warning] could not delete: ", dir
  51. proc runRodFiles(r: var TResults, cat: Category, options: string) =
  52. template test(filename: string, clearCacheFirst=false) =
  53. if clearCacheFirst: delNimCache(filename, options)
  54. testSpec r, makeTest(rodfilesDir / filename, options, cat)
  55. # test basic recompilation scheme:
  56. test "hallo", true
  57. test "hallo"
  58. when false:
  59. # test incremental type information:
  60. test "hallo2"
  61. # test type converters:
  62. test "aconv", true
  63. test "bconv"
  64. # test G, A, B example from the documentation; test init sections:
  65. test "deada", true
  66. test "deada2"
  67. when false:
  68. # test method generation:
  69. test "bmethods", true
  70. test "bmethods2"
  71. # test generics:
  72. test "tgeneric1", true
  73. test "tgeneric2"
  74. proc compileRodFiles(r: var TResults, cat: Category, options: string) =
  75. template test(filename: untyped, clearCacheFirst=true) =
  76. if clearCacheFirst: delNimCache(filename, options)
  77. testSpec r, makeTest(rodfilesDir / filename, options, cat)
  78. # test DLL interfacing:
  79. test "gtkex1", true
  80. test "gtkex2"
  81. # --------------------- flags tests -------------------------------------------
  82. proc flagTests(r: var TResults, cat: Category, options: string) =
  83. # --genscript
  84. const filename = testsDir/"flags"/"tgenscript"
  85. const genopts = " --genscript"
  86. let nimcache = nimcacheDir(filename, genopts, targetC)
  87. testSpec r, makeTest(filename, genopts, cat)
  88. when defined(windows):
  89. testExec r, makeTest(filename, " cmd /c cd " & nimcache &
  90. " && compile_tgenscript.bat", cat)
  91. elif defined(posix):
  92. testExec r, makeTest(filename, " sh -c \"cd " & nimcache &
  93. " && sh compile_tgenscript.sh\"", cat)
  94. # Run
  95. testExec r, makeTest(filename, " " & nimcache / "tgenscript", cat)
  96. # --------------------- DLL generation tests ----------------------------------
  97. proc safeCopyFile(src, dest: string) =
  98. try:
  99. copyFile(src, dest)
  100. except OSError:
  101. echo "[Warning] could not copy: ", src, " to ", dest
  102. proc runBasicDLLTest(c, r: var TResults, cat: Category, options: string) =
  103. const rpath = when defined(macosx):
  104. " --passL:-rpath --passL:@loader_path"
  105. else:
  106. ""
  107. var test1 = makeTest("lib/nimrtl.nim", options & " --outdir:tests/dll", cat)
  108. test1.spec.action = actionCompile
  109. testSpec c, test1
  110. var test2 = makeTest("tests/dll/server.nim", options & " --threads:on" & rpath, cat)
  111. test2.spec.action = actionCompile
  112. testSpec c, test2
  113. var test3 = makeTest("lib/nimhcr.nim", options & " --outdir:tests/dll" & rpath, cat)
  114. test3.spec.action = actionCompile
  115. testSpec c, test3
  116. # windows looks in the dir of the exe (yay!):
  117. when not defined(Windows):
  118. # posix relies on crappy LD_LIBRARY_PATH (ugh!):
  119. const libpathenv = when defined(haiku): "LIBRARY_PATH"
  120. else: "LD_LIBRARY_PATH"
  121. var libpath = getEnv(libpathenv).string
  122. # Temporarily add the lib directory to LD_LIBRARY_PATH:
  123. putEnv(libpathenv, "tests/dll" & (if libpath.len > 0: ":" & libpath else: ""))
  124. defer: putEnv(libpathenv, libpath)
  125. testSpec r, makeTest("tests/dll/client.nim", options & " --threads:on" & rpath, cat)
  126. testSpec r, makeTest("tests/dll/nimhcr_unit.nim", options & rpath, cat)
  127. if "boehm" notin options:
  128. # force build required - see the comments in the .nim file for more details
  129. var hcr_integration = makeTest("tests/dll/nimhcr_integration.nim",
  130. options & " --forceBuild --hotCodeReloading:on" & rpath, cat)
  131. hcr_integration.args = prepareTestArgs(hcr_integration.spec.getCmd, hcr_integration.name,
  132. hcr_integration.options, getTestSpecTarget())
  133. testSpec r, hcr_integration
  134. proc dllTests(r: var TResults, cat: Category, options: string) =
  135. # dummy compile result:
  136. var c = initResults()
  137. runBasicDLLTest c, r, cat, options
  138. runBasicDLLTest c, r, cat, options & " -d:release"
  139. when not defined(windows):
  140. # still cannot find a recent Windows version of boehm.dll:
  141. runBasicDLLTest c, r, cat, options & " --gc:boehm"
  142. runBasicDLLTest c, r, cat, options & " -d:release --gc:boehm"
  143. # ------------------------------ GC tests -------------------------------------
  144. proc gcTests(r: var TResults, cat: Category, options: string) =
  145. template testWithNone(filename: untyped) =
  146. testSpec r, makeTest("tests/gc" / filename, options &
  147. " --gc:none", cat)
  148. testSpec r, makeTest("tests/gc" / filename, options &
  149. " -d:release --gc:none", cat)
  150. template testWithoutMs(filename: untyped) =
  151. testSpec r, makeTest("tests/gc" / filename, options, cat)
  152. testSpec r, makeTest("tests/gc" / filename, options &
  153. " -d:release", cat)
  154. testSpec r, makeTest("tests/gc" / filename, options &
  155. " -d:release -d:useRealtimeGC", cat)
  156. template testWithoutBoehm(filename: untyped) =
  157. testWithoutMs filename
  158. testSpec r, makeTest("tests/gc" / filename, options &
  159. " --gc:markAndSweep", cat)
  160. testSpec r, makeTest("tests/gc" / filename, options &
  161. " -d:release --gc:markAndSweep", cat)
  162. template test(filename: untyped) =
  163. testWithoutBoehm filename
  164. when not defined(windows) and not defined(android):
  165. # AR: cannot find any boehm.dll on the net, right now, so disabled
  166. # for windows:
  167. testSpec r, makeTest("tests/gc" / filename, options &
  168. " --gc:boehm", cat)
  169. testSpec r, makeTest("tests/gc" / filename, options &
  170. " -d:release --gc:boehm", cat)
  171. testWithoutBoehm "foreign_thr"
  172. test "gcemscripten"
  173. test "growobjcrash"
  174. test "gcbench"
  175. test "gcleak"
  176. test "gcleak2"
  177. testWithoutBoehm "gctest"
  178. testWithNone "gctest"
  179. test "gcleak3"
  180. test "gcleak4"
  181. # Disabled because it works and takes too long to run:
  182. #test "gcleak5"
  183. testWithoutBoehm "weakrefs"
  184. test "cycleleak"
  185. testWithoutBoehm "closureleak"
  186. testWithoutMs "refarrayleak"
  187. testWithoutBoehm "tlists"
  188. testWithoutBoehm "thavlak"
  189. test "stackrefleak"
  190. test "cyclecollector"
  191. proc longGCTests(r: var TResults, cat: Category, options: string) =
  192. when defined(windows):
  193. let cOptions = "-ldl -DWIN"
  194. else:
  195. let cOptions = "-ldl"
  196. var c = initResults()
  197. # According to ioTests, this should compile the file
  198. testSpec c, makeTest("tests/realtimeGC/shared", options, cat)
  199. # ^- why is this not appended to r? Should this be discarded?
  200. testC r, makeTest("tests/realtimeGC/cmain", cOptions, cat), actionRun
  201. testSpec r, makeTest("tests/realtimeGC/nmain", options & "--threads: on", cat)
  202. # ------------------------- threading tests -----------------------------------
  203. proc threadTests(r: var TResults, cat: Category, options: string) =
  204. template test(filename: untyped) =
  205. testSpec r, makeTest(filename, options, cat)
  206. testSpec r, makeTest(filename, options & " -d:release", cat)
  207. testSpec r, makeTest(filename, options & " --tlsEmulation:on", cat)
  208. for t in os.walkFiles("tests/threads/t*.nim"):
  209. test(t)
  210. # ------------------------- IO tests ------------------------------------------
  211. proc ioTests(r: var TResults, cat: Category, options: string) =
  212. # We need readall_echo to be compiled for this test to run.
  213. # dummy compile result:
  214. var c = initResults()
  215. testSpec c, makeTest("tests/system/helpers/readall_echo", options, cat)
  216. testSpec r, makeTest("tests/system/tio", options, cat)
  217. # ------------------------- async tests ---------------------------------------
  218. proc asyncTests(r: var TResults, cat: Category, options: string) =
  219. template test(filename: untyped) =
  220. testSpec r, makeTest(filename, options, cat)
  221. for t in os.walkFiles("tests/async/t*.nim"):
  222. test(t)
  223. # ------------------------- debugger tests ------------------------------------
  224. proc debuggerTests(r: var TResults, cat: Category, options: string) =
  225. var t = makeTest("tools/nimgrep", options & " --debugger:on", cat)
  226. t.spec.action = actionCompile
  227. testSpec r, t
  228. # ------------------------- JS tests ------------------------------------------
  229. proc jsTests(r: var TResults, cat: Category, options: string) =
  230. template test(filename: untyped) =
  231. testSpec r, makeTest(filename, options & " -d:nodejs", cat), {targetJS}
  232. testSpec r, makeTest(filename, options & " -d:nodejs -d:release", cat), {targetJS}
  233. for t in os.walkFiles("tests/js/t*.nim"):
  234. test(t)
  235. for testfile in ["exception/texceptions", "exception/texcpt1",
  236. "exception/texcsub", "exception/tfinally",
  237. "exception/tfinally2", "exception/tfinally3",
  238. "actiontable/tactiontable", "method/tmultimjs",
  239. "varres/tvarres0", "varres/tvarres3", "varres/tvarres4",
  240. "varres/tvartup", "misc/tints", "misc/tunsignedinc",
  241. "async/tjsandnativeasync"]:
  242. test "tests/" & testfile & ".nim"
  243. for testfile in ["strutils", "json", "random", "times", "logging"]:
  244. test "lib/pure/" & testfile & ".nim"
  245. # ------------------------- nim in action -----------
  246. proc testNimInAction(r: var TResults, cat: Category, options: string) =
  247. let options = options & " --nilseqs:on"
  248. template test(filename: untyped) =
  249. testSpec r, makeTest(filename, options, cat)
  250. template testJS(filename: untyped) =
  251. testSpec r, makeTest(filename, options, cat), {targetJS}
  252. template testCPP(filename: untyped) =
  253. testSpec r, makeTest(filename, options, cat), {targetCPP}
  254. let tests = [
  255. "niminaction/Chapter1/various1",
  256. "niminaction/Chapter2/various2",
  257. "niminaction/Chapter2/resultaccept",
  258. "niminaction/Chapter2/resultreject",
  259. "niminaction/Chapter2/explicit_discard",
  260. "niminaction/Chapter2/no_def_eq",
  261. "niminaction/Chapter2/no_iterator",
  262. "niminaction/Chapter2/no_seq_type",
  263. "niminaction/Chapter3/ChatApp/src/server",
  264. "niminaction/Chapter3/ChatApp/src/client",
  265. "niminaction/Chapter3/various3",
  266. "niminaction/Chapter6/WikipediaStats/concurrency_regex",
  267. "niminaction/Chapter6/WikipediaStats/concurrency",
  268. "niminaction/Chapter6/WikipediaStats/naive",
  269. "niminaction/Chapter6/WikipediaStats/parallel_counts",
  270. "niminaction/Chapter6/WikipediaStats/race_condition",
  271. "niminaction/Chapter6/WikipediaStats/sequential_counts",
  272. "niminaction/Chapter6/WikipediaStats/unguarded_access",
  273. "niminaction/Chapter7/Tweeter/src/tweeter",
  274. "niminaction/Chapter7/Tweeter/src/createDatabase",
  275. "niminaction/Chapter7/Tweeter/tests/database_test",
  276. "niminaction/Chapter8/sdl/sdl_test"
  277. ]
  278. # Verify that the files have not been modified. Death shall fall upon
  279. # whoever edits these hashes without dom96's permission, j/k. But please only
  280. # edit when making a conscious breaking change, also please try to make your
  281. # commit message clear and notify me so I can easily compile an errata later.
  282. const refHashes = @[
  283. "51afdfa84b3ca3d810809d6c4e5037ba",
  284. "30f07e4cd5eaec981f67868d4e91cfcf",
  285. "d14e7c032de36d219c9548066a97e846",
  286. "b335635562ff26ec0301bdd86356ac0c",
  287. "6c4add749fbf50860e2f523f548e6b0e",
  288. "76de5833a7cc46f96b006ce51179aeb1",
  289. "705eff79844e219b47366bd431658961",
  290. "a1e87b881c5eb161553d119be8b52f64",
  291. "2d706a6ec68d2973ec7e733e6d5dce50",
  292. "c11a013db35e798f44077bc0763cc86d",
  293. "3e32e2c5e9a24bd13375e1cd0467079c",
  294. "a5452722b2841f0c1db030cf17708955",
  295. "dc6c45eb59f8814aaaf7aabdb8962294",
  296. "69d208d281a2e7bffd3eaf4bab2309b1",
  297. "ec05666cfb60211bedc5e81d4c1caf3d",
  298. "da520038c153f4054cb8cc5faa617714",
  299. "59906c8cd819cae67476baa90a36b8c1",
  300. "9a8fe78c588d08018843b64b57409a02",
  301. "8b5d28e985c0542163927d253a3e4fc9",
  302. "783299b98179cc725f9c46b5e3b5381f",
  303. "1a2b3fba1187c68d6a9bfa66854f3318",
  304. "391ff57b38d9ea6f3eeb3fe69ab539d3"
  305. ]
  306. for i, test in tests:
  307. let filename = testsDir / test.addFileExt("nim")
  308. let testHash = getMD5(readFile(filename).string)
  309. doAssert testHash == refHashes[i], "Nim in Action test " & filename & " 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. proc waitForExitEx(p: Process): int =
  385. var outp = outputStream(p)
  386. var line = newStringOfCap(120).TaintedString
  387. while true:
  388. if outp.readLine(line):
  389. discard
  390. else:
  391. result = peekExitCode(p)
  392. if result != -1: break
  393. close(p)
  394. proc getPackageDir(package: string): string =
  395. ## TODO - Replace this with dom's version comparison magic.
  396. let commandOutput = execCmdEx("nimble path $#" % package)
  397. if commandOutput.exitCode != QuitSuccess:
  398. return ""
  399. else:
  400. result = commandOutput[0].string
  401. iterator listPackages(): tuple[name, url, cmd: string, hasDeps: bool] =
  402. let defaultCmd = "nimble test"
  403. let packageList = parseFile(packageIndex)
  404. for n, cmd, hasDeps, url in important_packages.packages.items:
  405. let cmd = if cmd.len == 0: defaultCmd else: cmd
  406. if url.len != 0:
  407. yield (n, url, cmd, hasDeps)
  408. else:
  409. var found = false
  410. for package in packageList.items:
  411. let name = package["name"].str
  412. if name == n:
  413. found = true
  414. let p_url = package["url"].str
  415. yield (name, p_url, cmd, hasDeps)
  416. break
  417. if not found:
  418. raise newException(ValueError, "Cannot find package '$#'." % n)
  419. proc makeSupTest(test, options: string, cat: Category): TTest =
  420. result.cat = cat
  421. result.name = test
  422. result.options = options
  423. result.startTime = epochTime()
  424. proc testNimblePackages(r: var TResults, cat: Category) =
  425. if nimbleExe == "":
  426. echo "[Warning] - Cannot run nimble tests: Nimble binary not found."
  427. return
  428. if execCmd("$# update" % nimbleExe) == QuitFailure:
  429. echo "[Warning] - Cannot run nimble tests: Nimble update failed."
  430. return
  431. let packageFileTest = makeSupTest("PackageFileParsed", "", cat)
  432. let packagesDir = "pkgstemp"
  433. var errors = 0
  434. try:
  435. for name, url, cmd, hasDep in listPackages():
  436. inc r.total
  437. var test = makeSupTest(url, "", cat)
  438. let buildPath = packagesDir / name
  439. if not existsDir(buildPath):
  440. if hasDep:
  441. let installName = if url.len != 0: url else: name
  442. let (nimbleCmdLine, nimbleOutput, nimbleStatus) = execCmdEx2("nimble", ["install", "-y", installName])
  443. if nimbleStatus != QuitSuccess:
  444. let message = "nimble install failed:\n$ " & nimbleCmdLine & "\n" & nimbleOutput
  445. r.addResult(test, targetC, "", message, reInstallFailed)
  446. continue
  447. let (installCmdLine, installOutput, installStatus) = execCmdEx2("git", ["clone", url, buildPath])
  448. if installStatus != QuitSuccess:
  449. let message = "git clone failed:\n$ " & installCmdLine & "\n" & installOutput
  450. r.addResult(test, targetC, "", message, reInstallFailed)
  451. continue
  452. let cmdArgs = parseCmdLine(cmd)
  453. let (buildCmdLine, buildOutput, buildStatus) = execCmdEx2(cmdArgs[0], cmdArgs[1..^1], workingDir=buildPath)
  454. if buildStatus != QuitSuccess:
  455. let message = "package test failed\n$ " & buildCmdLine & "\n" & buildOutput
  456. r.addResult(test, targetC, "", message, reBuildFailed)
  457. else:
  458. inc r.passed
  459. r.addResult(test, targetC, "", "", reSuccess)
  460. errors = r.total - r.passed
  461. if errors == 0:
  462. r.addResult(packageFileTest, targetC, "", "", reSuccess)
  463. else:
  464. r.addResult(packageFileTest, targetC, "", "", reBuildFailed)
  465. except JsonParsingError:
  466. echo "[Warning] - Cannot run nimble tests: Invalid package file."
  467. r.addResult(packageFileTest, targetC, "", "Invalid package file", reBuildFailed)
  468. except ValueError:
  469. echo "[Warning] - $#" % getCurrentExceptionMsg()
  470. r.addResult(packageFileTest, targetC, "", "Unknown package", reBuildFailed)
  471. finally:
  472. if errors == 0: removeDir(packagesDir)
  473. # ----------------------------------------------------------------------------
  474. const AdditionalCategories = ["debugger", "examples", "lib"]
  475. const MegaTestCat = "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",
  555. "--listFullPaths:off", "--excessiveStackTrace:off", "megatest.nim"]
  556. proc onStdout(line: string) = echo line
  557. var (cmdLine, buf, exitCode) = execCmdEx2(command = compilerPrefix, args = args, input = "")
  558. if exitCode != 0:
  559. echo "$ ", cmdLine
  560. echo buf.string
  561. quit("megatest compilation failed")
  562. # Could also use onStdout here.
  563. (buf, exitCode) = execCmdEx("./megatest")
  564. if exitCode != 0:
  565. echo buf.string
  566. quit("megatest execution failed")
  567. norm buf.string
  568. writeFile("outputGotten.txt", buf.string)
  569. var outputExpected = ""
  570. for i, runSpec in specs:
  571. outputExpected.add marker & runSpec.file & "\n"
  572. outputExpected.add runSpec.output.strip
  573. outputExpected.add '\n'
  574. norm outputExpected
  575. if buf.string != outputExpected:
  576. writeFile("outputExpected.txt", outputExpected)
  577. discard execShellCmd("diff -uNdr outputExpected.txt outputGotten.txt")
  578. echo "output different!"
  579. # outputGotten.txt, outputExpected.txt not removed on purpose for debugging.
  580. quit 1
  581. else:
  582. echo "output OK"
  583. removeFile("outputGotten.txt")
  584. removeFile("megatest.nim")
  585. #testSpec r, makeTest("megatest", options, cat)
  586. # ---------------------------------------------------------------------------
  587. proc processCategory(r: var TResults, cat: Category,
  588. options, testsDir: string,
  589. runJoinableTests: bool) =
  590. case cat.string.normalize
  591. of "rodfiles":
  592. when false:
  593. compileRodFiles(r, cat, options)
  594. runRodFiles(r, cat, options)
  595. of "js":
  596. # only run the JS tests on Windows or Linux because Travis is bad
  597. # and other OSes like Haiku might lack nodejs:
  598. if not defined(linux) and isTravis:
  599. discard
  600. else:
  601. jsTests(r, cat, options)
  602. of "dll":
  603. dllTests(r, cat, options)
  604. of "flags":
  605. flagTests(r, cat, options)
  606. of "gc":
  607. gcTests(r, cat, options)
  608. of "longgc":
  609. longGCTests(r, cat, options)
  610. of "debugger":
  611. debuggerTests(r, cat, options)
  612. of "manyloc":
  613. manyLoc r, cat, options
  614. of "threads":
  615. threadTests r, cat, options & " --threads:on"
  616. of "io":
  617. ioTests r, cat, options
  618. of "async":
  619. asyncTests r, cat, options
  620. of "lib":
  621. testStdlib(r, "lib/pure/", options, cat)
  622. testStdlib(r, "lib/packages/docutils/", options, cat)
  623. of "examples":
  624. compileExample(r, "examples/*.nim", options, cat)
  625. compileExample(r, "examples/gtk/*.nim", options, cat)
  626. compileExample(r, "examples/talk/*.nim", options, cat)
  627. of "nimble-packages":
  628. testNimblePackages(r, cat)
  629. of "niminaction":
  630. testNimInAction(r, cat, options)
  631. of "untestable":
  632. # We can't test it because it depends on a third party.
  633. discard # TODO: Move untestable tests to someplace else, i.e. nimble repo.
  634. of "megatest":
  635. runJoinedTest(r, cat, testsDir)
  636. else:
  637. var testsRun = 0
  638. var files: seq[string]
  639. for file in walkDirRec(testsDir &.? cat.string):
  640. if isTestFile(file): files.add file
  641. files.sort # give reproducible order
  642. for i, name in files:
  643. var test = makeTest(name, options, cat)
  644. if runJoinableTests or not isJoinableSpec(test.spec) or cat.string in specialCategories:
  645. discard "run the test"
  646. else:
  647. test.spec.err = reJoined
  648. testSpec r, test
  649. inc testsRun
  650. if testsRun == 0:
  651. echo "[Warning] - Invalid category specified \"", cat.string, "\", no tests were run"