[libc] Create ErrnoCheckingTest harness to simplify errno tests. (#131703)
See the discussion in PR
https://github.com/llvm/llvm-project/pull/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.
diff --git a/libc/test/UnitTest/CMakeLists.txt b/libc/test/UnitTest/CMakeLists.txt
index 570bd60..b0a3a74 100644
--- a/libc/test/UnitTest/CMakeLists.txt
+++ b/libc/test/UnitTest/CMakeLists.txt
@@ -188,3 +188,12 @@
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 0000000..3d3b72f
--- /dev/null
+++ b/libc/test/UnitTest/ErrnoCheckingTest.h
@@ -0,0 +1,40 @@
+//===-- 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 d1f3050..c31a03d 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -357,6 +357,8 @@
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 e1ffaab..892779b 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 cec3fc1..27e468d 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel
@@ -74,6 +74,17 @@
)
libc_test_library(
+ name = "errno_test_helpers",
+ hdrs = [
+ "ErrnoCheckingTest.h",
+ ],
+ deps = [
+ ":LibcUnitTest",
+ "//libc:errno",
+ ],
+)
+
+libc_test_library(
name = "fp_test_helpers",
srcs = [
"FEnvSafeTest.cpp",
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 66d8ddb..0d66016 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:close",
"//libc:unlink",
],
+ deps = [
+ "//libc/test/UnitTest:errno_test_helpers",
+ ],
)
# libc_test(
@@ -261,7 +264,6 @@
# ],
# )
-
libc_test(
name = "getppid_test",
srcs = ["getppid_test.cpp"],