Skip to content

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

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

Merged
merged 4 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions libc/test/UnitTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
40 changes: 40 additions & 0 deletions libc/test/UnitTest/ErrnoCheckingTest.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions libc/test/src/unistd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ add_libc_unittest(
libc.src.fcntl.open
libc.src.unistd.close
libc.src.unistd.unlink
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
)

add_libc_unittest(
Expand Down
9 changes: 6 additions & 3 deletions libc/test/src/unistd/unlink_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
11 changes: 11 additions & 0 deletions utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ libc_test(
"//libc:close",
"//libc:unlink",
],
deps = [
"//libc/test/UnitTest:errno_test_helpers",
],
)

# libc_test(
Expand All @@ -261,7 +264,6 @@ libc_test(
# ],
# )


libc_test(
name = "getppid_test",
srcs = ["getppid_test.cpp"],
Expand Down