mirror of
https://github.com/wnagrodzki/SwiftLogger.git
synced 2025-04-07 04:51:52 +02:00
Update DiskLogger tests to use new API
This commit is contained in:
parent
04ab08f294
commit
59e25369e6
3 changed files with 92 additions and 86 deletions
|
@ -18,7 +18,7 @@ protocol OSFileHandle {
|
|||
extension FileHandle: OSFileHandle {
|
||||
|
||||
func osSeekToEndOfFile() throws -> UInt64 {
|
||||
if #available(iOS 13.0, *) {
|
||||
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
|
||||
var offsetInFile: UInt64 = 0
|
||||
try __seek(toEndReturningOffset:&offsetInFile)
|
||||
return offsetInFile
|
||||
|
@ -28,7 +28,7 @@ extension FileHandle: OSFileHandle {
|
|||
}
|
||||
|
||||
func osWrite(_ data: Data) throws {
|
||||
if #available(iOS 13.0, *) {
|
||||
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
|
||||
try __write(data, error: ())
|
||||
} else {
|
||||
fatalError()
|
||||
|
@ -36,7 +36,7 @@ extension FileHandle: OSFileHandle {
|
|||
}
|
||||
|
||||
func osSynchronizeFile() throws {
|
||||
if #available(iOS 13.0, *) {
|
||||
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
|
||||
try synchronize()
|
||||
} else {
|
||||
fatalError()
|
||||
|
@ -44,7 +44,7 @@ extension FileHandle: OSFileHandle {
|
|||
}
|
||||
|
||||
func osCloseFile() throws {
|
||||
if #available(iOS 13.0, *) {
|
||||
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
|
||||
try close()
|
||||
} else {
|
||||
fatalError()
|
||||
|
|
|
@ -30,6 +30,7 @@ class DiskLoggerTests: XCTestCase {
|
|||
let logURL = URL(fileURLWithPath: "/var/log/application.log")
|
||||
|
||||
func testLoggingMessageToFile() {
|
||||
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
|
||||
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
|
||||
expectation.expectedFulfillmentCount = 1
|
||||
|
||||
|
@ -52,12 +53,14 @@ class DiskLoggerTests: XCTestCase {
|
|||
|
||||
// "2018-08-30 17:23:11.514 <crit> DiskLoggerTests:46 testWritingMessageToFile() a message\n"
|
||||
let loggedMessage = String(decoding: sizeLimitedFileFactory.files[0].data, as: UTF8.self)
|
||||
let expectedSuffix = " <crit> DiskLoggerTests:46 testLoggingMessageToFile() message\n"
|
||||
let expectedSuffix = " <crit> DiskLoggerTests:47 testLoggingMessageToFile() message\n"
|
||||
|
||||
XCTAssertTrue(loggedMessage.hasSuffix(expectedSuffix))
|
||||
}
|
||||
}
|
||||
|
||||
func testExceedingFileSizeLimit() {
|
||||
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
|
||||
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
|
||||
expectation.expectedFulfillmentCount = 2
|
||||
|
||||
|
@ -81,12 +84,14 @@ class DiskLoggerTests: XCTestCase {
|
|||
|
||||
// "2018-08-31 18:29:34.748 <crit> DiskLoggerTests:75 testExceedingFileSizeLimit() 2st message\n"
|
||||
let loggedMessage = String(decoding: sizeLimitedFileFactory.files[1].data, as: UTF8.self)
|
||||
let expectedSuffix = " <crit> DiskLoggerTests:75 testExceedingFileSizeLimit() 2st message\n"
|
||||
let expectedSuffix = " <crit> DiskLoggerTests:78 testExceedingFileSizeLimit() 2st message\n"
|
||||
|
||||
XCTAssertTrue(loggedMessage.hasSuffix(expectedSuffix))
|
||||
}
|
||||
}
|
||||
|
||||
func testErrorDuringLogProcess() {
|
||||
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
|
||||
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
|
||||
expectation.expectedFulfillmentCount = 2
|
||||
|
||||
|
@ -115,6 +120,7 @@ class DiskLoggerTests: XCTestCase {
|
|||
XCTAssertTrue(loggedMessage.contains(expectedOccurence))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class FileManagerStub: OSFileManager {
|
||||
func itemExists(at URL: URL) -> Bool {
|
||||
|
|
|
@ -60,7 +60,7 @@ class SizeLimitedFileTests: XCTestCase {
|
|||
func testSynchronizingAndClosingFile() throws {
|
||||
let factory = FileMockFactory()
|
||||
let writer = try SizeLimitedFileImpl(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
|
||||
writer.synchronizeAndCloseFile()
|
||||
try writer.synchronizeAndCloseFile()
|
||||
XCTAssertTrue(factory.mock.synchronizeFileCallCount == 1 && factory.mock.closeFileCallCount == 1)
|
||||
}
|
||||
}
|
||||
|
@ -89,19 +89,19 @@ private class FileMock: OSFileHandle {
|
|||
private(set) var synchronizeFileCallCount = 0
|
||||
private(set) var closeFileCallCount = 0
|
||||
|
||||
func seekToEndOfFile() -> UInt64 {
|
||||
func osSeekToEndOfFile() throws -> UInt64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func swift_write(_ data: Data) throws {
|
||||
func osWrite(_ data: Data) throws {
|
||||
self.writtenData.append(data)
|
||||
}
|
||||
|
||||
func synchronizeFile() {
|
||||
func osSynchronizeFile() throws {
|
||||
synchronizeFileCallCount += 1
|
||||
}
|
||||
|
||||
func closeFile() {
|
||||
func osCloseFile() throws {
|
||||
closeFileCallCount += 1
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue