Skip to content

Commit e783413

Browse files
committed
[Core] Add support for TESTBRIDGE_TEST_ONLY in UnityParseOptions
Allow Bazel --test_filter to select tests when UNITY_USE_COMMAND_LINE_ARGS is enabled. Remarks: Avoid to include <stdlib.h>. Instead used extern ... . Maybe stdlib.h is more compatible.
1 parent 51d2db9 commit e783413

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/unity.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,10 +2402,22 @@ int UnityStrictMatch = 0;
24022402
int UnityParseOptions(int argc, char** argv)
24032403
{
24042404
int i;
2405+
const char* testbridge_filter = NULL;
24052406
UnityOptionIncludeNamed = NULL;
24062407
UnityOptionExcludeNamed = NULL;
24072408
UnityStrictMatch = 0;
24082409

2410+
#ifndef UNITY_GETENV
2411+
/*Allow Bazel test filtering via TESTBRIDGE_TEST_ONLY*/
2412+
extern char* getenv(const char* name);
2413+
#define UNITY_GETENV(name) getenv(name)
2414+
#endif
2415+
testbridge_filter = UNITY_GETENV("TESTBRIDGE_TEST_ONLY");
2416+
if (testbridge_filter && testbridge_filter[0] != 0)
2417+
{
2418+
UnityOptionIncludeNamed = (char*)testbridge_filter;
2419+
}
2420+
24092421
for (i = 1; i < argc; i++)
24102422
{
24112423
if (argv[i][0] == '-')

test/tests/test_unity_core.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
========================================================================= */
77

88
#include "unity.h"
9+
#ifdef UNITY_USE_COMMAND_LINE_ARGS
10+
#include "unity_internals.h"
11+
#include <stdlib.h>
12+
#endif
913
#define TEST_INSTANCES
1014
#include "self_assessment_utils.h"
1115

@@ -194,6 +198,67 @@ void testFail(void)
194198
VERIFY_FAILS_END
195199
}
196200

201+
#ifdef UNITY_USE_COMMAND_LINE_ARGS
202+
static void UnitySetTestbridgeFilter(const char* value)
203+
{
204+
#if defined(_WIN32) || defined(_MSC_VER)
205+
if (value)
206+
{
207+
_putenv_s("TESTBRIDGE_TEST_ONLY", value);
208+
}
209+
else
210+
{
211+
_putenv_s("TESTBRIDGE_TEST_ONLY", "");
212+
}
213+
#else
214+
if (value)
215+
{
216+
setenv("TESTBRIDGE_TEST_ONLY", value, 1);
217+
}
218+
else
219+
{
220+
unsetenv("TESTBRIDGE_TEST_ONLY");
221+
}
222+
#endif
223+
}
224+
225+
static void UnitySetTestContext(const char* testfile, const char* testname)
226+
{
227+
Unity.TestFile = testfile;
228+
Unity.CurrentTestName = testname;
229+
}
230+
231+
void testUnityParseOptionsUsesTestbridgeFilter(void)
232+
{
233+
char* argv[] = { (char*)"prog", NULL };
234+
235+
UnitySetTestbridgeFilter("test_my_function");
236+
UnityParseOptions(1, argv);
237+
238+
UnitySetTestContext("file.c", "test_my_function");
239+
TEST_ASSERT_TRUE(UnityTestMatches());
240+
UnitySetTestContext("file.c", "other");
241+
TEST_ASSERT_FALSE(UnityTestMatches());
242+
243+
UnitySetTestbridgeFilter(NULL);
244+
}
245+
246+
void testUnityParseOptionsArgsOverrideTestbridgeFilter(void)
247+
{
248+
char* argv[] = { (char*)"prog", (char*)"-n", (char*)"other", NULL };
249+
250+
UnitySetTestbridgeFilter("test_my_function");
251+
UnityParseOptions(3, argv);
252+
253+
UnitySetTestContext("file.c", "other");
254+
TEST_ASSERT_TRUE(UnityTestMatches());
255+
UnitySetTestContext("file.c", "test_my_function");
256+
TEST_ASSERT_FALSE(UnityTestMatches());
257+
258+
UnitySetTestbridgeFilter(NULL);
259+
}
260+
#endif
261+
197262
void testIsNull(void)
198263
{
199264
char* ptr1 = NULL;

0 commit comments

Comments
 (0)