diff --git a/dbtests/dbtests.cpp b/dbtests/dbtests.cpp index 29260dde5ce..9a859baf46d 100644 --- a/dbtests/dbtests.cpp +++ b/dbtests/dbtests.cpp @@ -19,6 +19,8 @@ #include "stdafx.h" +#include + #include "../db/instance.h" #include "../util/file_allocator.h" @@ -29,62 +31,71 @@ #include "dbtests.h" using namespace std; +namespace po = boost::program_options; namespace mongo { extern string dbpath; } -string dbpathSpec = "/tmp/unittest/"; - -void usage() { - string instructions = - "dbtests usage:\n" - " -help show this message\n" - " -dbpath configure db data path for this test run\n" - " (default is /tmp/unittest/)\n" - " -debug run tests with verbose output\n" - " -list list available test suites\n" - " -seed random number seed\n" - " run the specified test suite only"; - out() << instructions << endl; +void show_help_text(const char* name, po::options_description options) { + cout << "usage: " << name << " [options] [suite]..." << endl + << options << "suite: run the specified test suite(s) only" << endl; } int main( int argc, char** argv ) { - unsigned long long seed = time( 0 ); - - int offset = 0; - for ( int i = 1; i < argc; ++i ) { - if ( argv[ i ] == string( "-dbpath" ) ) { - if ( i == argc - 1 ) { - usage(); - dbexit( EXIT_BADOPTIONS ); - } - dbpathSpec = argv[ ++i ]; - offset += 2; - } else if ( argv[ i ] == string( "-seed" ) ) { - if ( i == argc - 1 ) { - usage(); - dbexit( EXIT_BADOPTIONS ); - } - // Don't bother checking for conversion error - seed = strtoll( argv[ ++i ], 0, 10 ); - offset += 2; - } else if ( argv[ i ] == string( "-help" ) ) { - usage(); - dbexit( EXIT_CLEAN ); - } else if ( offset ) { - argv[ i - offset ] = argv[ i ]; - } + string dbpathSpec; + + po::options_description shell_options("options"); + po::options_description hidden_options("Hidden options"); + po::options_description cmdline_options("Command line options"); + po::positional_options_description positional_options; + + shell_options.add_options() + ("help,h", "show this usage information") + ("dbpath", po::value(&dbpathSpec)->default_value("/tmp/unittest/"), + "db data path for this test run") + ("debug", "run tests with verbose output") + ("list,l", "list available test suites") + ("seed", po::value(&seed), "random number seed") + ; + + hidden_options.add_options() + ("suites", po::value< vector >(), "test suites to run") + ; + + positional_options.add("suites", -1); + + cmdline_options.add(shell_options).add(hidden_options); + + po::variables_map params; + int command_line_style = (((po::command_line_style::unix_style ^ + po::command_line_style::allow_guessing) | + po::command_line_style::allow_long_disguise) ^ + po::command_line_style::allow_sticky); + + try { + po::store(po::command_line_parser(argc, argv).options(cmdline_options). + positional(positional_options). + style(command_line_style).run(), params); + po::notify(params); + } catch (po::error &e) { + cout << "ERROR: " << e.what() << endl << endl; + show_help_text(argv[0], shell_options); + return EXIT_BADOPTIONS; + } + + if (params.count("help")) { + show_help_text(argv[0], shell_options); + return EXIT_CLEAN; } - argc -= offset; if ( dbpathSpec[ dbpathSpec.length() - 1 ] != '/' ) dbpathSpec += "/"; dbpath = dbpathSpec.c_str(); acquirePathLock(); - + srand( seed ); printGitVersion(); printSysInfo(); @@ -92,13 +103,18 @@ int main( int argc, char** argv ) { theFileAllocator().start(); - + vector suites; + if (params.count("suites")) { + suites = params["suites"].as< vector >(); + } + + int ret = mongo::regression::Suite::run(params.count("debug"), + params.count("list"), + suites); - int ret = mongo::regression::Suite::run( argc, argv ); - #if !defined(_WIN32) && !defined(__sunos__) flock( lockFile, LOCK_UN ); -#endif +#endif dbexit( (ExitCode)ret ); // so everything shuts down cleanly return ret; diff --git a/dbtests/framework.cpp b/dbtests/framework.cpp index 24666f8a983..70eab1744f7 100644 --- a/dbtests/framework.cpp +++ b/dbtests/framework.cpp @@ -90,31 +90,24 @@ namespace mongo { return r; } - int Suite::run( int argc , char ** argv ){ - list torun; - - for ( int i=1; i::iterator i=_suites->begin() ; i!=_suites->end(); i++ ) - cout << i->first << endl; - return 0; - } - - if ( s == "-debug" ){ - logLevel = 1; - continue; - } - - torun.push_back( s ); - if ( _suites->find( s ) == _suites->end() ){ - cout << "invalid test [" << s << "] use -list to see valid names" << endl; + int Suite::run( bool debug , bool list_suites, vector suites ){ + if ( list_suites ) { + for ( map::iterator i = _suites->begin() ; i != _suites->end(); i++ ) + cout << i->first << endl; + return 0; + } + if ( debug ) { + logLevel = 1; + } + for ( unsigned int i = 0; i < suites.size(); i++ ) { + if ( _suites->find( suites[i] ) == _suites->end() ) { + cout << "invalid test [" << suites[i] << "], use --list to see valid names" << endl; return -1; } } + list torun(suites.begin(), suites.end()); + if ( torun.size() == 0 ) for ( map::iterator i=_suites->begin() ; i!=_suites->end(); i++ ) torun.push_back( i->first ); diff --git a/dbtests/framework.h b/dbtests/framework.h index 5e119b92bc7..6e7afbd8d08 100644 --- a/dbtests/framework.h +++ b/dbtests/framework.h @@ -114,6 +114,7 @@ namespace mongo { Result * run(); + static int run( bool debug , bool list, vector suites ); static int run( int argc , char ** argv );