mirror of
https://github.com/wnagrodzki/SwiftLogger.git
synced 2025-04-07 13:01:51 +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 {
|
extension FileHandle: OSFileHandle {
|
||||||
|
|
||||||
func osSeekToEndOfFile() throws -> UInt64 {
|
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
|
var offsetInFile: UInt64 = 0
|
||||||
try __seek(toEndReturningOffset:&offsetInFile)
|
try __seek(toEndReturningOffset:&offsetInFile)
|
||||||
return offsetInFile
|
return offsetInFile
|
||||||
|
@ -28,7 +28,7 @@ extension FileHandle: OSFileHandle {
|
||||||
}
|
}
|
||||||
|
|
||||||
func osWrite(_ data: Data) throws {
|
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: ())
|
try __write(data, error: ())
|
||||||
} else {
|
} else {
|
||||||
fatalError()
|
fatalError()
|
||||||
|
@ -36,7 +36,7 @@ extension FileHandle: OSFileHandle {
|
||||||
}
|
}
|
||||||
|
|
||||||
func osSynchronizeFile() throws {
|
func osSynchronizeFile() throws {
|
||||||
if #available(iOS 13.0, *) {
|
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
|
||||||
try synchronize()
|
try synchronize()
|
||||||
} else {
|
} else {
|
||||||
fatalError()
|
fatalError()
|
||||||
|
@ -44,7 +44,7 @@ extension FileHandle: OSFileHandle {
|
||||||
}
|
}
|
||||||
|
|
||||||
func osCloseFile() throws {
|
func osCloseFile() throws {
|
||||||
if #available(iOS 13.0, *) {
|
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
|
||||||
try close()
|
try close()
|
||||||
} else {
|
} else {
|
||||||
fatalError()
|
fatalError()
|
||||||
|
|
|
@ -30,6 +30,7 @@ class DiskLoggerTests: XCTestCase {
|
||||||
let logURL = URL(fileURLWithPath: "/var/log/application.log")
|
let logURL = URL(fileURLWithPath: "/var/log/application.log")
|
||||||
|
|
||||||
func testLoggingMessageToFile() {
|
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")
|
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
|
||||||
expectation.expectedFulfillmentCount = 1
|
expectation.expectedFulfillmentCount = 1
|
||||||
|
|
||||||
|
@ -52,12 +53,14 @@ class DiskLoggerTests: XCTestCase {
|
||||||
|
|
||||||
// "2018-08-30 17:23:11.514 <crit> DiskLoggerTests:46 testWritingMessageToFile() a message\n"
|
// "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 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))
|
XCTAssertTrue(loggedMessage.hasSuffix(expectedSuffix))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func testExceedingFileSizeLimit() {
|
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")
|
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
|
||||||
expectation.expectedFulfillmentCount = 2
|
expectation.expectedFulfillmentCount = 2
|
||||||
|
|
||||||
|
@ -81,12 +84,14 @@ class DiskLoggerTests: XCTestCase {
|
||||||
|
|
||||||
// "2018-08-31 18:29:34.748 <crit> DiskLoggerTests:75 testExceedingFileSizeLimit() 2st message\n"
|
// "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 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))
|
XCTAssertTrue(loggedMessage.hasSuffix(expectedSuffix))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func testErrorDuringLogProcess() {
|
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")
|
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
|
||||||
expectation.expectedFulfillmentCount = 2
|
expectation.expectedFulfillmentCount = 2
|
||||||
|
|
||||||
|
@ -114,6 +119,7 @@ class DiskLoggerTests: XCTestCase {
|
||||||
|
|
||||||
XCTAssertTrue(loggedMessage.contains(expectedOccurence))
|
XCTAssertTrue(loggedMessage.contains(expectedOccurence))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class FileManagerStub: OSFileManager {
|
private class FileManagerStub: OSFileManager {
|
||||||
|
|
|
@ -60,7 +60,7 @@ class SizeLimitedFileTests: XCTestCase {
|
||||||
func testSynchronizingAndClosingFile() throws {
|
func testSynchronizingAndClosingFile() throws {
|
||||||
let factory = FileMockFactory()
|
let factory = FileMockFactory()
|
||||||
let writer = try SizeLimitedFileImpl(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
|
let writer = try SizeLimitedFileImpl(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
|
||||||
writer.synchronizeAndCloseFile()
|
try writer.synchronizeAndCloseFile()
|
||||||
XCTAssertTrue(factory.mock.synchronizeFileCallCount == 1 && factory.mock.closeFileCallCount == 1)
|
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 synchronizeFileCallCount = 0
|
||||||
private(set) var closeFileCallCount = 0
|
private(set) var closeFileCallCount = 0
|
||||||
|
|
||||||
func seekToEndOfFile() -> UInt64 {
|
func osSeekToEndOfFile() throws -> UInt64 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func swift_write(_ data: Data) throws {
|
func osWrite(_ data: Data) throws {
|
||||||
self.writtenData.append(data)
|
self.writtenData.append(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func synchronizeFile() {
|
func osSynchronizeFile() throws {
|
||||||
synchronizeFileCallCount += 1
|
synchronizeFileCallCount += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func closeFile() {
|
func osCloseFile() throws {
|
||||||
closeFileCallCount += 1
|
closeFileCallCount += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue