@@ -53,6 +53,7 @@ struct UnitTestRunnerOptions {
5353 int maxTestCases = -1 ;
5454 bool cleanupAfterTests = true ;
5555 bool listTests = false ;
56+ bool simulation = false ;
5657 bool showHelp = false ;
5758};
5859
@@ -71,6 +72,7 @@ enum UnitTestRunnerOption {
7172 OPT_MAX_TEST_CASES,
7273 OPT_NO_CLEANUP,
7374 OPT_LIST,
75+ OPT_SIMULATION,
7476};
7577
7678CSimpleOpt::SOption unitTestRunnerOptions[] = { { OPT_HELP, " -h" , SO_NONE },
@@ -83,6 +85,7 @@ CSimpleOpt::SOption unitTestRunnerOptions[] = { { OPT_HELP, "-h", SO_NONE },
8385 { OPT_MAX_TEST_CASES, " --max-test-cases" , SO_REQ_SEP },
8486 { OPT_NO_CLEANUP, " --no-cleanup" , SO_NONE },
8587 { OPT_LIST, " --list" , SO_NONE },
88+ { OPT_SIMULATION, " --simulation" , SO_NONE },
8689 SO_END_OF_OPTIONS };
8790
8891void printUsage (const char * program, const UnitTestRunnerConfig& config) {
@@ -99,6 +102,7 @@ void printUsage(const char* program, const UnitTestRunnerConfig& config) {
99102 " --max-test-cases N Stop after N matching tests\n "
100103 " --no-cleanup Keep the data directory after each test\n "
101104 " --list Print matching test names without running them\n "
105+ " --simulation Run using Sim2 (when supported by the target)\n "
102106 " -h, --help Show this help\n " ,
103107 program,
104108 config.suiteName (),
@@ -181,6 +185,9 @@ bool parseArgs(int argc, char** argv, UnitTestRunnerOptions* options) {
181185 case OPT_LIST:
182186 options->listTests = true ;
183187 break ;
188+ case OPT_SIMULATION:
189+ options->simulation = true ;
190+ break ;
184191 default :
185192 fmt::print (stderr, " ERROR: Unknown option id {}\n " , args.OptionId ());
186193 return false ;
@@ -335,7 +342,8 @@ Future<Void> stopNetworkAfter(Future<Void> what, std::string_view traceName, int
335342
336343} // namespace
337344
338- UnitTestRunnerConfig::UnitTestRunnerConfig (std::string_view sourceSubDir) : sourceSubDir(sourceSubDir) {}
345+ UnitTestRunnerConfig::UnitTestRunnerConfig (std::string_view sourceSubDir, SimulationInitializer simulationInitializer)
346+ : sourceSubDir(sourceSubDir), simulationInitializer(simulationInitializer) {}
339347
340348std::string_view UnitTestRunnerConfig::suiteName () const {
341349 return sourceSubDir;
@@ -349,6 +357,14 @@ std::string UnitTestRunnerConfig::traceName() const {
349357 return std::string (sourceSubDir) + " _test" ;
350358}
351359
360+ bool UnitTestRunnerConfig::supportsSimulation () const {
361+ return simulationInitializer != nullptr ;
362+ }
363+
364+ void UnitTestRunnerConfig::initializeSimulation () const {
365+ simulationInitializer ();
366+ }
367+
352368int runUnitTests (int argc, char ** argv, const UnitTestRunnerConfig& config) {
353369 platformInit ();
354370 Error::init ();
@@ -365,6 +381,10 @@ int runUnitTests(int argc, char** argv, const UnitTestRunnerConfig& config) {
365381 printUsage (argv[0 ], config);
366382 return 0 ;
367383 }
384+ if (options.simulation && !config.supportsSimulation ()) {
385+ fmt::print (stderr, " ERROR: Simulation mode is not supported by the {} test target\n " , config.suiteName ());
386+ return 1 ;
387+ }
368388
369389 if (options.randomSeed == 0 ) {
370390 options.randomSeed = platform::getRandomSeed ();
@@ -381,7 +401,11 @@ int runUnitTests(int argc, char** argv, const UnitTestRunnerConfig& config) {
381401 return 1 ;
382402 }
383403
384- g_network = newNet2 (TLSConfig ());
404+ if (options.simulation ) {
405+ config.initializeSimulation ();
406+ } else {
407+ g_network = newNet2 (TLSConfig ());
408+ }
385409 openTraceFile ({}, 10 << 20 , 10 << 20 , " ." , traceName);
386410
387411 int exitCode = 0 ;
0 commit comments