summaryrefslogtreecommitdiff
path: root/tests/runtest.sh
blob: 9abf620a61324763aab5db762d80084a7547d06e (plain)
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
#!/usr/bin/env bash

function print_usage {
    echo ''
    echo 'CoreCLR test runner script.'
    echo 'Script uses next files if they exist:'
    echo '    unsupportedTests.<arch>.txt'
    echo '    unsupportedCrossgenLibs.<arch>.txt'
    echo '    unsupportedCrossgenTests.<arch>.txt'
    echo ''
    echo 'Typical Tizen command:'
    echo '    coreclr/tests/runtest.sh'
    echo '    --arch=arm'
    echo '    --testRootDir="/opt/usr/coreclr-tc"'
    echo '    --coreOverlayDir="/opt/usr/coreclr-tc/coreroot"'
    echo '    --netcoreDir="/usr/share/dotnet.tizen/netcoreapp"'
    echo '    --copy-netcore-to-coreroot'
    echo '    --xunitOutputPath="/opt/usr/coreclr-tc/results/coreclrtests.xml"'
    echo '    --testsPassOutputPath="/opt/usr/coreclr-tc/results/coreclrtests.pass.txt"'
    echo '    --testsSkipOutputPath="/opt/usr/coreclr-tc/results/coreclrtests.skip.txt"'
    echo '    --testsFailOutputPath="/opt/usr/coreclr-tc/results/coreclrtests.fail.txt"'
    echo '    --libsCrossgenPassOutputPath="/opt/usr/coreclr-tc/results/crossgenlibs.pass.txt"'
    echo '    --libsCrossgenSkipOutputPath="/opt/usr/coreclr-tc/results/crossgenlibs.skip.txt"'
    echo '    --libsCrossgenFailOutputPath="/opt/usr/coreclr-tc/results/crossgenlibs.fail.txt"'
    echo ''
    echo 'Required arguments:'
    echo '  --arch=<arch>                    : Target arch (for TW targets "arm" should be passed instead of "arm64").'
    echo '  --testRootDir=<path>             : Root directory of the test build (default: /opt/usr/coreclr-tc).'
    echo '  --coreOverlayDir=<path>          : Directory containing CLR and FX (default: /opt/usr/coreclr-tc/coreroot).'
    echo ''
    echo 'Optional arguments:'
    echo '  --netcoreDir=<path>              : Netcore (CLR and FX) system dir (default: /usr/share/dotnet.tizen/netcoreapp).'
    echo '  --copy-netcore-to-coreroot       : Copy netcore (CLR and FX) from netcore system dir to coreroot.'
    echo '  --testDir=<path>                 : Run tests only in the specified directory. The path is relative to the directory'
    echo '                                     specified by --testRootDir. Multiple of this switch may be specified.'
    echo '  --testDirFile=<path>             : Run tests only in the directories specified by the file at <path>. Paths are listed'
    echo '                                     one line, relative to the directory specified by --testRootDir.'
    echo '  --num-procs=<N>                  : Run N test processes at the same time (default is 1).'
    echo '  -v, --verbose                    : Show output from each test.'
    echo '  -h, --help                       : Show usage information.'
    echo '  --xunitOutputPath=<path>         : Create xUnit XML report at the specifed path (default: <test root dir>/coreclrtests.xml)'
    echo '  --testsPassOutputPath=<path>     : Create pass report at the specifed path (default: <test root dir>/coreclrtests.pass.txt)'
    echo '  --testsSkipOutputPath=<path>     : Create skip report at the specifed path (default: <test root dir>/coreclrtests.skip.txt)'
    echo '  --testsFailOutputPath=<path>     : Create fail report at the specifed path (default: <test root dir>/coreclrtests.fail.txt)'
    echo '  --test-env                       : Script to set environment variables for tests.'
    echo ''
    echo 'Optional arguments with "export COMPlus_..." meaning:'
    echo '  --jitstress=<n>                  : Runs the tests with COMPlus_JitStress=n'
    echo '  --jitstressregs=<n>              : Runs the tests with COMPlus_JitStressRegs=n'
    echo '  --gcstresslevel=<n>              : Runs the tests with COMPlus_GCStress=n'
    echo ''
    echo 'Crossgen:'
    echo '  --crossgen-libs                  : Precompiles system libs.'
    echo '  --crossgen-tests                 : Precompiles tests.'
    echo '  --crossgenoptions                : Options passed to crossgen.'
    echo '  --allow-crossgen-fails           : Allow crossgen to ignore compilation fails.'
    echo '  --crossgen-lib=<path>            : Precompile only specified lib. The path is absolute.'
    echo '  --libsCrossgenPassOutputPath=<path>  : Create crossgen pass report at the specifed path (default: <test root dir>/crossgenlibs.pass.txt)'
    echo '  --libsCrossgenSkipOutputPath=<path>  : Create crossgen skip report at the specifed path (default: <test root dir>/crossgenlibs.skip.txt)'
    echo '  --libsCrossgenFailOutputPath=<path>  : Create crossgen fail report at the specifed path (default: <test root dir>/crossgenlibs.fail.txt)'
    echo '  --crossgen-spc-first             : Crossgen System.Private.CoreLib.dll before other dlls.'
}

function print_results {
    echo ""
    echo "======================="
    echo "     Test Results"
    echo "======================="
    echo "# Overlay          : $coreOverlayDir"
    echo "# Tests Discovered : $countTotalTests"
    echo "# Passed           : $countPassedTests"
    echo "# Failed           : $countFailedTests"
    echo "# Skipped          : $countSkippedTests"
    echo "======================="
}

# Initialize counters for bookkeeping.
countTotalTests=0
countPassedTests=0
countFailedTests=0
countSkippedTests=0

# Variables for xUnit-style XML output. XML format: https://xunit.github.io/docs/format-xml-v2.html
xunitOutputPath=
xunitTestOutputPath=

# Variables for text file output. These can be passed back to runtest.sh using the "--playlist" argument
# to rerun specific tests.
testsPassOutputPath=
testsFailOutputPath=
testsSkipOutputPath=
libsCrossgenPassOutputPath=
libsCrossgenFailOutputPath=
libsCrossgenSkipOutputPath=

function xunit_output_begin {
    if [ -z "$xunitOutputPath" ]; then
        xunitOutputPath=$testRootDir/coreclrtests.xml
    fi
    xunitTestOutputPath=${xunitOutputPath}.test
    if [ -f "$xunitOutputPath" ]; then
        rm -f -r "$xunitOutputPath"
    fi
    if [ -f "$xunitTestOutputPath" ]; then
        rm -f -r "$xunitTestOutputPath"
    fi
}

function xunit_output_add_test {
    # <assemblies>
    #   <assembly>
    #     <collection>
    #       <test .../> <!-- Write this element here -->

    local scriptFilePath=$1
    local outputFilePath=$2
    local testResult=$3 # Pass, Fail, or Skip
    local testScriptExitCode=$4
    local testRunningTime=$5

    local testPath=${scriptFilePath%.sh} # Remove trailing ".sh"
    local testDir=$(dirname "$testPath")
    local testName=$(basename "$testPath")

    # Replace '/' with '.'
    testPath=$(echo "$testPath" | tr / .)
    testDir=$(echo "$testDir" | tr / .)

    local line

    line="      "
    line="${line}<test"
    line="${line} name=\"${testPath}\""
    line="${line} type=\"${testDir}\""
    line="${line} method=\"${testName}\""
    line="${line} result=\"${testResult}\""
    if [ -n "$testRunningTime" ] && [ "$testResult" != "Skip" ]; then
        line="${line} time=\"${testRunningTime}\""
    fi

    if [ "$testResult" == "Pass" ]; then
        line="${line}/>"
        echo "$line" >>"$xunitTestOutputPath"
        return
    fi

    line="${line}>"
    echo "$line" >>"$xunitTestOutputPath"

    line="        "
    if [ "$testResult" == "Skip" ]; then
        line="${line}<reason><![CDATA[$(cat "$outputFilePath")]]></reason>"
        echo "$line" >>"$xunitTestOutputPath"
    else
        line="${line}<failure exception-type=\"Exit code: ${testScriptExitCode}\">"
        echo "$line" >>"$xunitTestOutputPath"

        line="          "
        line="${line}<message>"
        echo "$line" >>"$xunitTestOutputPath"
        line="            "
        line="${line}<![CDATA["
        echo "$line" >>"$xunitTestOutputPath"
        cat "$outputFilePath" >>"$xunitTestOutputPath"
        line="            "
        line="${line}]]>"
        echo "$line" >>"$xunitTestOutputPath"
        line="          "
        line="${line}</message>"
        echo "$line" >>"$xunitTestOutputPath"

        line="        "
        line="${line}</failure>"
        echo "$line" >>"$xunitTestOutputPath"
    fi

    line="      "
    line="${line}</test>"
    echo "$line" >>"$xunitTestOutputPath"
}

