categories.nim 27 KB

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