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..3820148c 100644 --- a/docs/UnityHelperScriptsGuide.md +++ b/docs/UnityHelperScriptsGuide.md @@ -231,6 +231,9 @@ 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. + ##### `:setup_name` Override the default test `setUp` function name. @@ -377,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. @@ -448,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: 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;