SwiftLogger/UnitTests/SizeLimitedFileTests.swift

108 lines
3.6 KiB
Swift
Raw Normal View History

2018-08-30 18:38:53 +02:00
//
// MIT License
//
// Copyright (c) 2018 Wojciech Nagrodzki
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import XCTest
@testable import Logger
class SizeLimitedFileTests: XCTestCase {
2018-08-30 18:38:53 +02:00
let logURL = URL(fileURLWithPath: "/var/log/application.log")
func testFileOpeningFailure() {
let factory = UnopenableFileFactory()
XCTAssertThrowsError(try SizeLimitedFileImpl(fileURL: logURL, fileSizeLimit: 0, fileFactory: factory), "file open failure") { (error) in
2018-08-30 18:38:53 +02:00
XCTAssertTrue(error is UnopenableFileFactory.OpenFileError)
}
}
func testKeepingFileSizeLimit() throws {
let factory = FileMockFactory()
let writer = try SizeLimitedFileImpl(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
2019-04-07 17:36:30 +02:00
let data = Data([0])
2018-08-30 18:38:53 +02:00
try writer.write(data)
XCTAssertEqual(factory.mock.writtenData, data)
}
func testExceedingFileSizeLimit() throws {
let factory = FileMockFactory()
let writer = try SizeLimitedFileImpl(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
2019-04-07 17:36:30 +02:00
let data = Data([0, 0])
2018-08-30 18:38:53 +02:00
XCTAssertThrowsError(try writer.write(data), "file size limit exceeded") { (error) in
XCTAssertTrue(error is SizeLimitedFileQuotaReached)
}
XCTAssertEqual(factory.mock.writtenData.count, 0)
}
func testSynchronizingAndClosingFile() throws {
let factory = FileMockFactory()
let writer = try SizeLimitedFileImpl(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
2019-11-04 19:22:36 +01:00
try writer.synchronizeAndCloseFile()
2018-08-30 18:38:53 +02:00
XCTAssertTrue(factory.mock.synchronizeFileCallCount == 1 && factory.mock.closeFileCallCount == 1)
}
}
private class UnopenableFileFactory: FileHandleFactory {
2018-08-30 18:38:53 +02:00
struct OpenFileError: Error {}
func makeInstance(forWritingTo: URL) throws -> OSFileHandle {
2018-08-30 18:38:53 +02:00
throw OpenFileError()
}
}
private class FileMockFactory: FileHandleFactory {
2018-08-30 18:38:53 +02:00
let mock = FileMock()
func makeInstance(forWritingTo: URL) throws -> OSFileHandle {
2018-08-30 18:38:53 +02:00
return mock
}
}
private class FileMock: OSFileHandle {
2018-08-30 18:38:53 +02:00
private(set) var writtenData = Data()
private(set) var synchronizeFileCallCount = 0
private(set) var closeFileCallCount = 0
2019-11-04 19:22:36 +01:00
func osSeekToEndOfFile() throws -> UInt64 {
2018-08-30 18:38:53 +02:00
return 0
}
2019-11-04 19:22:36 +01:00
func osWrite(_ data: Data) throws {
2018-08-30 18:38:53 +02:00
self.writtenData.append(data)
}
2019-11-04 19:22:36 +01:00
func osSynchronizeFile() throws {
2018-08-30 18:38:53 +02:00
synchronizeFileCallCount += 1
}
2019-11-04 19:22:36 +01:00
func osCloseFile() throws {
2018-08-30 18:38:53 +02:00
closeFileCallCount += 1
}
}