function xunit_output_end {
    local errorSource=$1
    local errorMessage=$2

    local errorCount
    if [ -z "$errorSource" ]; then
        ((errorCount = 0))
    else
        ((errorCount = 1))
    fi

    echo '<?xml version="1.0" encoding="utf-8"?>' >>"$xunitOutputPath"
    echo '<assemblies>' >>"$xunitOutputPath"

    local line

    # <assembly ...>
    line="  "
    line="${line}<assembly"
    line="${line} name=\"CoreClrTestAssembly\""
    line="${line} total=\"${countTotalTests}\""
    line="${line} passed=\"${countPassedTests}\""
    line="${line} failed=\"${countFailedTests}\""
    line="${line} skipped=\"${countSkippedTests}\""
    line="${line} errors=\"${errorCount}\""
    line="${line}>"
    echo "$line" >>"$xunitOutputPath"

    # <collection ...>
    line="    "
    line="${line}<collection"
    line="${line} name=\"CoreClrTestCollection\""
    line="${line} total=\"${countTotalTests}\""
    line="${line} passed=\"${countPassedTests}\""
    line="${line} failed=\"${countFailedTests}\""
    line="${line} skipped=\"${countSkippedTests}\""
    line="${line}>"
    echo "$line" >>"$xunitOutputPath"

    # <test .../> <test .../> ...
    if [ -f "$xunitTestOutputPath" ]; then
        cat "$xunitTestOutputPath" >>"$xunitOutputPath"
        rm -f "$xunitTestOutputPath"
    fi

    # </collection>
    line="    "
    line="${line}</collection>"
    echo "$line" >>"$xunitOutputPath"

    if [ -n "$errorSource" ]; then
        # <errors>
        line="    "
        line="${line}<errors>"
        echo "$line" >>"$xunitOutputPath"

        # <error ...>
        line="      "
        line="${line}<error"
        line="${line} type=\"TestHarnessError\""
        line="${line} name=\"${errorSource}\""
        line="${line}>"
        echo "$line" >>"$xunitOutputPath"

        # <failure .../>
        line="        "
        line="${line}<failure>${errorMessage}</failure>"
        echo "$line" >>"$xunitOutputPath"

        # </error>
        line="      "
        line="${line}</error>"
        echo "$line" >>"$xunitOutputPath"

        # </errors>
        line="    "
        line="${line}</errors>"
        echo "$line" >>"$xunitOutputPath"
    fi

    # </assembly>
    line="  "
    line="${line}</assembly>"
    echo "$line" >>"$xunitOutputPath"

    # </assemblies>
    echo '</assemblies>' >>"$xunitOutputPath"
}

