Skip to content

Commit d039af3

Browse files
authored
[libc] Create ErrnoCheckingTest harness to simplify errno tests. (#131703)
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.
1 parent 460b9cd commit d039af3

File tree

6 files changed

+71
-4
lines changed

6 files changed

+71
-4
lines changed

libc/test/UnitTest/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,12 @@ add_header_library(
188188
libc.src.__support.StringUtil.error_to_string
189189
libc.src.errno.errno
190190
)
191+
192+
add_header_library(
193+
ErrnoCheckingTest
194+
HDRS
195+
ErrnoCheckingTest.h
196+
DEPENDS
197+
libc.src.__support.common
198+
libc.src.errno.errno
199+
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===-- ErrnoCheckingTest.h ------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===---------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_TEST_UNITTEST_ERRNOCHECKINGTEST_H
10+
#define LLVM_LIBC_TEST_UNITTEST_ERRNOCHECKINGTEST_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include "src/errno/libc_errno.h"
14+
#include "test/UnitTest/Test.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
namespace testing {
18+
19+
// Provides a test fixture for tests that validate modifications of the errno.
20+
// It clears out the errno at the beginning of the test (e.g. in case it
21+
// contained the value pre-set by the system), and confirms it's still zero
22+
// at the end of the test, forcing the test author to explicitly account for all
23+
// non-zero values.
24+
class ErrnoCheckingTest : public Test {
25+
public:
26+
void SetUp() override {
27+
Test::SetUp();
28+
LIBC_NAMESPACE::libc_errno = 0;
29+
}
30+
31+
void TearDown() override {
32+
ASSERT_ERRNO_SUCCESS();
33+
Test::TearDown();
34+
}
35+
};
36+
37+
} // namespace testing
38+
} // namespace LIBC_NAMESPACE_DECL
39+
40+
#endif // LLVM_LIBC_TEST_UNITTEST_ERRNOCHECKINGTEST_H

libc/test/src/unistd/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ add_libc_unittest(
357357
libc.src.fcntl.open
358358
libc.src.unistd.close
359359
libc.src.unistd.unlink
360+
libc.test.UnitTest.ErrnoCheckingTest
361+
libc.test.UnitTest.ErrnoSetterMatcher
360362
)
361363

362364
add_libc_unittest(

libc/test/src/unistd/unlink_test.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,30 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/errno/libc_errno.h"
109
#include "src/fcntl/open.h"
1110
#include "src/unistd/close.h"
1211
#include "src/unistd/unlink.h"
12+
#include "test/UnitTest/ErrnoCheckingTest.h"
1313
#include "test/UnitTest/ErrnoSetterMatcher.h"
1414
#include "test/UnitTest/Test.h"
1515

1616
#include <sys/stat.h>
1717

18-
TEST(LlvmLibcUnlinkTest, CreateAndUnlink) {
18+
using LlvmLibcUnlinkTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
19+
20+
TEST_F(LlvmLibcUnlinkTest, CreateAndUnlink) {
1921
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
2022
constexpr const char *FILENAME = "unlink.test";
2123
auto TEST_FILE = libc_make_test_file_path(FILENAME);
2224
int write_fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
2325
ASSERT_ERRNO_SUCCESS();
2426
ASSERT_GT(write_fd, 0);
27+
2528
ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0));
2629
ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
2730
}
2831

29-
TEST(LlvmLibcUnlinkTest, UnlinkNonExistentFile) {
32+
TEST_F(LlvmLibcUnlinkTest, UnlinkNonExistentFile) {
3033
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
3134
ASSERT_THAT(LIBC_NAMESPACE::unlink("non-existent-file"), Fails(ENOENT));
3235
}

utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ libc_test_library(
7373
],
7474
)
7575

76+
libc_test_library(
77+
name = "errno_test_helpers",
78+
hdrs = [
79+
"ErrnoCheckingTest.h",
80+
],
81+
deps = [
82+
":LibcUnitTest",
83+
"//libc:errno",
84+
],
85+
)
86+
7687
libc_test_library(
7788
name = "fp_test_helpers",
7889
srcs = [

utils/bazel/llvm-project-overlay/libc/test/src/unistd/BUILD.bazel

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ libc_test(
248248
"//libc:close",
249249
"//libc:unlink",
250250
],
251+
deps = [
252+
"//libc/test/UnitTest:errno_test_helpers",
253+
],
251254
)
252255

253256
# libc_test(
@@ -261,7 +264,6 @@ libc_test(
261264
# ],
262265
# )
263266

264-
265267
libc_test(
266268
name = "getppid_test",
267269
srcs = ["getppid_test.cpp"],

0 commit comments

Comments
 (0)