mirror of
https://github.com/apple/swift-system
synced 2026-11-25 04:47:55 +00:00
Leverage conditional compilation to reduce mocking boilerplate
This commit is contained in:
1 parent
98a1531ea0
commit
a8d07f8a04
2 files changed
+36
-77
No files matched your search
@@ -15,17 +15,17 @@ import Glibc
|
||||
#error("Unsupported Platform")
|
||||
#endif
|
||||
|
||||
#if ENABLE_MOCKING
|
||||
// Strip the mock_system prefix and the arg list suffix
|
||||
private func originalSyscallName(_ s: String) -> String {
|
||||
precondition(s.starts(with: "mock_system_"))
|
||||
return String(s.dropFirst("mock_system_".count).prefix { $0.isLetter })
|
||||
precondition(s.starts(with: "system_"))
|
||||
return String(s.dropFirst("system_".count).prefix { $0.isLetter })
|
||||
}
|
||||
|
||||
private func mockImpl(
|
||||
name: String,
|
||||
_ args: [AnyHashable]
|
||||
) -> CInt {
|
||||
#if ENABLE_MOCKING
|
||||
let origName = originalSyscallName(name)
|
||||
guard let driver = currentMockingDriver else {
|
||||
fatalError("Mocking requested from non-mocking context")
|
||||
@@ -43,9 +43,6 @@ private func mockImpl(
|
||||
driver.forceErrno = count > 1 ? .counted(errno: e, count: count-1) : .none
|
||||
return -1
|
||||
}
|
||||
#else
|
||||
fatalError("Mocking uses in non-mocking-enabld build")
|
||||
#endif
|
||||
|
||||
return 0
|
||||
}
|
||||
@@ -67,117 +64,83 @@ private func mockOffT(
|
||||
) -> off_t {
|
||||
off_t(mockImpl(name: name, args))
|
||||
}
|
||||
#endif // ENABLE_MOCKING
|
||||
|
||||
// Interacting with the mocking system, tracing, etc., is a potentially significant
|
||||
// amount of code size, so we hand outline that code for every syscall
|
||||
|
||||
// open
|
||||
public func system_open(_ path: UnsafePointer<CChar>, _ oflag: Int32) -> CInt {
|
||||
if _fastPath(!mockingEnabled) { return open(path, oflag) }
|
||||
return mock_system_open(path, oflag)
|
||||
}
|
||||
|
||||
@inline(never)
|
||||
private func mock_system_open(_ path: UnsafePointer<CChar>, _ oflag: Int32) -> CInt {
|
||||
mock(String(cString: path), oflag)
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return mock(String(cString: path), oflag) }
|
||||
#endif
|
||||
return open(path, oflag)
|
||||
}
|
||||
|
||||
public func system_open(
|
||||
_ path: UnsafePointer<CChar>, _ oflag: Int32, _ mode: mode_t
|
||||
) -> CInt {
|
||||
if _fastPath(!mockingEnabled) { return open(path, oflag, mode) }
|
||||
return mock_system_open(path, oflag, mode)
|
||||
}
|
||||
|
||||
@inline(never)
|
||||
private func mock_system_open(
|
||||
_ path: UnsafePointer<CChar>, _ oflag: Int32, _ mode: mode_t
|
||||
) -> CInt {
|
||||
mock(String(cString: path), oflag, mode)
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return mock(String(cString: path), oflag, mode) }
|
||||
#endif
|
||||
return open(path, oflag, mode)
|
||||
}
|
||||
|
||||
// close
|
||||
public func system_close(_ fd: Int32) -> Int32 {
|
||||
if _fastPath(!mockingEnabled) { return close(fd) }
|
||||
return mock_system_close(fd)
|
||||
}
|
||||
|
||||
@inline(never)
|
||||
private func mock_system_close(_ fd: Int32) -> Int32 {
|
||||
mock(fd)
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return mock(fd) }
|
||||
#endif
|
||||
return close(fd)
|
||||
}
|
||||
|
||||
// read
|
||||
public func system_read(
|
||||
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int
|
||||
) -> Int {
|
||||
if _fastPath(!mockingEnabled) { return read(fd, buf, nbyte) }
|
||||
return mock_system_read(fd, buf, nbyte)
|
||||
}
|
||||
|
||||
@inline(never)
|
||||
private func mock_system_read(
|
||||
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int
|
||||
) -> Int {
|
||||
mockInt(fd, buf, nbyte)
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return mockInt(fd, buf, nbyte) }
|
||||
#endif
|
||||
return read(fd, buf, nbyte)
|
||||
}
|
||||
|
||||
// pread
|
||||
public func system_pread(
|
||||
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int, _ offset: off_t
|
||||
) -> Int {
|
||||
if _fastPath(!mockingEnabled) { return pread(fd, buf, nbyte, offset) }
|
||||
return mock_system_pread(fd, buf, nbyte, offset)
|
||||
}
|
||||
|
||||
@inline(never)
|
||||
private func mock_system_pread(
|
||||
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int, _ offset: off_t
|
||||
) -> Int {
|
||||
mockInt(fd, buf, nbyte, offset)
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return mockInt(fd, buf, nbyte, offset) }
|
||||
#endif
|
||||
return pread(fd, buf, nbyte, offset)
|
||||
}
|
||||
|
||||
// lseek
|
||||
public func system_lseek(
|
||||
_ fd: Int32, _ off: off_t, _ whence: Int32
|
||||
) -> off_t {
|
||||
if _fastPath(!mockingEnabled) { return lseek(fd, off, whence) }
|
||||
return mock_system_lseek(fd, off, whence)
|
||||
}
|
||||
|
||||
@inline(never)
|
||||
private func mock_system_lseek(
|
||||
_ fd: Int32, _ off: off_t, _ whence: Int32
|
||||
) -> off_t {
|
||||
mockOffT(fd, off, whence)
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return mockOffT(fd, off, whence) }
|
||||
#endif
|
||||
return lseek(fd, off, whence)
|
||||
}
|
||||
|
||||
// write
|
||||
public func system_write(
|
||||
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int
|
||||
) -> Int {
|
||||
if _fastPath(!mockingEnabled) { return write(fd, buf, nbyte) }
|
||||
return mock_system_write(fd, buf, nbyte)
|
||||
}
|
||||
|
||||
@inline(never)
|
||||
private func mock_system_write(
|
||||
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int
|
||||
) -> Int {
|
||||
mockInt(fd, buf, nbyte)
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return mockInt(fd, buf, nbyte) }
|
||||
#endif
|
||||
return write(fd, buf, nbyte)
|
||||
}
|
||||
|
||||
// pwrite
|
||||
public func system_pwrite(
|
||||
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int, _ offset: off_t
|
||||
) -> Int {
|
||||
if _fastPath(!mockingEnabled) { return pwrite(fd, buf, nbyte, offset) }
|
||||
return mock_system_pwrite(fd, buf, nbyte, offset)
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return mockInt(fd, buf, nbyte, offset) }
|
||||
#endif
|
||||
return pwrite(fd, buf, nbyte, offset)
|
||||
}
|
||||
|
||||
@inline(never)
|
||||
private func mock_system_pwrite(
|
||||
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int, _ offset: off_t
|
||||
) -> Int {
|
||||
mockInt(fd, buf, nbyte, offset)
|
||||
}
|
||||
@@ -98,9 +98,7 @@ internal struct MockTestCase: TestCase {
|
||||
// retry, to make sure they don't spin (e.g. if API changes to include
|
||||
// interruptable)
|
||||
do {
|
||||
let oldErrno = mocking.forceErrno
|
||||
mocking.forceErrno = .always(errno: EINTR)
|
||||
defer { mocking.forceErrno = oldErrno }
|
||||
try body(!interruptable)
|
||||
self.fail()
|
||||
} catch Errno.interrupted {
|
||||
@@ -113,8 +111,6 @@ internal struct MockTestCase: TestCase {
|
||||
// Force a limited number of EINTRs, and make sure interruptable functions
|
||||
// retry that number of times. Non-interruptable functions should throw it.
|
||||
do {
|
||||
let origForceErrno = mocking.forceErrno
|
||||
defer { mocking.forceErrno = origForceErrno }
|
||||
mocking.forceErrno = .counted(errno: EINTR, count: 3)
|
||||
|
||||
try body(interruptable)
|
||||
|
||||
Reference in new issue
Block a user