function text_file_output_begin {
    if [ -z "$testsPassOutputPath" ]; then
        testsPassOutputPath=$testRootDir/coreclrtests.pass.txt
    fi
    if [ -f "$testsPassOutputPath" ]; then
        rm -f "$testsPassOutputPath"
    fi
    if [ -z "$testsFailOutputPath" ]; then
        testsFailOutputPath=$testRootDir/coreclrtests.fail.txt
    fi
    if [ -f "$testsFailOutputPath" ]; then
        rm -f "$testsFailOutputPath"
    fi
    if [ -z "$testsSkipOutputPath" ]; then
        testsSkipOutputPath=$testRootDir/coreclrtests.skip.txt
    fi
    if [ -f "$testsSkipOutputPath" ]; then
        rm -f "$testsSkipOutputPath"
    fi

    if [ -z "$libsCrossgenPassOutputPath" ]; then
        libsCrossgenPassOutputPath=$testRootDir/crossgenlibs.pass.txt
    fi
    if [ -f "$libsCrossgenPassOutputPath" ]; then
        rm -f "$libsCrossgenPassOutputPath"
    fi
    if [ -z "$libsCrossgenFailOutputPath" ]; then
        libsCrossgenFailOutputPath=$testRootDir/crossgenlibs.fail.txt
    fi
    if [ -f "$libsCrossgenFailOutputPath" ]; then
        rm -f "$libsCrossgenFailOutputPath"
    fi
    if [ -z "$libsCrossgenSkipOutputPath" ]; then
        libsCrossgenSkipOutputPath=$testRootDir/crossgenlibs.skip.txt
    fi
    if [ -f "$libsCrossgenSkipOutputPath" ]; then
        rm -f "$libsCrossgenSkipOutputPath"
    fi
}

function text_file_output_add_test {
    local scriptFilePath=$1
    local testResult=$2 # Pass, Fail, or Skip

    if [ "$testResult" == "Pass" ]; then
        echo "$scriptFilePath" >>"$testsPassOutputPath"
    elif [ "$testResult" == "Skip" ]; then
        echo "$scriptFilePath" >>"$testsSkipOutputPath"
    else
        echo "$scriptFilePath" >>"$testsFailOutputPath"
    fi
}

function exit_with_error {
    local errorSource=$1
    local errorMessage=$2
    local printUsage=$3

    if [ -z "$printUsage" ]; then
        ((printUsage = 0))
    fi

    echo "$errorMessage"
    xunit_output_end "$errorSource" "$errorMessage"
    if ((printUsage != 0)); then
        print_usage
    fi
    exit $EXIT_CODE_EXCEPTION
}

# Handle Ctrl-C. We will stop execution and print the results that
# we gathered so far.
function handle_ctrl_c {
    local errorSource='handle_ctrl_c'

    echo ""
    echo "*** Stopping... ***"
    print_results
    exit_with_error "$errorSource" "Test run aborted by Ctrl+C."
}

# Register the Ctrl-C handler
trap handle_ctrl_c INT

# Variables for unsupported and failing tests
declare -a unsupportedCrossGenLibs
declare -a unsupportedCrossGenTests
declare -a unsupportedTests

# Get an array of items by reading the specified file line by line.
function read_array {
    local theArray=()

    if [ ! -f "$1" ]; then
        return
    fi

    # bash in Mac OS X doesn't support 'readarray', so using alternate way instead.
    # readarray -t theArray < "$1"
    # Any line that starts with '#' is ignored.
    while IFS='' read -r line || [ -n "$line" ]; do
        if [[ $line != "#"* ]]; then
            theArray[${#theArray[@]}]=$line
        fi
    done < "$1"
    echo ${theArray[@]}
}

function load_unsupported_tests {
    # Load the list of tests that are not supported on this platform
    unsupportedTests=($(read_array "$(dirname "$0")/unsupportedTests.$ARCH.txt"))
    # Load the list of libs that are not supported for crossgen on this platform
    unsupportedCrossGenLibs=($(read_array "$(dirname "$0")/unsupportedCrossgenLibs.$ARCH.txt"))
    # Load the list of test that are not supported for crossgen on this platform
    unsupportedCrossGenTests=($(read_array "$(dirname "$0")/unsupportedCrossgenTests.$ARCH.txt"))
}

function is_unsupported_crossgen_lib {
    for unsupported in "${unsupportedCrossGenLibs[@]}"; do
        if [ "$1" == "$unsupported" ]; then
            return 0
        fi
    done
    return 1
}

function is_unsupported_test {
    for unsupported in "${unsupportedTests[@]}"; do
        if [ "$1" == "$unsupported" ]; then
            return 0
        fi
    done

    if [ $doCrossgenTests == 1 ]; then
        for unsupported in "${unsupportedCrossGenTests[@]}"; do
            if [ "$1" == "$unsupported" ]; then
                return 0
            fi
        done
    fi

    return 1
}

function crossgen_file {
    local overlayDir=$1
    local dll_path=$2
    local name=$3

    local ni_path=$(echo $dll_path | sed 's/.dll$/.ni.dll/')

    echo Precompiling $dll_path

    if [ $doCrossgenLibs == 1 ]; then
        ${overlayDir}/crossgen /in $dll_path /out $ni_path $CrossGenOptions /p ${overlayDir} &>$dll_path.out
    fi

    local exitCode=$?
    if [[ $exitCode != 0 ]]; then
        echo Crossgen fail for $dll_path.
        echo "$dll_path" >> "$libsCrossgenFailOutputPath"

        cat $dll_path.out

        if [ $CrossGenAllowFail == 0 ]; then
            exit $exitCode
        fi
    else
        echo Crossgen success $dll_path.
        echo "$dll_path" >> "$libsCrossgenPassOutputPath"
    fi

    if [[ "${name}" == "System.Private.CoreLib.dll" ]]; then
        mv $dll_path $dll_path.bak
        mv $ni_path $dll_path
    fi

    rm $dll_path.out
}

function precompile_overlay_assemblies {
    local overlayDir=$CORE_ROOT

    if [ $doCopyNetcoreToCoreroot == 1 ]; then
        echo "Copying netcore ($netcoreDir) to coreroot ($overlayDir)"
        cp $netcoreDir/* $overlayDir
    fi

    echo Cleanup old ni.dll
    for file in `find $testRootDir -name "*.ni.*"`; do
        rm $file
    done

    test -f $overlayDir/System.Private.CoreLib.dll.Backup && mv $overlayDir/System.Private.CoreLib.dll.Backup $overlayDir/System.Private.CoreLib.dll
    test -f $overlayDir/System.Private.CoreLib.dll.bak && mv $overlayDir/System.Private.CoreLib.dll.bak $overlayDir/System.Private.CoreLib.dll

    if [ $doCrossgenLibs == 0 ]; then
        echo Skipping crossgen of libs.
        return 0
    else
        echo Precompiling libs
    fi

    if [ $doCrossgenSPCFirst == 1 ]; then
        local dll_path=$overlayDir/System.Private.CoreLib.dll
        local name=$(basename $dll_path)
        crossgen_file $overlayDir $dll_path $name
    fi

    if [ -z "$crossgenLibs" ]; then
        # No libs to crossgen were specified, so crossgen everything in the overlay
        filesToPrecompile=$(find -L $overlayDir -iname \*.dll -not -iname \*.ni.dll -type f)
    else
        # Otherwise, compile only specified libs
        filesToPrecompile=$(echo "${crossgenLibs[@]}")
    fi

    for fileToPrecompile in ${filesToPrecompile}
    do
        local dll_path=${fileToPrecompile}
        local name=$(basename $dll_path)

        if [[ "$name" == "System.Private.CoreLib.dll" && "$doCrossgenSPCFirst" == "1" ]]; then
            continue
        fi

        if is_unsupported_crossgen_lib "${name}"; then
            echo Skipping crossgen for $dll_path.
            echo "$dll_path" >> "$libsCrossgenSkipOutputPath"
            continue
        fi

        crossgen_file $overlayDir $dll_path $name
    done
}

function skip_unsupported_test {
    # This function runs in a background process. It should not echo anything, and should not use global variables. This
    # function is analogous to run_test, and causes the test to be skipped with the message below.

    local scriptFilePath=$1
    local outputFilePath=$2

    echo "Not supported on this platform or in this mode." >"$outputFilePath"
    return 2 # skip the test
}

function run_test {
    # This function runs in a background process. It should not echo anything, and should not use global variables.

    local scriptFilePath=$1
    local outputFilePath=$2

    # Switch to directory where the script is
    cd "$(dirname "$scriptFilePath")"

    local scriptFileName=$(basename "$scriptFilePath")
    local outputFileName=$(basename "$outputFilePath")

    "./$scriptFileName" >"$outputFileName" 2>&1
    local testScriptExitCode=$?

    return $testScriptExitCode
}

# Variables for running tests in the background
((maxProcesses = 1)) # long tests delay process creation, use a few more processors

((processCount = 0))
declare -a scriptFilePaths
declare -a outputFilePaths
declare -a processIds
declare -a testStartTimes
waitProcessIndex=
pidNone=0

function waitany {
    local pid
    local exitcode
    while true; do
        for (( i=0; i<$maxProcesses; i++ )); do
            pid=${processIds[$i]}
            if [ -z "$pid" ] || [ "$pid" == "$pidNone" ]; then
                continue
            fi
            if ! kill -0 $pid 2>/dev/null; then
                wait $pid
                exitcode=$?
                waitProcessIndex=$i
                processIds[$i]=$pidNone
                return $exitcode
            fi
        done
        sleep 0.1
    done
}

function get_available_process_index {
    local pid
    local i=0
    for (( i=0; i<$maxProcesses; i++ )); do
        pid=${processIds[$i]}
        if [ -z "$pid" ] || [ "$pid" == "$pidNone" ]; then
            break
        fi
    done
    echo $i
}

function finish_test {
    waitany
    local testScriptExitCode=$?
    local finishedProcessIndex=$waitProcessIndex
    ((--processCount))

    local scriptFilePath=${scriptFilePaths[$finishedProcessIndex]}
    local outputFilePath=${outputFilePaths[$finishedProcessIndex]}
    local scriptFileName=$(basename "$scriptFilePath")

    local testEndTime=
    local testRunningTime=
    local header=

    if ((verbose == 1)); then
        header=$(printf "[%4d]" $countTotalTests)
    fi

    testEndTime=$(date +%s)
    testRunningTime=$(( $testEndTime - ${testStartTimes[$finishedProcessIndex]} ))
    header=$header$(printf "[%4ds]" $testRunningTime)

    local testResult
    case $testScriptExitCode in
        0)
            let countPassedTests++
            testResult='Pass'
            if ((verbose == 1)); then
                echo "PASSED   - ${header}${scriptFilePath}"
            else
                echo "         - ${header}${scriptFilePath}"
            fi
            ;;
        2)
            let countSkippedTests++
            testResult='Skip'
            echo "SKIPPED  - ${header}${scriptFilePath}"
            ;;
        *)
            let countFailedTests++
            testResult='Fail'
            echo "FAILED   - ${header}${scriptFilePath}"
            ;;
    esac
    let countTotalTests++

    if ((verbose == 1 || testScriptExitCode != 0)); then
        while IFS='' read -r line || [ -n "$line" ]; do
            echo "               $line"
        done <"$outputFilePath"
    fi

    xunit_output_add_test "$scriptFilePath" "$outputFilePath" "$testResult" "$testScriptExitCode" "$testRunningTime"
    text_file_output_add_test "$scriptFilePath" "$testResult"
}

function finish_remaining_tests {
    # Finish the remaining tests in the order in which they were started
    while ((processCount > 0)); do
        finish_test
    done
}

function prep_test {
    local scriptFilePath=$1
    local scriptFileDir=$(dirname "$scriptFilePath")

    test "$verbose" == 1 && echo "Preparing $scriptFilePath"

    # Add executable file mode bit if needed
    chmod +x "$scriptFilePath"

    #remove any Locks
    rm -rf $scriptFileDir/lock
}

function start_test {
    local nextProcessIndex=$(get_available_process_index)
    local scriptFilePath=$1

    if ((nextProcessIndex == maxProcesses)); then
        finish_test
        nextProcessIndex=$(get_available_process_index)
    fi

    scriptFilePaths[$nextProcessIndex]=$scriptFilePath
    local scriptFileName=$(basename "$scriptFilePath")
    local outputFilePath=$(dirname "$scriptFilePath")/${scriptFileName}.out
    outputFilePaths[$nextProcessIndex]=$outputFilePath

    testStartTimes[$nextProcessIndex]=$(date +%s)

    test "$verbose" == 1 && echo "Starting $scriptFilePath"
    if is_unsupported_test "$scriptFilePath"; then
        skip_unsupported_test "$scriptFilePath" "$outputFilePath" &
    else
        run_test "$scriptFilePath" "$outputFilePath" &
    fi
    processIds[$nextProcessIndex]=$!

    ((++processCount))
}

# Get a list of directories in which to scan for tests by reading the
# specified file line by line.
function set_test_directories {
    local errorSource='set_test_directories'

    local listFileName=$1

    if [ ! -f "$listFileName" ]
    then
        exit_with_error "$errorSource" "Test directories file not found at $listFileName"
    fi
    testDirectories=($(read_array "$listFileName"))
}

function run_tests_in_directory {
    local testDir=$1
    local skipScriptsInDir=$2

    local minDepth="1"

    # Need to skip scripts in testDir since they are not tests
    if [[ "$skipScriptsInDir" == "1" ]]; then
        minDepth="2"
    fi

    # Recursively search through directories for .sh files to prepare them.
    # Note: This needs to occur before any test runs as some of the .sh files
    # depend on other .sh files
    echo "Preparing tests..."
    for scriptFilePath in $(find "$testDir" -mindepth ${minDepth} -type f -iname '*.sh' | sort)
    do
        prep_test "${scriptFilePath:2}"
    done
    echo "The tests have been prepared"
    # Recursively search through directories for .sh files to run.
    for scriptFilePath in $(find "$testDir" -mindepth ${minDepth} -type f -iname '*.sh' | sort)
    do
        start_test "${scriptFilePath:2}"
    done
}

# Exit code constants
readonly EXIT_CODE_SUCCESS=0       # Script ran normally.
readonly EXIT_CODE_EXCEPTION=1     # Script exited because something exceptional happened (e.g. bad arguments, Ctrl-C interrupt).
readonly EXIT_CODE_TEST_FAILURE=2  # Script completed successfully, but one or more tests failed.

# Argument variables
testRootDir="/opt/usr/coreclr-tc/"
coreOverlayDir="/opt/usr/coreclr-tc/coreroot/"
doCopyNetcoreToCoreroot=0
netcoreDir="/usr/share/dotnet.tizen/netcoreapp"
testEnv=

# Handle arguments
verbose=0
doCrossgenLibs=0
CrossGenAllowFail=0
doCrossgenTests=0
doCrossgenSPCFirst=0

for i in "$@"
do
    case $i in
        -h|--help)
            print_usage
            exit $EXIT_CODE_SUCCESS
            ;;
        -v|--verbose)
            verbose=1
            ;;
        --arch=*)
            ARCH=${i#*=}
            ;;
        --crossgen-libs)
            doCrossgenLibs=1
            ;;
        --crossgen-tests)
            doCrossgenTests=1
            export RunCrossGen=1
            ;;
        --crossgen-spc-first)
            doCrossgenSPCFirst=1
            ;;
        --crossgenoptions=*)
            export CrossGenOptions="${i#*=}"
            ;;
        --allow-crossgen-fails)
            CrossGenAllowFail=1
            ;;
        --crossgen-lib=*)
            crossgenLibs[${#crossgenLibs[@]}]=${i#*=}
            ;;
        --jitstress=*)
            export COMPlus_JitStress=${i#*=}
            ;;
        --jitstressregs=*)
            export COMPlus_JitStressRegs=${i#*=}
            ;;
        --testRootDir=*)
            testRootDir=${i#*=}
            ;;
        --coreOverlayDir=*)
            coreOverlayDir=${i#*=}
            ;;
        --netcoreDir=*)
            netcoreDir=${i#*=}
            ;;
        --copy-netcore-to-coreroot)
            doCopyNetcoreToCoreroot=1
            ;;
        --testDir=*)
            testDirectories[${#testDirectories[@]}]=${i#*=}
            ;;
        --testDirFile=*)
            set_test_directories "${i#*=}"
            ;;
        --num-procs=*)
            ((maxProcesses = ${i#*=}))
            ;;
        --test-env=*)
            testEnv=${i#*=}
            ;;
        --gcstresslevel=*)
            export COMPlus_GCStress=${i#*=}
            ;;
        --xunitOutputPath=*)
            xunitOutputPath=${i#*=}
            ;;
        --testsPassOutputPath=*)
            testsPassOutputPath=${i#*=}
            ;;
        --testsSkipOutputPath=*)
            testsSkipOutputPath=${i#*=}
            ;;
        --testsFailOutputPath=*)
            testsFailOutputPath=${i#*=}
            ;;
        --libsCrossgenPassOutputPath=*)
            libsCrossgenPassOutputPath=${i#*=}
            ;;
        --libsCrossgenSkipOutputPath=*)
            libsCrossgenSkipOutputPath=${i#*=}
            ;;
        --libsCrossgenFailOutputPath=*)
            libsCrossgenFailOutputPath=${i#*=}
            ;;
        *)
            echo "Unknown switch: $i"
            print_usage
            exit $EXIT_CODE_SUCCESS
            ;;
    esac
done

if [ -z "$ARCH" ]; then
    echo "--arch is required."
    print_usage
    exit $EXIT_CODE_EXCEPTION
fi

if [ -z "$testRootDir" ]; then
    echo "--testRootDir is required."
    print_usage
    exit $EXIT_CODE_EXCEPTION
fi
if [ ! -d "$testRootDir" ]; then
    echo "Directory specified by --testRootDir does not exist: $testRootDir"
    exit $EXIT_CODE_EXCEPTION
fi

if [ -z "$coreOverlayDir" ]; then
    echo "--coreOverlayDir is required."
    print_usage
    exit $EXIT_CODE_EXCEPTION
fi
if [ ! -d "$coreOverlayDir" ]; then
    echo "Directory specified by --coreOverlayDir does not exist: $coreOverlayDir"
    exit $EXIT_CODE_EXCEPTION
fi

export CORE_ROOT="$coreOverlayDir"

echo "! Make sure CLR/FX are copied to $coreOverlayDir !"

echo "Running on CPU-$ARCH"

xunit_output_begin
text_file_output_begin
load_unsupported_tests
precompile_overlay_assemblies

export __TestEnv=$testEnv
cd "$testRootDir"

time_start=$(date +"%s")
if [ -z "$testDirectories" ]
then
    # No test directories were specified, so run everything in the current
    # directory and its subdirectories.
    run_tests_in_directory "." 1
else
    # Otherwise, run all the tests in each specified test directory.
    for testDir in "${testDirectories[@]}"
    do
        if [ ! -d "$testDir" ]; then
            echo "Test directory does not exist: $testDir"
        else
            run_tests_in_directory "./$testDir" 0
        fi
    done
fi

finish_remaining_tests
print_results

time_end=$(date +"%s")
time_diff=$(($time_end-$time_start))
echo "$(($time_diff / 60)) minutes and $(($time_diff % 60)) seconds taken to run CoreCLR tests."

xunit_output_end

if ((countFailedTests > 0)); then
    exit $EXIT_CODE_TEST_FAILURE
fi

exit $EXIT_CODE_SUCCESS