This addition reads command line input to run specific single tests
within a larger call to run all the tests for a particular function.
When the user adds a second argument to the command line, the code skips
all the tests that don't match the user's specified binary. If the user
doesn't specify a test correctly and/or no tests are run, a failure
message prints.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
could you run this through clang-format to properly format this? see the clang-format part of https://llvm.org/docs/Contributing.html#how-to-submit-a-patch
to be more specific about the commit title and the error message, we're not picking a specific _binary_, we're picking a specific _test_.
[libc]
Fix formatting issues and adjust the no tests run message to be more specific.
variable names should match existing variable names and be CamelCase, not underscores
libc/utils/UnitTest/LibcTest.cpp | ||
---|---|---|
146 | it's kinda weird to pass argc/argv directly to a function besides main. maybe just passing a std::optional<std::string> Filter is better | |
157 | perhaps something like TestFilter? | |
158 | IIRC std::string != const char* works, so probably no need to create str_test_name | |
160 | this should be a continue, if we skip a test then we'll never run anything else after (this is what writing a couple tests would help with) | |
182–183 | I think just "No tests run." is good enough. | |
188 | we should make this also fail if TestCount == 0 |
libc/utils/UnitTest/LibcTest.cpp | ||
---|---|---|
146 | I agree - we should move the argv parsing code to a separate helper function. It would be cleaner and also scale (we plan to add ability to run flaky tests with run count etc.) @aeubanks' suggestion to use std::optional for the argument is perfect. | |
159 | The skipping message can get really noisy, so may be just avoid it. | |
160 | I agree here as well - we should write dummy tests to test this feature. It might require a little more surgery to the unit testing library. For example, we might have to separate the main function into a different library of its own like how gtest does. | |
182–183 | +1. Also, you should add something like this: if (TestFilter) { std::cout << "No mathing test for " << TestFilter << '\n'; return 1; } The early return here will avoid checking for TestCount later. |
libc/utils/UnitTest/LibcTest.cpp | ||
---|---|---|
146 | So the goal is to make a separate function to generate a Filter string that may/may not exist depending on the argc/argv conditions? I've never used the std::optional template so this is taking me a bit longer than expected to write out, many apologies! |
libc/utils/UnitTest/LibcTest.cpp | ||
---|---|---|
146 | yup if we have argv[1] containing a string, set the std::optional<std::string> to std::optional<std::string>{argv[1]} |
libc/utils/UnitTest/LibcTest.cpp | ||
---|---|---|
146 | Ok sounds great! It looks like std::optional needs another header, is adding #include <optional> in LibcTest.h okay or does libc have our own header somewhere? |
libc/utils/UnitTest/LibcTest.cpp | ||
---|---|---|
146 | That's a C++ (not a C) header, so #include <optional> is fine |
libc/utils/UnitTest/LibcTest.cpp | ||
---|---|---|
146 | Sigh! Even C++ headers cannot be included as they can potentially include other C headers and lead to mixups. We have a standalone, C++ utilities library but it does not have std::optional: https://github.com/llvm/llvm-project/tree/main/libc/utils/CPP One not so C++ like option for the interim is to use const char * and condition on whether it is a nullptr or not. |
[libc] Simplify test name comparisons
This adjustment resolves some format/linting issues, and simplifies some of the
logic used to compare the user-inputted test filter and the iterated test name.
It creates a new function to set the test filter as a string outside of main
to avoid passing the command line variables directly to runTests.
this is looking much better
I think you need to clang-format again
libc/utils/UnitTest/LibcTest.cpp | ||
---|---|---|
183 | "No tests run.\n" | |
186 | I'm tempted to always fail if we didn't run any tests since that seems like it should never happen, any thoughts? | |
361 | this is more of a get than a set | |
361 | I think keeping argc/argv as the variable names is clearer, that way it's obvious these are directly accessing the command line flags |
[libc] Fail if no tests run
This addition adjusts the final conditionals to return 1 to indicate failure,
and fixes formatting errors.
Almost there.
libc/utils/UnitTest/LibcTest.cpp | ||
---|---|---|
184 | Please run clang-format for all patches touching c++ code. It will fix errors like this. Also, LLVM style says we shouldn't be using the curly brackets for single statement blocks unless the statement spans multiple lines. So, this if block does not need the curly brackets. | |
186 | Either way is fine. |
libc/utils/UnitTest/LibcTest.cpp | ||
---|---|---|
184 | I ran clang format before this last upload, but it only caught some of the things so I'm just going to hand-fix the leftovers! |
[libc] Remove Main from LibcTest.cpp
This patch takes the main function out of LibcTest.cpp and moves it to its own
file, in addition to adding infrastructure to support testing of tests by
allowing opt-in inclusion of that main or to use the main within
testfilter_test.cpp to test the runTests functionality.
libc/test/utils/CMakeLists.txt | ||
---|---|---|
10 | extraneous empty lines | |
libc/test/utils/UnitTest/testfilter_test.cpp | ||
19 | what are these for? | |
21 | IncorrectFilter | |
33 | I'd add a test that tests that we run a test that's not the first test in the file, since that was one of the bugs that came up earlier. e.g. add a CorrectFilter2 and run that via a filter | |
38 | it's weird to me that we're using runTests() to test runTests(), but hey I guess if it works... | |
libc/utils/UnitTest/LibcTest.cpp | ||
2 | this shouldn't change | |
186 | moving this to be part of the return statement below would be clearer, it's confusing when we have multiple unbalanced return statements something like return FailCount > 0 || TestCount == 0 ? 1 : 0; |
libc/utils/UnitTest/LibcTestMain.cpp | ||
---|---|---|
13–16 | Just need to keep <string> to return NULL, but got rid of the unnecessary ones. Thanks!! |
libc/test/utils/UnitTest/testfilter_test.cpp | ||
---|---|---|
12 | this still isn't used right? | |
libc/utils/UnitTest/LibcTestMain.cpp | ||
13–16 | it seemed weird to me that <string> defined NULL, so I did a little research and apparently <cstddef> is actually what defines it, <string> probably just includes <cstddef> but in any case, in C++ we should use nullptr over NULL, and we won't need to include either |
libc/cmake/modules/LLVMLibCTestRules.cmake | ||
---|---|---|
77 | May be name is it NO_LIBC_UNITTEST_TEST_MAIN. | |
153 | You should not add testfileter_test here. It will be its own binary. | |
libc/test/utils/UnitTest/CMakeLists.txt | ||
2 | The suite name should probably be libc_unittest_tests. | |
libc/utils/UnitTest/CMakeLists.txt | ||
16 | Can you actually add a target as an include directory? | |
libc/utils/UnitTest/LibcTestMain.cpp | ||
12 | Is this used in this file? |
libc/utils/UnitTest/CMakeLists.txt | ||
---|---|---|
16 | It didn't cause any issues to take LibcUnitTest out and the documentation doesn't warn about any problems with it, so I'm just going to leave it out. Does ${LIBC_SOURCE_DIR} include it after line 7 is run? |
super nit: the very first lines of the files that start with //=== are inconsistent, I think usually it's //===-- description here ------===//
otherwise lgtm
libc/utils/UnitTest/CMakeLists.txt | ||
---|---|---|
18 | This line should be target_link_libraries(LibcUnitTestMain PUBLIC **LibcUnitTest** libc_test_utils) otherwise build is failing when building with shared libraries. |
May be name is it NO_LIBC_UNITTEST_TEST_MAIN.