From e783413e1781548a4500450225ce307b23fa416a Mon Sep 17 00:00:00 2001 From: tragisch Date: Fri, 26 Dec 2025 12:55:38 +0100 Subject: [PATCH 1/3] [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 . Instead used extern ... . Maybe stdlib.h is more compatible. --- src/unity.c | 12 +++++++ test/tests/test_unity_core.c | 65 ++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/src/unity.c b/src/unity.c index 8be0d033..c13850bc 100644 --- a/src/unity.c +++ b/src/unity.c @@ -2402,10 +2402,22 @@ int UnityStrictMatch = 0; int UnityParseOptions(int argc, char** argv) { int i; + const char* testbridge_filter = NULL; UnityOptionIncludeNamed = NULL; UnityOptionExcludeNamed = NULL; UnityStrictMatch = 0; +#ifndef UNITY_GETENV +/*Allow Bazel test filtering via TESTBRIDGE_TEST_ONLY*/ +extern char* getenv(const char* name); +#define UNITY_GETENV(name) getenv(name) +#endif + testbridge_filter = UNITY_GETENV("TESTBRIDGE_TEST_ONLY"); + if (testbridge_filter && testbridge_filter[0] != 0) + { + UnityOptionIncludeNamed = (char*)testbridge_filter; + } + for (i = 1; i < argc; i++) { if (argv[i][0] == '-') diff --git a/test/tests/test_unity_core.c b/test/tests/test_unity_core.c index 1f5a9a76..dc5c70a1 100644 --- a/test/tests/test_unity_core.c +++ b/test/tests/test_unity_core.c @@ -6,6 +6,10 @@ ========================================================================= */ #include "unity.h" +#ifdef UNITY_USE_COMMAND_LINE_ARGS +#include "unity_internals.h" +#include +#endif #define TEST_INSTANCES #include "self_assessment_utils.h" @@ -194,6 +198,67 @@ void testFail(void) VERIFY_FAILS_END } +#ifdef UNITY_USE_COMMAND_LINE_ARGS +static void UnitySetTestbridgeFilter(const char* value) +{ +#if defined(_WIN32) || defined(_MSC_VER) + if (value) + { + _putenv_s("TESTBRIDGE_TEST_ONLY", value); + } + else + { + _putenv_s("TESTBRIDGE_TEST_ONLY", ""); + } +#else + if (value) + { + setenv("TESTBRIDGE_TEST_ONLY", value, 1); + } + else + { + unsetenv("TESTBRIDGE_TEST_ONLY"); + } +#endif +} + +static void UnitySetTestContext(const char* testfile, const char* testname) +{ + Unity.TestFile = testfile; + Unity.CurrentTestName = testname; +} + +void testUnityParseOptionsUsesTestbridgeFilter(void) +{ + char* argv[] = { (char*)"prog", NULL }; + + UnitySetTestbridgeFilter("test_my_function"); + UnityParseOptions(1, argv); + + UnitySetTestContext("file.c", "test_my_function"); + TEST_ASSERT_TRUE(UnityTestMatches()); + UnitySetTestContext("file.c", "other"); + TEST_ASSERT_FALSE(UnityTestMatches()); + + UnitySetTestbridgeFilter(NULL); +} + +void testUnityParseOptionsArgsOverrideTestbridgeFilter(void) +{ + char* argv[] = { (char*)"prog", (char*)"-n", (char*)"other", NULL }; + + UnitySetTestbridgeFilter("test_my_function"); + UnityParseOptions(3, argv); + + UnitySetTestContext("file.c", "other"); + TEST_ASSERT_TRUE(UnityTestMatches()); + UnitySetTestContext("file.c", "test_my_function"); + TEST_ASSERT_FALSE(UnityTestMatches()); + + UnitySetTestbridgeFilter(NULL); +} +#endif + void testIsNull(void) { char* ptr1 = NULL; From 8f624aa94fcb0c69fb819e7663bde6c94a4b8d11 Mon Sep 17 00:00:00 2001 From: tragisch Date: Fri, 26 Dec 2025 12:56:15 +0100 Subject: [PATCH 2/3] [Docs] Document Bazel test_filter support Explain that Unity honors TESTBRIDGE_TEST_ONLY when UNITY_USE_COMMAND_LINE_ARGS is enabled and that UNITY_GETENV can be overridden to avoid . --- docs/UnityChangeLog.md | 1 + docs/UnityHelperScriptsGuide.md | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/docs/UnityChangeLog.md b/docs/UnityChangeLog.md index a9fec00f..a3f29aca 100644 --- a/docs/UnityChangeLog.md +++ b/docs/UnityChangeLog.md @@ -18,6 +18,7 @@ Prior to 2008, the project was an internal project and not released to the publi New Features: - Add `-n` comand line option as strict matcher again + - Support `TESTBRIDGE_TEST_ONLY` for Bazel `--test_filter` when `UNITY_USE_COMMAND_LINE_ARGS` is enabled Significant Bugfixes: diff --git a/docs/UnityHelperScriptsGuide.md b/docs/UnityHelperScriptsGuide.md index 30841cf9..3ea0c5db 100644 --- a/docs/UnityHelperScriptsGuide.md +++ b/docs/UnityHelperScriptsGuide.md @@ -231,6 +231,11 @@ These are the available options: | `-v` | increase Verbosity | | `-x NAME` | eXclude tests whose name includes NAME | +Unity also supports the `TESTBRIDGE_TEST_ONLY` environment variable (used by +Bazel's `--test_filter`) when `UNITY_USE_COMMAND_LINE_ARGS` is enabled. +If you prefer to avoid including ``, define `UNITY_GETENV` to your +own `getenv`-compatible function (for example, via `unity_config.h`). + ##### `:setup_name` Override the default test `setUp` function name. From 4011d05169b3eb5412a8de0efbfcaccc3c6ccb3c Mon Sep 17 00:00:00 2001 From: tragisch Date: Fri, 26 Dec 2025 13:45:43 +0100 Subject: [PATCH 3/3] [Docs] Simplify Bazel Description for UnityHelperScriptsGuide.md --- docs/UnityHelperScriptsGuide.md | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/docs/UnityHelperScriptsGuide.md b/docs/UnityHelperScriptsGuide.md index 3ea0c5db..3820148c 100644 --- a/docs/UnityHelperScriptsGuide.md +++ b/docs/UnityHelperScriptsGuide.md @@ -233,8 +233,6 @@ These are the available options: Unity also supports the `TESTBRIDGE_TEST_ONLY` environment variable (used by Bazel's `--test_filter`) when `UNITY_USE_COMMAND_LINE_ARGS` is enabled. -If you prefer to avoid including ``, define `UNITY_GETENV` to your -own `getenv`-compatible function (for example, via `unity_config.h`). ##### `:setup_name` @@ -382,11 +380,11 @@ tests/test_unity_parameterizedDemo.c:14:test_demoParamFunction(4, 6, 30):PASS As we can see: -| Parameter | Format | Possible values | Total of values | Format number | -|---|---|---|---|---| -| `a` | `[3, 4, 1]` | `3`, `4` | 2 | Format 1 | -| `b` | `[10, 5, -2]` | `10`, `8`, `6` | 3 | Format 1, negative step, end number is not included | -| `c` | `<30, 31, 1>` | `30` | 1 | Format 2 | +| Parameter | Format | Possible values | Total of values | Format number | +| --------- | ------------- | --------------- | --------------- | --------------------------------------------------- | +| `a` | `[3, 4, 1]` | `3`, `4` | 2 | Format 1 | +| `b` | `[10, 5, -2]` | `10`, `8`, `6` | 3 | Format 1, negative step, end number is not included | +| `c` | `<30, 31, 1>` | `30` | 1 | Format 2 | _Note_, that format 2 also supports negative step. @@ -453,11 +451,11 @@ tests/test_unity_parameterizedDemo.c:18:test_demoParamFunction(7, 1, 20.0f):PASS As we can see: -| Parameter | Format | Count of values | -|---|---|---| -| `a` | `[3, 4, 7]` | 3 | -| `b` | `[10, 8, 2, 1]` | 4 | -| `c` | `[30u, 20.0f]` | 2 | +| Parameter | Format | Count of values | +| --------- | --------------- | --------------- | +| `a` | `[3, 4, 7]` | 3 | +| `b` | `[10, 8, 2, 1]` | 4 | +| `c` | `[30u, 20.0f]` | 2 | We totally have 3 * 4 * 2 = 24 equal test cases, that can be written as following: