categories.nim 29 KB

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