categories.nim 25 KB

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