summary refs log tree commit diff stats
path: root/doc
diff options
context:
space:
mode:
authorflywind <43030857+xflywind@users.noreply.github.com>2022-06-09 00:17:25 +0800
committerGitHub <noreply@github.com>2022-06-08 18:17:25 +0200
commit415689323c625afb7d39d923c4165a541beb9927 (patch)
treec6c61b51ac1cd16c748085f564a0e3ca97f3815c /doc
parente1702ae1e6da5bf560d7b692bcd5fa513ece8a69 (diff)
downloadNim-415689323c625afb7d39d923c4165a541beb9927.tar.gz
clarify how to restart CI (#19845)
Diffstat (limited to 'doc')
-rw-r--r--doc/contributing.rst1
1 files changed, 1 insertions, 0 deletions
diff --git a/doc/contributing.rst b/doc/contributing.rst
index d3b2770f3..0ca0d0cbc 100644
--- a/doc/contributing.rst
+++ b/doc/contributing.rst
@@ -539,6 +539,7 @@ Debugging CI failures, flaky tests, etc
 2. If CI failure seems unrelated to your PR, it could be caused by a flaky test.
    File a bug for it if it isn't already reported. A PR push (or opening/closing PR)
    will re-trigger all CI jobs (even successful ones, which can be wasteful). Instead,
+   request collaboration from the Nim team. The Nim team should
    follow these instructions to only restart the jobs that failed:
 
    * Azure: if on your own fork, it's possible from inside azure console
2019-08-08 08:41:05 +0200 Incremental compilation (IC): Improvements (#11881)' href='/ahoang/Nim/commit/testament/categories.nim?h=devel&id=c8cffaf42037ae8defe59d9a1fb7d202655aa1ee'>c8cffaf42 ^
3b783f7e1 ^


5a9b3c27c ^

3b783f7e1 ^
3b783f7e1 ^





ac785b062 ^

72237e2bc ^

3b783f7e1 ^

c8cffaf42 ^


20b5f31c0 ^
c8cffaf42 ^









































20b5f31c0 ^
e931f3b5a ^



4a720394b ^
e931f3b5a ^







7f8eccb46 ^
e931f3b5a ^





20b5f31c0 ^

20b5f31c0 ^
e5aefbd1d ^




ca4b971bc ^
b26378a46 ^

ca4b971bc ^
b26378a46 ^

ca4b971bc ^


470faa97c ^


64903e705 ^
ca4b971bc ^

20b5f31c0 ^
ca4b971bc ^

7ef268274 ^
ac0f62084 ^
7ef268274 ^

64903e705 ^
ca4b971bc ^

470faa97c ^
ca4b971bc ^


c8cffaf42 ^
ca4b971bc ^
c8cffaf42 ^



20b5f31c0 ^



64903e705 ^
20b5f31c0 ^

16260b23c ^



20b5f31c0 ^



68e30d7d5 ^
b26378a46 ^
20b5f31c0 ^
b26378a46 ^
72237e2bc ^

83a736a34 ^
72237e2bc ^
83a736a34 ^
7524610b3 ^
68e30d7d5 ^
7524610b3 ^
20b5f31c0 ^
b26378a46 ^
20b5f31c0 ^
b26378a46 ^
808863325 ^
68e30d7d5 ^
9deab06c1 ^
de88fdb86 ^
645cf7011 ^


b26378a46 ^
645cf7011 ^
b26378a46 ^
0284e8d11 ^
6fa1dba51 ^
ed103e8e3 ^
0284e8d11 ^
20b5f31c0 ^


b38f6d49b ^
20b5f31c0 ^
a087f6057 ^
7524610b3 ^

9deab06c1 ^
20b5f31c0 ^
9deab06c1 ^
7524610b3 ^
64903e705 ^
d691867c8 ^
dcf830bba ^
d691867c8 ^
7524610b3 ^

20b5f31c0 ^
c55f884b5 ^

73af427f2 ^
c55f884b5 ^
73af427f2 ^
c55f884b5 ^


b26378a46 ^



c55f884b5 ^
20b5f31c0 ^


68e30d7d5 ^
b26378a46 ^


8310d252c ^

20b5f31c0 ^







756f50d0a ^
20b5f31c0 ^
015e22c8c ^



015e22c8c ^


20b5f31c0 ^


dd5ccc3e5 ^



20b5f31c0 ^



68e30d7d5 ^
b26378a46 ^

64903e705 ^
20b5f31c0 ^

c93c014bc ^


749dbce4c ^
2166b7bc4 ^
c93655e8b ^

c93c014bc ^
20b5f31c0 ^
a994f142b ^

2fcc37fd2 ^
7889c03cb ^


99384c023 ^

b26378a46 ^

7889c03cb ^

b26378a46 ^
7889c03cb ^

0944b0f44 ^
7889c03cb ^

9e8623785 ^







7889c03cb ^

9e8623785 ^
7889c03cb ^





9d4f5c6d2 ^
7889c03cb ^


9e8623785 ^
7889c03cb ^
9e8623785 ^
3ba3307d6 ^




































694611627 ^
9e8623785 ^
7889c03cb ^
b26378a46 ^
7889c03cb ^

7889c03cb ^


20b5f31c0 ^
20b5f31c0 ^

cd2c6128d ^
64903e705 ^
cd2c6128d ^
20b5f31c0 ^



64903e705 ^
20b5f31c0 ^


0944b0f44 ^
20b5f31c0 ^



16260b23c ^

ae82406c8 ^
20b5f31c0 ^
cb3c99fe1 ^
dec863a85 ^


20b5f31c0 ^


dec863a85 ^


20b5f31c0 ^

f9d58b930 ^






















4a720394b ^
f9d58b930 ^


4a720394b ^

f9d58b930 ^



20b5f31c0 ^
24ae0c387 ^
3249f16d8 ^
616db85c6 ^

64903e705 ^
24ae0c387 ^
f4c76bc8e ^
3249f16d8 ^
5a9b3c27c ^





3249f16d8 ^
5a9b3c27c ^




a4883b842 ^
639e9a7aa ^
a4883b842 ^





c8cffaf42 ^

a4883b842 ^


f4c76bc8e ^





3249f16d8 ^
5a9b3c27c ^
24ae0c387 ^
091da5c97 ^
ab0fea919 ^
24ae0c387 ^
091da5c97 ^
ab0fea919 ^
3249f16d8 ^
f4c76bc8e ^
091da5c97 ^

370f06d55 ^
5a9b3c27c ^
88566cc65 ^

091da5c97 ^
f4c76bc8e ^
6d8ce609d ^


639e9a7aa ^

6d8ce609d ^
3ef727adf ^

6d8ce609d ^

3ef727adf ^
6d8ce609d ^
3ef727adf ^

6d8ce609d ^


091da5c97 ^
3ef727adf ^
6d8ce609d ^
3ef727adf ^

f4c76bc8e ^
091da5c97 ^
6d8ce609d ^
c55ceafd9 ^
091da5c97 ^





6713ebcd9 ^
091da5c97 ^




f4c76bc8e ^
091da5c97 ^
3249f16d8 ^
3249f16d8 ^
20b5f31c0 ^

c8cffaf42 ^
3d9269195 ^
20b5f31c0 ^
fc452787e ^

0944b0f44 ^
fc452787e ^
ddb70d33f ^
4a720394b ^
cf9da7d75 ^
da78c3d04 ^

3b783f7e1 ^
330b3c445 ^
ddb70d33f ^
3b783f7e1 ^


3b783f7e1 ^
f9d58b930 ^
3b783f7e1 ^
f9d58b930 ^
3b783f7e1 ^
ac785b062 ^

3b783f7e1 ^





c0c35839c ^
0d99ff611 ^






4a720394b ^



0d99ff611 ^
39290cf88 ^
0d99ff611 ^
0d99ff611 ^



f9d58b930 ^
c8cffaf42 ^



0d99ff611 ^
4a720394b ^

0d99ff611 ^

f9d58b930 ^





0d99ff611 ^
4a720394b ^




fbf9b1f88 ^
4a720394b ^











0d99ff611 ^


721534119 ^

c8cffaf42 ^
3ef727adf ^
0d99ff611 ^
3ef727adf ^
ca4b971bc ^
0d99ff611 ^

f3d2c18a1 ^
0d99ff611 ^
ca4b971bc ^
0d99ff611 ^

ca4b971bc ^

0d99ff611 ^

4a720394b ^
0d99ff611 ^



ca4b971bc ^
0d99ff611 ^


4a720394b ^
0d99ff611 ^


8aa4b6b55 ^
0d99ff611 ^




3c689c1f2 ^
3d9269195 ^
0d99ff611 ^
20b5f31c0 ^
c8cffaf42 ^
07a986afa ^

20b5f31c0 ^
6c319d4f7 ^

81645deb8 ^


6c319d4f7 ^
20b5f31c0 ^

e931f3b5a ^

20b5f31c0 ^

c55f884b5 ^

20b5f31c0 ^

20b5f31c0 ^





015e22c8c ^

73dda8a81 ^
f9d58b930 ^

20b5f31c0 ^



5a9b3c27c ^



7889c03cb ^

82f3cab1a ^


0d99ff611 ^

20b5f31c0 ^
089529c16 ^
f9d58b930 ^
4a720394b ^
fbf9b1f88 ^
f9d58b930 ^


ac785b062 ^
01892eee4 ^
ac785b062 ^
c0c35839c ^
ac785b062 ^

089529c16 ^


82d5e773e ^


9ffec7930 ^
















82d5e773e ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757

 
                       
                                         







                                                                              
                             
 

                         










                       
         


              

                        
                  





                 

                 

                     

   


                                                 
 









































                                                                                 
 



                                                                               
                                                







                                                             
                      





                                                                    

                                                                               
                                                                          




                                            
                                                                              

                                   
                                                                                      

                                   


                                                                                      


                                                                                       
 

                                               
                                                    

                                                          
                                           
                                                           

                                                                                  
 

                                                                                      
                                                                        


                                                                               
                                                           
                                                                                                



                                                                            



                                                                
 

                                                    



                                                                 



                                                                               
                                             
                                                             
                                                         
                                                      

                                                           
                                     
                                                           
                                                
 
                                                
                          
                                                         
                                            
                                                         
                                                       
 
                                    
                             
                                                       


                                                                        
                                       
                                                           
                                                  
 
                                
                     
                     


                
                           
                
                

                                                        
                             
                  
                                
                              
 
                           
                            
 

                       
 

                                                                   
                               
       
                         


                                                      



                                                                                
 


                                                                               
                                    


                                                                       

                                                







                                                                               
                                                        
 



                                                                               


                                              


                                                                               



                                                                      



                                                                               
                                    

                                                                                      
 

                                           


                                                                
                                                                  
                                                                           

                                                                       
                                     
 

                                                                     
 


                                                                       

                                         

                                                

                                      
                                                            

                                       
                                                             

               







                                            

                                              
                                    





                                                            
                                                           


                                                       
                                       
     
 




































                                                                                 
 
                  
                        
                                     

                                                                  


                                                               
                                                                               

                                        
                                                                    
                                                         
                           



                                    
                                                                        


                                         
                                    



                                                               

                                       
                                                   
                                      
                        


                                                   


                                                                               


                                           

                                                                           






















                                                                                           
         


                                                                     

                                                                            



                                          
 
                                                                               
 

                                                           
   
                               
                                                     
 





                                                                                    
                                           




                                                  
                    
                                  





                                       

                                          


                                                                       





                                                               
 
                                                                                               
                     
                                                                        
          
                                                     
                                                                     
          
 
                                                                 

                              
      
                                                     

                                  
                 
                                          


                                        

                                                                                                                
                                         

                                                                                            

                    
                                                                                                         
                                        

                                                                                       


                                     
 
                                                                                                                 
                                    

                                                                                   
           
                    
                                                     
 





                                                                  
                          




                                                                                    
          
                                          
 
 

                                                                              
                                                                  
                              
 

                                  
                                            
 
                                                                               
                                           
                                                                      

                                                      
       
                                                 
 


                                        
                                                      
                                                       
                                                        
                                                            
                         

                              





                                                     
 






                                     



                                       
                                                                      
                                               
                             



                                                     
                                             



                                    
 

                                                         

                                    





                                      
                      




                                                                
    











                                                                  


                                     

                                                                                 
 
                                                                                              
                   
                      
                   

                                       
                                           
                   
                   

                                     

                                           

                          
                                                   



                                           
                                  


                                                                          
                                                                                


                    
                                  




                                                                             
                                                    
                                               
                                              
                           
          

                                        
          

                                                                     


                                       
                              

                             

                              

                            

                                

                                  





                                                 

                              
           

                                                         



                                                          



                                              

                                    


                                                                              

                                   
       
                    
                          
                                                    
                                         


                                        
                                             
                                                                                              
                              
           

                                


                                                                                           


                                                                                
















                                                               

                                                     
#
#
#            Nim Tester
#        (c) Copyright 2015 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## Include for the tester that contains test suites that test special features
## of the compiler.

# included from testament.nim

import important_packages

const
  specialCategories = [
    "assert",
    "async",
    "debugger",
    "dll",
    "examples",
    "flags",
    "gc",
    "io",
    "js",
    "ic",
    "lib",
    "longgc",
    "manyloc",
    "nimble-packages-1",
    "nimble-packages-2",
    "niminaction",
    "threads",
    "untestable",
    "stdlib",
    "testdata",
    "nimcache",
    "coroutines",
    "osproc",
    "shouldfail",
    "dir with space",
    "destructor"
  ]

proc isTestFile*(file: string): bool =
  let (_, name, ext) = splitFile(file)
  result = ext == ".nim" and name.startsWith("t")

# ---------------- IC tests ---------------------------------------------

proc icTests(r: var TResults; testsDir: string, cat: Category, options: string) =
  const
    tooltests = ["compiler/nim.nim", "tools/nimgrep.nim"]
    writeOnly = " --incremental:writeonly "
    readOnly = " --incremental:readonly "
    incrementalOn = " --incremental:on "

  template test(x: untyped) =
    testSpecWithNimcache(r, makeRawTest(file, x & options, cat), nimcache)

  template editedTest(x: untyped) =
    var test = makeTest(file, x & options, cat)
    test.spec.targets = {getTestSpecTarget()}
    testSpecWithNimcache(r, test, nimcache)

  const tempExt = "_temp.nim"
  for it in walkDirRec(testsDir / "ic"):
    if isTestFile(it) and not it.endsWith(tempExt):
      let nimcache = nimcacheDir(it, options, getTestSpecTarget())
      removeDir(nimcache)

      let content = readFile(it)
      for fragment in content.split("#!EDIT!#"):
        let file = it.replace(".nim", tempExt)
        writeFile(file, fragment)
        let oldPassed = r.passed
        editedTest incrementalOn
        if r.passed != oldPassed+1: break

  for file in tooltests:
    let nimcache = nimcacheDir(file, options, getTestSpecTarget())
    removeDir(nimcache)

    let oldPassed = r.passed
    test writeOnly

    if r.passed == oldPassed+1:
      test readOnly
      if r.passed == oldPassed+2:
        test readOnly & "-d:nimBackendAssumesChange "

# --------------------- flags tests -------------------------------------------

proc flagTests(r: var TResults, cat: Category, options: string) =
  # --genscript
  const filename = testsDir/"flags"/"tgenscript"
  const genopts = " --genscript"
  let nimcache = nimcacheDir(filename, genopts, targetC)
  testSpec r, makeTest(filename, genopts, cat)

  when defined(windows):
    testExec r, makeTest(filename, " cmd /c cd " & nimcache &
                         " && compile_tgenscript.bat", cat)

  elif defined(posix):
    testExec r, makeTest(filename, " sh -c \"cd " & nimcache &
                         " && sh compile_tgenscript.sh\"", cat)

  # Run
  testExec r, makeTest(filename, " " & nimcache / "tgenscript", cat)

# --------------------- DLL generation tests ----------------------------------

proc runBasicDLLTest(c, r: var TResults, cat: Category, options: string) =
  const rpath = when defined(macosx):
      " --passL:-rpath --passL:@loader_path"
    else:
      ""

  var test1 = makeTest("lib/nimrtl.nim", options & " --outdir:tests/dll", cat)
  test1.spec.action = actionCompile
  testSpec c, test1
  var test2 = makeTest("tests/dll/server.nim", options & " --threads:on" & rpath, cat)
  test2.spec.action = actionCompile
  testSpec c, test2
  var test3 = makeTest("lib/nimhcr.nim", options & " --outdir:tests/dll" & rpath, cat)
  test3.spec.action = actionCompile
  testSpec c, test3
  var test4 = makeTest("tests/dll/visibility.nim", options & " --app:lib" & rpath, cat)
  test4.spec.action = actionCompile
  testSpec c, test4

  # windows looks in the dir of the exe (yay!):
  when not defined(Windows):
    # posix relies on crappy LD_LIBRARY_PATH (ugh!):
    const libpathenv = when defined(haiku): "LIBRARY_PATH"
                       else: "LD_LIBRARY_PATH"
    var libpath = getEnv(libpathenv).string
    # Temporarily add the lib directory to LD_LIBRARY_PATH:
    putEnv(libpathenv, "tests/dll" & (if libpath.len > 0: ":" & libpath else: ""))
    defer: putEnv(libpathenv, libpath)

  testSpec r, makeTest("tests/dll/client.nim", options & " --threads:on" & rpath, cat)
  testSpec r, makeTest("tests/dll/nimhcr_unit.nim", options & rpath, cat)
  testSpec r, makeTest("tests/dll/visibility.nim", options & rpath, cat)

  if "boehm" notin options:
    # force build required - see the comments in the .nim file for more details
    var hcri = makeTest("tests/dll/nimhcr_integration.nim",
                                   options & " --forceBuild --hotCodeReloading:on" & rpath, cat)
    let nimcache = nimcacheDir(hcri.name, hcri.options, getTestSpecTarget())
    hcri.args = prepareTestArgs(hcri.spec.getCmd, hcri.name,
                                hcri.options, nimcache, getTestSpecTarget())
    testSpec r, hcri

proc dllTests(r: var TResults, cat: Category, options: string) =
  # dummy compile result:
  var c = initResults()

  runBasicDLLTest c, r, cat, options
  runBasicDLLTest c, r, cat, options & " -d:release"
  when not defined(windows):
    # still cannot find a recent Windows version of boehm.dll:
    runBasicDLLTest c, r, cat, options & " --gc:boehm"
    runBasicDLLTest c, r, cat, options & " -d:release --gc:boehm"

# ------------------------------ GC tests -------------------------------------

proc gcTests(r: var TResults, cat: Category, options: string) =
  template testWithoutMs(filename: untyped) =
    testSpec r, makeTest("tests/gc" / filename, options, cat)
    testSpec r, makeTest("tests/gc" / filename, options &
                  " -d:release -d:useRealtimeGC", cat)
    when filename != "gctest":
      testSpec r, makeTest("tests/gc" / filename, options &
                    " --gc:orc", cat)
      testSpec r, makeTest("tests/gc" / filename, options &
                    " --gc:orc -d:release", cat)

  template testWithoutBoehm(filename: untyped) =
    testWithoutMs filename
    testSpec r, makeTest("tests/gc" / filename, options &
                  " --gc:markAndSweep", cat)
    testSpec r, makeTest("tests/gc" / filename, options &
                  " -d:release --gc:markAndSweep", cat)

  template test(filename: untyped) =
    testWithoutBoehm filename
    when not defined(windows) and not defined(android):
      # AR: cannot find any boehm.dll on the net, right now, so disabled
      # for windows:
      testSpec r, makeTest("tests/gc" / filename, options &
                    " --gc:boehm", cat)
      testSpec r, makeTest("tests/gc" / filename, options &
                    " -d:release --gc:boehm", cat)

  testWithoutBoehm "foreign_thr"
  test "gcemscripten"
  test "growobjcrash"
  test "gcbench"
  test "gcleak"
  test "gcleak2"
  testWithoutBoehm "gctest"
  test "gcleak3"
  test "gcleak4"
  # Disabled because it works and takes too long to run:
  #test "gcleak5"
  testWithoutBoehm "weakrefs"
  test "cycleleak"
  testWithoutBoehm "closureleak"
  testWithoutMs "refarrayleak"

  testWithoutBoehm "tlists"
  testWithoutBoehm "thavlak"

  test "stackrefleak"
  test "cyclecollector"

proc longGCTests(r: var TResults, cat: Category, options: string) =
  when defined(windows):
    let cOptions = "-ldl -DWIN"
  else:
    let cOptions = "-ldl"

  var c = initResults()
  # According to ioTests, this should compile the file
  testSpec c, makeTest("tests/realtimeGC/shared", options, cat)
  #        ^- why is this not appended to r? Should this be discarded?
  testC r, makeTest("tests/realtimeGC/cmain", cOptions, cat), actionRun
  testSpec r, makeTest("tests/realtimeGC/nmain", options & "--threads: on", cat)

# ------------------------- threading tests -----------------------------------

proc threadTests(r: var TResults, cat: Category, options: string) =
  template test(filename: untyped) =
    testSpec r, makeTest(filename, options, cat)
    testSpec r, makeTest(filename, options & " -d:release", cat)
    testSpec r, makeTest(filename, options & " --tlsEmulation:on", cat)
  for t in os.walkFiles("tests/threads/t*.nim"):
    test(t)

# ------------------------- IO tests ------------------------------------------

proc ioTests(r: var TResults, cat: Category, options: string) =
  # We need readall_echo to be compiled for this test to run.
  # dummy compile result:
  var c = initResults()
  testSpec c, makeTest("tests/system/helpers/readall_echo", options, cat)
  testSpec r, makeTest("tests/system/tio", options, cat)

# ------------------------- async tests ---------------------------------------
proc asyncTests(r: var TResults, cat: Category, options: string) =
  template test(filename: untyped) =
    testSpec r, makeTest(filename, options, cat)
  for t in os.walkFiles("tests/async/t*.nim"):
    test(t)

# ------------------------- debugger tests ------------------------------------

proc debuggerTests(r: var TResults, cat: Category, options: string) =
  if fileExists("tools/nimgrep.nim"):
    var t = makeTest("tools/nimgrep", options & " --debugger:on", cat)
    t.spec.action = actionCompile
    testSpec r, t

# ------------------------- JS tests ------------------------------------------

proc jsTests(r: var TResults, cat: Category, options: string) =
  template test(filename: untyped) =
    testSpec r, makeTest(filename, options & " -d:nodejs", cat), {targetJS}
    testSpec r, makeTest(filename, options & " -d:nodejs -d:release", cat), {targetJS}

  for t in os.walkFiles("tests/js/t*.nim"):
    test(t)
  for testfile in ["exception/texceptions", "exception/texcpt1",
                   "exception/texcsub", "exception/tfinally",
                   "exception/tfinally2", "exception/tfinally3",
                   "actiontable/tactiontable", "method/tmultimjs",
                   "varres/tvarres0", "varres/tvarres3", "varres/tvarres4",
                   "varres/tvartup", "misc/tints", "misc/tunsignedinc",
                   "async/tjsandnativeasync"]:
    test "tests/" & testfile & ".nim"

  for testfile in ["strutils", "json", "random", "times", "logging"]:
    test "lib/pure/" & testfile & ".nim"

# ------------------------- nim in action -----------

proc testNimInAction(r: var TResults, cat: Category, options: string) =
  let options = options & " --nilseqs:on"

  template test(filename: untyped) =
    testSpec r, makeTest(filename, options, cat)

  template testJS(filename: untyped) =
    testSpec r, makeTest(filename, options, cat), {targetJS}

  template testCPP(filename: untyped) =
    testSpec r, makeTest(filename, options, cat), {targetCpp}

  let tests = [
    "niminaction/Chapter1/various1",
    "niminaction/Chapter2/various2",
    "niminaction/Chapter2/resultaccept",
    "niminaction/Chapter2/resultreject",
    "niminaction/Chapter2/explicit_discard",
    "niminaction/Chapter2/no_def_eq",
    "niminaction/Chapter2/no_iterator",
    "niminaction/Chapter2/no_seq_type",
    "niminaction/Chapter3/ChatApp/src/server",
    "niminaction/Chapter3/ChatApp/src/client",
    "niminaction/Chapter3/various3",
    "niminaction/Chapter6/WikipediaStats/concurrency_regex",
    "niminaction/Chapter6/WikipediaStats/concurrency",
    "niminaction/Chapter6/WikipediaStats/naive",
    "niminaction/Chapter6/WikipediaStats/parallel_counts",
    "niminaction/Chapter6/WikipediaStats/race_condition",
    "niminaction/Chapter6/WikipediaStats/sequential_counts",
    "niminaction/Chapter6/WikipediaStats/unguarded_access",
    "niminaction/Chapter7/Tweeter/src/tweeter",
    "niminaction/Chapter7/Tweeter/src/createDatabase",
    "niminaction/Chapter7/Tweeter/tests/database_test",
    "niminaction/Chapter8/sdl/sdl_test"
    ]

  when false:
    # Verify that the files have not been modified. Death shall fall upon
    # whoever edits these hashes without dom96's permission, j/k. But please only
    # edit when making a conscious breaking change, also please try to make your
    # commit message clear and notify me so I can easily compile an errata later.
    # ---------------------------------------------------------
    # Hash-checks are disabled for Nim 1.1 and beyond
    # since we needed to fix the deprecated unary '<' operator.
    const refHashes = @[
      "51afdfa84b3ca3d810809d6c4e5037ba",
      "30f07e4cd5eaec981f67868d4e91cfcf",
      "d14e7c032de36d219c9548066a97e846",
      "b335635562ff26ec0301bdd86356ac0c",
      "6c4add749fbf50860e2f523f548e6b0e",
      "76de5833a7cc46f96b006ce51179aeb1",
      "705eff79844e219b47366bd431658961",
      "a1e87b881c5eb161553d119be8b52f64",
      "2d706a6ec68d2973ec7e733e6d5dce50",
      "c11a013db35e798f44077bc0763cc86d",
      "3e32e2c5e9a24bd13375e1cd0467079c",
      "a5452722b2841f0c1db030cf17708955",
      "dc6c45eb59f8814aaaf7aabdb8962294",
      "69d208d281a2e7bffd3eaf4bab2309b1",
      "ec05666cfb60211bedc5e81d4c1caf3d",
      "da520038c153f4054cb8cc5faa617714",
      "59906c8cd819cae67476baa90a36b8c1",
      "9a8fe78c588d08018843b64b57409a02",
      "8b5d28e985c0542163927d253a3e4fc9",
      "783299b98179cc725f9c46b5e3b5381f",
      "1a2b3fba1187c68d6a9bfa66854f3318",
      "391ff57b38d9ea6f3eeb3fe69ab539d3"
    ]
    for i, test in tests:
      let filename = testsDir / test.addFileExt("nim")
      let testHash = getMD5(readFile(filename).string)
      doAssert testHash == refHashes[i], "Nim in Action test " & filename &
          " was changed: " & $(i: i, testHash: testHash, refHash: refHashes[i])

  # Run the tests.
  for testfile in tests:
    test "tests/" & testfile & ".nim"
  let jsFile = "tests/niminaction/Chapter8/canvas/canvas_test.nim"
  testJS jsFile
  let cppFile = "tests/niminaction/Chapter8/sfml/sfml_test.nim"
  testCPP cppFile

# ------------------------- manyloc -------------------------------------------

proc findMainFile(dir: string): string =
  # finds the file belonging to ".nim.cfg"; if there is no such file
  # it returns the some ".nim" file if there is only one:
  const cfgExt = ".nim.cfg"
  result = ""
  var nimFiles = 0
  for kind, file in os.walkDir(dir):
    if kind == pcFile:
      if file.endsWith(cfgExt): return file[.. ^(cfgExt.len+1)] & ".nim"
      elif file.endsWith(".nim"):
        if result.len == 0: result = file
        inc nimFiles
  if nimFiles != 1: result.setLen(0)

proc manyLoc(r: var TResults, cat: Category, options: string) =
  for kind, dir in os.walkDir("tests/manyloc"):
    if kind == pcDir:
      when defined(windows):
        if dir.endsWith"nake": continue
      if dir.endsWith"named_argument_bug": continue
      let mainfile = findMainFile(dir)
      if mainfile != "":
        var test = makeTest(mainfile, options, cat)
        test.spec.action = actionCompile
        testSpec r, test

proc compileExample(r: var TResults, pattern, options: string, cat: Category) =
  for test in os.walkFiles(pattern):
    var test = makeTest(test, options, cat)
    test.spec.action = actionCompile
    testSpec r, test

proc testStdlib(r: var TResults, pattern, options: string, cat: Category) =
  var files: seq[string]

  proc isValid(file: string): bool =
    for dir in parentDirs(file, inclusive = false):
      if dir.lastPathPart in ["includes", "nimcache"]:
        # eg: lib/pure/includes/osenv.nim gives: Error: This is an include file for os.nim!
        return false
    let name = extractFilename(file)
    if name.splitFile.ext != ".nim": return false
    for namei in disabledFiles:
      # because of `LockFreeHash.nim` which has case
      if namei.cmpPaths(name) == 0: return false
    return true

  for testFile in os.walkDirRec(pattern):
    if isValid(testFile):
      files.add testFile

  files.sort # reproducible order
  for testFile in files:
    let contents = readFile(testFile).string
    var testObj = makeTest(testFile, options, cat)
    #[
    todo:
    this logic is fragile:
    false positives (if appears in a comment), or false negatives, eg
    `when defined(osx) and isMainModule`.
    Instead of fixing this, see https://github.com/nim-lang/Nim/issues/10045
    for a much better way.
    ]#
    if "when isMainModule" notin contents:
      testObj.spec.action = actionCompile
    testSpec r, testObj

# ----------------------------- nimble ----------------------------------------

var nimbleDir = getEnv("NIMBLE_DIR").string
if nimbleDir.len == 0: nimbleDir = getHomeDir() / ".nimble"
let
  nimbleExe = findExe("nimble")
  packageIndex = nimbleDir / "packages_official.json"

type
  PkgPart = enum
    ppOne
    ppTwo

iterator listPackages(part: PkgPart): tuple[name, url, cmd: string, hasDeps: bool] =
  let packageList = parseFile(packageIndex)
  let importantList =
    case part
    of ppOne: important_packages.packages1
    of ppTwo: important_packages.packages2
  for n, cmd, hasDeps, url in importantList.items:
    if url.len != 0:
      yield (n, url, cmd, hasDeps)
    else:
      var found = false
      for package in packageList.items:
        let name = package["name"].str
        if name == n:
          found = true
          let pUrl = package["url"].str
          yield (name, pUrl, cmd, hasDeps)
          break
      if not found:
        raise newException(ValueError, "Cannot find package '$#'." % n)

proc makeSupTest(test, options: string, cat: Category): TTest =
  result.cat = cat
  result.name = test
  result.options = options
  result.startTime = epochTime()

proc testNimblePackages(r: var TResults; cat: Category; packageFilter: string, part: PkgPart) =
  if nimbleExe == "":
    echo "[Warning] - Cannot run nimble tests: Nimble binary not found."
    return
  if execCmd("$# update" % nimbleExe) == QuitFailure:
    echo "[Warning] - Cannot run nimble tests: Nimble update failed."
    return

  let packageFileTest = makeSupTest("PackageFileParsed", "", cat)
  let packagesDir = "pkgstemp"
  var errors = 0
  try:
    for name, url, cmd, hasDep in listPackages(part):
      if packageFilter notin name:
        continue
      inc r.total
      var test = makeSupTest(url, "", cat)
      let buildPath = packagesDir / name
      if not existsDir(buildPath):
        if hasDep:
          let installName = if url.len != 0: url else: name
          let (nimbleCmdLine, nimbleOutput, nimbleStatus) = execCmdEx2("nimble", ["install", "-y", installName])
          if nimbleStatus != QuitSuccess:
            let message = "nimble install failed:\n$ " & nimbleCmdLine & "\n" & nimbleOutput
            r.addResult(test, targetC, "", message, reInstallFailed)
            continue

        let (installCmdLine, installOutput, installStatus) = execCmdEx2("git", ["clone", url, buildPath])
        if installStatus != QuitSuccess:
          let message = "git clone failed:\n$ " & installCmdLine & "\n" & installOutput
          r.addResult(test, targetC, "", message, reInstallFailed)
          continue

      let cmdArgs = parseCmdLine(cmd)

      let (buildCmdLine, buildOutput, buildStatus) = execCmdEx2(cmdArgs[0], cmdArgs[1..^1], workingDir=buildPath)
      if buildStatus != QuitSuccess:
        let message = "package test failed\n$ " & buildCmdLine & "\n" & buildOutput
        r.addResult(test, targetC, "", message, reBuildFailed)
      else:
        inc r.passed
        r.addResult(test, targetC, "", "", reSuccess)

    errors = r.total - r.passed
    if errors == 0:
      r.addResult(packageFileTest, targetC, "", "", reSuccess)
    else:
      r.addResult(packageFileTest, targetC, "", "", reBuildFailed)

  except JsonParsingError:
    echo "[Warning] - Cannot run nimble tests: Invalid package file."
    r.addResult(packageFileTest, targetC, "", "Invalid package file", reBuildFailed)
  except ValueError:
    echo "[Warning] - $#" % getCurrentExceptionMsg()
    r.addResult(packageFileTest, targetC, "", "Unknown package", reBuildFailed)
  finally:
    if errors == 0: removeDir(packagesDir)


# ----------------------------------------------------------------------------

const AdditionalCategories = ["debugger", "examples", "lib", "ic"]
const MegaTestCat = "megatest"

proc `&.?`(a, b: string): string =
  # candidate for the stdlib?
  result = if b.startsWith(a): b else: a & b

proc processSingleTest(r: var TResults, cat: Category, options, test: string) =
  let test = testsDir &.? cat.string / test
  let target = if cat.string.normalize == "js": targetJS else: targetC
  if existsFile(test):
    testSpec r, makeTest(test, options, cat), {target}
  else:
    doAssert false, test & " test does not exist"

proc isJoinableSpec(spec: TSpec): bool =
  result = not spec.sortoutput and
    spec.action == actionRun and
    not fileExists(spec.file.changeFileExt("cfg")) and
    not fileExists(spec.file.changeFileExt("nims")) and
    not fileExists(parentDir(spec.file) / "nim.cfg") and
    not fileExists(parentDir(spec.file) / "config.nims") and
    spec.cmd.len == 0 and
    spec.err != reDisabled and
    not spec.unjoinable and
    spec.exitCode == 0 and
    spec.input.len == 0 and
    spec.nimout.len == 0 and
    spec.outputCheck != ocSubstr and
    spec.ccodeCheck.len == 0 and
    (spec.targets == {} or spec.targets == {targetC})

proc norm(s: var string) =
  while true:
    let tmp = s.replace("\n\n", "\n")
    if tmp == s: break
    s = tmp
  s = s.strip

proc quoted(a: string): string =
  # todo: consider moving to system.nim
  result.addQuoted(a)

proc runJoinedTest(r: var TResults, cat: Category, testsDir: string) =
  ## returns a list of tests that have problems
  var specs: seq[TSpec] = @[]
  for kind, dir in walkDir(testsDir):
    assert testsDir.startsWith(testsDir)
    let cat = dir[testsDir.len .. ^1]
    if kind == pcDir and cat notin specialCategories:
      for file in walkDirRec(testsDir / cat):
        if isTestFile(file):
          let spec = parseSpec(file)
          if isJoinableSpec(spec):
            specs.add spec

  proc cmp(a: TSpec, b:TSpec): auto = cmp(a.file, b.file)
  sort(specs, cmp=cmp) # reproducible order
  echo "joinable specs: ", specs.len

  if simulate:
    var s = "runJoinedTest: "
    for a in specs: s.add a.file & " "
    echo s
    return

  var megatest: string
  #[
  TODO(minor):
  get from Nim cmd
  put outputGotten.txt, outputGotten.txt, megatest.nim there too
  delete upon completion, maybe
  ]#
  var outDir = nimcacheDir(testsDir / "megatest", "", targetC)
  const marker = "megatest:processing: "

  for i, runSpec in specs:
    let file = runSpec.file
    let file2 = outDir / ("megatest_" & $i & ".nim")
    # `include` didn't work with `trecmod2.nim`, so using `import`
    let code = "echo \"" & marker & "\", " & quoted(file) & "\n"
    createDir(file2.parentDir)
    writeFile(file2, code)
    megatest.add "import " & quoted(file2) & "\n"
    megatest.add "import " & quoted(file) & "\n"

  writeFile("megatest.nim", megatest)

  let args = ["c", "--nimCache:" & outDir, "-d:testing", "--listCmd",
              "--listFullPaths:off", "--excessiveStackTrace:off", "megatest.nim"]

  var (cmdLine, buf, exitCode) = execCmdEx2(command = compilerPrefix, args = args, input = "")
  if exitCode != 0:
    echo "$ ", cmdLine
    echo buf.string
    quit("megatest compilation failed")

  (buf, exitCode) = execCmdEx("./megatest")
  if exitCode != 0:
    echo buf.string
    quit("megatest execution failed")

  norm buf.string
  writeFile("outputGotten.txt", buf.string)
  var outputExpected = ""
  for i, runSpec in specs:
    outputExpected.add marker & runSpec.file & "\n"
    outputExpected.add runSpec.output.strip
    outputExpected.add '\n'
  norm outputExpected

  if buf.string != outputExpected:
    writeFile("outputExpected.txt", outputExpected)
    discard execShellCmd("diff -uNdr outputExpected.txt outputGotten.txt")
    echo "output different!"
    # outputGotten.txt, outputExpected.txt not removed on purpose for debugging.
    quit 1
  else:
    echo "output OK"
    removeFile("outputGotten.txt")
    removeFile("megatest.nim")
  #testSpec r, makeTest("megatest", options, cat)

# ---------------------------------------------------------------------------

proc processCategory(r: var TResults, cat: Category,
                     options, testsDir: string,
                     runJoinableTests: bool) =
  case cat.string.normalize
  of "ic":
    when false:
      icTests(r, testsDir, cat, options)
  of "js":
    # only run the JS tests on Windows or Linux because Travis is bad
    # and other OSes like Haiku might lack nodejs:
    if not defined(linux) and isTravis:
      discard
    else:
      jsTests(r, cat, options)
  of "dll":
    dllTests(r, cat, options)
  of "flags":
    flagTests(r, cat, options)
  of "gc":
    gcTests(r, cat, options)
  of "longgc":
    longGCTests(r, cat, options)
  of "debugger":
    debuggerTests(r, cat, options)
  of "manyloc":
    manyLoc r, cat, options
  of "threads":
    threadTests r, cat, options & " --threads:on"
  of "io":
    ioTests r, cat, options
  of "async":
    asyncTests r, cat, options
  of "lib":
    testStdlib(r, "lib/pure/", options, cat)
    testStdlib(r, "lib/packages/docutils/", options, cat)
  of "examples":
    compileExample(r, "examples/*.nim", options, cat)
    compileExample(r, "examples/gtk/*.nim", options, cat)
    compileExample(r, "examples/talk/*.nim", options, cat)
  of "nimble-packages-1":
    testNimblePackages(r, cat, options, ppOne)
  of "nimble-packages-2":
    testNimblePackages(r, cat, options, ppTwo)
  of "niminaction":
    testNimInAction(r, cat, options)
  of "untestable":
    # We can't test it because it depends on a third party.
    discard # TODO: Move untestable tests to someplace else, i.e. nimble repo.
  of "megatest":
    runJoinedTest(r, cat, testsDir)
  else:
    var testsRun = 0
    var files: seq[string]
    for file in walkDirRec(testsDir &.? cat.string):
      if isTestFile(file): files.add file
    files.sort # give reproducible order

    for i, name in files:
      var test = makeTest(name, options, cat)
      if runJoinableTests or not isJoinableSpec(test.spec) or cat.string in specialCategories:
        discard "run the test"
      else:
        test.spec.err = reJoined
      testSpec r, test
      inc testsRun
    if testsRun == 0:
      echo "[Warning] - Invalid category specified \"", cat.string, "\", no tests were run"

proc processPattern(r: var TResults, pattern, options: string; simulate: bool) =
  var testsRun = 0
  if dirExists(pattern):
    for k, name in walkDir(pattern):
      if k in {pcFile, pcLinkToFile} and name.endsWith(".nim"):
        if simulate:
          echo "Detected test: ", name
        else:
          var test = makeTest(name, options, Category"pattern")
          testSpec r, test
        inc testsRun
  else:
    for name in walkPattern(pattern):
      if simulate:
        echo "Detected test: ", name
      else:
        var test = makeTest(name, options, Category"pattern")
        testSpec r, test
      inc testsRun
  if testsRun == 0:
    echo "no tests were found for pattern: ", pattern