|
| 1 | +import std.file; |
| 2 | +import std.getopt; |
| 3 | +import std.process; |
| 4 | +import std.file; |
| 5 | +import std.process; |
| 6 | +import std.stdio; |
| 7 | +import std.string; |
| 8 | + |
| 9 | +// Commandline args |
| 10 | +bool useUnitThreaded; |
| 11 | +bool coreTestsOnly; |
| 12 | +enum Mode { phobos, vibe, combined, travis }; |
| 13 | +Mode mode; |
| 14 | +string[] unitThreadedArgs; |
| 15 | + |
| 16 | +// Exit code to be returned |
| 17 | +int exitCode=0; |
| 18 | + |
| 19 | +// Utils /////////////////////////////////////////////// |
| 20 | + |
| 21 | +pure string flagUseUnitThreaded(bool on) |
| 22 | +{ |
| 23 | + return on? " --ut" : ""; |
| 24 | +} |
| 25 | + |
| 26 | +pure string flagCoreTestsOnly(bool on) |
| 27 | +{ |
| 28 | + return on? " --core" : ""; |
| 29 | +} |
| 30 | + |
| 31 | +pure string flagMode(Mode mode) |
| 32 | +{ |
| 33 | + final switch(mode) |
| 34 | + { |
| 35 | + case Mode.phobos: return " --mode=phobos"; |
| 36 | + case Mode.vibe: return " --mode=vibe"; |
| 37 | + case Mode.combined: return " --mode=combined"; |
| 38 | + case Mode.travis: return " --mode=travis"; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +string fixSlashes(string path) |
| 43 | +{ |
| 44 | + version(Windows) return path.replace(`/`, `\`); |
| 45 | + else version(Posix) return path.replace(`\`, `/`); |
| 46 | + else static assert(0); |
| 47 | +} |
| 48 | + |
| 49 | +string envGet(string name) |
| 50 | +{ |
| 51 | + return environment.get(name, null); |
| 52 | +} |
| 53 | + |
| 54 | +bool envBool(string name) |
| 55 | +{ |
| 56 | + return environment.get(name, null) == "true"; |
| 57 | +} |
| 58 | + |
| 59 | +void tryMkdir(string dir) |
| 60 | +{ |
| 61 | + if(!exists(dir)) |
| 62 | + mkdir(dir); |
| 63 | +} |
| 64 | + |
| 65 | +int runFromCurrentDir(string command) |
| 66 | +{ |
| 67 | + version(Windows) return run(command); |
| 68 | + else return run("./"~command); |
| 69 | +} |
| 70 | + |
| 71 | +int run(string command) |
| 72 | +{ |
| 73 | + writeln(command); |
| 74 | + return spawnShell(command).wait; |
| 75 | +} |
| 76 | + |
| 77 | +int run(string[] command) |
| 78 | +{ |
| 79 | + writeln(command); |
| 80 | + return spawnProcess(command).wait; |
| 81 | +} |
| 82 | + |
| 83 | +bool canRun(string command) |
| 84 | +{ |
| 85 | + return executeShell(command).status == 0; |
| 86 | +} |
| 87 | + |
| 88 | +string findRdmd() |
| 89 | +{ |
| 90 | + // LDC/GDC don't always include rdmd, so allow user to specify path to it in $RDMD. |
| 91 | + // Otherwise, use "rdmd". |
| 92 | + // |
| 93 | + // For travis, if "rdmd" doesn't work (ie, LDC/GDC is being tested), then use |
| 94 | + // the copy of rdmd that was downloaded by the 'ci_setup.d' script. |
| 95 | + auto rdmd = environment.get("RDMD"); |
| 96 | + if(rdmd is null) |
| 97 | + { |
| 98 | + rdmd = "rdmd"; |
| 99 | + if(!canRun("rdmd --help")) |
| 100 | + { |
| 101 | + auto travisOsName = environment.get("TRAVIS_OS_NAME"); |
| 102 | + if(travisOsName == "osx") |
| 103 | + rdmd = "local-dmd/dmd2/"~travisOsName~"/bin/rdmd"; |
| 104 | + else |
| 105 | + rdmd = "local-dmd/dmd2/"~travisOsName~"/bin64/rdmd"; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return rdmd; |
| 110 | +} |
| 111 | + |
| 112 | +// Main (Process command line) /////////////////////////////////////////////// |
| 113 | + |
| 114 | +int main(string[] args) |
| 115 | +{ |
| 116 | + try |
| 117 | + { |
| 118 | + auto argsHelp = getopt(args, |
| 119 | + "ut", "Use unit-threaded (Always includes ut's --trace)", &useUnitThreaded, |
| 120 | + "core", "Only run basic core tests (Can only be used with --mode=phobos)", &coreTestsOnly, |
| 121 | + std.getopt.config.required, |
| 122 | + "m|mode", "'-m=phobos': Run Phobos tests | '-m=vibe': Run Vibe.d tests | '-m=combined': Run core-only Phobos tests and all Vibe.d tests | '-m=travis': Travis-CI mode (autodetect settings from envvars)", &mode, |
| 123 | + ); |
| 124 | + |
| 125 | + if(argsHelp.helpWanted) |
| 126 | + { |
| 127 | + defaultGetoptPrinter( |
| 128 | + "Runs the mysql-native test suite with Phobos sockets, Vibe.d sockets, or combined.\n"~ |
| 129 | + "\n"~ |
| 130 | + "Usage:\n"~ |
| 131 | + " run_tests --mode=(phobos|vibe|combined|travis) [OPTIONS] [-- [UNIT-THREADED OPTIONS]]\n"~ |
| 132 | + "\n"~ |
| 133 | + "Examples:\n"~ |
| 134 | + " run_tests --mode=combined\n"~ |
| 135 | + " run_tests --ut --mode=vibe -- --help\n"~ |
| 136 | + " run_tests --ut --mode=vibe -- --single --random\n"~ |
| 137 | + "\n"~ |
| 138 | + "Options:", |
| 139 | + argsHelp.options |
| 140 | + ); |
| 141 | + |
| 142 | + return 0; |
| 143 | + } |
| 144 | + } |
| 145 | + catch(Exception e) |
| 146 | + { |
| 147 | + stderr.writeln(e.msg); |
| 148 | + stderr.writeln("For help: run_tests --help"); |
| 149 | + stderr.writeln("Recommended: run_tests -m=combined"); |
| 150 | + return 1; |
| 151 | + } |
| 152 | + |
| 153 | + if(coreTestsOnly && mode==Mode.combined) |
| 154 | + { |
| 155 | + stderr.writeln("Cannot use --core and --mode=combined together."); |
| 156 | + //stderr.writeln("Instead, use:"); |
| 157 | + //stderr.writeln(" run_tests --core --mode=phobos && run_tests --core --mode=vibe"); |
| 158 | + stderr.writeln("For help: run_tests --help"); |
| 159 | + return 1; |
| 160 | + } |
| 161 | + |
| 162 | + if(coreTestsOnly && mode==Mode.vibe) |
| 163 | + { |
| 164 | + stderr.writeln("Cannot use --core and --mode=vibe together."); |
| 165 | + stderr.writeln("For help: run_tests --help"); |
| 166 | + return 1; |
| 167 | + } |
| 168 | + |
| 169 | + if(mode==Mode.travis && (coreTestsOnly || useUnitThreaded)) |
| 170 | + { |
| 171 | + stderr.writeln("Cannot use --mode=travis together with any other option."); |
| 172 | + stderr.writeln("For help: run_tests --help"); |
| 173 | + return 1; |
| 174 | + } |
| 175 | + |
| 176 | + unitThreadedArgs = args[1..$]; |
| 177 | + runTests(); |
| 178 | + return exitCode; |
| 179 | +} |
| 180 | + |
| 181 | +// Run Tests /////////////////////////////////////////////// |
| 182 | + |
| 183 | +void runTests() |
| 184 | +{ |
| 185 | + //writeln("unitThreadedArgs: ", unitThreadedArgs); |
| 186 | + |
| 187 | + // GDC doesn't autocreate the dir (and git doesn't beleive in empty dirs) |
| 188 | + tryMkdir("bin"); |
| 189 | + |
| 190 | + final switch(mode) |
| 191 | + { |
| 192 | + case Mode.phobos: runPhobosTests(); break; |
| 193 | + case Mode.vibe: runVibeTests(); break; |
| 194 | + case Mode.combined: runCombinedTests(); break; |
| 195 | + case Mode.travis: runTravisTests(); break; |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +void runPhobosTests() |
| 200 | +{ |
| 201 | + // Setup compilers |
| 202 | + auto rdmd = findRdmd(); |
| 203 | + auto dmd = environment.get("DMD", "dmd"); |
| 204 | + |
| 205 | + writeln("Using:"); |
| 206 | + writeln(" RDMD=", rdmd); |
| 207 | + writeln(" DMD=", dmd); |
| 208 | + stdout.flush(); |
| 209 | + |
| 210 | + // Setup --core |
| 211 | + auto debugTestsId = coreTestsOnly? "MYSQLN_CORE_TESTS" : "MYSQLN_TESTS"; |
| 212 | + auto msgSuffix = coreTestsOnly? " (core tests only)" : ""; |
| 213 | + |
| 214 | + // Setup unit-threaded |
| 215 | + string dFile; |
| 216 | + string utSuffix; |
| 217 | + string utArgs; |
| 218 | + if(useUnitThreaded) |
| 219 | + { |
| 220 | + msgSuffix = msgSuffix~" (unit-threaded)"; |
| 221 | + auto utVer="0.7.45"; |
| 222 | + utSuffix="-ut"; |
| 223 | + utArgs="-version=MYSQLN_TESTS_NO_MAIN -version=Have_unit_threaded -Iunit-threaded-"~utVer~"/unit-threaded/source/ --extra-file=unit-threaded-"~utVer~"/unit-threaded/libunit-threaded.a --exclude=unit_threaded"; |
| 224 | + dFile = "bin/ut.d"; |
| 225 | + |
| 226 | + // Setup local unit-threaded |
| 227 | + run("dub fetch unit-threaded --version="~utVer~" --cache=local"); |
| 228 | + chdir(("unit-threaded-"~utVer~"/unit-threaded").fixSlashes); |
| 229 | + run("dub build -c gen_ut_main"); |
| 230 | + run("dub build -c library"); |
| 231 | + chdir(("../..").fixSlashes); |
| 232 | + run(("unit-threaded-"~utVer~"/unit-threaded/gen_ut_main -f bin/ut.d").fixSlashes); |
| 233 | + } |
| 234 | + else |
| 235 | + { |
| 236 | + dFile = "source/mysql/package.d"; |
| 237 | + } |
| 238 | + |
| 239 | + // Compile tests |
| 240 | + writeln("Compiling Phobos-socket tests", msgSuffix, "..."); |
| 241 | + auto status = run(rdmd~" --compiler="~dmd~" --build-only -g -unittest "~utArgs~" -debug="~debugTestsId~" -ofbin/mysqln-tests-phobos"~utSuffix~" -Isource "~dFile); |
| 242 | + //$RDMD --compiler=$DMD --build-only -g -unittest $UT_ARGS -debug=$DEBUG_TESTS_ID -ofbin/mysqln-tests-phobos${UT_SUFFIX} -Isource $D_FILE |
| 243 | + if(status != 0) |
| 244 | + { |
| 245 | + exitCode = status; |
| 246 | + return; |
| 247 | + } |
| 248 | + |
| 249 | + writeln("Running Phobos-socket tests", msgSuffix, "..."); |
| 250 | + status = run(["bin/mysqln-tests-phobos"~utSuffix, "-t"] ~ unitThreadedArgs); |
| 251 | + // bin/mysqln-tests-phobos${UT_SUFFIX} -t "$@" |
| 252 | + if(status != 0) |
| 253 | + { |
| 254 | + exitCode = status; |
| 255 | + return; |
| 256 | + } |
| 257 | +} |
| 258 | + |
| 259 | +void runVibeTests() |
| 260 | +{ |
| 261 | + auto coreMsg = coreTestsOnly? " (core tests only)" : ""; |
| 262 | + writeln("Doing Vibe-socket tests", coreMsg, "..."); |
| 263 | + |
| 264 | + if(useUnitThreaded) |
| 265 | + exitCode = run(["dub", "run", "-c", "unittest-vibe-ut", "--", "-t"] ~ unitThreadedArgs); |
| 266 | + else |
| 267 | + exitCode = run("dub test -c unittest-vibe"); |
| 268 | +} |
| 269 | + |
| 270 | +void runCombinedTests() |
| 271 | +{ |
| 272 | + auto phobosStatus = runFromCurrentDir( |
| 273 | + "run_tests"~ |
| 274 | + flagMode(Mode.phobos)~ |
| 275 | + flagCoreTestsOnly(true)~ |
| 276 | + flagUseUnitThreaded(useUnitThreaded) |
| 277 | + ); |
| 278 | + if(phobosStatus != 0) |
| 279 | + { |
| 280 | + exitCode = phobosStatus; |
| 281 | + return; |
| 282 | + } |
| 283 | + |
| 284 | + auto vibeStatus = runFromCurrentDir( |
| 285 | + "run_tests"~ |
| 286 | + flagMode(Mode.vibe)~ |
| 287 | + flagCoreTestsOnly(false)~ |
| 288 | + flagUseUnitThreaded(useUnitThreaded) |
| 289 | + ); |
| 290 | + exitCode = vibeStatus; |
| 291 | +} |
| 292 | + |
| 293 | +void runTravisTests() |
| 294 | +{ |
| 295 | + useUnitThreaded = envBool("USE_UNIT_THREADED"); |
| 296 | + auto noVibe = envBool("NO_VIBE"); |
| 297 | + |
| 298 | + if(noVibe) |
| 299 | + { |
| 300 | + auto phobosStatus = runFromCurrentDir( |
| 301 | + "run_tests"~ |
| 302 | + flagMode(Mode.phobos)~ |
| 303 | + flagCoreTestsOnly(false)~ |
| 304 | + flagUseUnitThreaded(useUnitThreaded) |
| 305 | + ); |
| 306 | + exitCode = phobosStatus; |
| 307 | + } |
| 308 | + else |
| 309 | + { |
| 310 | + runCombinedTests(); |
| 311 | + } |
| 312 | +} |
0 commit comments