Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc] Create ErrnoCheckingTest harness to simplify errno tests. #131703

Merged
merged 4 commits into from
Mar 18, 2025

Conversation

vonosmas
Copy link
Contributor

See the discussion in PR #131650 on why we need to clear the errno at
the beginning of some tests, and outlining the various solutions.

Introduce ErrnoCheckingTest base class and use it for unlink_test
as an example.

See the discussion in PR llvm#131650 on why we need to clear the errno at
the beginning of some tests, and outlining the various solutions.

Introduce ErrnoCheckingTest base class and use it for unlink_test
as an example.
@llvmbot llvmbot added libc bazel "Peripheral" support tier build system: utils/bazel labels Mar 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 18, 2025

@llvm/pr-subscribers-libc

Author: Alexey Samsonov (vonosmas)

Changes

See the discussion in PR #131650 on why we need to clear the errno at
the beginning of some tests, and outlining the various solutions.

Introduce ErrnoCheckingTest base class and use it for unlink_test
as an example.


Full diff: https://github.com/llvm/llvm-project/pull/131703.diff

6 Files Affected:

  • (modified) libc/test/UnitTest/CMakeLists.txt (+9)
  • (added) libc/test/UnitTest/ErrnoCheckingTest.h (+41)
  • (modified) libc/test/src/unistd/CMakeLists.txt (+2-1)
  • (modified) libc/test/src/unistd/unlink_test.cpp (+6-3)
  • (modified) utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel (+11)
  • (modified) utils/bazel/llvm-project-overlay/libc/test/src/unistd/BUILD.bazel (+3-1)
diff --git a/libc/test/UnitTest/CMakeLists.txt b/libc/test/UnitTest/CMakeLists.txt
index 570bd600d1a43..b0a3a7431c222 100644
--- a/libc/test/UnitTest/CMakeLists.txt
+++ b/libc/test/UnitTest/CMakeLists.txt
@@ -188,3 +188,12 @@ add_header_library(
     libc.src.__support.StringUtil.error_to_string
     libc.src.errno.errno
 )
+
+add_header_library(
+  ErrnoCheckingTest
+  HDRS
+    ErrnoCheckingTest.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.errno.errno
+)
diff --git a/libc/test/UnitTest/ErrnoCheckingTest.h b/libc/test/UnitTest/ErrnoCheckingTest.h
new file mode 100644
index 0000000000000..c4583d09705fd
--- /dev/null
+++ b/libc/test/UnitTest/ErrnoCheckingTest.h
@@ -0,0 +1,41 @@
+//===-- ErrnoCheckingTest.h ------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===---------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TEST_UNITTEST_ERRNOCHECKINGTEST_H
+#define LLVM_LIBC_TEST_UNITTEST_ERRNOCHECKINGTEST_H
+
+#include "src/__support/macros/config.h"
+#include "src/errno/libc_errno.h"
+#include "test/UnitTest/Test.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace testing {
+
+// Provides a test fixture for tests that validate modifications of the errno.
+// It clears out the errno at the beginning of the test (e.g. in case it
+// contained the value pre-set by the system), and confirms it's still zero
+// at the end of the test, forcing the test author to explicitly account for all
+// non-zero values.
+class ErrnoCheckingTest : public Test {
+public:
+  void SetUp() override {
+    Test::SetUp();
+    LIBC_NAMESPACE::libc_errno = 0;
+  }
+
+  void TearDown() override {
+    ASSERT_ERRNO_SUCCESS();
+    Test::TearDown();
+  }
+};
+
+} // namespace testing
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_TEST_UNITTEST_ERRNOCHECKINGTEST_H
+
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index d1f3050e6cccf..d0f1214b6609e 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -353,10 +353,11 @@ add_libc_unittest(
     unlink_test.cpp
   DEPENDS
     libc.include.unistd
-    libc.src.errno.errno
     libc.src.fcntl.open
     libc.src.unistd.close
     libc.src.unistd.unlink
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
 )
 
 add_libc_unittest(
diff --git a/libc/test/src/unistd/unlink_test.cpp b/libc/test/src/unistd/unlink_test.cpp
index e1ffaab78147e..892779b30cb8e 100644
--- a/libc/test/src/unistd/unlink_test.cpp
+++ b/libc/test/src/unistd/unlink_test.cpp
@@ -6,27 +6,30 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/errno/libc_errno.h"
 #include "src/fcntl/open.h"
 #include "src/unistd/close.h"
 #include "src/unistd/unlink.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
 #include <sys/stat.h>
 
-TEST(LlvmLibcUnlinkTest, CreateAndUnlink) {
+using LlvmLibcUnlinkTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcUnlinkTest, CreateAndUnlink) {
   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
   constexpr const char *FILENAME = "unlink.test";
   auto TEST_FILE = libc_make_test_file_path(FILENAME);
   int write_fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
   ASSERT_ERRNO_SUCCESS();
   ASSERT_GT(write_fd, 0);
+
   ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0));
   ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
 }
 
-TEST(LlvmLibcUnlinkTest, UnlinkNonExistentFile) {
+TEST_F(LlvmLibcUnlinkTest, UnlinkNonExistentFile) {
   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
   ASSERT_THAT(LIBC_NAMESPACE::unlink("non-existent-file"), Fails(ENOENT));
 }
diff --git a/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel
index cec3fc16c62d1..27e468ddeb5f5 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel
@@ -73,6 +73,17 @@ libc_test_library(
     ],
 )
 
+libc_test_library(
+    name = "errno_test_helpers",
+    hdrs = [
+        "ErrnoCheckingTest.h",
+    ],
+    deps = [
+        ":LibcUnitTest",
+        "//libc:errno",
+    ],
+)
+
 libc_test_library(
     name = "fp_test_helpers",
     srcs = [
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/unistd/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/unistd/BUILD.bazel
index 66d8ddb0a67eb..0d6601633956f 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/unistd/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/unistd/BUILD.bazel
@@ -248,6 +248,9 @@ libc_test(
         "//libc:close",
         "//libc:unlink",
     ],
+    deps = [
+        "//libc/test/UnitTest:errno_test_helpers",
+    ],
 )
 
 # libc_test(
@@ -261,7 +264,6 @@ libc_test(
 #     ],
 # )
 
-
 libc_test(
     name = "getppid_test",
     srcs = ["getppid_test.cpp"],

Copy link

github-actions bot commented Mar 18, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@vonosmas vonosmas merged commit d039af3 into llvm:main Mar 18, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bazel "Peripheral" support tier build system: utils/bazel libc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants