categories.nim 26 KB

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