Skip to content

Commit

Permalink
Support arbitrary tagging and selection of testcases.
Browse files Browse the repository at this point in the history
A testcase can optionally have a list of tags associated with it.
Srunner can be run with an optional include list of tags and an optional
exclude list of tags. These will have the effect of filtering testcases
that would otherwise be run.
  • Loading branch information
Crispin Dent-Young committed Jun 6, 2016
1 parent 6cadd63 commit ae15d29
Show file tree
Hide file tree
Showing 10 changed files with 428 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/check.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ static void suite_free(Suite * s)
free(s);
}


TCase *tcase_create(const char *name)
{
char *env;
Expand Down Expand Up @@ -149,7 +150,19 @@ TCase *tcase_create(const char *name)
tc->ch_sflst = check_list_create();
tc->unch_tflst = check_list_create();
tc->ch_tflst = check_list_create();
tc->tags = NULL;

return tc;
}

TCase *tcase_create_tagged(const char *name, const char *tags)
{
TCase *tc;

tc = tcase_create(name);
if ((tc != NULL) && (tags != NULL)) {
tc->tags = strdup(tags);
}
return tc;
}

Expand All @@ -166,7 +179,10 @@ static void tcase_free(TCase * tc)
check_list_free(tc->ch_sflst);
check_list_free(tc->unch_tflst);
check_list_free(tc->ch_tflst);

if (tc->tags) {
free(tc->tags);
tc->tags = NULL;
}
free(tc);
}

Expand Down
18 changes: 18 additions & 0 deletions src/check.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,23 @@ CK_DLL_EXP void CK_EXPORT suite_add_tcase(Suite * s, TCase * tc);
* */
CK_DLL_EXP TCase *CK_EXPORT tcase_create(const char *name);

/**
* Create a test case associated with certain tags.
*
* Once created, tests can be added with the tcase_add_test()
* function, and the test case assigned to a suite with the
* suite_add_tcase() function.
*
* @param name name of the test case
* @param tags string containing arbitrary tags separated by spaces
*
* @return test case containing no tests
*
* @since 0.11.0
* */
CK_DLL_EXP TCase *CK_EXPORT tcase_create_tagged(const char *name,
const char *tags);

/**
* Add a test function to a test case
*
Expand Down Expand Up @@ -980,6 +997,7 @@ CK_DLL_EXP void CK_EXPORT srunner_run_all(SRunner * sr,
*/
CK_DLL_EXP void CK_EXPORT srunner_run(SRunner * sr, const char *sname,
const char *tcname,
const char *inc_tags, const char *exc_tags,
enum print_output print_mode);


Expand Down
1 change: 1 addition & 0 deletions src/check_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct TCase
List *unch_tflst;
List *ch_sflst;
List *ch_tflst;
char *tags;
};

typedef struct TestStats
Expand Down
62 changes: 60 additions & 2 deletions src/check_run.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ static void srunner_run_init(SRunner * sr, enum print_output print_mode);
static void srunner_run_end(SRunner * sr, enum print_output print_mode);
static void srunner_iterate_suites(SRunner * sr,
const char *sname, const char *tcname,
const char *inc_tags, const char *exc_tags,
enum print_output print_mode);
static void srunner_iterate_tcase_tfuns(SRunner * sr, TCase * tc);
static void srunner_add_failure(SRunner * sr, TestResult * tf);
Expand Down Expand Up @@ -158,8 +159,50 @@ static void srunner_run_end(SRunner * sr,
set_fork_status(CK_FORK);
}

/*
* Helper func to compare two lists of tags and return true if there is a
* common tag.
*/
static unsigned int matching_tag(const char *tags1_orig, const char *tags2_orig)
{

char *saveptr1;
char *saveptr2;
char *tags1;
char *tag1;


if ((tags1_orig == NULL) || (tags2_orig == NULL))
return 0;

tags1 = strdup(tags1_orig);
tag1 = strtok_r(tags1, " ", &saveptr1);
while (tag1) {

char *tags2, *tag2;

tags2 = strdup(tags2_orig);
tag2 = strtok_r(tags2, " ", &saveptr2);
while (tag2) {
if (strcmp(tag1, tag2) == 0) {

free(tags2);
free(tags1);
return 1;
}

tag2 = strtok_r(NULL, " ", &saveptr2);
}
free(tags2);
tag1 = strtok_r(NULL, " ", &saveptr1);
}
free(tags1);
return 0;
}

static void srunner_iterate_suites(SRunner * sr,
const char *sname, const char *tcname,
const char *inc_tags, const char *exc_tags,
enum print_output CK_ATTRIBUTE_UNUSED
print_mode)
{
Expand Down Expand Up @@ -191,6 +234,14 @@ static void srunner_iterate_suites(SRunner * sr,
{
continue;
}
if (inc_tags != NULL) {
if (!matching_tag(inc_tags, tc->tags))
continue;
}
if (exc_tags != NULL) {
if (matching_tag(inc_tags, tc->tags))
continue;
}

srunner_run_tcase(sr, tc);
}
Expand Down Expand Up @@ -738,10 +789,13 @@ void srunner_run_all(SRunner * sr, enum print_output print_mode)
{
srunner_run(sr, NULL, /* All test suites. */
NULL, /* All test cases. */
NULL, /* Include all tags */
NULL, /* Exclude no tags */
print_mode);
}

void srunner_run(SRunner * sr, const char *sname, const char *tcname,
const char *inc_tags, const char *exc_tags,
enum print_output print_mode)
{
#if defined(HAVE_SIGACTION) && defined(HAVE_FORK)
Expand All @@ -756,7 +810,11 @@ void srunner_run(SRunner * sr, const char *sname, const char *tcname,
if(!tcname)
tcname = getenv("CK_RUN_CASE");
if(!sname)
sname = getenv("CK_RUN_SUITE");
sname = getenv("CK_RUN_SUITE");
if(!inc_tags)
inc_tags = getenv("CK_INC_TAGS");
if(!exc_tags)
exc_tags = getenv("CK_EXC_TAGS");

if(sr == NULL)
return;
Expand All @@ -779,7 +837,7 @@ void srunner_run(SRunner * sr, const char *sname, const char *tcname,
sigaction(SIGTERM, &sigterm_new_action, &sigterm_old_action);
#endif /* HAVE_SIGACTION && HAVE_FORK */
srunner_run_init(sr, print_mode);
srunner_iterate_suites(sr, sname, tcname, print_mode);
srunner_iterate_suites(sr, sname, tcname, inc_tags, exc_tags, print_mode);
srunner_run_end(sr, print_mode);
#if defined(HAVE_SIGACTION) && defined(HAVE_FORK)
sigaction(SIGALRM, &sigalarm_old_action, NULL);
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ set(CHECK_CHECK_SOURCES
check_check_pack.c
check_check_selective.c
check_check_sub.c
check_check_tags.c
check_list.c)
set(CHECK_CHECK_HEADERS check_check.h)
add_executable(check_check ${CHECK_CHECK_HEADERS} ${CHECK_CHECK_SOURCES})
Expand Down
3 changes: 3 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ check_check_export_SOURCES = \
check_check_master.c \
check_check_log.c \
check_check_fork.c \
check_check_tags.c \
check_check_export_main.c
check_check_export_LDADD = $(top_builddir)/src/libcheck.la $(top_builddir)/lib/libcompat.la

Expand All @@ -58,6 +59,7 @@ check_check_SOURCES = \
check_check_pack.c \
check_check_exit.c \
check_check_selective.c \
check_check_tags.c \
check_check_main.c
check_check_LDADD = $(top_builddir)/src/libcheckinternal.la $(top_builddir)/lib/libcompat.la

Expand All @@ -67,6 +69,7 @@ check_mem_leaks_SOURCES = \
check_check_fork.c \
check_check_exit.c \
check_check_selective.c \
check_check_tags.c \
check_check_sub.c \
check_check_master.c
check_mem_leaks_LDADD = $(top_builddir)/src/libcheck.la $(top_builddir)/lib/libcompat.la
Expand Down
1 change: 1 addition & 0 deletions tests/check_check.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Suite *make_fixture_suite(void);
Suite *make_pack_suite(void);
Suite *make_exit_suite(void);
Suite *make_selective_suite(void);
Suite *make_tag_suite(void);

extern int master_tests_lineno[];
void init_master_tests_lineno(int num_master_tests);
Expand Down
1 change: 1 addition & 0 deletions tests/check_check_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ int main (void)
srunner_add_suite(sr, make_fork_suite());
srunner_add_suite(sr, make_fixture_suite());
srunner_add_suite(sr, make_pack_suite());
srunner_add_suite(sr, make_tag_suite());

#if defined(HAVE_FORK) && HAVE_FORK==1
srunner_add_suite(sr, make_exit_suite());
Expand Down
14 changes: 14 additions & 0 deletions tests/check_check_selective.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ START_TEST(test_srunner_run_run_all)
srunner_run (sr,
NULL, /* NULL tsuite name. */
NULL, /* NULL tcase name. */
NULL, /* include any tags */
NULL, /* exclude no tags */
CK_VERBOSE);

ck_assert_msg (srunner_ntests_run(sr) == 3,
Expand All @@ -116,6 +118,8 @@ START_TEST(test_srunner_run_suite)
srunner_run (sr,
"suite1",
NULL, /* NULL tcase name. */
NULL, /* include any tags */
NULL, /* exclude no tags */
CK_VERBOSE);

ck_assert_msg (test_tc11_executed
Expand All @@ -134,6 +138,8 @@ START_TEST(test_srunner_run_no_suite)
srunner_run (sr,
"non-existing-suite",
NULL, /* NULL tcase name. */
NULL, /* include any tags */
NULL, /* exclude no tags */
CK_VERBOSE);

ck_assert_msg (!(test_tc11_executed
Expand All @@ -152,6 +158,8 @@ START_TEST(test_srunner_run_tcase)
srunner_run (sr,
NULL, /* NULL suite name. */
"tcase12",
NULL, /* include any tags */
NULL, /* exclude no tags */
CK_VERBOSE);

ck_assert_msg (!test_tc11_executed
Expand All @@ -170,6 +178,8 @@ START_TEST(test_srunner_no_tcase)
srunner_run (sr,
NULL, /* NULL suite name. */
"non-existant-test-case",
NULL, /* include any tags */
NULL, /* exclude no tags */
CK_VERBOSE);

ck_assert_msg (!(test_tc11_executed
Expand All @@ -188,6 +198,8 @@ START_TEST(test_srunner_suite_tcase)
srunner_run (sr,
"suite2",
"tcase21",
NULL, /* include any tags */
NULL, /* exclude no tags */
CK_VERBOSE);

ck_assert_msg (!test_tc11_executed
Expand All @@ -206,6 +218,8 @@ START_TEST(test_srunner_suite_no_tcase)
srunner_run (sr,
"suite1",
"non-existant-test-case",
NULL, /* include any tags */
NULL, /* exclude no tags */
CK_VERBOSE);

ck_assert_msg (!(test_tc11_executed
Expand Down
Loading

0 comments on commit ae15d29

Please sign in to comment.