[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / components / testing_framework / core / Build / Scripts / splitFunctionalTests.sh
1 #!/bin/bash
2
3 #########################
4 #
5 # This file is typically executed by travis and / or bamboo.
6 # It expects to be run from the core root.
7 #
8 # ./components/testing_framework/core/Build/Scripts/splitFunctionalTests.sh <numberOfConfigs>
9 #
10 # The scripts finds all functional tests and creates <numberOfConfigs> number
11 # of phpunit .xml configuration files where each configuration lists a weighted
12 # number of single functional tests.
13 #
14 # components/testing_framework/core/Build/FunctionalTests-Job-<counter>.xml
15 #
16 #########################
17
18 numberOfFunctionalTestJobs=${1}
19 numberOfFunctionalTestJobsMinusOne=$(( numberOfFunctionalTestJobs - 1 ))
20
21 # Have a dir for temp files and clean up possibly existing stuff
22 if [ ! -d buildTemp ]; then
23 mkdir buildTemp || exit 1
24 fi
25 if [ -f buildTemp/testFiles.txt ]; then
26 rm buildTemp/testFiles.txt
27 fi
28 if [ -f buildTemp/testFilesWithNumberOfTestFiles.txt ]; then
29 rm buildTemp/testFilesWithNumberOfTestFiles.txt
30 fi
31 if [ -f buildTemp/testFilesWeighted.txt ]; then
32 rm buildTemp/testFilesWeighted.txt
33 fi
34
35 # A list of all functional test files
36 find . -name \*Test.php -path \*typo3/sysext/*/Tests/Functional* > buildTemp/testFiles.txt
37
38 # File with test files of format "42 ./path/to/file"
39 while read testFile; do
40 numberOfTestsInTestFile=`grep "@test" ${testFile} | wc -l`
41 echo "${numberOfTestsInTestFile} ${testFile}" >> buildTemp/testFilesWithNumberOfTestFiles.txt
42 done < buildTemp/testFiles.txt
43
44 # Sort list of files numeric
45 cat buildTemp/testFilesWithNumberOfTestFiles.txt | sort -n -r > buildTemp/testFilesWeighted.txt
46
47 # Config file boilerplate per job
48 for (( i=0; i<${numberOfFunctionalTestJobs}; i++)); do
49 if [ -f components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml ]; then
50 rm components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
51 fi
52 echo '<phpunit' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
53 echo ' backupGlobals="true"' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
54 echo ' backupStaticAttributes="false"' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
55 echo ' bootstrap="FunctionalTestsBootstrap.php"' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
56 echo ' colors="true"' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
57 echo ' convertErrorsToExceptions="true"' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
58 echo ' convertWarningsToExceptions="true"' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
59 echo ' forceCoversAnnotation="false"' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
60 echo ' processIsolation="true"' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
61 echo ' stopOnError="false"' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
62 echo ' stopOnFailure="false"' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
63 echo ' stopOnIncomplete="false"' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
64 echo ' stopOnSkipped="false"' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
65 echo ' verbose="false"' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
66 echo '>' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
67 echo ' <testsuites>' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
68 echo ' <testsuite name="Core tests">' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
69 done
70
71 counter=0
72 direction=ascending
73 while read testFileWeighted; do
74 # test file only, without leading ./
75 testFile=`echo ${testFileWeighted} | cut -f2 -d" " | cut -f2-40 -d"/"`
76
77 # Goal: with 3 jobs, have:
78 # file #0 to job #0 (asc)
79 # file #1 to job #1 (asc)
80 # file #2 to job #2 (asc)
81 # file #3 to job #2 (desc)
82 # file #4 to job #1 (desc)
83 # file #5 to job #0 (desc)
84 # file #6 to job #0 (asc)
85 # ...
86 testFileModuleNumberOfJobs=$(( counter % numberOfFunctionalTestJobs ))
87 if [[ ${direction} == "descending" ]]; then
88 targetJobNumberForFile=$(( numberOfFunctionalTestJobs - testFileModuleNumberOfJobs))
89 else
90 targetJobNumberForFile=${testFileModuleNumberOfJobs}
91 fi
92 if [ ${testFileModuleNumberOfJobs} -eq ${numberOfFunctionalTestJobs} ]; then
93 if [[ ${direction} == "descending" ]]; then
94 direction=ascending
95 else
96 direction=descending
97 fi
98 fi
99
100 echo ' <directory>' >> components/testing_framework/core/Build/FunctionalTests-Job-${targetJobNumberForFile}.xml
101 echo " ../../../../${testFile}" >> components/testing_framework/core/Build/FunctionalTests-Job-${targetJobNumberForFile}.xml
102 echo ' </directory>' >> components/testing_framework/core/Build/FunctionalTests-Job-${targetJobNumberForFile}.xml
103 (( counter ++ ))
104 done < buildTemp/testFilesWeighted.txt
105
106 # Final part of config file
107 for (( i=0; i<${numberOfFunctionalTestJobs}; i++)); do
108 echo ' </testsuite>' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
109 echo ' </testsuites>' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
110 echo '</phpunit>' >> components/testing_framework/core/Build/FunctionalTests-Job-${i}.xml
111 done
112
113 # Clean up
114 rm buildTemp/testFiles.txt
115 rm buildTemp/testFilesWeighted.txt
116 rm buildTemp/testFilesWithNumberOfTestFiles.txt
117 rmdir buildTemp