categories.nim 28 KB

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