Skip to content

Commit e252442

Browse files
committed
support --skip, --after, and multiple -l in test-libmongoc
1 parent 68d0948 commit e252442

File tree

2 files changed

+102
-58
lines changed

2 files changed

+102
-58
lines changed

src/libmongoc/tests/TestSuite.c

Lines changed: 98 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ TestSuite_Init (TestSuite *suite, const char *name, int argc, char **argv)
137137
suite->flags = 0;
138138
suite->prgname = bson_strdup (argv[0]);
139139
suite->silent = false;
140+
_mongoc_array_init (&suite->match_patterns, sizeof (char *));
141+
_mongoc_array_init (&suite->skip_patterns, sizeof (char *));
140142

141143
for (i = 1; i < argc; i++) {
142144
if (0 == strcmp ("-d", argv[i])) {
@@ -177,10 +179,24 @@ TestSuite_Init (TestSuite *suite, const char *name, int argc, char **argv)
177179
(0 == strcmp ("--silent", argv[i]))) {
178180
suite->silent = true;
179181
} else if (0 == strcmp ("-l", argv[i])) {
182+
char *val;
180183
if (argc - 1 == i) {
181184
test_error ("-l requires an argument.");
182185
}
183-
suite->testname = bson_strdup (argv[++i]);
186+
val = bson_strdup (argv[++i]);
187+
_mongoc_array_append_val (&suite->match_patterns, val);
188+
} else if (0 == strcmp ("--skip", argv[i])) {
189+
char *val;
190+
if (argc - 1 == i) {
191+
test_error ("--skip requires an argument.");
192+
}
193+
val = bson_strdup (argv[++i]);
194+
_mongoc_array_append_val (&suite->skip_patterns, val);
195+
} else if (0 == strcmp ("--after", argv[i])) {
196+
if (argc - 1 == i) {
197+
test_error ("--after requires an argument.");
198+
}
199+
suite->after = bson_strdup (argv[++i]);
184200
} else {
185201
test_error ("Unknown option: %s\n"
186202
"Try using the --help option.",
@@ -662,21 +678,24 @@ TestSuite_RunTest (TestSuite *suite, /* IN */
662678
static void
663679
TestSuite_PrintHelp (TestSuite *suite) /* IN */
664680
{
665-
printf ("usage: %s [OPTIONS]\n"
666-
"\n"
667-
"Options:\n"
668-
" -h, --help Show this help menu.\n"
669-
" --list-tests Print list of available tests.\n"
670-
" -f, --no-fork Do not spawn a process per test (abort on first "
671-
"error).\n"
672-
" -l NAME Run test by name, e.g. \"/Client/command\" or "
673-
"\"/Client/*\".\n"
674-
" -s, --silent Suppress all output.\n"
675-
" -F FILENAME Write test results (JSON) to FILENAME.\n"
676-
" -d Print debug output (useful if a test hangs).\n"
677-
" -t, --trace Enable mongoc tracing (useful to debug tests).\n"
678-
"\n",
679-
suite->prgname);
681+
printf (
682+
"usage: %s [OPTIONS]\n"
683+
"\n"
684+
"Options:\n"
685+
" -h, --help Show this help menu.\n"
686+
" --list-tests Print list of available tests.\n"
687+
" -f, --no-fork Do not spawn a process per test (abort on first "
688+
"error).\n"
689+
" -l PATTERN Run test by name, e.g. \"/Client/command\" or "
690+
"\"/Client/*\". May be repeated.\n"
691+
" -s, --silent Suppress all output.\n"
692+
" -F FILENAME Write test results (JSON) to FILENAME.\n"
693+
" -d Print debug output (useful if a test hangs).\n"
694+
" -t, --trace Enable mongoc tracing (useful to debug tests).\n"
695+
" --skip PATTERN Skip test by name or pattern. May be repeated.\n"
696+
" --after NAME Start running tests after this test name.\n"
697+
"\n",
698+
suite->prgname);
680699
}
681700

682701

@@ -857,36 +876,6 @@ TestSuite_PrintJsonFooter (FILE *stream) /* IN */
857876
fflush (stream);
858877
}
859878

860-
861-
static int
862-
TestSuite_RunSerial (TestSuite *suite) /* IN */
863-
{
864-
Test *test;
865-
int count = 0;
866-
int status = 0;
867-
868-
for (test = suite->tests; test; test = test->next) {
869-
count++;
870-
}
871-
872-
for (test = suite->tests; test; test = test->next) {
873-
status += TestSuite_RunTest (suite, test, &count);
874-
count--;
875-
}
876-
877-
if (suite->silent) {
878-
return status;
879-
}
880-
881-
TestSuite_PrintJsonFooter (stdout);
882-
if (suite->outfile) {
883-
TestSuite_PrintJsonFooter (suite->outfile);
884-
}
885-
886-
return status;
887-
}
888-
889-
890879
static bool
891880
TestSuite_TestMatchesName (const TestSuite *suite,
892881
const Test *test,
@@ -906,26 +895,70 @@ TestSuite_TestMatchesName (const TestSuite *suite,
906895
}
907896

908897

898+
bool
899+
test_matches (TestSuite *suite, Test *test)
900+
{
901+
int i;
902+
bool matches;
903+
904+
/* If no match patterns were provided, then assume all match unless they are
905+
* skipped. */
906+
if (suite->match_patterns.len == 0) {
907+
matches = true;
908+
} else {
909+
matches = false;
910+
for (i = 0; i < suite->match_patterns.len; i++) {
911+
char *pattern =
912+
_mongoc_array_index (&suite->match_patterns, char *, i);
913+
if (TestSuite_TestMatchesName (suite, test, pattern)) {
914+
matches = true;
915+
break;
916+
}
917+
}
918+
}
919+
920+
/* Remove it if it matches anything in the skips. */
921+
for (i = 0; i < suite->skip_patterns.len; i++) {
922+
char *pattern = _mongoc_array_index (&suite->skip_patterns, char *, i);
923+
if (TestSuite_TestMatchesName (suite, test, pattern)) {
924+
return false;
925+
}
926+
}
927+
928+
return matches;
929+
}
930+
909931
static int
910-
TestSuite_RunNamed (TestSuite *suite, /* IN */
911-
const char *testname) /* IN */
932+
TestSuite_RunAll (TestSuite *suite /* IN */)
912933
{
913934
Test *test;
914935
int count = 0;
915936
int status = 0;
937+
int num_to_skip = 0;
916938

917939
ASSERT (suite);
918-
ASSERT (testname);
919940

920941
/* initialize "count" so we can omit comma after last test output */
921942
for (test = suite->tests; test; test = test->next) {
922-
if (TestSuite_TestMatchesName (suite, test, testname)) {
943+
if (test_matches (suite, test)) {
923944
count++;
924945
}
946+
if (suite->after && 0 == strcmp (test->name, suite->after)) {
947+
num_to_skip = count - 1;
948+
}
949+
}
950+
951+
if (suite->after && num_to_skip == 0) {
952+
test_error ("Error: specified --after \"%s\", but no tests matched", suite->after);
925953
}
926954

927955
for (test = suite->tests; test; test = test->next) {
928-
if (TestSuite_TestMatchesName (suite, test, testname)) {
956+
if (num_to_skip > 0) {
957+
num_to_skip--;
958+
count--;
959+
continue;
960+
}
961+
if (test_matches (suite, test)) {
929962
status += TestSuite_RunTest (suite, test, &count);
930963
count--;
931964
}
@@ -971,11 +1004,7 @@ TestSuite_Run (TestSuite *suite) /* IN */
9711004

9721005
start_us = bson_get_monotonic_time ();
9731006
if (suite->tests) {
974-
if (suite->testname) {
975-
failures += TestSuite_RunNamed (suite, suite->testname);
976-
} else {
977-
failures += TestSuite_RunSerial (suite);
978-
}
1007+
failures += TestSuite_RunAll (suite);
9791008
} else if (!suite->silent) {
9801009
TestSuite_PrintJsonFooter (stdout);
9811010
if (suite->outfile) {
@@ -994,6 +1023,7 @@ TestSuite_Destroy (TestSuite *suite)
9941023
{
9951024
Test *test;
9961025
Test *tmp;
1026+
int i;
9971027

9981028
bson_mutex_lock (&gTestMutex);
9991029
gTestSuite = NULL;
@@ -1019,7 +1049,18 @@ TestSuite_Destroy (TestSuite *suite)
10191049

10201050
free (suite->name);
10211051
free (suite->prgname);
1022-
free (suite->testname);
1052+
for (i = 0; i < suite->skip_patterns.len; i++) {
1053+
char *val = _mongoc_array_index (&suite->skip_patterns, char *, i);
1054+
bson_free (val);
1055+
}
1056+
_mongoc_array_destroy (&suite->skip_patterns);
1057+
for (i = 0; i < suite->match_patterns.len; i++) {
1058+
char *val = _mongoc_array_index (&suite->match_patterns, char *, i);
1059+
bson_free (val);
1060+
}
1061+
1062+
_mongoc_array_destroy (&suite->match_patterns);
1063+
free (suite->after);
10231064
}
10241065

10251066

src/libmongoc/tests/TestSuite.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <math.h>
2525
#include <stdlib.h>
2626

27+
#include "mongoc/mongoc-array-private.h"
2728
#include "mongoc/mongoc-util-private.h"
2829

2930

@@ -660,13 +661,15 @@ struct _Test {
660661
struct _TestSuite {
661662
char *prgname;
662663
char *name;
663-
char *testname;
664+
mongoc_array_t match_patterns;
665+
mongoc_array_t skip_patterns;
664666
Test *tests;
665667
FILE *outfile;
666668
int flags;
667669
int silent;
668670
bson_string_t *mock_server_log_buf;
669671
FILE *mock_server_log;
672+
char *after;
670673
};
671674

672675

0 commit comments

Comments
 (0)