[libc-commits] [PATCH] D105843: [libc] Add option to run specific tests

Caitlyn Cano via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Jul 14 08:41:28 PDT 2021


caitlyncano updated this revision to Diff 358620.
caitlyncano added a comment.

[libc] Fail if no tests run

This addition adjusts the final conditionals to return 1 to indicate failure,
and fixes formatting errors.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D105843/new/

https://reviews.llvm.org/D105843

Files:
  libc/utils/UnitTest/LibcTest.cpp
  libc/utils/UnitTest/LibcTest.h


Index: libc/utils/UnitTest/LibcTest.h
===================================================================
--- libc/utils/UnitTest/LibcTest.h
+++ libc/utils/UnitTest/LibcTest.h
@@ -56,6 +56,8 @@
 
 template <typename T> struct Matcher : public MatcherBase { bool match(T &t); };
 
+const char * getTestFilter(int, char *[]);
+
 // NOTE: One should not create instances and call methods on them directly. One
 // should use the macros TEST or TEST_F to write test cases.
 class Test {
@@ -70,7 +72,7 @@
   virtual void SetUp() {}
   virtual void TearDown() {}
 
-  static int runTests();
+  static int runTests(const char*);
 
 protected:
   static void addTest(Test *T);
Index: libc/utils/UnitTest/LibcTest.cpp
===================================================================
--- libc/utils/UnitTest/LibcTest.cpp
+++ libc/utils/UnitTest/LibcTest.cpp
@@ -143,14 +143,18 @@
   End = T;
 }
 
-int Test::runTests() {
+int Test::runTests(const char *TestFilter) {
   int TestCount = 0;
   int FailCount = 0;
-  for (Test *T = Start; T != nullptr; T = T->Next, ++TestCount) {
+  for (Test *T = Start; T != nullptr; T = T->Next) {
     const char *TestName = T->getName();
+    std::string StrTestName(TestName);
     constexpr auto GREEN = "\033[32m";
     constexpr auto RED = "\033[31m";
     constexpr auto RESET = "\033[0m";
+    if ((TestFilter != NULL) && (StrTestName != TestFilter))  {
+      continue;
+    }
     std::cout << GREEN << "[ RUN      ] " << RESET << TestName << '\n';
     RunContext Ctx;
     T->SetUp();
@@ -167,11 +171,21 @@
       std::cout << GREEN << "[       OK ] " << RESET << TestName << '\n';
       break;
     }
-  }
+    ++TestCount;
+}
 
-  std::cout << "Ran " << TestCount << " tests. "
-            << " PASS: " << TestCount - FailCount << ' '
-            << " FAIL: " << FailCount << '\n';
+  if (TestCount > 0) {
+    std::cout << "Ran " << TestCount << " tests. "
+              << " PASS: " << TestCount - FailCount << ' '
+              << " FAIL: " << FailCount << '\n';
+  }
+  else {
+    std::cout << "No tests run.\n";
+    if(TestFilter)  {
+      std::cout << "No matching test for " << TestFilter << '\n';
+    }
+    return 1;
+  }
 
   return FailCount > 0 ? 1 : 0;
 }
@@ -346,8 +360,16 @@
   return false;
 }
 
+const char * getTestFilter(int argc,char *argv[])  {
+  return argc > 1 ? argv[1] : NULL;
+}
+
 #endif // ENABLE_SUBPROCESS_TESTS
 } // namespace testing
 } // namespace __llvm_libc
 
-int main() { return __llvm_libc::testing::Test::runTests(); }
+int main(int argc, char *argv[]) {
+  const char *TestFilter;
+  TestFilter = __llvm_libc::testing::getTestFilter(argc, argv);
+  return __llvm_libc::testing::Test::runTests(TestFilter);
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105843.358620.patch
Type: text/x-patch
Size: 2702 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20210714/7e482956/attachment.bin>


More information about the libc-commits mailing list