This is an archive of the discontinued LLVM Phabricator instance.

[libc] Add option to run specific tests
ClosedPublic

Authored by caitlyncano on Jul 12 2021, 2:03 PM.

Details

Summary

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.

Diff Detail

Event Timeline

caitlyncano created this revision.Jul 12 2021, 2:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 12 2021, 2:03 PM
caitlyncano requested review of this revision.Jul 12 2021, 2:03 PM

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_.

caitlyncano retitled this revision from [libc] Add option to run specific test binaries to [libc] Add option to run specific tess.Jul 12 2021, 2:21 PM
caitlyncano retitled this revision from [libc] Add option to run specific tess to [libc] Add option to run specific tests.

[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

sivachandra added inline comments.Jul 12 2021, 10:02 PM
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.

caitlyncano marked 6 inline comments as done.Jul 13 2021, 11:02 AM
caitlyncano marked an inline comment as done.Jul 13 2021, 11:30 AM
caitlyncano added inline comments.
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!

aeubanks added inline comments.Jul 13 2021, 11:34 AM
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]}
else set the std::optional<std::string> to std::nullopt

caitlyncano added inline comments.Jul 13 2021, 12:49 PM
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?

aeubanks added inline comments.Jul 13 2021, 12:54 PM
libc/utils/UnitTest/LibcTest.cpp
146

That's a C++ (not a C) header, so #include <optional> is fine

sivachandra added inline comments.Jul 13 2021, 1:07 PM
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

caitlyncano marked 3 inline comments as done.Jul 14 2021, 8:33 AM
caitlyncano marked an inline comment as done.Jul 14 2021, 8:36 AM

[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.

caitlyncano added inline comments.Jul 14 2021, 9:29 AM
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!

git clang-format HEAD~ -f should work (if you're using git-clang-format)

Sorry, another small nit.

libc/utils/UnitTest/LibcTest.cpp
361

Can this function be static like this and avoid the public declaration as pointed out above:

static const char *getTestFileter(...) {
  ...
}
libc/utils/UnitTest/LibcTest.h
59

Do we need this declaration?

[libc] Fix formatting errors

caitlyncano marked an inline comment as done.Jul 14 2021, 9:45 AM

[libc] Redeclare getTestFilter as static

the code looks good, but it'd be nice if we had tests for this

libc/utils/UnitTest/LibcTest.cpp
179

I think it'd be nice if we listed the number of skipped tests, or just the fact that there is a filter at all. But not necessary right now.

364–365

merge these two into one line

[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] Add UnitTests directory in libc/utils

[libc] Update previous patch edit for functional linking

[libc] Add testfilter tests

aeubanks added inline comments.Jul 21 2021, 9:08 AM
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;

caitlyncano marked 3 inline comments as done.Jul 21 2021, 1:09 PM
caitlyncano marked an inline comment as done.
caitlyncano marked 3 inline comments as done.Jul 21 2021, 1:39 PM

[libc] Add additional check for correct results and fix formatting

aeubanks added inline comments.Jul 21 2021, 1:47 PM
libc/test/utils/UnitTest/testfilter_test.cpp
12–15

is all of this is no longer necessary?

libc/utils/UnitTest/LibcTest.cpp
1

unintentional change?

libc/utils/UnitTest/LibcTestMain.cpp
3

formatting

13–16

ditto

caitlyncano marked 4 inline comments as done.Jul 22 2021, 8:21 AM
caitlyncano added inline comments.
libc/utils/UnitTest/LibcTestMain.cpp
13–16

Just need to keep <string> to return NULL, but got rid of the unnecessary ones. Thanks!!

caitlyncano marked an inline comment as done.Jul 22 2021, 8:22 AM

[libc] Remove unnecessary headers and fix formatting

aeubanks added inline comments.Jul 22 2021, 8:57 AM
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

sivachandra added inline comments.Jul 22 2021, 9:58 AM
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?

caitlyncano marked 6 inline comments as done.Jul 22 2021, 11:52 AM
caitlyncano added inline comments.
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?

[libc] Fix variable names and remove unnecessary lines

aeubanks accepted this revision.Jul 22 2021, 3:07 PM

super nit: the very first lines of the files that start with //=== are inconsistent, I think usually it's //===-- description here ------===//
otherwise lgtm

This revision is now accepted and ready to land.Jul 22 2021, 3:07 PM
sivachandra accepted this revision.Jul 22 2021, 10:51 PM

super nit: the very first lines of the files that start with //=== are inconsistent, I think usually it's //===-- description here ------===//
otherwise lgtm

+1

[libc] Fix title formatting

[libc] Remove accidental edits to windows config

This revision was landed with ongoing or failed builds.Jul 23 2021, 9:08 AM
This revision was automatically updated to reflect the committed changes.
gchatelet added inline comments.
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.
Fixed in https://reviews.llvm.org/rG47afd43eaa9b